Пример #1
0
 def __init__(self):
     self.sensors = []
     self.route_map = {}
     self.reverse_route_map = {}
     self.variables = []
     self.snapper = Snapper(self)
     self.matrix = AssociationMatrix()
def test_should_add_relationship_to_relationships():
    Vx = Variable()
    Vy = Variable()
    Rxy = Relationship(Vx, Vy)
    Am = AssociationMatrix()
    Am.add_relationship(Rxy)
    assert Am.relationships == {frozenset((Vx.uuid, Vy.uuid)): Rxy}
def test_should_return_whole_dict():
    Rxy = SpearframeRelationship(Variable(), Variable())
    Rwz = SpearframeRelationship(Variable(), Variable())
    Am = AssociationMatrix()
    Am.add_relationship(Rxy)
    Am.add_relationship(Rwz)
    key1 = frozenset((Rxy.sensor_x.get_uuid(), Rxy.sensor_y.get_uuid()))
    key2 = frozenset((Rwz.sensor_x.get_uuid(), Rwz.sensor_y.get_uuid()))
    a = Am.get_value_matrix()
    assert a == {key1: 0.0, key2: 0.0}
def test_should_return_values_in_range(rel_25, rel_50, rel_75):
    association_matrix = AssociationMatrix()
    association_matrix.add_relationship(rel_25)
    association_matrix.add_relationship(rel_50)
    association_matrix.add_relationship(rel_75)
    min_val = 0.1
    max_val = 0.6
    sensor_pairs_in_range = \
        association_matrix.get_relationships_by_value_range(min_val, max_val)
    for key, value in sensor_pairs_in_range.items():
        assert min_val <= value <= max_val
def test_should_remove_specified_relationship():
    Vx = Variable()
    Vy = Variable()
    Rxy = Relationship(Vx, Vy)
    Rwz = Relationship(Variable(), Variable())
    Am = AssociationMatrix()
    Am.add_relationship(Rxy)
    Am.add_relationship(Rwz)
    Am.remove_relationship(Rwz)
    assert Am.relationships == {frozenset((Vx.uuid, Vy.uuid)): Rxy}
def test_should_return_requested_relationship():
    Rxy = Relationship(Variable(), Variable())
    Rwz = Relationship(Variable(), Variable())
    Am = AssociationMatrix()
    Am.add_relationship(Rxy)
    Am.add_relationship(Rwz)

    a = Am.get_relationship_from_sensors(Rxy.sensor_x, Rxy.sensor_y)
    assert a == Rxy

    b = Am.get_relationship_from_sensors(Rwz.sensor_x, Rwz.sensor_y)
    assert b == Rwz
Пример #7
0
class Manager:
    def __init__(self):
        self.sensors = []
        self.route_map = {}
        self.reverse_route_map = {}
        self.variables = []
        self.snapper = Snapper(self)
        self.matrix = AssociationMatrix()

    def set_window_size(self, new_window_size):
        self.snapper.set_window_size(new_window_size)

    def add_sensor(self, sensor):
        """
        This function adds a new sensor to the snapper module and generates
        a corresponding variable object with mappings.

        :return:
        """
        self.sensors.append(sensor)
        self.snapper.add_sensor(sensor)
        newVariable = Variable()

        for var in self.variables:
            relationship = SpearframeRelationship(newVariable, var)
            self.matrix.add_relationship(relationship)

        self.variables.append(newVariable)
        self.route_map[sensor.uuid] = newVariable
        self.reverse_route_map[str(newVariable.uuid)] = sensor.uuid

    def remove_sensor(self, sensor):
        """
        This function removes a new sensor to the snapper module, removes
        variable from manager, and removes the corresponding relationships
        from the matrix.

        :return:
        """
        variable = self.route_map[sensor.uuid]
        self.route_map.pop(sensor.uuid)
        self.reverse_route_map.pop(str(variable.uuid))

        for var in self.variables:
            if var == variable:
                pass
            else:
                relationship = SpearframeRelationship(variable, var)
                self.matrix.remove_relationship(relationship)

        self.snapper.remove_sensor(sensor)
        self.sensors.remove(sensor)
        self.variables.remove(variable)

    def get_matrix(self):
        """
        Returns the underlying matrix on demand.

        :return:
        """
        return self.matrix

    def get_value_matrix(self):
        """
        Returns the underlying matrix on demand.

        :return:
        """
        return self.matrix.get_value_matrix()

    def get_relationships_by_value_range(self, minvalue, maxvalue):
        """
        Returns the underlying matrix on demand.

        :return:
        """
        return self.matrix.get_relationships_by_value_range(minvalue, maxvalue)

    def on_data(self, snapshot):
        """
        Routes all data from incoming snapshot to the appropriate variables.

        :param snapshot:
        :return:
        """
        start = snapshot.pop("start")
        stop = snapshot.pop("end")
        for sensorID in snapshot:
            variable = self.route_map[sensorID]
            value = snapshot[sensorID]
            variable.on_data(value, start, stop)

    def get_relationship_from_sensors(self, sensor1, sensor2):
        return self.matrix.get_relationship_from_sensors(sensor1, sensor2)

    def get_all_relationships(self):
        return self.matrix.relationships
def test_should_have_relationships_field_as_empty_dictionary_on_init():
    associationMatrix = AssociationMatrix()
    assert associationMatrix.relationships == dict()