def test_resultset_empty_results(self): resultset = RDFQueryResultSet("Subject", "Predicate", "Object", []) self.assertIsNotNone(resultset) self.assertIsNotNone(resultset.subject) self.assertIsNotNone(resultset.predicate) self.assertIsNotNone(resultset.object) self.assertEquals([], resultset.results) self.assertEquals("", resultset.to_string())
def execute(self, bot, clientid, parameters=None): rdf_subject = self._subject_node.resolve(bot, clientid) rdf_predicate = self._predicate_node.resolve(bot, clientid) rdf_object = self._object_node.resolve(bot, clientid) # Now see if any are variables rather than data if rdf_subject.startswith("?"): if parameters is not None: subj_val = RDFQuery.get_parameter_value(rdf_subject, parameters) else: subj_val = None else: subj_val = rdf_subject if rdf_predicate.startswith("?"): if parameters is not None: pred_val = RDFQuery.get_parameter_value(rdf_predicate, parameters) else: pred_val = None else: pred_val = rdf_predicate if rdf_object.startswith("?"): if parameters is not None: obj_val = RDFQuery.get_parameter_value(rdf_object, parameters) else: obj_val = None else: obj_val = rdf_object # Query using subj, pred and obj data if self.query_type == RDFQuery.QUERY: entities = bot.brain.rdf.match(rdf_subject=subj_val, rdf_predicate=pred_val, rdf_object=obj_val) else: entities = bot.brain.rdf.not_match(rdf_subject=subj_val, rdf_predicate=pred_val, rdf_object=obj_val) results = [] for entity in entities: result = [] if rdf_subject.startswith("?"): result.append([rdf_subject, entity.subject]) else: result.append([None, entity.subject]) if rdf_predicate.startswith("?"): result.append([rdf_predicate, entity.predicate]) else: result.append([None, entity.predicate]) if rdf_object.startswith("?"): result.append([rdf_object, entity.object]) else: result.append([None, entity.object]) results.append(result) return RDFQueryResultSet(rdf_subject, rdf_predicate, rdf_object, results)
def test_entity_defaults(self): entity = RDFEntity() self.assertIsNotNone(entity) self.assertIsNone(entity.subject) self.assertIsNone(entity.predicate) self.assertIsNone(entity.object) resultset = RDFQueryResultSet("Subject", "Predicate", "Object", []) self.assertEquals("( )", entity.to_string(resultset)) self.assertEquals("<subj></subj><pred></pred><obj></obj>", entity.to_xml(None, None))
def test_resultset_with_result(self): results = [ RDFEntity(rdf_subject="Subject1", rdf_predicate="Predicate1", rdf_object="Object1") ] resultset = RDFQueryResultSet("Subject", "Predicate", "Object", results) self.assertIsNotNone(resultset) self.assertIsNotNone(resultset.subject) self.assertIsNotNone(resultset.predicate) self.assertIsNotNone(resultset.object) self.assertEquals(results, resultset.results) self.assertEquals("( _=Subject1, _=Predicate1, _=Object1 )\n", resultset.to_string()) self.assertEquals("Subject1", resultset.get_variable_value("Subject", results[0])) self.assertEquals( "Predicate1", resultset.get_variable_value("Predicate", results[0])) self.assertEquals("Object1", resultset.get_variable_value("Object", results[0]))
def test_entity_variables(self): entity = RDFEntity(rdf_subject="Subject", rdf_predicate="Predicate", rdf_object="Object") self.assertIsNotNone(entity) self.assertIsNotNone(entity.subject) self.assertEquals("Subject", entity.subject) self.assertIsNotNone(entity.predicate) self.assertEquals("Predicate", entity.predicate) self.assertIsNotNone(entity.object) self.assertEquals("Object", entity.object) resultset = RDFQueryResultSet("?x", "?y", "?z", []) self.assertEquals("( ?x=Subject, ?y=Predicate, ?z=Object )", entity.to_string(resultset)) self.assertEquals( "<subj>Subject</subj><pred>Predicate</pred><obj>Object</obj>", entity.to_xml(None, None))
def test_resultset(self): resultset = RDFQueryResultSet(TemplateWordNode("Subject"), TemplateWordNode("Predicate"), TemplateWordNode("Object"), []) self.assertIsNotNone(resultset)