def test_isStateValid_relationOrder(self):
     state = {
         "A": {
             "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
         },
         "B": {
             "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
         },
         "C": {
             "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
         },
     }
     relations = [
         {
             "type": "I+",
             "args": None,
             "Q1": ("A", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "I-",
             "args": None,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "P+",
             "args": None,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
     ]
     self.assertEqual(isStateValid(state, relations), False)
    def test_isStateValid_multiRelation_valid(self):
        state = {
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
        }
        relations = [
            {
                "type": "EX",
                "args": None,
                "Q1": ("B", "Q"),
                "Q2": ("B", "Q"),
            },
            {
                "type": "I-",
                "args": None,
                "Q1": ("C", "Q"),
                "Q2": ("B", "Q"),
            },
            {
                "type": "P+",
                "args": None,
                "Q1": ("B", "Q"),
                "Q2": ("C", "Q"),
            },
        ]

        self.assertEqual(isStateValid(state, relations), True)
    def test_isStateValid_multiRelation_sameTail_ambiguous(self):
        state1 = {
            "A": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
        }
        state2 = {
            "A": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
        }
        state3 = {
            "A": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.PLUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
        }
        relations = [
            {
                "type": "EX",
                "args": None,
                "Q1": ("A", "Q"),
                "Q2": ("A", "Q"),
            },
            {
                "type": "P+",
                "args": None,
                "Q1": ("A", "Q"),
                "Q2": ("B", "Q"),
            },
            {
                "type": "I+",
                "args": None,
                "Q1": ("C", "Q"),
                "Q2": ("B", "Q"),
            },
        ]

        self.assertEqual(isStateValid(state1, relations), True)
        self.assertEqual(isStateValid(state2, relations), True)
        self.assertEqual(isStateValid(state3, relations), True)
def over_generate(blue_print=None, mag_Enum=MValue, der_Enum=DValue):
    """generates all combinations of states.
    Assumes that all quantities have the can take the same magnetude and 
    derivative values.
    Input:
        blue_print: dict(dict: (list, list))) defining the state and the values it 
        can take.
        mag_Enum: IntEnum with the possible values for magntudes.
        der_Enum: IntEnum with the possible values for derivatives.            
    Output:
        list(states) all possible states.
    """

    #defaut state blueprint in case none is given.
    if blue_print == None:

        mags = list(map(int, mag_Enum))
        ders = list(map(int, der_Enum))

        blue_print = {
            "Hoose": {
                "Inflow": ([0, 1], ders)
            },
            "Container": {
                "Volume": (mags, ders)
            },
            "Drain": {
                "Outflow": (mags, ders)
            },
        }

    #Creates all states
    states = []
    t = []

    for e in blue_print:
        for q in blue_print[e]:
            t.append(blue_print[e][q][0])
            t.append(blue_print[e][q][1])

    t = tuple(t)
    combs = list(product(*t))

    for c in combs:
        idx = 0

        state = {}
        for e in blue_print:
            state[e] = {}
            for q in blue_print[e]:
                mag_bound = blue_print[e][q][0][-1]
                state[e][q] = Quantity(Magnitude(c[idx], upperBound=mag_bound),
                                       Derivative(c[idx + 1]))
                idx += 2

        states.append(state)

    return states
    def test_isStateValid_noEX(self):
        state = {
            "A": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS))
            }
        }
        relations = []

        self.assertEqual(isStateValid(state, relations), False)
    def test_isStateValid_singleRelation_valid(self):
        state = {
            "B": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
            },
        }
        relations = [
            {
                "type": "P+",
                "args": None,
                "Q1": ("B", "Q"),
                "Q2": ("C", "Q"),
            },
        ]

        self.assertEqual(isStateValid(state, relations), True)
    def test_isStateValid_lower_boundary_derivatives(self):
        state_invalid = {
            "A": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.MINUS))
            },
        }
        state_valid = {
            "A": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS))
            },
        }
        relations = [{
            "type": "EX",
            "args": None,
            "Q1": ("A", "Q"),
            "Q2": ("A", "Q"),
        }]

        self.assertEqual(isStateValid(state_invalid, relations), False)
        self.assertEqual(isStateValid(state_valid, relations), True)
    def test_getRelationQuantities(self):
        state = {
            "B": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
            },
        }
        relation = {
            "type": "P+",
            "args": None,
            "Q1": ("B", "Q"),
            "Q2": ("C", "Q"),
        }

        head, tail = getRelationQuantities(state, relation)
        self.assertEqual(
            head, Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS)))
        self.assertEqual(
            tail, Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO)))
