def save_to_ontology(event): """Saving an event into the graph""" graph.set((home['clock'], qol['hasValue'], Literal(event['date']))) if 'signal' in event: # if we deal with a sensor event sparql_event = graph.query( event_query, initBindings={'sensorLabel': Literal(event['sensor']), 'stateLabel': Literal(event['signal'])}) for row in sparql_event: graph.set((row.sensor, qol['lastUpdate'], Literal(event['date']))) graph.set((row.sensor, qol['hasLastUpdate'], Literal(True))) graph.set((row.sensor, qol['hasCurrentState'], row.state))
class Sensor(object): def __init__(self, sensor, room): self.room = room self.sensor = sensor self.on_states = [] self.room.add_sensor(self) # INIT house = graph.value(predicate=RDF.type, object=qol["House"]) room_query = graph.query( """SELECT DISTINCT ?room WHERE { ?room rdf:type ?class . ?class rdfs:subClassOf* qol:Room . ?room qol:partOf ?house . }""", initBindings={"house": house}, initNs={"qol": qol}, ) rooms = {row.room: Room(row.room) for row in room_query} # We don't care of door sensors here sensor_query = graph.query( "SELECT DISTINCT ?sensor ?room WHERE { ?sensor qol:deployedIn ?room . }", initNs={"qol": qol} ) sensors = {row.sensor: Sensor(row.sensor, rooms[row.room]) for row in sensor_query} def estimate_motion(): """Saves the motion level of the patient into the ontology"""