コード例 #1
0
ファイル: test_ontology_api.py プロジェクト: spidysenses/etk
    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'})
コード例 #2
0
ファイル: test_ontology_api.py プロジェクト: spidysenses/etk
    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)))
コード例 #3
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)))
コード例 #4
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')
コード例 #5
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))