# Path: kll/common/id.py
# class PixelAddressId(Id):
#     '''
#     Pixel address identifier container class
#     '''
# 
#     def __init__(self, index=None, col=None, row=None, relCol=None, relRow=None):
#         Id.__init__(self)
# 
#         # Check to make sure index, col or row is set
#         if index is None and col is None and row is None and relRow is None and relCol is None:
#             print("{0} index, col or row must be set".format(ERROR))
# 
#         self.index = index
#         self.col = col
#         self.row = row
#         self.relCol = relCol
#         self.relRow = relRow
# 
#         self.type = 'PixelAddress'
# 
#     def inferred_type(self):
#         '''
#         Determine which PixelAddressType based on set values
#         '''
# 
#         if self.index is not None:
#             return 'PixelAddressId_Index'
#         if self.col is not None and self.row is None:
#             return 'PixelAddressId_ColumnFill'
#         if self.col is None and self.row is not None:
#             return 'PixelAddressId_RowFill'
#         if self.col is not None and self.row is not None:
#             return 'PixelAddressId_Rect'
#         if self.relCol is not None and self.relRow is None:
#             return 'PixelAddressId_RelativeColumnFill'
#         if self.relCol is None and self.relRow is not None:
#             return 'PixelAddressId_RelativeRowFill'
#         if self.relCol is not None and self.relRow is not None:
#             return 'PixelAddressId_RelativeRect'
# 
#         print("{0} Unknown PixelAddressId, this is a bug!".format(ERROR))
#         return "<UNKNOWN PixelAddressId>"
# 
#     def uid_set(self):
#         '''
#         Returns a tuple of uids, depends on what has been set.
#         '''
#         if self.index is not None:
#             return tuple([self.index])
#         if self.col is not None and self.row is None:
#             return tuple([self.col, self.row])
#         if self.col is None and self.row is not None:
#             return tuple([self.col, self.row])
#         if self.col is not None and self.row is not None:
#             return tuple([self.col, self.row])
#         if self.relCol is not None and self.relRow is None:
#             return tuple([self.relCol, self.relRow])
#         if self.relCol is None and self.relRow is not None:
#             return tuple([self.relCol, self.relRow])
#         if self.relCol is not None and self.relRow is not None:
#             return tuple([self.relCol, self.relRow])
# 
#         print("{0} Unknown uid set, this is a bug!".format(ERROR))
#         return "<UNKNOWN uid set"
# 
#     def merge(self, address):
#         '''
#         Merge in a PixelAddressId
# 
#         Will error-out instead of overwriting
# 
#         @param address: PixelAddressId object to merge in
#         '''
#         # Make sure we are either index or col/row
#         if not self.index is None or not address.index is None:
#             print("{0} Cannot merge into index PixelAddressIds".format(ERROR))
#             raise NoParseError
# 
#         # Make sure we don't overwrite
#         if not self.col is None and not address.col is None:
#             print("{0} Duplicate column fields '{1}' '{2}'".format(ERROR, self, address))
#             raise NoParseError
#         if not self.row is None and not address.row is None:
#             print("{0} Duplicate row fields '{1}' '{2}'".format(ERROR, self, address))
#             raise NoParseError
# 
#         # Merge over values
#         if not address.col is None:
#             self.col = address.col
#         if not address.row is None:
#             self.row = address.row
#         if not address.relCol is None:
#             self.relCol = address.relCol
#         if not address.relRow is None:
#             self.relRow = address.relRow
# 
#     def valueStr(self, value):
#         '''
#         Prepare numerical value based on type
# 
#         @param value: Float or Integer position
# 
#         @return: String
#         '''
#         output = ""
# 
#         # Check if float
#         if isinstance(value, float):
#             output += "{0}%".format(value * 100)
#         else:
#             output += "{0:03d}".format(value)
# 
#         return output
# 
#     def outputStrList(self):
#         '''
#         List of string items used for __repr__ and kllify
# 
#         @returns: List of strings
#         '''
#         output = []
# 
#         # Construct representation
#         if not self.index is None:
#             output.append("{0}".format(self.valueStr(self.index)))
#         if not self.row is None:
#             cur_out = "r:{0}".format(self.valueStr(self.row))
#             output.append(cur_out)
#         if not self.col is None:
#             cur_out = "c:{0}".format(self.valueStr(self.col))
#             output.append(cur_out)
#         if not self.relRow is None:
#             cur_out = "r:i"
#             cur_out += self.relRow >= 0 and "+" or ""
#             cur_out += "{0}".format(self.valueStr(self.relRow))
#             output.append(cur_out)
#         if not self.relCol is None:
#             cur_out = "c:i"
#             cur_out += self.relCol >= 0 and "+" or ""
#             cur_out += "{0}".format(self.valueStr(self.relCol))
#             output.append(cur_out)
# 
#         return output
# 
#     def __repr__(self):
#         return "{0}".format(self.outputStrList())
# 
#     def kllify(self):
#         '''
#         KLL syntax compatible output for PixelAddress object
#         '''
#         return ",".join(self.outputStrList())


