コード例 #1
0
 def _convert_ontology_owl_restriction(self):
     for c, p, exact, min_, max_ in self.onto_graph.query("""
       SELECT ?c ?p ?exact ?min ?max
       WHERE {
         ?c rdfs:subClassOf|owl:equivalentClass ?res .
         ?res a owl:Restriction ;
              owl:onProperty ?p .
         OPTIONAL {?res owl:cardinality ?exact}
         OPTIONAL {?res owl:minCardinality ?min}
         OPTIONAL {?res owl:maxCardinality ?max}
       }
     """):
         node_subject = self._class_shape(c)
         property_shape = self._property_shape(p)
         if exact:
             property_shape.add_property(URI('sh:count'), Literal(str(exact), type_=LiteralType.integer))
         if min_:
             property_shape.add_property(URI('sh:minCount'), Literal(str(min_), type_=LiteralType.integer))
         if max_:
             property_shape.add_property(URI('sh:maxCount'), Literal(str(max_), type_=LiteralType.integer))
         ranges = []
         for r, in self.onto_graph.query("""
           SELECT ?r
           WHERE {
             ?c rdfs:subClassOf|owl:equivalentClass ?res .
             ?res a owl:Restriction ;
                  owl:onProperty ?p ;
                  owl:allValuesFrom|owl:someValuesFrom|owl:hasValue ?r
           }
         """, initBindings={'c': c, 'p': p}):
             ranges.append(r)
         self._build_property_ranges(property_shape, ranges)
         node_subject.add_property(URI('sh:property'), property_shape)
         self._g.add_subject(node_subject)
コード例 #2
0
ファイル: test_kg_node.py プロジェクト: vishalbelsare/etk
 def test_nodes_literal(self):
     lit = Literal('name', 'en', LiteralType.string)
     lit2 = Literal('nombre', 'es')
     lit3 = Literal('name', 'en', 'xsd:string')
     self.assertNotEqual(lit, lit2)
     self.assertEqual(lit, lit3)
     self.assertEqual(len({lit, lit2, lit3}), 2)
     self.assertEqual(lit.type, LiteralType('xsd:string'))
     self.assertEqual(lit.raw_type, LiteralType.string.value)
     self.assertTrue(lit.is_valid())
コード例 #3
0
ファイル: value.py プロジェクト: vishalbelsare/etk
 def __init__(self, latitude, longitude, precision, globe=None):
     self.globe = globe
     self.latitude = Literal(str(latitude), type_=LiteralType.decimal)
     self.longitude = Literal(str(longitude), type_=LiteralType.decimal)
     self.precision = Literal(str(precision), type_=LiteralType.decimal)
     s = 'Point({} {})'.format(latitude, longitude)
     if globe:
         s = '<{}> {}'.format(globe.value.value.replace('wd:', 'http://www.wikidata.org/entity/'), s)
     self.value = Literal(s,
                          type_=LiteralType('http://www.opengis.net/ont/geosparql#wktLiteral', common_check=False))
     self.__build_full_value()
コード例 #4
0
ファイル: test_kg_node.py プロジェクト: vishalbelsare/etk
    def test_nodes_init_with_nodes(self):
        uri = URI('rdf:type')
        self.assertIsInstance(URI(uri), URI)
        self.assertEqual(URI(uri), uri)

        bnode = BNode()
        self.assertIsInstance(BNode(bnode), BNode)
        self.assertEqual(BNode(bnode), bnode)

        lit = Literal('name', 'en', LiteralType.string)
        self.assertIsInstance(Literal(lit), Literal)
        self.assertEqual(Literal(lit), lit)
コード例 #5
0
ファイル: value.py プロジェクト: vishalbelsare/etk
 def __init__(self, amount, unit=None, upper_bound=None, lower_bound=None, normalized=True,
              type=LiteralType.decimal):
     self.value = Literal(str(amount), type_=type)
     self.upper_bound = upper_bound is not None and Literal(upper_bound, type_=type)
     self.lower_bound = lower_bound is not None and Literal(lower_bound, type_=type)
     self.unit = unit is not None and unit
     self.__build_full_value()
     if isinstance(normalized, QuantityValue):
         self.normalized_value = normalized.full_value
     else:
         self.normalized_value = self.full_value
     self.full_value.add_property(URI('wikibase:quantityNormalized'), self.normalized_value)
