Example #1
0
    def test_property_domain(self):
        rdf_content = rdf_prefix + '''
:Entity a owl:Class .
:Type a owl:Class .
:Place a owl:Class .
:Other a owl:Class ;
    :common_properties :code .
:code a owl:DatatypeProperty ;
    rdfs:domain [
        a owl:Class ;
        owl:unionOf (
            :Type
            :Place
        )
    ] ;
    schema:domainIncludes :Entity .
        '''
        ontology = Ontology(rdf_content)
        property_code = ontology.get_entity(DIG.code.toPython())
        self.assertIsInstance(property_code, OntologyDatatypeProperty)
        self.assertEqual(len(property_code.included_domains()), 4)
        self.assertSetEqual(
            property_code.included_domains(),
            set(
                map(ontology.get_entity,
                    map(str, (DIG.Entity, DIG.Type, DIG.Other, DIG.Place)))))
Example #2
0
    def test_property_merge_config_closest_inherit(self):
        rdf_content = rdf_prefix + '''
:had_participant a owl:ObjectProperty ; .
:carried_out_by a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ; .
:custody_received_by a owl:ObjectProperty ;
    owl:subPropertyOf :carried_out_by ; .
        '''
        ontology = Ontology(rdf_content)
        config = ontology.merge_with_master_config(
            '{"fields":{"had_participant":{"color":"red"},'
            '"carried_out_by":{"color":"blue"}}}')
        self.assertTrue('fields' in config)
        fields = config['fields']
        self.assertIn('had_participant', fields)
        property_ = fields['had_participant']
        self.assertIn('color', property_)
        self.assertEqual('red', property_['color'])
        self.assertIn('carried_out_by', fields)
        sub_property = fields['carried_out_by']
        self.assertIn('color', sub_property)
        self.assertEqual('blue', sub_property['color'])
        self.assertIn('custody_received_by', fields)
        subsub = fields['custody_received_by']
        self.assertIn('color', subsub)
        self.assertEqual(sub_property['color'], subsub['color'])
Example #3
0
 def setUp(self):
     ontology_content = '''
             @prefix : <http://dig.isi.edu/ontologies/dig/> .
             @prefix owl: <http://www.w3.org/2002/07/owl#> .
             @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
             @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
             @prefix schema: <http://schema.org/> .
             @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
             :Person a owl:Class ;
                 rdfs:subClassOf :Actor, :Biological_Object ;
                 :common_properties :label, :title, :religion ; .
             :has_name a owl:DatatypeProperty ;
                 schema:domainIncludes :Person ;
                 schema:rangeIncludes xsd:string ; .
             :has_child a owl:ObjectProperty ;
                 schema:domainIncludes :Person ;
                 schema:rangeIncludes :Person ; .
         '''
     ontology = Ontology(ontology_content,
                         validation=False,
                         include_undefined_class=True,
                         quiet=True)
     kg_schema = KGSchema(ontology.merge_with_master_config(dict()))
     etk = ETK(kg_schema=kg_schema,
               ontology=ontology,
               generate_json_ld=True)
     etk2 = ETK(kg_schema=kg_schema,
                ontology=ontology,
                generate_json_ld=False)
     self.doc = etk.create_document(dict(),
                                    doc_id='http://xxx/1',
                                    type_=[DIG.Person.toPython()])
     self.doc2 = etk2.create_document(dict(),
                                      doc_id='http://xxx/2',
                                      type_=[DIG.Person.toPython()])
Example #4
0
 def test_property_merge_master_config(self):
     rdf_content = rdf_prefix + ':had_participant a owl:ObjectProperty . '
     ontology = Ontology(rdf_content)
     d_color = 'red'
     config = ontology.merge_with_master_config('{}', {'color': d_color})
     self.assertIn('fields', config)
     fields = config['fields']
     self.assertIn('had_participant', fields)
     property_ = fields['had_participant']
     self.assertIn('color', property_)
     self.assertEqual(property_['color'], d_color)
