def json_to_rdflib(json_object): """ Convert a json result entry to an :mod:`rdfLib` type. :param dict json_object: the *JSON* object :return: the converted value (if possible) :rtype: :class:`rdflib.term.Literal` or :class:`rdflib.term.BNode` or :class:`rdflib.term.URIRef` or None """ try: type = json_object["type"] except KeyError: raise ValueError("No type specified") if type == 'uri': return URIRef(json_object["value"]) elif type == 'literal': if "xml:lang" in json_object: return Literal(json_object["value"], lang=json_object['xml:lang']) else: return Literal(json_object["value"]) elif type == 'typed-literal': return Literal(json_object["value"], datatype=URIRef(json_object['datatype'])) elif type == 'bnode': return BNode(json_object["value"]) else: return None
def value_to_rdf(value): """ Convert the value to an :mod:`rdflib` compatible type if appropriate. :param object value: the value :return: the converted value (if possible) :rtype: :class:`rdflib.term.Literal` or :class:`rdflib.term.BNode` or :class:`rdflib.term.URIRef` or object """ if isinstance(value, (URIRef, BNode)): return value elif isinstance(value, (basestring, str, float, int, bool, datetime, date, time, decimal.Decimal)): if type(value) is basestring and string_conforms_to_base64(value): return Literal( value, datatype=URIRef( 'http://www.w3.org/2001/XMLSchema#base64Binary')) return Literal(value) elif isinstance(value, (list, tuple)): language = value[1] if len(value) > 1 else None datatype = value[2] if len(value) > 2 else None return Literal(value[0], lang=language, datatype=datatype) elif isinstance(value, dict): val = value.get("value") language = value.get("language") datatype = value.get("datatype") if val: return Literal(val, lang=language, datatype=datatype) return value return value
def toRdfLib(term): if type(term) is fURIRef: return URIRef(term.getURI()) elif type(term) is fLiteral: try: if term.getDatatype() is None: return Literal(term.getLabel(), lang=term.getLanguage()) else: dtype = term.getDatatype().getURI() if dtype.startswith('<') and dtype.endswith('>'): dtype = dtype.strip('<>') dtype = URIRef(dtype) else: dtype = URIRef(dtype) return Literal(term.getLabel(), lang=term.getLanguage(), datatype=dtype) except Exception as e: print(e) elif type(term) is fBNode: return BNode(term.getID()) elif type(term) in [list,tuple]: return map(toRdfLib, term) return term
def value_to_rdf(value): """ Convert the value to an `rdflib` compatible type if appropriate. """ if type(value) in [ basestring, str, unicode, float, int, long, bool, datetime, date, time, decimal.Decimal ]: if type(value) is basestring and string_conforms_to_base64(value): return Literal( value, datatype=URIRef( 'http://www.w3.org/2001/XMLSchema#base64Binary')) return Literal(value) elif type(value) in [list, tuple]: language = len(value) > 1 and value[1] or None datatype = len(value) > 2 and value[2] or None return Literal(value[0], lang=language, datatype=datatype) elif type(value) is dict: val = value.get("value") language = value.get("language") datatype = value.get("datatype") if val: return Literal(val, lang=language, datatype=datatype) return value return value
def test_val2rdf(self): _, session = self._get_store_session(use_default_context=False) self._create_logic(session) Logic = session.get_class(surf.ns.SURF.Logic) res = Logic.all().limit(1).first() method = value_to_rdf XSD = surf.ns.XSD assert type(method(str('Literal'))) is Literal and method(str('Literal')) == Literal('Literal') assert type(method(u'Literal')) is Literal and method(u'Literal') == Literal(u'Literal') # list assert method(['Literal', 'en', None]) == Literal('Literal', lang = 'en') assert method(['Literal', None, XSD['string']]) == Literal('Literal', datatype = XSD['string']) # tuple assert method(('Literal', 'en', None)) == Literal('Literal', lang = 'en') assert method(('Literal', None, XSD['string'])) == Literal('Literal', datatype = XSD['string']) # dict assert method({'value':'Literal', 'language':'en'}) == Literal('Literal', lang = 'en') assert method({'value':'Literal', 'datatype' : XSD['string']}) == Literal('Literal', datatype = XSD['string']) # other assert method(10) == Literal(10) assert method(10.0) == Literal(10.0) # unknown o = object() assert method(o) == o
def add_resource_keywords(self, resource_uri, uuid, resource_labels = None, keywords = None): resource = self.session.get_resource(resource_uri, self.CignoResources) resource.rdfs_label = [] # clean for lang in resource_labels.keys(): resource.rdfs_label.append(Literal(resource_labels[lang], lang)) resource.cigno_uuid=Literal(uuid) resource.dcterms_subject = [] # clean if keywords and keywords != '': for k in keywords: resource.dcterms_subject.append(self.Concepts(k[1])) resource.save()
def value_to_rdf(value): """ Convert the value to an `rdflib` compatible type if appropriate. """ if type(value) in [str, unicode, basestring, float, int, long, bool, datetime, date, time]: return Literal(value) elif type(value) in [list, tuple]: language = len(value) > 1 and value[1] or None datatype = len(value) > 2 and value[2] or None return Literal(value[0], lang=language, datatype=datatype) elif type(value) is dict: val = value.get("value") language = value.get("language") datatype = value.get("datatype") if val: return Literal(val, lang=language, datatype=datatype) return value return value
def json_to_rdflib(obj): """Convert a json result entry to an rdfLib type.""" try: type = obj["type"] except KeyError: raise ValueError("No type specified") if type == 'uri': return URIRef(obj["value"]) elif type == 'literal': if "xml:lang" in obj: return Literal(obj["value"], lang=obj['xml:lang']) else: return Literal(obj["value"]) elif type == 'typed-literal': return Literal(obj["value"], datatype=URIRef(obj['datatype'])) elif type == 'bnode': return BNode(obj["value"]) else: return None
def toRdfLib(term): if type(term) is fURIRef: return URIRef(term.getURI()) elif type(term) is fLiteral: try: if term.getDatatype() is None: return Literal(term.getLabel(), lang=term.getLanguage()) else: dtype = term.getDatatype().getURI() if dtype.startswith('<') and dtype.endswith('>'): dtype = dtype.strip('<>') dtype = URIRef(dtype) else: dtype = URIRef(dtype) return Literal(term.getLabel(), lang=term.getLanguage(), datatype=dtype) except Exception, e: print e
def test_get_by(self): """ Test reader.get_by() """ _, session = self._get_store_session() Person = session.get_class(surf.ns.FOAF + "Person") jay = session.get_resource("http://jay", Person) jay.foaf_name = "Jay" jay.save() persons = Person.all().get_by(foaf_name=Literal("Jay")) persons = list(persons) self.assertTrue(persons[0].foaf_name.first, "Jay")
def test_full(self): """ Test loading details. """ _, session = self._get_store_session() self._create_persons(session) Person = session.get_class(surf.ns.FOAF + "Person") # Create inverse foaf_knows attribute for Mary jane = session.get_resource("http://Jane", Person) jane.foaf_knows = URIRef("http://Mary") jane.save() persons = Person.all().get_by(foaf_name = Literal("Mary")).full() persons = list(persons) self.assertTrue(len(persons[0].rdf_direct) > 1) self.assertTrue(len(persons[0].rdf_inverse) > 0) # Now, only direct persons = Person.all().get_by(foaf_name = Literal("Mary")).full(only_direct = True) persons = list(persons) self.assertTrue(len(persons[0].rdf_direct) > 1) self.assertTrue(len(persons[0].rdf_inverse) == 0)
def get_binding(node): cnt = None for cn in node.childNodes: if cn.nodeType == cn.ELEMENT_NODE: cnt = cn break if cnt: txt = get_text(cnt) if cnt.nodeName == 'uri': return URIRef(txt) elif cnt.nodeName == 'literal': return Literal(txt) elif cnt.nodeName == 'bnode': return BNode(txt) else: return None
def test_full(self): """ Test loading details. """ _, session = self._get_store_session() john, mary, jane = self._create_persons(session) Person = session.get_class(surf.ns.FOAF + "Person") # Create inverse foaf_knows attribute for Mary jane.foaf_knows = URIRef("http://Mary") jane.save() persons = Person.all().get_by(foaf_name=Literal("Mary")).full() mary_double = persons.one() self.assertTrue(len(mary_double.rdf_direct) > 1) self.assertTrue(len(mary_double.rdf_inverse) > 0) self.assertEquals(mary_double.foaf_name.first, "Mary") self.assertEquals(mary_double.is_foaf_knows_of.first, jane)
def test_full_direct_only(self): """ Test loading details with only_direct=True. """ _, session = self._get_store_session() john, mary, jane = self._create_persons(session) Person = session.get_class(surf.ns.FOAF + "Person") # Create inverse foaf_knows attribute for Mary jane.foaf_knows = mary jane.save() persons = Person.all().get_by(foaf_name=Literal("Mary")).full( direct_only=True) mary_double = persons.one() # At first, rdf_inverse should be empty self.assertTrue(len(mary_double.rdf_inverse) == 0) # But inverse attributes should still load on request self.assertEquals(mary_double.is_foaf_knows_of.first, jane)
def test_queries_context(self): """ Test resource.all() and get_by() with specified context. """ store, session = self._get_store_session() Person = session.get_class(surf.ns.FOAF + "Person") context = URIRef("http://my_context_1") store.clear(context) jane = session.get_resource("http://jane", Person, context=context) jane.foaf_name = "Jane" jane.save() persons = list(Person.all().context(context)) self.assertEquals(len(persons), 1) persons = Person.get_by(foaf_name=Literal("Jane")).context(context) self.assertEquals(len(list(persons)), 1) persons = Person.get_by_attribute(["foaf_name"], context=context) self.assertEquals(len(persons), 1)
def _prepare_add_many_query(resources, context=None): query = insert() if context: query = insert() query.into(context) else: query = insert(data=True) for resource in resources: s = resource.subject for p, objs in list(resource.rdf_direct.items()): for o in objs: if isinstance(o, Literal) and isinstance( o.value, str) and ("'" in o.value or '"' in o.value or '\\'): o = Literal(_escape_string(o.value), datatype=o.datatype) query.template((s, p, o)) return query
def filter(self, **kwargs): """ Add filter conditions. Expects arguments in form:: ns_predicate = "(%s > 15)" ``ns_predicate`` specifies which predicate will be used for filtering, a query variable will be bound to it. `%s` is a placeholder for this variable. Filter expression (in example: "(%s > 15)") must follow SPARQL specification, on execution "%s" will be substituted with variable and the resulting string will be placed in query as-is. Because of string substitution percent signs need to be escaped. For example:: Person.all().filter(foaf_name = "(%s LIKE 'J%%')") This Virtuoso-specific filter is intended to select persons with names starting with "J". In generated query it will look like this:: ... ?s <http://xmlns.com/foaf/0.1/name> ?f1 . FILTER (?f1 LIKE 'J%') ... """ params = self.__params.copy() params.setdefault("filter", []) for name, value in kwargs.items(): attr, direct = attr2rdf(name) assert direct, "Only direct attributes can be used for filters" # Assume by plain strings user means literals if type(value) in [str, unicode]: value = Literal(value) params["filter"].append((attr, value, direct)) return ResultProxy(params)