def fire_action(self, action): model = self.model log.info("running action %s" % action) for res in RDF.SPARQLQuery(self.prefixes + ''' SELECT ?light ?level WHERE { <%s> room:lightLevel [room:light ?light; room:to ?level] . }''' % action).execute(model): self.do_lightlevel(res['light'].uri, res['level']) for res in RDF.SPARQLQuery(self.prefixes + ''' SELECT ?command WHERE { <%s> room:lightCommand ?command . }''' % action).execute(model): self.light_cmd(res['command']) for res in RDF.SPARQLQuery(self.prefixes + ''' SELECT ?command WHERE { <%s> room:execute ?command. }''' % action).execute(model): log.info("running %s" % str(res['command'])) self.system_cmd(str(res['command']))
def fire(self, pred, obj): """ fire actions with given pred/obj (both must be quoted n3) """ try: self.load_rdf() except ValueError: log.debug("cannot search for actions") return model = self.model matches = 0 for res in RDF.SPARQLQuery(self.prefixes + ''' SELECT ?action WHERE { [%s %s; room:triggers ?action] . }''' % (pred, obj)).execute(model): action = res['action'].uri matches = matches + 1 self.fire_action(action) if matches == 0: log.debug("no commands matched %r %r" % (pred, obj)) return matches
def localEvaluation(modelSourceUri, queryString): model = RDF.Model() # TODO add possibility to explicitely set the content type and register things like namespaces = ttlParser.namespaces_seen() model.load(modelSourceUri) query = RDF.SPARQLQuery(queryString) result = query.execute(model) printQueryResults(result)
def _query(self, sparql): if isinstance(sparql, unicode): sparql = sparql.encode('utf8') query = RDF.SPARQLQuery(sparql) try: result = query.execute(self._model) except RDF.RedlandError, err: self.logger.error("Error in query:\n%s",sparql) raise QueryError(err)
def _query(self, sparql): # if isinstance(sparql, unicode): # sparql = sparql.encode('utf8') query = RDF.SPARQLQuery(sparql) try: result = query.execute(self._model) except RDF.RedlandError as err: raise QueryError(err) return result
def sparql(self, query): query = RDF.SPARQLQuery(query) results = query.execute(self.model) return results
def query(self, qstr, base_uri=None): q = RDF.SPARQLQuery(qstr, base_uri=base_uri) results = q.execute(self._model) return results