Example #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)
Example #6
0
    def test_ontology_api_is_valid_with_empty_kg(self):
        import json

        rdf_content = rdf_prefix + '''
:Place a owl:Class ;
    :common_properties :region ; .
:region a owl:DatatypeProperty ;
    schema:domainIncludes :Place ;
    schema:rangeIncludes xsd:string ; .
            '''
        kg = '{}'
        ontology = Ontology(rdf_content)
        self.assertTrue(ontology.is_valid('region', 'somewhere', json.loads(kg)))
        self.assertFalse(ontology.is_valid('region', 1, json.loads(kg)))
        self.assertFalse(ontology.is_valid('region', True, json.loads(kg)))
Example #7
0
    def test_property_merge_config_delete_orphan(self):
        rdf_content = rdf_prefix + '''
:had_participant a owl:ObjectProperty ; .
:carried_out_by a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ; .
        '''
        ontology = Ontology(rdf_content)
        config = ontology.merge_with_master_config('{"fields":{"had_participant":{"color":"red"},'
                                                   '"custody_received_by":{"color":"blue"}}}',
                                                   delete_orphan_fields=True)
        self.assertTrue('fields' in config)
        fields = config['fields']
        self.assertIn('had_participant', fields)
        self.assertIn('carried_out_by', fields)
        self.assertNotIn('custody_received_by', fields)
Example #8
0
    def test_property_range_inherit_consistency(self):
        rdf_content = rdf_prefix + '''
:Event a owl:Class ; .
:Entity a owl:Class ;
    .
:Group a owl:Class ;
    rdfs:subClassOf :Entity ;
    .
:Actor a owl:Class ;
    rdfs:subClassOf :Group ;
    .
:Physical_Object a owl:Class ;
    rdfs:subClassOf :Entity ;
    .
:Man_Made_Thing a owl:Class ;
    rdfs:subClassOf :Entity ;
    .
:had_participant a owl:ObjectProperty ;
    schema:rangeIncludes :Actor ;
    .
:transferred_custody_of a owl:ObjectProperty ;
    schema:rangeIncludes :Physical_Object, :Man_Made_Thing ;
    owl:subPropertyOf :had_participant ;
    .
        '''
        with self.assertRaises(ValidationError):
            Ontology(rdf_content)
Example #9
0
    def test_class_cycle_detection(self):
        rdf_content = rdf_prefix + '''
:Entity a owl:Class ;
    rdfs:label "Entity" ;
    skos:definition """Everything that can be described, similar to owl:Thing.""" ;
    owl:subClassOf :Actor ;
    :common_properties :label, :title, :description ;
    :crm_equivalent crm:E1_CRM_Entity
    .

:Group a owl:Class ;
    rdfs:label "Group" ;
    skos:definition """A """ ;
    rdfs:subClassOf :Entity ;
    :crm_equivalent crm:E74_Group ;
    :common_properties :label, :title ;
    .

:Actor a owl:Class ;
    rdfs:label "Actor" ;
    skos:definition """A group, organization, person who have the potential to do actions.""" ;
    rdfs:subClassOf :Group ;
    :crm_equivalent crm:E39_Actor ;
    :common_properties :label, :title, :religion ;
    .
        '''
        with self.assertRaises(ValidationError):
            Ontology(rdf_content)
Example #10
0
    def test_datatype_property_range(self):
        rdf_content = rdf_prefix + '''
:code a owl:DatatypeProperty ;
    schema:rangeIncludes xsd:string ;
    .
:iso_code a owl:DatatypeProperty ;
    rdfs:subPropertyOf :code ;
    .
        '''
        ontology = Ontology(rdf_content)
        property_code = ontology.get_entity(DIG.code.toPython())
        property_iso_code = ontology.get_entity(DIG.code.toPython())
        self.assertIsInstance(property_iso_code, OntologyDatatypeProperty)
        self.assertTrue(property_code.is_legal_object(XSD.string.toPython()))
        self.assertTrue(property_iso_code.is_legal_object(XSD.string.toPython()))
        self.assertFalse(property_code.is_legal_object(XSD.dateTime.toPython()))
        self.assertFalse(property_iso_code.is_legal_object(XSD.dateTime.toPython()))