Example #9
0
    def test_i_minus_inactive(self):
        q1 = Quantity(Magnitude(MValue.ZERO), None)
        q2 = Quantity(None, Derivative(DValue.ZERO))

        r.influenceNegative(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.ZERO)
Example #10
0
    def test_i_plus_active(self):
        q1 = Quantity(Magnitude(MValue.PLUS), None)
        q2 = Quantity(None, Derivative(DValue.ZERO))

        r.influencePositive(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.PLUS)
Example #11
0
 def copy(self):
     return Quantity(Magnitude(self.magnitude.value, self.magnitude.upperBound), Derivative(self.derivative.value))
Example #12
0
    def __init__(self):
        rospy.init_node('env_monitor', log_level=rospy.DEBUG)

        # load ros parameters
        # mongo server location, database and collection
        # They are global config params, see global.yaml in ais_rosparam
        self.mongoServer = rospy.get_param("/ais_hostname", "192.168.1.182")
        self.mongoPort = int(rospy.get_param('/mongoPort', '27017'))
        self.mongoDBName = str(rospy.get_param('/domoticDBName', 'openhab'))
        self.mongoCollName = str(rospy.get_param('/domoticCollName',
                                                 'openhab'))
        #self.sensorConfigFile=str(rospy.get_param('~yamlConfigFile','../../cfg/sensorConfigFDG.yaml'))
        self.sensorConfig = rospy.get_param('~sensorConfig')

        #NCP parameters
        self.targetCD = str(rospy.get_param('/targetCD', '123456'))
        self.userName = rospy.get_param('/NCPuserName', 'manuel.carmona')
        self.myPass = rospy.get_param('/NCPPass', 'Prova!2016')
        self.baseURL = rospy.get_param(
            '/NCPbaseURL', 'https://secure.tesanonline.it/pac/api')

        self.urlLogIn = self.baseURL + '/account/login'
        self.urlLogout = self.baseURL + '/account'
        self.urlCareplan = self.baseURL + '/careplan/' + self.targetCD + '/ENRI'

        # load yaml parameters
        # data previously hardcoded
        # mongoSensorNameList =[ ['Env_cO'], ['Env_particleCount'],  ['Env_vOCResistance'],        ['Env_temp','Kitchen_Multi_Temp','Entry_Multi_Temp','Lounge_Multi_Temp','Workshop_Multi_Temp','Office1_Multi_Temp','Office2_Multi_Temp'],          ['Office1_Multi_Humid','Env_humidity'],         ['Env_light','Kitchen_Multi_Lux','Entry_Multi_Lux','Lounge_Multi_Lux','Workshop_Multi_Lux','Office1_Multi_Lux','Office2_Multi_Lux' ]      ]
        # updatePeriodList    =[ 10,          10,                     10,        30,          120,         300]
        #self.loadYAML()

        # load data from NCP
        # data previously hardcoded
        # magnitudeNamesList  =[ 'CO',        'particleCount',       'VOC',     'temp',      'humidity', 'light']
        # comfortRangesList   =[ [0, 60],    [0, 20],                [85,115],   [18,24],     [30,50],    [100,250] ]
        # safeRangesList      =[ [0, 70],    [0, 25],                [65,135],   [16,25],     [30,60],    [float('-Inf'),float('Inf')] ]
        self.loadDataNCP()

        self.magnitudesDict = dict()

        for i in range(0, len(self.magnitudeNamesList)):
            # According to data from NCP
            name = self.magnitudeNamesList[i]
            comfortRange = self.comfortRangesList[i]
            safeRange = self.safeRangesList[i]

            # Get updatePeriod and mongoList for magnitude
            for entry in self.sensorConfig:
                if entry['magnitude'] == name:
                    updatePeriod = entry['updateTime']
                    mongoSensorNameL = entry['mongoNames']

                    for sensorName in mongoSensorNameL:
                        m = Magnitude(name, updatePeriod, self.targetCD)
                        m.setComfortRanges(comfortRange[0], comfortRange[1])
                        m.setSafeRanges(safeRange[0], safeRange[1])
                        # UEM sensor provides particle count in pp/0.01CF but most regulations offer values in ug/m3
                        # this tells magnitude class to cast values provided by low level sensor
                        if name is 'particleCount':
                            m.setHumidityMongoSensor('Env_humidity')
                        m.configureMongo(sensorName, self.mongoServer,
                                         self.mongoPort, self.mongoDBName,
                                         self.mongoCollName)
                        m.start()
                        self.magnitudesDict[sensorName] = m

        rospy.loginfo('Environment monitor server now active')
        rospy.spin()