import copy
import re
import kll.common.expression as expression
from kll.common.id import PixelAddressId
    Capability -> C Function/Identifier
    '''


class DefineData(Data):
    '''
    KLL datastructure for Define mapping

    Variable -> C Define/Identifier
    '''


class PixelChannelData(Data):
    '''
    KLL datastructure for Pixel Channel mapping

    Pixel -> Channels
    '''

    def maxpixelid(self):
        '''
        Find max pixel id per connect id

        @return: dictionary of connect id to max pixel id
        '''
        max_pixel = {}
        for key, value in self.data.items():
            connect_id = value.connect_id

            # Make sure this is a PixelAddressId

# Path: kll/common/id.py
# class PixelAddressId(Id):
#     '''
#     Pixel address identifier container class
#     '''
# 
#     def __init__(self, index=None, col=None, row=None, relCol=None, relRow=None):
#         Id.__init__(self)
# 
#         # Check to make sure index, col or row is set
#         if index is None and col is None and row is None and relRow is None and relCol is None:
#             print("{0} index, col or row must be set".format(ERROR))
# 
#         self.index = index
#         self.col = col
#         self.row = row
#         self.relCol = relCol
#         self.relRow = relRow
# 
#         self.type = 'PixelAddress'
# 
#     def inferred_type(self):
#         '''
#         Determine which PixelAddressType based on set values
#         '''
# 
#         if self.index is not None:
#             return 'PixelAddressId_Index'
#         if self.col is not None and self.row is None:
#             return 'PixelAddressId_ColumnFill'
#         if self.col is None and self.row is not None:
#             return 'PixelAddressId_RowFill'
#         if self.col is not None and self.row is not None:
#             return 'PixelAddressId_Rect'
#         if self.relCol is not None and self.relRow is None:
#             return 'PixelAddressId_RelativeColumnFill'
#         if self.relCol is None and self.relRow is not None:
#             return 'PixelAddressId_RelativeRowFill'
#         if self.relCol is not None and self.relRow is not None:
#             return 'PixelAddressId_RelativeRect'
# 
#         print("{0} Unknown PixelAddressId, this is a bug!".format(ERROR))
#         return "<UNKNOWN PixelAddressId>"
# 
#     def uid_set(self):
#         '''
#         Returns a tuple of uids, depends on what has been set.
#         '''
#         if self.index is not None:
#             return tuple([self.index])
#         if self.col is not None and self.row is None:
#             return tuple([self.col, self.row])
#         if self.col is None and self.row is not None:
#             return tuple([self.col, self.row])
#         if self.col is not None and self.row is not None:
#             return tuple([self.col, self.row])
#         if self.relCol is not None and self.relRow is None:
#             return tuple([self.relCol, self.relRow])
#         if self.relCol is None and self.relRow is not None:
#             return tuple([self.relCol, self.relRow])
#         if self.relCol is not None and self.relRow is not None:
#             return tuple([self.relCol, self.relRow])
# 
#         print("{0} Unknown uid set, this is a bug!".format(ERROR))
#         return "<UNKNOWN uid set"
# 
#     def merge(self, address):
#         '''
#         Merge in a PixelAddressId
# 
#         Will error-out instead of overwriting
# 
#         @param address: PixelAddressId object to merge in
#         '''
#         # Make sure we are either index or col/row
#         if not self.index is None or not address.index is None:
#             print("{0} Cannot merge into index PixelAddressIds".format(ERROR))
#             raise NoParseError
# 
#         # Make sure we don't overwrite
#         if not self.col is None and not address.col is None:
#             print("{0} Duplicate column fields '{1}' '{2}'".format(ERROR, self, address))
#             raise NoParseError
#         if not self.row is None and not address.row is None:
#             print("{0} Duplicate row fields '{1}' '{2}'".format(ERROR, self, address))
#             raise NoParseError
# 
#         # Merge over values
#         if not address.col is None:
#             self.col = address.col
#         if not address.row is None:
#             self.row = address.row
#         if not address.relCol is None:
#             self.relCol = address.relCol
#         if not address.relRow is None:
#             self.relRow = address.relRow
# 
#     def valueStr(self, value):
#         '''
#         Prepare numerical value based on type
# 
#         @param value: Float or Integer position
# 
#         @return: String
#         '''
#         output = ""
# 
#         # Check if float
#         if isinstance(value, float):
#             output += "{0}%".format(value * 100)
#         else:
#             output += "{0:03d}".format(value)
# 
#         return output
# 
#     def outputStrList(self):
#         '''
#         List of string items used for __repr__ and kllify
# 
#         @returns: List of strings
#         '''
#         output = []
# 
#         # Construct representation
#         if not self.index is None:
#             output.append("{0}".format(self.valueStr(self.index)))
#         if not self.row is None:
#             cur_out = "r:{0}".format(self.valueStr(self.row))
#             output.append(cur_out)
#         if not self.col is None:
#             cur_out = "c:{0}".format(self.valueStr(self.col))
#             output.append(cur_out)
#         if not self.relRow is None:
#             cur_out = "r:i"
#             cur_out += self.relRow >= 0 and "+" or ""
#             cur_out += "{0}".format(self.valueStr(self.relRow))
#             output.append(cur_out)
#         if not self.relCol is None:
#             cur_out = "c:i"
#             cur_out += self.relCol >= 0 and "+" or ""
#             cur_out += "{0}".format(self.valueStr(self.relCol))
#             output.append(cur_out)
# 
#         return output
# 
#     def __repr__(self):
#         return "{0}".format(self.outputStrList())
# 
#     def kllify(self):
#         '''
#         KLL syntax compatible output for PixelAddress object
#         '''
#         return ",".join(self.outputStrList())


# Path: kll/common/organization.py
import copy
import re
import kll.common.expression as expression
from kll.common.id import PixelAddressId

    Capability -> C Function/Identifier
    '''


class DefineData(Data):
    '''
    KLL datastructure for Define mapping

    Variable -> C Define/Identifier
    '''


class PixelChannelData(Data):
    '''
    KLL datastructure for Pixel Channel mapping

    Pixel -> Channels
    '''

    def maxpixelid(self):
        '''
        Find max pixel id per connect id

        @return: dictionary of connect id to max pixel id
        '''
        max_pixel = {}
        for key, value in self.data.items():
            connect_id = value.connect_id

            # Make sure this is a PixelAddressId

            if isinstance(value.pixel.uid, PixelAddressId):