Example #11
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>'
Example #12
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})
Example #13
0
    def test_property_merge_config_inherit(self):
        rdf_content = rdf_prefix + '''
:had_participant a owl:ObjectProperty ; .
:transferred_custody_of a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ; .
        '''
        ontology = Ontology(rdf_content)
        config = ontology.merge_with_master_config('{"fields":{"had_participant":{"color":"red"}}}')
        self.assertIn('fields', config)
        fields = config['fields']
        self.assertIn('had_participant', fields)
        property_ = fields['had_participant']
        self.assertIn('color', property_)
        self.assertEqual(property_['color'], 'red')
        self.assertIn('transferred_custody_of', fields)
        sub_property = fields['transferred_custody_of']
        self.assertIn('color', sub_property)
        self.assertEqual(property_['color'], sub_property['color'])
Example #14
0
    def test_ontology_api_is_valid_with_kg(self):
        import json

        rdf_content = rdf_prefix + '''
:Human a owl:Class ; .
:Place a owl:Class ;
    :common_properties :region ; .
:region a owl:DatatypeProperty ;
    schema:domainIncludes :Place ;
    schema:rangeIncludes xsd:string ; .
            '''
        kg = '''
{
  "@type": ["dig:Place"],
  "@id": "some_doc_id",
  "@context": {
    "dig": "http://dig.isi.edu/ontologies/dig/"
  }
}
        '''
        kg_wrong_domain = '''
{
  "@type": ["dig:Human"],
  "@id": "some_doc_id",
  "@context": {
    "dig": "http://dig.isi.edu/ontologies/dig/"
  }
}
        '''
        kg_domain_doesnt_exist = '''
{
  "@type": ["dig:People"],
  "@id": "some_doc_id",
  "@context": {
    "dig": "http://dig.isi.edu/ontologies/dig/"
  }
}
        '''
        ontology = Ontology(rdf_content)
        self.assertTrue(ontology.is_valid('region', 'somewhere', json.loads(kg)))
        self.assertFalse(ontology.is_valid('region', 'somewhere', json.loads(kg_wrong_domain)))
        self.assertFalse(ontology.is_valid('region', 'somewhere', json.loads(kg_domain_doesnt_exist)))
Example #15
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'})
Example #16
0
def run(args):
    contents = [open(f).read() for f in args.files]
    ontology = Ontology(contents,
                        validation=args.validation,
                        include_undefined_class=args.include_class,
                        quiet=args.quiet)
    doc_content = OntologyReportGenerator(ontology).generate_html_report(
        include_turtle=args.include_turtle,
        exclude_warning=args.exclude_warning,
        list_auxiliary_line=args.list_auxiliary)

    with open(args.out, "w") as f:
        f.write(doc_content)
Example #17
0
    def test_ontology_condensed_json(self):
        import json

        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, expanded_jsonld=False)
        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, 'person name1')
Example #18
0
    def test_property_class_consistency(self):
        rdf_content = rdf_prefix + '''
:Entity a owl:Class ; .
:Place a owl:Class ; .
:code a owl:DatatypeProperty ;
    schema:domainIncludes :Entity ;
    schema:rangeIncludes xsd:string ; .

:country a owl:ObjectProperty ;
    rdfs:subPropertyOf :code ;
    schema:domainIncludes :Place ;
    schema:rangeIncludes :Country ; .
        '''
        with self.assertRaises(ValidationError):
            Ontology(rdf_content)
Example #19
0
    def test_property_domain_inherit_redundant(self):
        rdf_content = rdf_prefix + '''
:Event a owl:Class ; .
:had_participant a owl:ObjectProperty ;
    schema:domainIncludes :Event ;
    .
:transferred_ownership_to a owl:ObjectProperty ;
    schema:domainIncludes :Event ;
    owl:subPropertyOf :carried_out_by ;
    .
:carried_out_by a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ;
    .
        '''
        with self.assertLogs(level="WARNING"):
            Ontology(rdf_content)
