'''
Created on Apr 09, 2011
@author:  = 'Dilum Bandara'
@version:  = '0.1'
@copyright:  = 'Copyright(c) 2011, Dilum Bandara'
@contact:  = 'dilumb@engr.colostate.edu'
@license:  = 'GPL'
'''

import BaseConfig
import sys

class ReadData(object):
    '''
    Read data class. Read resource attributes & queries from input files
    '''
    def __init__(self, res_file, que_file):
        '''        
        @param res_file: Resource file name
        @param que_file: Query file name
        '''
        self.fdIn_res = open(res_file, 'r')
        self.fdIn_que = open(que_file, 'r')        
        tmp = self.fdIn_res.readline() #Read 1st line containing attribute names
        self._att_set = tmp.split()


    def __del__(self):
        self.fdIn_res.close()
        self.fdIn_que.close()


    def get_resource(self):
        '''
        Read resource attribute set of a node
        @return: List of attributes
        '''
        tmp = self.fdIn_res.readline()
        if len(tmp) == 0: return []
        tmp_atts = tmp.split()
        atts = []
        for i in range(0, len(tmp_atts)): #Construct list of attributes
            if self._att_set[i] == 'nid': tmp_atts[i] = int(tmp_atts[i])
            elif self._att_set[i] == 'time': tmp_atts[i] = float(tmp_atts[i])
            elif self._att_set[i] == 'RspT': tmp_atts[i] = float(tmp_atts[i])
            elif self._att_set[i] == 'SIn': tmp_atts[i] = int(tmp_atts[i])
            elif self._att_set[i] == 'SOut': tmp_atts[i] = int(tmp_atts[i])
            elif self._att_set[i] == 'SUsed': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'DIn': tmp_atts[i] = int(tmp_atts[i])
            elif self._att_set[i] == 'DOut': tmp_atts[i] = int(tmp_atts[i])
            elif self._att_set[i] == 'DSvc': tmp_atts[i] = float(tmp_atts[i])
            elif self._att_set[i] == 'DUtil': tmp_atts[i] = float(tmp_atts[i])
            elif self._att_set[i] == 'DUsed': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'DFree': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'DSize': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'UpT': tmp_atts[i] = float(tmp_atts[i])
            elif self._att_set[i] == '1mLd': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == '5mLd': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == '15mLd': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'TMax': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'TAve': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'MFree': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'MSize': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'MAct': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'KVer': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'CFree': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'CSp': tmp_atts[i] = float(tmp_atts[i])                
            elif self._att_set[i] == 'NCore': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'CorePC': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'FCName': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'Tx': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'Rx': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'Boot': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'NType': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'BWLim': 
                if tmp_atts[i] == 'none': tmp_atts[i] = BaseConfig.MAX_BW_LIMIT
                else: tmp_atts[i] = int(tmp_atts[i])
            elif self._att_set[i] == 'Lati': 
                if tmp_atts[i] == 'None': tmp_atts[i] = BaseConfig.MIN_LATITUDE
                else: tmp_atts[i] = float(tmp_atts[i])
            elif self._att_set[i] == 'Longi':
                if tmp_atts[i] == 'None': tmp_atts[i] = BaseConfig.MIN_LONGITUDE
                else: tmp_atts[i] = float(tmp_atts[i])
            elif self._att_set[i] == 'Loc': tmp_atts[i] = int(tmp_atts[i])                
            elif self._att_set[i] == 'Drift': tmp_atts[i] = float(tmp_atts[i])                
            else: 
                print 'Unmatched attribute ' + self._att_set[i] + ' in resources\n'
                sys.exit(0)
            item = [self._att_set[i], tmp_atts[i]]
            atts.append(item)
        return atts 


    def get_query(self):
        '''
        Read next query
        @return: Query as a list. First item indicate number of required resources
        '''
        tmp_line = self.fdIn_que.readline().split()
        if len(tmp_line) == 0: return []
        query = [[tmp_line[0], int(tmp_line[1])]] #set no of required resources
        tmp_attrib = []
        for i in range(2, len(tmp_line)): #read attribute name, min, max
            if (i % 3) == 2: #Read starting from 3rd entry
                if tmp_line[i] == 'RspT' or tmp_line[i] == 'UpT':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == 'SIn' or tmp_line[i] == 'SOut':
                    tmp_attrib = [tmp_line[i], int(tmp_line[i + 1]), int(tmp_line[i + 2])]
                elif tmp_line[i] == 'DIn' or tmp_line[i] == 'DOut':
                    tmp_attrib = [tmp_line[i], int(tmp_line[i + 1]), int(tmp_line[i + 2])]
                elif tmp_line[i] == 'DSvc' or tmp_line[i] == 'DUtil':
                        tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == '1mLd' or tmp_line[i] == '5mLd' or \
                        tmp_line[i] == '15mLd':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == 'TMax' or tmp_line[i] == 'TAve':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == 'DUsed' or tmp_line[i] == 'DFree':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == 'DSize' or tmp_line[i] == 'MSize':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == 'MAct' or tmp_line[i] == 'MFree' \
                        or tmp_line[i] == 'SUsed':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == 'CFree' or tmp_line[i] == 'CSp':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                elif tmp_line[i] == 'NCore' or tmp_line[i] == 'CorePC':
                    tmp_attrib = [tmp_line[i], int(tmp_line[i + 1]), int(tmp_line[i + 2])]
                elif tmp_line[i] == 'Tx' or tmp_line[i] == 'Rx':
                    tmp_attrib = [tmp_line[i], int(tmp_line[i + 1]), int(tmp_line[i + 2])]                    
                elif tmp_line[i] == 'Boot' or tmp_line[i] == 'NType' or \
                        tmp_line[i] == 'KVer' or tmp_line[i] == 'FCName':
                    tmp_attrib = [tmp_line[i], int(tmp_line[i + 1]), int(tmp_line[i + 2])]
                elif tmp_line[i] == 'BWLim' or tmp_line[i] == 'Loc':
                    tmp_attrib = [tmp_line[i], int(tmp_line[i + 1]), int(tmp_line[i + 2])]
                elif tmp_line[i] == 'Lati' or tmp_line[i] == 'Longi' \
                        or tmp_line[i] == 'Drift':
                    tmp_attrib = [tmp_line[i], float(tmp_line[i + 1]), float(tmp_line[i + 2])]
                else: 
                    print 'Unmatched attribute ' + tmp_line[i] + ' in queries\n'
                    sys.exit(0)
                query.append(tmp_attrib)
                tmp_attrib = [] 
        return query
