Beispiel #1
0
    def test_property_hierachy(self):
        rdf_content = rdf_prefix + '''
:code a owl:DatatypeProperty ; .

:iso_code a owl:DatatypeProperty ;
    rdfs:subPropertyOf :code ; .

:cameo_code a owl:DatatypeProperty ;
    rdfs:subPropertyOf :code ; .

:iso_country_code a owl:DatatypeProperty ;
    rdfs:subPropertyOf :iso_code ; .

:adm1_code a owl:DatatypeProperty ;
    rdfs:subPropertyOf :iso_code ; .
        '''
        ontology = Ontology(rdf_content)
        self.assertEqual(len(ontology.all_classes()), 0)
        self.assertEqual(len(ontology.all_properties()), 5)
        code = ontology.get_entity(DIG.code.toPython())
        self.assertIsNotNone(code)
        self.assertIsInstance(code, OntologyDatatypeProperty)
        iso_code = ontology.get_entity(DIG.iso_code.toPython())
        self.assertIsNotNone(iso_code)
        iso_country_code = ontology.get_entity(DIG.iso_country_code.toPython())
        self.assertIsNotNone(iso_country_code)
        self.assertSetEqual(iso_code.super_properties(), {code})
        self.assertSetEqual(iso_country_code.super_properties_closure(),
                            {code, iso_code})
Beispiel #2
0
 def __init__(self, ontology: Ontology):
     self.ontology = ontology
     self.classes = ontology.classes
     self.properties = ontology.all_properties()
     self.data_properties = ontology.data_properties
     self.object_properties = ontology.object_properties
     self.item = '<h4 id="{}">{}</h4>\n<div class="entity">\n<table>\n{}\n</table>\n</div>'
     self.row = '<tr>\n<th align="right" valign="top">{}</th>\n<td>{}</td>\n</tr>'
Beispiel #3
0
    def test_basic_entity_info(self):
        rdf_content = rdf_prefix + '''
:Entity a owl:Class ;
    rdfs:label "Entity" ;
    skos:definition """Everything that can be described, similar to owl:Thing.""" ;
    :common_properties :label, :title, :description ;
    .
        '''
        ontology = Ontology(rdf_content)
        self.assertEqual(len(ontology.all_classes()), 1)
        for entity in ontology.all_classes():
            self.assertIsInstance(entity, OntologyEntity)
            self.assertIsInstance(entity, OntologyClass)
        self.assertEqual(len(ontology.all_properties()), 0)
        for entity in ontology.all_properties():
            self.assertIsInstance(entity, OntologyEntity)
            self.assertIsInstance(entity, OntologyProperty)
        self.assertEqual(len(ontology.root_classes()), 1)
        for entity in ontology.root_classes():
            self.assertIsInstance(entity, OntologyClass)
            self.assertEqual(len(entity.super_classes()), 0)
Beispiel #4
0
    def test_ontology_schema_org_datatype(self):
        import json

        rdf_content = rdf_prefix + '''
        schema:Person a owl:Class ; .
        schema:Text a schema:DataType, rdfs:Class ; .
        schema:name
              a rdf:Property ;
              schema:domainIncludes schema:Person ;
              schema:rangeIncludes schema:Person .
        '''
        kg = '''
        {
          "@type": ["http://schema.org/Person"],
          "@id": "person1",
          "http://schema.org/name": "Person Name"
        }
        '''

        ontology = Ontology(rdf_content)
        for entity in ontology.all_classes():
            self.assertIsInstance(entity, OntologyEntity)
            self.assertIsInstance(entity, OntologyClass)
        self.assertEqual(len(ontology.all_properties()), 1)

        return_value = ontology.is_valid('schema:name',
                                          "person name1",
                                          json.loads(kg))
        self.assertEqual(return_value, {'@id': 'person name1'})

        rdf_content = rdf_prefix + '''
                schema:Person a owl:Class ; .
                schema:DataType a owl:Class ; .
                schema:Text a schema:DataType, owl:Class ; .
                schema:name
                      a rdf:Property ;
                      schema:domainIncludes schema:Person ;
                      schema:rangeIncludes schema:Text .
                '''
        ontology = Ontology(rdf_content)
        kg = '''
                {
                  "@type": ["http://schema.org/Person"],
                  "@id": "person1",
                  "http://schema.org/name": "Person Name"
                }
                '''
        return_value = ontology.is_valid('schema:name',
                                         "person name1",
                                         json.loads(kg))
        self.assertEqual(return_value, {'@value': 'person name1'})
Beispiel #5
0
    def test_inverse_object_property(self):
        rdf_content = rdf_prefix + '''
:moved_to a owl:ObjectProperty ;
    :inverse :was_destination_of .
        '''
        ontology = Ontology(rdf_content)
        self.assertEqual(len(ontology.all_properties()), 2)
        property_moved = ontology.get_entity(DIG.moved_to.toPython())
        property_inverse = ontology.get_entity(DIG.was_destination_of.toPython())
        self.assertIsNotNone(property_moved)
        self.assertIsNotNone(property_inverse)
        self.assertTrue(property_moved.is_primary())
        self.assertFalse(property_inverse.is_primary())
        self.assertEqual(property_moved.inverse(), property_inverse)
        self.assertEqual(property_inverse.inverse(), property_moved)
Beispiel #6
0
    def test_ontology_schema_org(self):
        import json

        rdf_content = rdf_prefix + '''
        schema:Person a owl:Class ; .
        schema:relatedTo
              a rdf:Property ;
              schema:domainIncludes schema:Person ;
              schema:rangeIncludes schema:Person .
        '''
        kg = '''
        {
          "@type": ["http://schema.org/Person"],
          "@id": "person1",
          "http://schema.org/relatedTo": {
            "@type": ["http://schema.org/Person"],
            "@id": "person2"
          }
        }
        '''

        ontology = Ontology(rdf_content)
        for entity in ontology.all_classes():
            self.assertIsInstance(entity, OntologyEntity)
            self.assertIsInstance(entity, OntologyClass)
        self.assertEqual(len(ontology.all_properties()), 1)

        kg_json = json.loads(kg)
        config = ontology.merge_with_master_config(kg_json,
                                                   delete_orphan_fields=True)
        self.assertTrue('fields' in config)
        fields = config['fields']
        self.assertIn('relatedTo', fields)
        #
        self.assertTrue(
            ontology.is_valid('schema:relatedTo',
                              kg_json["http://schema.org/relatedTo"], kg_json))