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 test_save_context(self): """ Test saving resource with specified context. """ _, session = self._get_store_session() self._create_persons(session) Person = session.get_class(surf.ns.FOAF + "Person") context = URIRef("http://my_context_1") jane = session.get_resource("http://jane", Person, context=context) jane.foaf_name = "Jane" jane.save() # Same context. jane2 = session.get_resource("http://jane", Person, context=context) jane2.load() self.assertEqual(jane2.foaf_name.one, "Jane") self.assertEqual(jane2.context, context) # Different context. other_context = URIRef("http://other_context_1") jane3 = session.get_resource("http://jane", Person, context=other_context) self.assertEqual(jane3.is_present(), False)
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 test_unicode(self): """ Check that returned query string is unicode. """ statement = URIRef("http://a"), URIRef("http://b"), URIRef("http://c") query = insert().template(statement) result = SparulTranslator(query).translate() self.assertTrue(isinstance(result, unicode))
def _results_to_json(self, results): bindings = results.getBindingNames() r_dict = {} r_dict['head'] = {'vars': bindings} r_dict['results'] = {'bindings':[]} for bindingSet in results: json_binding = {} for b in bindings: value = bindingSet.getValue(b) if type(value) is sv.URI: json_binding[b] = {'type':'uri', 'value': value.getURI()} elif type(value) is sv.BNode: json_binding[b] = {'type':'bnode', 'value': value.getID()} elif type(value) is sl.Literal: dtype = value.getDatatype() if value.getDatatype() else None lang = value.getLanguage() if value.getLanguage() else None lit_type = 'typed-literal' if dtype else 'literal' json_binding[b] = {'type':lit_type, 'value': value.getLabel()} if dtype: if type(dtype) is sv.URI: json_binding[b]['datatype'] = dtype.getURI() elif type(dtype) in [str, unicode] and dtype.startswith('<') and dtype.endswith('>'): json_binding[b]['datatype'] = URIRef(dtype.strip('<>')) else: json_binding[b]['datatype'] = URIRef(dtype) if lang: json_binding[b]['xml:lang'] = lang r_dict['results']['bindings'].append(json_binding) return r_dict
def _get_store_session(self, use_default_context=True): """ Return initialized SuRF store and session objects. """ # FIXME: take endpoint from configuration file, # maybe we can mock SPARQL endpoint. kwargs = { "reader": "sparql_protocol", "writer": "sparql_protocol", "endpoint": "http://localhost:9980/sparql", "use_subqueries": True, "combine_queries": True } if True: #use_default_context: kwargs["default_context"] = "http://surf_test_graph/dummy2" store = surf.Store(**kwargs) session = surf.Session(store) # Fresh start! store.clear("http://surf_test_graph/dummy2") store.clear(URIRef("http://my_context_1")) store.clear(URIRef("http://other_context_1")) # store.clear() return store, session
def test_multiple_request_context(self): """ Test multiple query contexts. """ store, session = self._get_store_session() Person = session.get_class(surf.ns.FOAF + "Person") # Put each person into one context persons = {} for name, context in [("John", None), ("Mary", URIRef("http://my_context_1")), ("Jane", URIRef("http://other_context_1"))]: person = session.get_resource("http://%s" % name, Person, context=context) person.foaf_name = name person.save() persons[name] = person persons['Jane'].foaf_knows = persons['Mary'] persons['Jane'].update() query_contexts = (URIRef("http://my_context_1"), URIRef("http://other_context_1")) jane = Person.get_by(foaf_name='Jane').context(*query_contexts).one() # Make sure that query context is handed down self.assertEquals(jane.query_contexts, query_contexts) self.assertEquals(jane.foaf_knows.first.query_contexts, query_contexts)
def test_save_context2(self): """ Test saving resource with specified context. """ # Copied from surf.sparql_protocol/test/test_sparql_protocol.py _, session = self._get_store_session(use_default_context=False) Person = session.get_class(surf.ns.FOAF + "Person") context = URIRef("http://my_context_1") jane = session.get_resource("http://jane", Person, context=context) jane.foaf_name = "Jane" jane.save() # Same context. jane2 = session.get_resource("http://jane", Person, context=context) jane2.load() self.assertEqual(jane2.foaf_name.one, "Jane") self.assertEqual(jane2.context, context) # Different context. other_context = URIRef("http://other_context_1") jane3 = session.get_resource("http://jane", Person, context=other_context) self.assertEqual(jane3.is_present(), False)
def test_delete_from(self): """ Try to produce DELETE DATA FROM ... { ... } query. """ expected = canonical(u"DELETE DATA FROM <g> { <a> <b> <c> }") statement = URIRef("a"), URIRef("b"), URIRef("c") query = delete(data=True).from_(URIRef("g")).template(statement) result = canonical(SparulTranslator(query).translate()) self.assertEqual(expected, result)
def test_insert(self): """ Try to produce INSERT ..." query. """ expected = canonical(u"INSERT { <http://a> <http://b> <http://c> }") statement = URIRef("http://a"), URIRef("http://b"), URIRef("http://c") query = insert().template(statement) result = canonical(SparulTranslator(query).translate()) self.assertEqual(expected, result)
def test_add_triples(get_store_session): """Test adding triples to store """ store, session = get_store_session store.add_triple(URIRef("http://surf_test_graph/dummy2/Test"), RDF.type, URIRef("http://surf_test_graph/dummy2/cat"), context=None)
def __init__(self, subject=None, block_auto_load=False, context=None, query_contexts=None, namespace=None): """ Initialize a Resource, with the `subject` (a URI - either a string or a URIRef). If ``subject`` is None than a unique subject will be generated using the :func:`surf.util.uuid_subject` function. If ``namespace`` is specified, generated subject will be in that namespace. ``block_auto_load`` will prevent the resource from autoloading all rdf attributes associated with the subject of the resource. """ if subject is None: subject = uuid_subject(namespace) elif not type(subject) in [URIRef, BNode]: subject = URIRef(subject) self.__subject = subject if context == NO_CONTEXT: self.__context = None elif context: self.__context = URIRef(unicode(context)) elif self.session and self.store_key: self.__context = self.session[self.store_key].default_context if query_contexts: self.__query_contexts = query_contexts elif self.__context: self.__query_contexts = [self.__context] else: self.__query_contexts = [] self.__expired = False self.__rdf_direct = {} self.__rdf_direct[a] = [self.uri] self.__rdf_inverse = {} self.__namespaces = all() # __full is set to true after doing full load. This is used by # __getattr__ to decide if it's worth to query triplestore. self.__full = False if self.session: if not self.store_key: self.store_key = self.session.default_store_key if self.session.auto_load and not block_auto_load: self.load()
def test_str(self): """ Test that __str__ translates query to string. """ expected = canonical(u"INSERT { <http://a> <http://b> <http://c> }") statement = URIRef("http://a"), URIRef("http://b"), URIRef("http://c") query = insert().template(statement) # test str() self.assertEqual(expected, canonical(unicode(str(query)))) # test unicode() self.assertEqual(expected, canonical(unicode(query)))
def test_load_into(self): """ LOAD ... INTO ... """ expected = canonical(u""" LOAD <http://example.com> INTO <http://example.com/graph> """) query = load().load(URIRef("http://example.com"))\ .into(URIRef("http://example.com/graph")) result = canonical(SparulTranslator(query).translate()) self.assertEqual(expected, result)
def test_instancemaker(self): """ Test instancemaker. """ _, session = self._get_store_session() self._create_persons(session) Person = session.get_class(surf.ns.FOAF + "Person") john = session.get_resource(URIRef("http://John"), Person) john.foaf_knows = [URIRef("http://Joe")] john.save() # Get this instance again, test its foaf_knows attribute john = session.get_resource(URIRef("http://John"), Person) assert isinstance(john.foaf_knows.first, URIRef)
def __init__(self, subject=None, block_auto_load=False, context=None, namespace=None): """ Initialize a Resource, with the `subject` (a URI - either a string or a URIRef). If ``subject`` is None than a unique subject will be generated using the :func:`surf.util.uuid_subject` function. If ``namespace`` is specified, generated subject will be in that namespace. ``block_auto_load`` will prevent the resource from autoloading all rdf attributes associated with the subject of the resource. """ if subject is None: subject = uuid_subject(namespace) elif not isinstance(subject, (URIRef, BNode)): subject = URIRef(subject) self.__subject = subject if context == NO_CONTEXT: self.__context = None elif context: self.__context = URIRef(str(context)) elif self.session and self.store_key: self.__context = self.session[self.store_key].default_context self.__expired = False self.__rdf_direct = defaultdict(list) self.__rdf_inverse = defaultdict(list) self.rdf_type = self.uri self.dirty = False # Reset dirty flag, which was automatically set in previous command self.__namespaces = all() # __full_direct and __full_inverse are set to true after doing full load. # These are used by __getattr__ to decide if it's worth to query # triplestore. self.__full_direct = False self.__full_inverse = False if self.session: if not self.store_key: self.store_key = self.session.default_store_key if self.session.auto_load and not block_auto_load: self.load()
def test_order_by_attr(self): """ Test ordering by attribute other than subject. """ _, session = self._get_store_session() Person = session.get_class(surf.ns.FOAF + "Person") for i in range(0, 10): person = session.get_resource("http://A%d" % i, Person) person.foaf_name = "A%d" % (10 - i) person.save() sort_uri = URIRef(surf.ns.FOAF["name"]) persons = list(Person.all().order(sort_uri).limit(1)) self.assertEquals(len(persons), 1) self.assertEquals(persons[0].subject, URIRef("http://A9"))
def get_resource(self, subject, concept=None, store=None, graph=None, block_auto_load=False, context=None, classes=None): """ Same as `map_type` but `set` the resource from the `graph`. """ classes = classes if isinstance(classes, (tuple, set, list)) else [] if not isinstance(subject, URIRef): subject = URIRef(str(subject)) if concept is None: concept = Resource.concept(subject) resource = self.map_instance(concept, subject, store=store, classes=classes, block_auto_load=block_auto_load, context=context) if graph: resource.set(graph) return resource
def test_order_limit_offset(self): """ Test ordering by subject, limit, offset. """ _, session = self._get_store_session() Person = session.get_class(surf.ns.FOAF + "Person") self._create_persons(session) for i in range(0, 10): person = session.get_resource("http://A%d" % i, Person) person.foaf_name = "A%d" % i person.save() persons = Person.all().order().limit(2).offset(5) uris = [person.subject for person in persons] self.assertEquals(len(uris), 2) self.assertTrue(URIRef("http://A5") in uris) self.assertTrue(URIRef("http://A6") in uris)
def test_first(self): """ Test ResourceProxy.first(). """ _, session = self._get_store_session() Person = session.get_class(surf.ns.FOAF + "Person") person = Person.all().first() self.assertEquals(person.subject, URIRef("http://John"))
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 test_attr_order_by(self): """ Test ordering of attribute value. """ _, session = self._get_store_session(use_default_context=False) Person = session.get_class(surf.ns.FOAF + "Person") # First remove any previously created for p in Person.all(): p.remove() for i in range(0, 10): person = session.get_resource("http://A%d" % i, Person) person.foaf_name = "A%d" % i person.save() all_persons = list(Person.all()) random.shuffle(all_persons) person = person = session.get_resource("http://A0", Person) person.foaf_knows = all_persons person.foaf_name = [] person.update() persons = list(person.foaf_knows.order(surf.ns.FOAF["name"]).limit(1)) self.assertEquals(len(persons), 1) # Unbound results sort earliest self.assertEquals(persons[0].subject, URIRef("http://A0"))
def __init__(self, reader=None, writer=None, *args, **kwargs): super(Store, self).__init__() info('initializing the store') self.__default_context = None if "default_context" in kwargs: self.__default_context = URIRef(kwargs["default_context"]) if reader: self.reader = reader if isinstance( reader, RDFReader) else get_reader(reader, *args, **kwargs) else: self.reader = NoneReader(*args, **kwargs) if writer: self.writer = writer if isinstance( writer, RDFWriter) else get_writer(writer, self.reader, *args, **kwargs) else: self.writer = NoneWriter(self.reader, *args, **kwargs) if hasattr(self.reader, 'use_subqueries'): self.use_subqueries = property( fget=lambda self: self.reader.use_subqueries) info('store initialized')
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 __init__(self, reader = None, writer = None, *args, **kwargs): super(Store, self).__init__() self.log_level = kwargs.get('log_level', logging.NOTSET) self.log.info('initializing the store') load_plugins(logger=self.log) self.__default_context = None if "default_context" in kwargs: self.__default_context = URIRef(kwargs["default_context"]) if reader: self.reader = reader if isinstance(reader, RDFReader) else get_reader(reader, *args, **kwargs) else: self.reader = RDFReader(*args, **kwargs) self.reader.log_level = self.log_level if writer: self.writer = writer if isinstance(writer, RDFWriter) else get_writer(writer, self.reader, *args, **kwargs) else: self.writer = RDFWriter(self.reader, *args, **kwargs) self.writer.log_level = self.log_level if hasattr(self.reader, 'use_subqueries'): self.use_subqueries = property(fget = lambda self: self.reader.use_subqueries) self.log.info('store initialized')
def get_resource(self, subject, uri=None, store=None, graph=None, block_auto_load=False, context=None, query_contexts=None, *classes): """ Same as `map_type` but `set` the resource from the `graph`. """ if not isinstance(subject, URIRef): subject = URIRef(unicode(subject)) if uri is None: uri = Resource.concept(subject) resource = self.map_instance(uri, subject, store, classes, block_auto_load=block_auto_load, context=context, query_contexts=query_contexts) if graph: resource.set(graph) return resource
def test_attribute_limit(self): """ Test limit on attributes. """ _, session = self._get_store_session() Person = session.get_class(surf.ns.FOAF + "Person") john = session.get_resource(URIRef("http://John"), Person) john.foaf_knows = [URIRef("http://Mary"), URIRef("http://Joe")] john.save() # Get this instance again, test its foaf_knows attribute john = session.get_resource(URIRef("http://John"), Person) self.assertEquals(len(list(john.foaf_knows)), 2) # Get this instance again, test its foaf_knows attribute john = session.get_resource(URIRef("http://John"), Person) self.assertEquals(len(list(john.foaf_knows.limit(1))), 1) assert isinstance(john.foaf_knows.limit(1).first(), surf.Resource)
def __set_query_contexts(self, values): uri_refs = [] for value in values: if not isinstance(value, URIRef): value = URIRef(value) uri_refs.append(value) self.__query_contexts = tuple(uri_refs)
def __init__(self, *args, **kwargs): self.define = kwargs.pop('define', None) self._inference = False if "default_context" in kwargs: self._default_context = URIRef(kwargs["default_context"]) SPARQLReaderPlugin.__init__(self, *args, **kwargs)
def test_query_foreign_context_attribute(self): store, session = self._get_store_session(use_default_context=False) Person = session.get_class(surf.ns.FOAF + "Person") # Put each person into one context persons = {} for name, context in [("John", None), ("Mary", URIRef("http://my_context_1")), ("Jane", URIRef("http://other_context_1"))]: person = session.get_resource("http://%s" % name, Person, context=context) person.foaf_name = name person.save() persons[name] = person persons['Mary'].foaf_knows = [persons['Jane'], persons['John']] persons['Mary'].update() mary = Person.get_by(foaf_name='Mary').context(NO_CONTEXT).one() knows = list(mary.foaf_knows.order(surf.ns.FOAF.name)\ .context(NO_CONTEXT)) self.assertEquals(knows[0].context, persons['Jane'].context) self.assertEquals(knows[1].context, persons['John'].context) # TODO load() only queries context of resource ## Same with load() #mary = Person.get_by(foaf_name='Mary').context(NO_CONTEXT).one() #mary.load() #self.assertEquals(mary.foaf_knows[0].context, persons['Jane'].context) #self.assertEquals(mary.foaf_knows[1].context, persons['John'].context) # Same with full() mary = Person.get_by(foaf_name='Mary').full().context(NO_CONTEXT).one() persons_and_context = [(p, p.context) for p in [persons['Jane'], persons['John']]] # TODO fails under Virtuoso as V. doesn't allow ?g to be bound to two # optional matches get_person_and_context = lambda p: (p, p.context) self.assertEquals(set(map(get_person_and_context, mary.foaf_knows)), set(persons_and_context))