Example #13
0
def test_newmag():
    lb = Magnitude(0.45359237, kg=1)
    new_mag('lb', lb)
    assert lb.val - 0.45359237 < 1e-6
    assert lb.unit == [0, 0, 0, 1, 0, 0, 0, 0, 0]
    assert mg(1, 'lb') == lb
 def test_isStateValid_complexValid(self):
     state = {
         "A": {
             "Q": Quantity(Magnitude(0), Derivative(0))
         },
         "B": {
             "Q": Quantity(Magnitude(0), Derivative(0))
         },
         "C": {
             "Q": Quantity(Magnitude(0), Derivative(0))
         },
     }
     relations = [
         {
             "type": "EX",
             "args": 1,
             "Q1": ("A", "Q"),
             "Q2": ("A", "Q"),
         },
         {
             "type": "I+",
             "args": None,
             "Q1": ("A", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "I-",
             "args": None,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "P+",
             "args": None,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.MAX,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.ZERO,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.MAX,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.ZERO,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
     ]
     self.assertEqual(isStateValid(state, relations), True)
Example #15
0
    def test_i_minus_active_boundary(self):
        q1 = Quantity(Magnitude(MValue.PLUS), None)
        q2 = Quantity(None, Derivative(DValue.MINUS))

        r.influenceNegative(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.MINUS)
Example #16
0
    def test_c_active(self):
        q1 = Quantity(Magnitude(MValue.MAX), None)
        q2 = Quantity(Magnitude(MValue.PLUS), None)

        r.getFunc("VC", MValue.MAX)(q1, q2)
        self.assertEqual(q2.magnitude.value, MValue.MAX)
Example #17
0
#Only create node classes once, when module is first loaded
DEFAULT_DUMMY_NODE = BaseNode() #Used if name of node is not in the dictionary

NODE_DICTIONARY = {'HalfScaleGaussian'  : HalfScaleGaussian(),
                   'Subtract'           : Subtract(),
                   'Threshold'          : Threshold(),
                   'Sobel3x3'           : Sobel3x3(),
                   'AbsDiff'            : AbsDiff(),
                   'ConvertDepth'       : ConvertDepth(),
                   'Dilate3x3'          : Dilate3x3(),
                   'Erode3x3'           : Erode3x3(),
                   'Add'                : Add(),
                   'Multiply'           : Multiply(),
                   'ScaleImage'         : ScaleImage(),
                   'Magnitude'          : Magnitude(),
                   'TableLookup'        : TableLookup(),
                   'Or'                 : Or(),
                   'And'                : And(),
                   'WarpAffine'         : WarpAffine(),
                   'DubbelIoTest'       : DubbelIoTest(), #This is a dummy node used only for testing the parser.
                   'Dilate2x2'          : Dilate2x2(),
                   'Erode2x2'           : Erode2x2()
                   }

#This dict gives the first parameter index for the first input image in the vxCreateXXXNode function call
#Note that the vx_graph parameter is not counted when accessing node parameters
#Therefore the first index starts on 0, for the first parameter AFTER vx_graph.
FIRST_INPUT_IMAGE_INDEX_DICT = {'HalfScaleGaussian' :    0,
                                'Subtract'          :    0,
                                'Threshold'         :    0,