コード例 #6
0
class Precision(Enum):
    # https://www.wikidata.org/wiki/Help:Dates#Precision
    day = Literal('11', type_=LiteralType.integer)
    month = Literal('10', type_=LiteralType.integer)
    year = Literal('9', type_=LiteralType.integer)
    decade = Literal('8', type_=LiteralType.integer)
    century = Literal('7', type_=LiteralType.integer)
    millennium = Literal('6', type_=LiteralType.integer)
    hundred_thousand_years = Literal('4', type_=LiteralType.integer)
    million_years = Literal('3', type_=LiteralType.integer)
    billion_years = Literal('0', type_=LiteralType.integer)
コード例 #7
0
    def test_subject_reification_back_compatible(self):
        s = URI('ex:ex1')
        t = Subject(s)
        lit = Literal('jack', 'en', 'xsd:string')
        statement1 = t.add_property(URI('rdf:type'), URI('dig:Person'))
        statement2 = t.add_property(URI('dig:name'),
                                    lit,
                                    reify=URI('digg:name'))
        statement3 = t.add_property(URI('dig:name'),
                                    lit,
                                    reify=(URI('digg:name'), None))
        statement4 = t.add_property(URI('dig:name'),
                                    lit,
                                    reify=(URI('digg:name'),
                                           URI('ex:assert1')))
        self.assertEqual(t.subject, s)
        self.assertEqual(len(t._resource), 3)
        self.assertIn(URI('rdf:type'), t._resource)
        self.assertIn(URI('dig:name'), t._resource)
        self.assertIn(URI('digg:name'), t._resource)
        self.assertNotIsInstance(statement1, Subject)
        self.assertIsInstance(statement2, Subject)
        self.assertIn(URI('digg:name'), statement2._resource)
        self.assertIsInstance(statement3, Subject)
        self.assertIsInstance(statement3.subject, BNode)
        self.assertIsInstance(statement4, Subject)
        self.assertIsInstance(statement4.subject, URI)

        # exception
        with self.assertRaises(InvalidParameter):
            t.add_property(URI('dig:name'), lit, reify=(None, URI('s')))
        with self.assertRaises(InvalidParameter):
            t.add_property(URI('dig:name'), lit, reify=(URI('digg:name'), 3))
        with self.assertRaises(InvalidParameter):
            t.add_property(URI('dig:name'), lit, reify=(URI('digg:name'), ''))
コード例 #8
0
 def _add_master_config(self, config):
     self.ontology._ns.bind_for_master_config()
     try:
         for field in config["fields"]:
             t = Subject(URI(field))
             if config["fields"][field]["type"] == "kg_id":
                 t.add_property(URI('rdf:type'), URI('owl:ObjectProperty'))
             elif config["fields"][field]["type"] == "number":
                 t.add_property(URI('rdf:type'),
                                URI('owl:DatatypeProperty'))
             elif config["fields"][field]["type"] == "date":
                 t.add_property(URI('rdf:type'),
                                URI('owl:DatatypeProperty'))
             elif config["fields"][field]["type"] == "location":
                 t.add_property(URI('rdf:type'),
                                URI('owl:DatatypeProperty'))
                 t.add_property(URI('rdf:range'), URI('xsd:string'))
             else:
                 t.add_property(URI('rdf:type'),
                                URI('owl:DatatypeProperty'))
                 t.add_property(URI('rdf:range'), URI('xsd:string'))
             if "description" in config["fields"][field] and config[
                     "fields"][field]["description"]:
                 t.add_property(
                     URI('rdf:comment'),
                     Literal(config["fields"][field]["description"]))
             self.ontology.add_subject(t)
     except KeyError as key:
         print(str(key) + " not in config")
コード例 #9
0
ファイル: value.py プロジェクト: vishalbelsare/etk
 def __init__(self, s, normalized_value=None):
     super().__init__()
     self.value = Literal(s, type_=LiteralType.string)
     if isinstance(normalized_value, URI):
         self.normalized_value = normalized_value
     elif isinstance(normalized_value, str):
         self.normalized_value = URI(normalized_value)