Example #20
0
    def test_property_domain_inherit_consistency(self):
        rdf_content = rdf_prefix + '''
:Thing a owl:Class ; .
:Event a owl:Class ;
    rdfs:subClassOf :Thing ; .

:had_participant a owl:ObjectProperty ;
    schema:domainIncludes :Event ; .

:transferred_ownership_to a owl:ObjectProperty ;
    schema:domainIncludes :Thing ;
    owl:subPropertyOf :carried_out_by ; .

:carried_out_by a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ; .
        '''
        with self.assertRaises(ValidationError):
            Ontology(rdf_content)
Example #21
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)
Example #22
0
    def test_property_cycle_detection(self):
        rdf_content = rdf_prefix + '''
:Entity a owl:Class ; .
:code a owl:DatatypeProperty ;
    rdfs:label "code" ;
    skos:definition """A short string used as a code for an entity.""" ;
    skos:note """Examples are country codes, currency codes.""" ;
    schema:domainIncludes :Entity ;
    schema:rangeIncludes xsd:string ;
    rdfs:subPropertyOf :iso_code ;
    .

:iso_code a owl:DatatypeProperty ;
    rdfs:label "ISO code" ;
    skos:definition """A sanctioned ISO code for something.""" ;
    rdfs:subPropertyOf :code ;
    .
        '''
        with self.assertRaises(ValidationError):
            Ontology(rdf_content)
Example #23
0
    def test_class_hierachy(self):
        rdf_content = rdf_prefix + '''
:Entity a owl:Class ; .
:Group a owl:Class ;
    rdfs:subClassOf :Entity ; .
:Actor a owl:Class ;
    owl:subClassOf :Group ; .
        '''
        ontology = Ontology(rdf_content)
        self.assertEqual(len(ontology.all_classes()), 3)
        entity = ontology.get_entity(DIG.Entity.toPython())
        self.assertIsNotNone(entity)
        self.assertSetEqual(ontology.root_classes(), {entity})
        group = ontology.get_entity(DIG.Group.toPython())
        self.assertIsNotNone(group)
        actor = ontology.get_entity(DIG.Actor.toPython())
        self.assertIsNotNone(actor)
        self.assertSetEqual(group.super_classes(), {entity})
        self.assertSetEqual(actor.super_classes(), {group})
        self.assertSetEqual(actor.super_classes_closure(), {entity, group})
Example #24
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))
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix schema: <http://schema.org/> .
:Event a owl:Class ; .
:Entity a owl:Class ; .
:Organization a owl:Class ; .
:MOVEMENT_TRANSPORT a owl:Class ; .
:GeopoliticalEntity a owl:Class ; .
skos:prefLabel a owl:DatatypeProperty ; 
    schema:domainIncludes :Entity, :Event ;
    rdfs:range xsd:string ; .
:conflict_attack_place a owl:ObjectProperty ;
    schema:domainIncludes :Entity, :Event ;
    schema:rangeIncludes :GeopoliticalEntity ; .
    '''

    ontology = Ontology(ontology_content,
                        validation=False,
                        include_undefined_class=True,
                        quiet=True)
    kg_schema = KGSchema(ontology.merge_with_master_config(dict()))
    etk = ETK(modules=ExampleETKModule, kg_schema=kg_schema, ontology=ontology)
    input_data = {'doc_id': '1', 'data': json.loads(sample_input)}
    doc = etk.create_document(input_data)
    docs = etk.process_ems(doc)
    kgs = [json.dumps(doc.kg.value) for doc in docs[1:]]
    with open('output.jsonl', 'w') as f:
        f.write('\n'.join(kgs))
    with open('output.nt', 'w') as f:
        f.writelines(map(rdf_generation, kgs))
Example #26
0
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
from etk.ontology_api import Ontology
from etk.ontology_report_generator import OntologyReportGenerator

dir_path = os.path.dirname(os.path.abspath(__file__))
input_turtle = dir_path + "/../../ontologies/default-ontology.ttl"
output_doc = dir_path + "/default-ontology.html"
doc_content = ""

with open(input_turtle) as f:
    ontology = Ontology(f.read(),
                        validation=False,
                        include_undefined_class=True)
    doc_content = OntologyReportGenerator(ontology).generate_html_report(
        include_turtle=True)

with open(output_doc, "w") as f:
    f.write(doc_content)