def modeltest(): from IPython import embed # this hardlocks ms = RDF.MemoryStorage('test') m = RDF.Model(ms) p1 = Path('~/git/NIF-Ontology/ttl/NIF-Molecule.ttl').expanduser() p = RDF.Parser(name='turtle') p.parse_into_model(m, p1.as_uri()) embed()
def _initOntology(self): #storage = RDF.HashStorage('dbpedia', options="hash-type='bdb'") storage = RDF.MemoryStorage() model = RDF.Model(storage) rdfParser = RDF.Parser(name="rdfxml") ontologyPath = 'file://' + os.path.join(self._getCurrentDir(), 'dbpedia_3.9.owl') rdfParser.parse_into_model(model, ontologyPath, "http://example.org/") return model
def get_complete_metadata(self, user_uri, work_uri, format='json'): work = Work.from_model(self._model, work_uri, user_uri) if not self._can_access('read', work): raise EntryAccessError("Can't access work {0}".format(work_uri)) if format not in ('ntriples', 'rdfxml', 'json'): raise ParamError('invalid RDF format: {0}'.format(format)) query_format = """ PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX catalog: <http://catalog.commonsmachinery.se/ns#> PREFIX rem3: <http://scam.sf.net/schema#> CONSTRUCT { ?s ?p ?o . ?work dc:source ?sourceWork . } WHERE { BIND (<%s> AS ?work) BIND (<%s> AS ?user) ?work catalog:creator ?creator . ?work catalog:visible ?visible . ?work rem3:metadata ?workMetadata . ?work catalog:source ?sourceRef . ?sourceRef rem3:resource ?sourceWork . { ?sourceWork rem3:metadata ?sourceMetadata . } UNION { ?sourceRef rem3:cachedExternalMetadata ?sourceMetadata . } GRAPH ?g { ?s ?p ?o . } FILTER((?g = ?workMetadata || ?g = ?sourceMetadata) && ((?visible = "public") || (?visible = "private") && (?creator = ?user))) } """ query_string = query_format % (work_uri, user_uri) query = RDF.Query(query_string) query_results = query.execute(self._model) # TODO: use results.to_string() with proper format URIs temp_model = RDF.Model(RDF.MemoryStorage()) for statement in query_results.as_stream(): temp_model.append(statement) result = temp_model.to_string(name=format, base_uri=None) return result
def createVoIDModel(to): """Creates an RDF Model according to the VoID Dataset spec for the given arguments. Returns: RDF.Model""" # Validate the to string if not isinstance(to, str): logging.error( "Value of 'to' parameter not a string. Failed to update VoID file. Value=%s.", to) return None if not len(to) > 0: logging.error( "Value of 'to' parameter is zero-length. Failed to update VoID file. Value=%s.", to) return None # Prepare the model m = RDF.Model(RDF.MemoryStorage()) rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' void = "http://rdfs.org/ns/void#" d1lod = "http://dataone.org/" dcterms = "http://purl.org/dc/terms/" subject_node = RDF.Node(blank="d1lod") # Add in our statements m.append( RDF.Statement(subject_node, RDF.Uri(rdf + 'type'), RDF.Uri(void + 'Dataset'))) m.append( RDF.Statement(subject_node, RDF.Uri(void + 'feature'), RDF.Uri(d1lod + 'fulldump'))) m.append( RDF.Statement(subject_node, RDF.Uri(dcterms + 'modified'), RDF.Node(to))) m.append( RDF.Statement(subject_node, RDF.Uri(void + 'dataDump'), RDF.Uri(d1lod + DUMP_FILENAME))) return m
def load_rdf(self): mtime = os.path.getmtime(self.filename) if self.model is not None and mtime <= self.modelMtime: return self.modelMtime = mtime log.info("loading rdf from %r" % self.filename) self.model = RDF.Model(RDF.MemoryStorage()) u = RDF.Uri("file:%s" % self.filename) try: for s in RDF.Parser('turtle').parse_as_stream(u): self.model.add_statement(s) except (Exception, ), e: # e.__class__.__module__ is "RDF", not the real module! if e.__class__.__name__ != "RedlandError": raise raise ValueError("Error parsing %s: %s" % (u, e))
def __init__(self, ns_tbl, large=False): if large: self._storage = RDF.HashStorage( 'db4', options="new='yes',hash-type='bdb'") else: self._storage = RDF.MemoryStorage() self._model = RDF.Model(self._storage) self._g_pred_map = {} self._pred_tbl = {} self.l_true = Literal('true') self.l_false = Literal('false') self.namespace_tbl = ns_tbl
def save(self): """ Saves the pass process modell to a file using the given file path. If no file path is given, an error is thrown. @return : @author """ if (self._filePath is not None): #Create the needed datatypes (storage and model) for rdf storage = RDF.MemoryStorage() if (storage is None): raise Exception( "Failed to create storage for saving the model!") #The model model = RDF.Model(storage) if (model is None): raise Exception("Failed to creat a new RDF model for saving!") #Now request all statements from the resources and add them to the model for resource in self.resources: for statement in resource.serialize(): model.add_statement(statement) #Now serialize to file #Get right type of serializer depending on the ending fileExtension = os.path.splitext(self._filePath)[-1] name = None mimeType = None if (fileExtension == ".nt"): name = "ntriples" else: #By default use the default serialize pass #Now serialize serializer = RDF.Serializer(name=name, mime_type=mimeType) serializer.serialize_model_to_file(self.filePath, model) print(("INFO! Saving model to file \"" + self.filePath + "\" was successfull!")) else: #No file path is currently set raise Exception( "Cannot save a file directly that has never been saved before. Use \"saveAs(self, filePath)\" instead " )
def __init__(self, filePath=None): """ Constructor - Reads the pass process model from the file. The filePath can either be a local path or an absolute path in the internet, specifing the uri where the model to load is defined. In the current Implementation the namespace is cut off for certain namespaces and a local search is performed! If no filePath is given a new model is created @param string filePath : (Absolute) path to the file to read the pass process model from @return : @author """ self._resources = [] self._classMapper = ClassMapper() self._attrMapper = AttributeMapper() self._filePath = None self._changeListeners = [] self._currentlyLoading = False self._model = None #Now decide whether to load a model or create a new one if (filePath is None): #Import here because is only needed here (to prevent loops) from PASSProcessModel import * from BaseLayer import * self._model = PASSProcessModel(self) layer = BaseLayer(self) self._model.hasModelComponent.append(layer) else: #========= Load a model ======= #Check the type of the path and whether the path exists isString = isinstance(filePath, str) if (not isString): raise Exception("Parameter \"filePath\" must be of type str!") #If the basepath is used to define the uri delete it an replace it with a relative path filePath = filePath.replace(ModelManager.DEFAULT_BASE_URI, "./") hasWriteAccess = os.access(os.path.dirname(filePath), os.W_OK) validExtension = os.path.splitext( filePath)[-1] in ModelManager.POSSIBLE_EXTENSIONS #Raise exceptions if above restrictions are not valid if (not hasWriteAccess): raise Exception( "Parameter \"filePath\" must point to a valid file address!" ) if (not validExtension): raise Exception( "Parameter \"filePath\" must have a valid extension!") #========= Continue if everything is alright ======== #Set the variable to not fire change events currently self._currentlyLoading = True #Set the file path self._filePath = filePath #========= Now load the model ========= storage = RDF.MemoryStorage() if storage is None: raise Exception( "Failed to create storage for reading from the file!") model = RDF.Model(storage) if (model is None): raise Exception( "Faile to create model for reading from the file!") #========= Now start parsing ========= #Select parser by file type if (os.path.splitext(filePath)[-1] == ".nt"): parser = RDF.NTriplesParser() else: parser = RDF.Parser("raptor") #Read from file uri = RDF.Uri(string="file:" + filePath) for statement in parser.parse_as_stream(uri, uri): model.add_statement(statement) #Get all class types of each subject node by rdf:type and store them all in the classTypes dict typeQuery = RDF.Query( "SELECT ?s ?o WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?o }" ) classTypes = {} for result in typeQuery.execute(model): #Check the type of s if (result["s"].is_resource()): subjectString = str(result["s"].uri) elif (result["s"].is_blank()): subjectString = str(result["s"].blank_identifier) else: raise Exception( "Received a RDFNode that is a subject and neither of type normal nor blank!" ) #Now insert it into dict and append type if (not (subjectString in classTypes)): classTypes[subjectString] = [] classTypes[subjectString].append(str(result["o"])) #Now generate the instances depending on their rdf:type links stored in the classTypes dict ownClasses = {} for (key, value) in list(classTypes.items()): className = self._classMapper.getClassName(value) #Do a dynamic import of the missing class exec(str("from " + className + " import *"), globals()) classConstructor = globals()[className] if (key.startswith("http://")): newClass = classConstructor(self, uri=key) else: newClass = classConstructor(self, isBlank=True, blankNodeId=key) #Set the PASSProcessModel-Reference if (className == "PASSProcessModel"): if (self._model is not None): print( "WARNING! Two Process Models were read from the file while only one can be instanciated!" ) self._model = newClass ownClasses[key] = newClass #Go through all triples with the component id and perform them before the others to set the right ids on the PASSProcessModelElements tripleQuery = RDF.Query( "SELECT ?s ?o WHERE { ?s <http://www.imi.kit.edu/abstract-pass-ont#hasModelComponentID> ?o }" ) for result in tripleQuery.execute(model): self._convertTriples( result, ownClasses, "http://www.imi.kit.edu/abstract-pass-ont#hasModelComponentID" ) #Go through all triples and include them - Eventually generate additional class instances or literals if this object has not been created yet tripleQuery = RDF.Query("SELECT ?s ?a ?o WHERE { ?s ?a ?o }") for result in tripleQuery.execute(model): self._convertTriples(result, ownClasses) #Finished loading self._currentlyLoading = False
def __init__(self, model=None): super(Redland, self).__init__() if model is None: model = RDF.Model( RDF.MemoryStorage(options_string="contexts='yes'")) self.model = model