Exemplo n.º 1
0
 def test_get_parameter_value(self):
     parameters = [["name1", "value1"], ["name2", "value2"]]
     self.assertEqual("value1",
                      RDFQuery.get_parameter_value("name1", parameters))
     self.assertEqual("value2",
                      RDFQuery.get_parameter_value("name2", parameters))
     self.assertIsNone(RDFQuery.get_parameter_value("name3", parameters))
Exemplo n.º 2
0
 def test_query(self):
     query = RDFQuery(subject=TemplateWordNode("Subject"),
                      predicate=TemplateWordNode("Predicate"),
                      object=TemplateWordNode("Object"))
     self.assertIsNotNone(query)
     self.assertEquals(query.query_type, RDFQuery.QUERY)
     self.assertEquals(
         "<q><subj>Subject</subj><pred>Predicate</pred><obj>Object</obj></q>",
         query.to_xml(None, None))
Exemplo n.º 3
0
 def test_not_query(self):
     query = RDFQuery(rdf_subject=TemplateWordNode("Subject"),
                      rdf_predicate=TemplateWordNode("Predicate"),
                      rdf_object=TemplateWordNode("Object"),
                      query_type=RDFQuery.NOT_QUERY)
     self.assertIsNotNone(query)
     self.assertEquals(query.query_type, RDFQuery.NOT_QUERY)
     self.assertEquals(
         "<notq><subj>Subject</subj><pred>Predicate</pred><obj>Object</obj></notq>",
         query.to_xml(None, None))
     self.assertEquals("not=( subj=Subject, pred=Predicate, obj=Object )",
                       query.to_string(None, None))
Exemplo n.º 4
0
    def parse_expression(self, graph, expression):

        subj = None
        pred = None
        obj = None

        for child in expression:
            tag_name = TextUtils.tag_from_text(child.tag)

            if tag_name == 'subj':
                subj = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'pred':
                pred = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'obj':
                obj = self.parse_children_as_word_node(graph, child)
            else:
                if logging.getLogger().isEnabledFor(logging.WARNING):
                    logging.warning("Unknown tag name [%s] in select query",
                                    tag_name)

        if subj is None:
            raise ParserException("<subj> element missing from select query")

        if pred is None:
            raise ParserException("<pred> element missing from select query")

        if obj is None:
            raise ParserException("<obj> element missing from select query")

        self._query = RDFUniqueStatement(
            RDFQuery(subj, pred, obj, RDFQuery.QUERY))
Exemplo n.º 5
0
 def test_execute_multi_vars(self):
     bot = TestBot()
     bot.brain.rdf.load_from_text("""
         ACCOUNT:hasPurpose:to track money
         ACCOUNT:hasSize:0
         ACCOUNT:hasSyllables:2
         ACCOUNT:isa:Concept
         ACCOUNT:lifeArea:Finances
         ACT:hasPurpose:to entertain by performing
     """)
     query = RDFQuery(subject=TemplateWordNode("ACCOUNT"),
                      predicate=TemplateWordNode("?x"),
                      object=TemplateWordNode("?y"))
     resultset = query.execute(bot, "testid")
     self.assertIsNotNone(resultset)
     self.assertEquals(5, len(resultset.results))
Exemplo n.º 6
0
    def parse_query(self, graph, query_name, query):

        if query_name == "q":
            query_type = RDFQuery.QUERY
        else:
            query_type = RDFQuery.NOT_QUERY

        for child in query:
            tag_name = TextUtils.tag_from_text(child.tag)

            if tag_name == 'subj':
                subj = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'pred':
                pred = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'obj':
                obj = self.parse_children_as_word_node(graph, child)
            else:
                if logging.getLogger().isEnabledFor(logging.WARNING):
                    logging.warning("Unknown tag name [%s] in select query",
                                    tag_name)

        if subj is None:
            raise ParserException("<subj> element missing from select query")

        if pred is None:
            raise ParserException("<pred> element missing from select query")

        if obj is None:
            raise ParserException("<obj> element missing from select query")

        self._query.queries.append(RDFQuery(subj, pred, obj, query_type))
Exemplo n.º 7
0
    def test_select_multi_query(self):
        bot = TestBot()
        self.load_data(bot.brain.rdf)

        select = RDFSelectStatement(["?x"], [
            RDFQuery(rdf_subject=TemplateWordNode("?x"),
                     rdf_predicate=TemplateWordNode("legs"),
                     rdf_object=TemplateWordNode("2")),
            RDFQuery(rdf_subject=TemplateWordNode("?x"),
                     rdf_predicate=TemplateWordNode("feathers"),
                     rdf_object=TemplateWordNode("yes"))
        ])
        self.assertIsNotNone(select)

        result = select.execute(bot, "testid")
        self.assertIsNotNone(result)
        self.assertEquals([['?x', 'BIRD']], result)