コード例 #10
0
 def test_add_subject(self):
     kg = self.doc.kg
     subject = Subject(URI('http://xxx/1'))
     subject.add_property(URI('dig:name'), Literal('Jack', lang='en'))
     subject.add_property(URI('dig:age'),
                          Literal('18', type_=LiteralType.int))
     friend = Subject(BNode())
     friend.add_property(URI('rdf:type'), Literal('dig:Person'))
     friend.add_property(URI('dig:name'), Literal('José', lang='es'))
     friend.add_property(URI('dig:age'), Literal('19',
                                                 type_=LiteralType.int))
     friend.add_property(URI('dig:friend'), subject)
     subject.add_property(URI('dig:friend'), friend)
     kg.add_subject(subject)
     self.assertEqual(len(kg._g), 8)
     self.assertIn((rdflib.URIRef('http://xxx/1'), DIG.name,
                    rdflib.Literal('Jack', lang='en')), kg._g)
コード例 #11
0
ファイル: value.py プロジェクト: vishalbelsare/etk
    def __init__(self, value, calendar, precision, time_zone):
        super().__init__()
        self.value = Literal(value, type_=LiteralType.dateTime)
        if not self.value.is_valid():
            raise ValueError('Invalid datetime format')
        self._calendar = calendar
        if isinstance(precision, Precision):
            self._precision = precision.value
        elif isinstance(precision, Literal):
            self._precision = precision
        else:
            self._precision = Literal(str(precision), type_=LiteralType.integer)
        if isinstance(time_zone, Literal):
            self._time_zone = time_zone
        else:
            self._time_zone = Literal(str(time_zone), type_=LiteralType.integer)

        self.__build_full_value()
コード例 #12
0
    def __init__(self, value, calendar, precision, time_zone):
        super().__init__()
        self.value = Literal(value, type_=LiteralType.dateTime)
        self._calendar = calendar
        if isinstance(precision, Precision):
            self._precision = precision.value
        elif isinstance(precision, Literal):
            self._precision = precision
        else:
            self._precision = Literal(str(precision),
                                      type_=LiteralType.integer)
        if isinstance(time_zone, Literal):
            self._time_zone = time_zone
        else:
            self._time_zone = Literal(str(time_zone),
                                      type_=LiteralType.integer)

        self.__build_full_value()
