def estimate_motion(): """Saves the motion level of the patient into the ontology""" clock = graph.value(home["clock"], qol["hasValue"]).toPython() # set the room motions in the window frame for room in rooms.values(): room.motion = 0 for sensor in room.sensors: # We exclude the old states sensor.on_states = [state for state in sensor.on_states if clock - state <= TIME_WINDOW] room.motion = room.motion + len(sensor.on_states) # integrate new updates to the room motions for uri, sensor in sensors.items(): try: last_update = graph.value(uri, qol["lastUpdate"]).toPython() except AttributeError: last_update = None if not sensor.on_states or last_update != sensor.on_states[-1]: state = graph.value(uri, qol["hasCurrentState"]) if state is not None and (state, qol["indicateLocation"], Literal(True)) in graph: sensor.on_states.append(last_update) sensor.room.motion = sensor.room.motion + 1 # update the ontology with the room motions for uri, room in rooms.items(): graph.set((uri, qol["motionMeasured"], Literal(room.motion))) # update the ontology with the house motion home_motion = sum([room.motion for room in rooms.values()]) graph.set((house, qol["motionMeasured"], Literal(home_motion)))
def save(eye_dump): """Place the euler output into the ontology""" dump_graph = Graph() dump_graph.parse(data=eye_dump, format='n3') ts = Namespace("file://%s/infer-triplestore#" % ONTOLOGY_DIR) for action, formula in dump_graph.predicate_objects(ts['n3store']): if action in (ts['update'], ts['add'], ts['remove']): for s, p, o in formula: if action == ts['update']: graph.set((s, p, o)) elif action == ts['add']: graph.add((s, p, o)) elif action == ts['remove']: graph.remove((s, p, o))
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))