Exemplo n.º 8
0
    def test_to_xml(self):
        root = TemplateNode()
        query = RDFQuery(subject=TemplateWordNode("S"), predicate=TemplateWordNode("P"), object=TemplateWordNode("O"))
        statement = RDFUniqueStatement(query)
        node = TemplateUniqNode(statement)
        root.append(node)

        xml = root.xml_tree(self.bot, self.clientid)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual("<template><uniq><subj>S</subj><pred>P</pred><obj>O</obj></uniq></template>", xml_str)
Exemplo n.º 9
0
    def test_node_no_defaults(self):
        root = TemplateNode()
        query = RDFQuery(subject=TemplateWordNode("S"), predicate=TemplateWordNode("P"), object=TemplateWordNode("O"))
        statement = RDFUniqueStatement(query)
        node = TemplateUniqNode(statement)
        self.assertIsNotNone(node.query)
        self.assertIsInstance(node.query, RDFUniqueStatement)
        self.assertEquals(node.query, statement)

        root.append(node)
        result = root.resolve(self.bot, self.clientid)
        self.assertIsNotNone(result)
        self.assertEquals("", result)
Exemplo n.º 10
0
    def test_select_single_query(self):
        bot = TestBot()
        self.load_data(bot.brain.rdf)

        select = RDFSelectStatement(["?x"], [
            RDFQuery(subject=TemplateWordNode("?x"),
                     predicate=TemplateWordNode("legs"),
                     object=TemplateWordNode("2"))
        ])
        self.assertIsNotNone(select)

        result = select.execute(bot, "testid")
        self.assertIsNotNone(result)
        self.assertEquals([['?x', 'MONKEY'], ['?x', 'BIRD']], result)
Exemplo n.º 11
0
    def test_select_single_not_query(self):
        bot = TestBot()
        self.load_data(bot.brain.rdf)

        select = RDFSelectStatement(["?x"], [
            RDFQuery(rdf_subject=TemplateWordNode("?x"),
                     rdf_predicate=TemplateWordNode("legs"),
                     rdf_object=TemplateWordNode("2"),
                     query_type=RDFQuery.NOT_QUERY)
        ])
        self.assertIsNotNone(select)

        result = select.execute(bot, "testid")
        self.assertIsNotNone(result)
        self.assertEquals([['?x', 'BEAR'], ['?x', 'BEAR']], result)
Exemplo n.º 12
0
    def test_execute_single_var(self):
        bot = TestBot()
        bot.brain.rdf.load_from_text("""
            ACCOUNT:hasPurpose:to track money
            ACCOUNT:hasSize:0
            ACCOUNT:hasSyllables:2
            ACCOUNT:isa:Concept
            ACCOUNT:lifeArea:Finances
            ACT:hasPurpose:to entertain by performing
        """)
        query = RDFQuery(rdf_subject=TemplateWordNode("ACCOUNT"),
                         rdf_predicate=TemplateWordNode("hasSize"),
                         rdf_object=TemplateWordNode("?x"))

        self.assertEquals("query=( subj=ACCOUNT, pred=hasSize, obj=?x )",
                          query.to_string(None, None))

        resultset = query.execute(bot, "testid")
        self.assertIsNotNone(resultset)
        result = resultset.results[0]
        self.assertIsNotNone(result)
        self.assertEquals(result[0][1], "ACCOUNT")
        self.assertEquals(result[1][1], "hasSize")
        self.assertEquals(result[2][1], "0")
Exemplo n.º 13
0
    def test_execute_single_var(self):
        bot = TestBot()
        bot.brain.rdf.load_from_text("""
            ACCOUNT:hasPurpose:to track money
            ACCOUNT:hasSize:0
            ACCOUNT:hasSyllables:2
            ACCOUNT:isa:Concept
            ACCOUNT:lifeArea:Finances
            ACT:hasPurpose:to entertain by performing
        """)

        query = RDFQuery(subject=TemplateWordNode("ACCOUNT"),
                         predicate=TemplateWordNode("hasSize"),
                         object=TemplateWordNode("?x"))

        uniq = RDFUniqueStatement(query)

        result = uniq.execute(bot, "testid")
        self.assertIsNotNone(result)
        self.assertEquals(result, [['0']])