Example #1
0
    def test_exception_handling(self):

        root = TemplateNode()
        node = MockTemplateAddTripleNode(
            RDFEntity(rdf_subject="S", rdf_predicate="P", rdf_object="O"))
        root.append(node)

        with self.assertRaises(Exception):
            node.resolve_to_string(self.bot, self.clientid)

        self.assertEquals("", root.resolve(self.bot, self.clientid))
Example #2
0
    def test_to_xml(self):
        root = TemplateNode()
        node = TemplateDeleteTripleNode(
            RDFEntity(rdf_subject="S", rdf_predicate="P", rdf_object="O"))
        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><deletetriple><subj>S</subj><pred>P</pred><obj>O</obj></deletetriple></template>",
            xml_str)
Example #3
0
    def test_resultset_with_result2(self):

        results = [
            RDFEntity(rdf_subject="Subject1",
                      rdf_predicate="Predicate1",
                      rdf_object="Object1"),
            RDFEntity(rdf_subject="Subject2",
                      rdf_predicate="Predicate2",
                      rdf_object="Object2")
        ]

        resultset = RDFQueryResultSet("Subject", "Predicate", "Object",
                                      results)
        self.assertIsNotNone(resultset)
        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( _=Subject2, _=Predicate2, _=Object2 )\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]))

        self.assertEquals("Subject2",
                          resultset.get_variable_value("Subject", results[1]))
        self.assertEquals(
            "Predicate2", resultset.get_variable_value("Predicate",
                                                       results[1]))
        self.assertEquals("Object2",
                          resultset.get_variable_value("Object", results[1]))
Example #4
0
    def add_entity(self, rdf_subject, rdf_predicate, rdf_object):
        if self.has_subject(rdf_subject) is False:
            self._subjects[rdf_subject] = {}

        if self.has_predicate(rdf_subject, rdf_predicate) is False:
            self._subjects[rdf_subject][rdf_predicate] = {}

        if self.has_object(rdf_subject, rdf_predicate, rdf_object) is False:
            entity = RDFEntity(rdf_subject, rdf_predicate, rdf_object)
            self._subjects[rdf_subject][rdf_predicate][rdf_object] = entity
            self._entities.append(entity)
        else:
            if logging.getLogger().isEnabledFor(logging.WARNING):
                logging.warning("Duplicate RDF Entity [%s][%s][%s]",
                                rdf_subject, rdf_predicate, rdf_object)
Example #5
0
    def parse_expression(self, graph, expression):
        subject = None
        predicate = None
        object = None

        if 'subj' in expression.attrib:
            subject = graph.get_word_node(expression.attrib['subj'])

        if 'pred' in expression.attrib:
            predicate = graph.get_word_node(expression.attrib['pred'])

        if 'obj' in expression.attrib:
            object = graph.get_word_node(expression.attrib['obj'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'subj':
                subject = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'pred':
                predicate = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'obj':
                object = self.parse_children_as_word_node(graph, child)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)

        if subject is None:
            raise ParserException(
                "<%s> node missing subject attribue/element" % self.node_name)

        if predicate is None:
            raise ParserException(
                "<%s> node missing predicate attribue/element" %
                self.node_name)

        if object is None:
            raise ParserException("<%s> node missing object attribue/element" %
                                  self.node_name)

        self._entity = RDFEntity(subject=subject,
                                 predicate=predicate,
                                 object=object)
Example #6
0
    def process_splits(self, splits):
        rdf_subject = splits[0]
        rdf_predicate = splits[1]
        rdf_object = splits[2]

        if rdf_subject not in self._subjects:
            self._subjects[rdf_subject] = {}

        if rdf_predicate not in self._subjects[rdf_subject]:
            self._subjects[rdf_subject][rdf_predicate] = {}

        if object not in self._subjects[rdf_subject][rdf_predicate]:
            self._subjects[rdf_subject][rdf_predicate][rdf_object] = {}

        entity = RDFEntity(rdf_subject, rdf_predicate, rdf_object)
        self._subjects[rdf_subject][rdf_predicate][rdf_object] = entity
        self._entities.append(entity)

        return True
Example #7
0
    def process_splits(self, splits):
        subject = splits[0]
        predicate = splits[1]
        object = splits[2]

        if subject not in self._subjects:
            self._subjects[subject] = {}

        if predicate not in self._subjects[subject]:
            self._subjects[subject][predicate] = {}

        if object not in self._subjects[subject][predicate]:
            self._subjects[subject][predicate][object] = {}

        entity = RDFEntity(subject, predicate, object)
        self._subjects[subject][predicate][object] = entity
        self._entities.append(entity)

        return True
Example #8
0
 def test_entity_defaults(self):
     entity = RDFEntity()
     self.assertIsNotNone(entity)
     self.assertIsNone(entity.subject)
     self.assertIsNone(entity.predicate)
     self.assertIsNone(entity.object)