コード例 #13
0
ファイル: test_kg_graph.py プロジェクト: vishalbelsare/etk
    def test_graph(self):
        g = Graph()
        g.bind('ex', 'http://ex.com/')
        g.bind('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
        g.bind('foaf', 'http://xmlns.com/foaf/0.1/')

        t_nest = Subject(BNode())
        t_nest.add_property(URI('rdf:type'), URI('foaf:Person'))
        t_nest.add_property(URI('foaf:name'), Literal('Alfred'))
        t_nest.add_property(URI('foaf:age'), Literal('36', type_=LiteralType.int))

        t = Subject(URI('ex:john'))
        t.add_property(URI('rdf:type'), URI('foaf:Person'))
        t.add_property(URI('foaf:name'), Literal('John'))
        t.add_property(URI('foaf:age'), Literal('12', type_=LiteralType.int))
        t.add_property(URI('foaf:knows'), t_nest)
        g.add_subject(t)

        self.assertEqual(len(g._g), 7)
コード例 #14
0
 def field_type(self, field_name: str,
                value: object) -> Union[Subject, URI, Literal, None]:
     """
     Return the type of a field defined in schema, if field not defined, return None
     """
     property_ = self.ontology._resolve_uri(URI(field_name))
     if property_ in self.ontology.object_properties:
         return value if isinstance(value, Subject) else URI(value)
     elif property_ in self.ontology.datatype_properties:
         # TODO: check Literal type
         return Literal(value)
     else:
         return None
コード例 #15
0
    def test_subject_exception(self):
        with self.assertRaises(InvalidParameter):
            Subject(None)

        with self.assertRaises(InvalidParameter):
            Subject('ex:ex1')

        with self.assertRaises(InvalidParameter):
            Subject(Literal('test'))

        s = Subject(URI('ex:ex1'))
        with self.assertRaises(InvalidParameter):
            s.add_property(URI('rdf:type'), 'dig:Person')

        with self.assertRaises(InvalidParameter):
            s.add_property(BNode(), URI('dig:Person'))
コード例 #16
0
ファイル: value.py プロジェクト: vishalbelsare/etk
class TimeValue(DataValue):
    type = URI('wikibase:Time')

    def __init__(self, value, calendar, precision, time_zone):
        super().__init__()
        self.value = Literal(value, type_=LiteralType.dateTime)
        if not self.value.is_valid():
            raise ValueError('Invalid datetime format')
        self._calendar = calendar
        if isinstance(precision, Precision):
            self._precision = precision.value
        elif isinstance(precision, Literal):
            self._precision = precision
        else:
            self._precision = Literal(str(precision), type_=LiteralType.integer)
        if isinstance(time_zone, Literal):
            self._time_zone = time_zone
        else:
            self._time_zone = Literal(str(time_zone), type_=LiteralType.integer)

        self.__build_full_value()

    def __build_full_value(self):
        self._create_full_value()
        self.full_value.add_property(URI('rdf:type'), URI('wikibase:Time'))
        self.full_value.add_property(URI('wikibase:timePrecision'), self._precision)
        self.full_value.add_property(URI('wikibase:timeTimezone'), self._time_zone)
        self.full_value.add_property(URI('wikibase:timeCalendarModel'), self._calendar.value)
        self.full_value.add_property(URI('wikibase:timeValue'), self.value) 
        # TODO fix import bug
        # if not self.value.startswith("+"):
        #     self.full_value.add_property(URI('wikibase:timeValue'), self.value) 
        # else:
        #     self.full_value.add_property(URI('wikibase:timeValue'), self.value[1:]) 

    def _v_name(self):
        time = self.value.value.replace(':', '').replace(' ', '-')
        calendar = self._calendar.value.value[3]
        precision = self._precision.value
        time_zone = self._time_zone.value
        return 'c'.join(('Time', time, calendar, precision, time_zone))
コード例 #17
0
    def test_subject(self):
        s = URI('ex:ex1')
        t = Subject(s)
        lit = Literal('jack', 'en', 'xsd:string')
        t.add_property(URI('rdf:type'), URI('dig:Person'))
        t.add_property(URI('dig:name'), lit)
        self.assertEqual(t.subject, s)
        self.assertEqual(len(t._resource), 2)
        self.assertIn(URI('rdf:type'), t._resource)
        self.assertIn(URI('dig:name'), t._resource)

        # remove property
        self.assertFalse(t.remove_property(URI('rdf:type'), URI('dig:Male')))
        self.assertTrue(t.remove_property(URI('rdf:type'), URI('dig:Person')))
        self.assertNotIn(URI('rdf:type'), t._resource)

        self.assertTrue(t.remove_property(URI('dig:name')))
        self.assertNotIn(URI('dig:name'), t._resource)

        self.assertFalse(t.remove_property(URI('rdf:type')))
        self.assertFalse(t.remove_property(URI('dig:name')))
コード例 #18
0
ファイル: value.py プロジェクト: vishalbelsare/etk
 def __init__(self, s, lang):
     self.value = Literal(s, lang=lang)
コード例 #19
0
ファイル: value.py プロジェクト: vishalbelsare/etk
 def __init__(self, s):
     super().__init__()
     self.value = Literal(s, type_=LiteralType.string)
コード例 #20
0
 def add_description(self, s: str, lang='en'):
     self.add_property(URI('schema:description'), Literal(s, lang=lang))
コード例 #21
0
 def add_alias(self, s: str, lang='en'):
     self.add_property(URI('skos:altLabel'), Literal(s, lang=lang))
コード例 #22
0
 def add_label(self, s: str, lang='en'):
     literal = Literal(s, lang=lang)
     self.add_property(URI('rdfs:label'), literal)
     self.add_property(URI('schema:name'), literal)
     self.add_property(URI('skos:prefLabel'), literal)