def header_graph_description(self): return rdflib.Literal(f'SPARC single dataset graph for {self.id}')
SKIP_NETWORK, reason='Skipping due to network requirement') oq.OntCuries(CURIE_MAP) suffixes = ( '', 'hello', 'world', 'ev:il', '1234567', '1232/123123/asdfasdf', 'lol_this#is/even-worse/_/123', ) test_graph = rdflib.Graph() triples = ( ('UBERON:0000955', 'rdf:type', 'owl:Class'), ('UBERON:0000955', 'rdfs:label', 'brain'), ('UBERON:0000955', 'rdfs:subClassOf', 'owl:Thing'), ('BIRNLEX:796', 'rdf:type', 'owl:Class'), ('BIRNLEX:796', 'rdfs:label', 'Brain'), ('BIRNLEX:796', 'definition:', 'Gray mushy thing in the nogin.'), ('BIRNLEX:796', 'NIFRID:synonym', 'biocpu'), ('BIRNLEX:796', 'NIFRID:synonym', 'thinkthink'), ) for proto_t in triples: test_graph.add( rdflib.URIRef(oq.OntId(e)) if ':' in e else rdflib.Literal(e) for e in proto_t)
def from_user(self, value: Any) -> Union[None, rdflib.Literal]: if value is None: return None if not isinstance(value, str): raise TypeError(f'Expecting string, got {type(value)}') return rdflib.Literal(value)
def combind_using_template_to_super_context(self, inputModel, used_data_inputs, stored_transformation, template, spatial_resultion, context_url=None): super_class_name_url = self.find_context_super_class( used_data_inputs, spatial_resultion, inputModel) if super_class_name_url is None: return inputModel super_class_name = super_class_name_url.split('#')[-1] output_domain_data_type = stored_transformation.get_template_output_domain_data_type( ) for data_obj in used_data_inputs: inputModel.add((data_obj.subject_name, self.PRIVVULN.feeds, data_obj.template_name + super_class_name)) inputModel.add((data_obj.domain_data_type, self.PRIVVULN.feeds, data_obj.template_name + super_class_name)) self._add_element_data_creates_from_data_types( data_obj.subject_name, output_domain_data_type, inputModel) inputModel.add((data_obj.template_name + super_class_name, self.RDF.type, self.PRIVVULN.Transformation)) inputModel.add((data_obj.template_name + super_class_name, self.PRIVVULNV2.name, data_obj.template_name)) output_subjet = stored_transformation.get_template_output_subject() output_data_type = stored_transformation.get_template_template_output_data_type( ) self._add_element_data_creates_from_data_types( output_subjet, output_domain_data_type, inputModel) self._add_element_data_creates_from_data_types( super_class_name_url, output_domain_data_type, inputModel) inputModel.add((output_subjet + super_class_name, self.RDF.type, output_domain_data_type)) inputModel.add((output_subjet + super_class_name, self.RDF.type, output_data_type)) inputModel.add((output_subjet + super_class_name, self.PRIVVULNV2.name, output_subjet)) inputModel.add( (data_obj.template_name + super_class_name, self.PRIVVULNV2.creates, output_subjet + super_class_name)) inputModel.add((super_class_name_url, self.PRIVVULNV2.has, output_subjet + super_class_name)) time_resolutions = stored_transformation.get_time_relations_for_transformation( ) time_resultion = self._find_output_time_resultion_for_transformation( time_resolutions, used_data_inputs) if time_resultion is not None: if (output_subjet + super_class_name, self.PRIVVULNV2.TemporalResolution, None) in inputModel: temporalResolution = inputModel.value( subject=output_subjet + super_class_name, predicate=self.PRIVVULNV2.TemporalResolution) if temporalResolution.value < time_resultion: inputModel.remove((output_subjet + super_class_name, self.PRIVVULNV2.TemporalResolution, temporalResolution)) inputModel.add((output_subjet + super_class_name, self.PRIVVULNV2.TemporalResolution, rdflib.Literal(time_resultion))) else: inputModel.add((output_subjet + super_class_name, self.PRIVVULNV2.TemporalResolution, rdflib.Literal(time_resultion))) template_count = self._find_template_count(used_data_inputs) inputModel.add( (output_subjet + super_class_name, self.PRIVVULNV2.TemplateCount, rdflib.Literal(template_count))) context_types = self._get_context_types(inputModel) self._get_context_data_types(inputModel)[context_url].append( Data( output_data_type, time_resultion, subject_name=output_subjet + super_class_name, template_count=template_count, context_subject=context_url, # spatial_resolutions=rdflib.term.URIRef('https://ontology.hviidnet.com/2020/01/03/smartbuildingprivacyvunl.ttl#Room'), context=context_types[context_url])) # domain_data_type, temporal_resolutions,subject_name, template_count, context, context_subject return inputModel
def triplifyCompany(company): global typy global typ_map g = rdflib.Graph() if company is None: return g gr = rdflib.Namespace('http://purl.org/goodrelations/v1#') schema = rdflib.Namespace('http://schema.org/') base_ns = 'http://data.eghuro.cz/resource/' rejstrik_ns = base_ns + 'public-register/' rejstrik = rdflib.Namespace(rejstrik_ns) org = rdflib.Namespace('http://www.w3.org/ns/org#') foaf = rdflib.Namespace('http://xmlns.com/foaf/0.1/') skos = rdflib.Namespace('http://www.w3.org/2004/02/skos/core#') dc = rdflib.Namespace('http://purl.org/dc/elements/1.1/') time = rdflib.Namespace('http://www.w3.org/2006/time#') g.bind('gr', gr) g.bind('schema', schema) g.bind('org', org) g.bind('foaf', foaf) g.bind('skos', skos) g.bind('dc', dc) g.bind('time', time) base_ns = 'http://data.eghuro.cz/resource/' ico_ns = base_ns + 'business-entity/' business = rdflib.URIRef(ico_ns + 'CZ' + company.ico) zeme_iso_ns = base_ns + 'country/iso/' zeme_iso = rdflib.Namespace(zeme_iso_ns) activity_ns = base_ns + 'business-activity/' activity = rdflib.Namespace(activity_ns) person_ns = base_ns + 'person/' person = rdflib.Namespace(person_ns) b1 = rdflib.BNode() #TODO: b1 = rdflib.URIRef(ico_ns + 'CZ' + company.ico+"/address") btype = schema.Organization if company.rejstrik == Rejstrik.OR: btype = schema.Corporation if company.sidlo != None: if company.sidlo.cisloOr != None: addr_str = company.sidlo.ulice + " " + company.sidlo.cisloPop + "/" + company.sidlo.cisloOr elif company.sidlo.cisloPop != None: addr_str = company.sidlo.ulice + " " + company.sidlo.cisloPop else: addr_str = company.sidlo.ulice addr_str = addr_str.strip() if company.sidlo.psc == None: company.sidlo.psc = "" if company.sidlo.okres == None: company.sidlo.okres = "" # TODO (not used when generating last data) #if company.sidlo.obec == None: # company.sidlo.obec == "" if company.firma != None: if "v likvidaci" in company.firma: company.firma = company.firma.strip("v likvidaci")[0] company.likvidace = True else: company.likvidace = False if company.firma.replace(" ", "").replace(".","").endswith("sro"): typ = "s.r.o." else: x = company.firma.split(',') if len(x) > 1: typ = x[-1] typy.add(typ) else: typ = None else: typ = None for t in [ (business, RDF.type, gr.BusinessEntity), (business, RDF.type, btype), (business, RDF.type, foaf.Organization), (business, RDF.type, org.FormalOrganization), (business, schema.identifier, rdflib.Literal(company.ico)), (business, org.identifier, rdflib.Literal(company.ico)), ]: g.add(t) if company.firma != None: for t in [ (business, gr.legalName, rdflib.Literal(company.firma, lang="cs")), (business, schema.legalName, rdflib.Literal(company.firma, lang="cs")), (business, skos.prefLabel, rdflib.Literal(company.firma, lang="cs")), ]: g.add(t) if company.likvidace: g.add((business, foaf.status, rdflib.URIRef(base_ns + "business-in-liquidation"))) if company.sidlo != None: for t in [ (business, schema.address, b1), (business, org.hasRegisteredSite, b1), (b1, RDF.type, schema.PostalAddress), (b1, RDF.type, org.Site), (b1, org.siteOf, business), (b1, schema.postalCode, rdflib.Literal(company.sidlo.psc)), (b1, schema.addressLocality, rdflib.Literal(company.sidlo.obec, lang="cs")), (b1, schema.streetAddress, rdflib.Literal(addr_str, lang="cs")), (b1, schema.addressCountry, zeme_iso[company.sidlo.stat]), (b1, schema.identifier, rdflib.Literal(company.sidlo.ruian)) ]: g.add(t) if len(company.sidlo.okres) > 0: g.add((b1, schema.region, rdflib.Literal(company.sidlo.okres, lang="cs"))) if company.rejstrik is not None: g.add((business, dc.isReferencedBy, rejstrik[company.rejstrik.name])) if company.firma is not None: if typ is None: typ = company.firma.split()[-1] typ = typ.strip() if typ in typ_map.keys(): ot = rdflib.Namespace(base_ns + 'organisation-type/') g.add( (business, dc.type, ot[typ_map[typ].name]) ) else: print(company.firma + ": \"" + typ + "\"") g.add((rdflib.URIRef(activity_ns), RDF.type, skos.ConceptScheme)) for activ in company.cinnosti: aid = str(getUUIDforActivity(activ)) for t in [ (activity[aid], RDF.type, skos.Concept), (activity[aid], skos.prefLabel, Literal(activ, lang="cs")), (activity[aid], skos.inScheme, rdflib.URIRef(activity_ns)), (business, org.classification, activity[aid]) ]: g.add(t) for member in company.members: #ba = rdflib.BNode() if type(member) is MemberFO: mid = str(getUUIDforActivity(" ".join([member.jmeno, member.prijmeni]).strip())) for t in [ (person[mid], RDF.type, foaf.Person), (person[mid], foaf.givenName, Literal(member.jmeno)), (person[mid], foaf.familyName, Literal(member.prijmeni)), ]: g.add(t) x = mid else: oid = str(getUUIDforActivity(member.nazev)) for t in [ (person[oid], RDF.type, foaf.Organization), (person[oid], skos.prefLabel, Literal(member.nazev)) ]: g.add(t) if member.ico != None: g.add((person[oid], org.identifier, Literal(member.ico))) x = oid #adresa if member.adresa != None: if member.adresa.cisloOr != None: addr_str = member.adresa.ulice + " " + member.adresa.cisloPop + "/" + member.adresa.cisloOr elif member.adresa.cisloPop != None: addr_str = member.adresa.ulice + " " + member.adresa.cisloPop else: addr_str = member.adresa.ulice addr_str = addr_str.strip() if member.adresa.psc == None: member.adresa.psc = "" if member.adresa.okres == None: member.adresa.okres = "" bb = rdflib.BNode() #TODO: bb = rdflib.URIRef(person_ns + x + "/address") for t in [ (person[x], schema.address, bb), (bb, RDF.type, schema.PostalAddress), (bb, schema.postalCode, rdflib.Literal(member.adresa.psc)), (bb, schema.addressLocality, rdflib.Literal(member.adresa.obec)), (bb, schema.streetAddress, rdflib.Literal(addr_str)), (bb, schema.addressCountry, zeme_iso[member.adresa.stat]), ]: g.add(t) mem = person[x] oid = str(getUUIDforActivity(member.organ)) organ = rdflib.URIRef(str(business) + "/organ/" + oid) for t in [ (organ, RDF.type, org.Organization), (organ, org.subOrganizationOf, business), (organ, skos.prefLabel, rdflib.Literal(member.organ)) ]: g.add(t) inte = rdflib.BNode() mid = str(uuid.uuid4()) membership = rdflib.URIRef(organ + "/membership/" + mid) #TODO: inte = rdflib.URIRef(organ + "/membership/" + mid + "/interval") for t in [ (inte, RDF.type, time.ProperInterval), (inte, time.intervalStarts, rdflib.Literal(member.zapsan, datatype=XSD.date)), (inte, time.intervalFinishes, rdflib.Literal(member.vyskrtnut, datatype=XSD.date)), #FIXME (membership, RDF.type, org.Membership), (membership, org.member, mem), (membership, org.organization, organ), (membership, org.memberDuring, inte), ]: g.add(t) if member.funkce is not None: rid = str(getUUIDforActivity(member.funkce.lower())) role = rdflib.URIRef(str(business) + "/role/" + rid) role_g = rdflib.URIRef(base_ns + "business-role/" + rid) for t in [ (role, RDF.type, org.Role), (role, skos.prefLabel, rdflib.Literal(member.funkce.lower())), (role_g, RDF.type, org.Role), (role_g, skos.prefLabel, rdflib.Literal(member.funkce.lower())), (membership, org.role, role), (membership, org.role, role_g), ]: g.add(t) #statutarni organ? return g
def delete_several_depth(self, method="MIN"): """Deletes words with several depths Args: word (str): The word to add to the graph .. code:: python >>> g = Graph() >>> g.add_root_word('car') >>> g.add_word('bus', 1, 'synonym', 'car', comesFrom='http://example/com') >>> g.add_word('bus', 2, 'synonym', 'car', comesFrom='http://example/com') >>> print(g) @prefix ns1: <urn:default:baseUri:#> . @prefix ns2: <http://taxref.mnhn.fr/lod/property/> . @prefix ns3: <http://www.w3.org/2004/02/skos/core#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . ns1:bus ns2:isSynonymOf ns1:car, ns1:root_word_uri ; ns3:prefLabel "bus" ; ns1:comesFrom <http://example/com> ; ns1:depth 1, 2 . ns1:root_word_uri a ns1:root_word ; ns3:prefLabel "car" . >>> g.delete_several_depth() >>> print(g) @prefix ns1: <urn:default:baseUri:#> . @prefix ns2: <http://taxref.mnhn.fr/lod/property/> . @prefix ns3: <http://www.w3.org/2004/02/skos/core#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . ns1:bus ns2:isSynonymOf ns1:car, ns1:root_word_uri ; ns3:prefLabel "bus" ; ns1:comesFrom <http://example/com> ; ns1:depth 1 . ns1:root_word_uri a ns1:root_word ; ns3:prefLabel "car" . """ # TODO should be implemented using one sparql query q_words = """SELECT ?uri ( COUNT(?depth) AS ?c ) WHERE {?uri <urn:default:baseUri:#depth> ?depth} GROUP BY ?uri ORDER BY ASC (?uri)""" for uri, depth in self.query(q_words): if int(depth) < 2: # skipping the uri that do not have several # depth properties continue q_d = ("SELECT (MIN(?o) AS ?int) WHERE { <" + str(uri) + """> <urn:default:baseUri:#depth> ?o } """) cur_dep = [int(dep) for dep, in self.query(q_d)][0] q_all_depth = ("SELECT ?unwanted_depth WHERE { <" + str(uri) + "> <urn:default:baseUri:#depth> ?unwanted_depth }") for (unwanted_tripple, ) in self.query(q_all_depth): if int(unwanted_tripple) == cur_dep: continue self.remove((uri, self.base_local.depth, rdflib.Literal(int(unwanted_tripple))))
def make_generic_messages(self, datagraph: GraphLike, focus_node, value_node) -> List[rdflib.Literal]: m = "String language is not in {}".format( stringify_node(datagraph, self.string_rules[0])) return [rdflib.Literal(m)]
def test_literal_from_bool(self): l = rdflib.Literal(True) self.assertEquals(l.datatype, rdflib.XSD["boolean"])
def convert_mapping_to_rdf(input_mapping_file,extract_subnode_info=False,split_node_by=":",keep_index=1,layer_type="uniprotkb",annotation_mapping_file="test.gaf",go_identifier="GO:",prepend_string=None): ## generate input examples based on community assignment g = rdflib.graph.Graph() KT = rdflib.Namespace('http://kt.ijs.si/hedwig#') amp_uri = 'http://kt.ijs.si/ontology/hedwig#' obo_uri = "http://purl.obolibrary.org/obo/" AMP = rdflib.Namespace(amp_uri) ## include neighbors as instances or not.. mapping_file = {} if extract_subnode_info: for k,v in input_mapping_file.items(): node,layer = k if layer_type == None: mapping_file[node] = v if layer_type != False: if layer == layer_type: mapping_file[node.split(split_node_by)[keep_index]] = v else: for k,v in input_mapping_file.items(): try: node, layer = k except: parts = k.split(split_node_by) ## PSI-MI format if layer_type in parts: layer, node= k.split(split_node_by) ## PSI-MI format else: continue if layer_type == None: mapping_file[node] = v if layer_type != False: if layer == layer_type: mapping_file[node] = v id_identifier = 0 if ".gaf" in annotation_mapping_file: uniGO = parse_gaf_file(annotation_mapping_file) else: print("Please, provide gaf-based item-term mappings") ## iterate through community assignments and construct the trainset ## tukaj morda dodaj example name for node, com in mapping_file.items(): try: id_identifier += 1 u = rdflib.term.URIRef('%sexample#%s%s' % (amp_uri, node,str(id_identifier))) g.add((u, rdflib.RDF.type, KT.Example)) g.add((u, KT.class_label, rdflib.Literal(str(com)+"_community"))) for goterm in uniGO[node]: if prepend_string is not None: goterm = prepend_string+goterm if go_identifier is not None: if go_identifier in goterm: annotation_uri = rdflib.term.URIRef('%s%s' % (obo_uri, rdflib.Literal(goterm))) blank = rdflib.BNode() g.add((u, KT.annotated_with, blank)) g.add((blank, KT.annotation, annotation_uri)) else: annotation_uri = rdflib.term.URIRef('%s%s' % (obo_uri, rdflib.Literal(goterm))) blank = rdflib.BNode() g.add((u, KT.annotated_with, blank)) g.add((blank, KT.annotation, annotation_uri)) except Exception as err: print(err) ## incorrect mappings are ignored.. pass return g
def time_stamp_now_node(): # TODO: Use xsd:dateTimeStamp datetime_now = datetime.datetime.now() timestamp_literal = datetime_now.strftime("%Y-%m-%d %H:%M:%S") return rdflib.Literal(timestamp_literal)
def test_repr_quote(self): a = rdflib.Literal('"') b = eval(repr(a)) self.assertEquals(a, b)
def _setup_global_graph(): """Lazy creation of the graph used to store events by events generators, and read by CGI scripts. """ global _store_input global _events_conjunctive_graph if _store_input is None: if _events_conjunctive_graph is not None: raise Exception("_events_conjunctive_graph should be None") if not isinstance(_events_storage_style, tuple): raise Exception("Wrong type for _events_storage_style") if _events_storage_style[0] == "IOMemory": _store_input = IOMemory() _events_conjunctive_graph = rdflib.ConjunctiveGraph( store=_store_input) elif _events_storage_style[0] == "SQLAlchemy": # How to install rdflib-sqlalchemy # pip install rdflib-sqlalchemy # # py -2.7 -m pip install rdflib-sqlalchemy # from glob import glob # ImportError: No module named glob # # py -3.6 -m pip install rdflib-sqlalchemy # OK sqlite_ident = rdflib.URIRef("rdflib_survol") sqlite_path = _events_storage_style[1] # This path might contain environment variables. sqlite_path_expanded = os.path.expandvars(sqlite_path) sqlite_uri = rdflib.Literal(sqlite_path_expanded) _store_input = rdflib.plugin.get( "SQLAlchemy", rdflib.store.Store)(identifier=sqlite_ident) _events_conjunctive_graph = rdflib.ConjunctiveGraph( _store_input, identifier=sqlite_ident) try: # _events_conjunctive_graph.open(sqlite_uri, create=True) # Open previously created store, or create it if it doesn't exist yet _log_db_access("_setup_global_graph", "O", "1", sqlite_uri) rt = _events_conjunctive_graph.open(sqlite_uri, create=False) except Exception as exc: logging.error("sqlite_uri=%s. Exception=%s", sqlite_uri, exc) logging.error("Trace=%s" % traceback.format_exc()) logging.error("Stack=%s" % traceback.format_stack()) # According to the documentation, it should rather return this value instead of throwing. rt = rdflib.store.NO_STORE try: if rt == rdflib.store.NO_STORE: # There is no underlying SQLAlchemy infrastructure, create it _log_db_access("_setup_global_graph", "C", "2", sqlite_uri) _events_conjunctive_graph.open(sqlite_uri, create=True) elif rt != rdflib.store.VALID_STORE: raise Exception("sqlite_uri=%s rt=%d" % (sqlite_uri, rt)) except Exception as exc: raise Exception("sqlite_uri=%s.Exception=%s" % (sqlite_uri, exc)) else: raise Exception("Unknown storage style:" + str(_events_storage_style)) _check_globals("_setup_global_graph") return _events_conjunctive_graph
def header_graph_description(self): return rdflib.Literal(f'Additional metadata for identifiers referenced in export.')
def id_(v): s = rdflib.URIRef(dsid) yield s, rdf.type, owl.NamedIndividual yield s, rdf.type, sparc.Dataset yield s, rdfs.label, rdflib.Literal(self.folder_name) # not all datasets have titles
def main(): # load in our existing graph # note: while it would be nice to allow specification of phenotypes to be decoupled # from insertion into the graph... maybe we could enable this, but it definitely seems # to break a number of nice features... and we would need the phenotype graph anyway Config('temporary-graph') EXISTING_GRAPH = graphBase.in_graph #EXISTING_GRAPH = rdflib.Graph() #graphBase.in_graph = EXISTING_GRAPH #graphBase.core_graph = EXISTING_GRAPH local_prefix = auth.get_path('ontology-local-repo') / 'ttl' sources = (f'{local_prefix}/NIF-Neuron-Defined.ttl', f'{local_prefix}/NIF-Neuron.ttl', f'{local_prefix}/NIF-Neuron-Phenotype.ttl', f'{local_prefix}/phenotype-core.ttl', f'{local_prefix}/phenotypes.ttl', f'{local_prefix}/hbp-special.ttl') for file in sources: EXISTING_GRAPH.parse(file, format='turtle') #EXISTING_GRAPH.namespace_manager.bind('PR', makePrefixes('PR')['PR']) #graphBase.core_graph = EXISTING_GRAPH #graphBase.out_graph = rdflib.Graph() graphBase.__import_name__ = 'neurondm.lang' proot = graphBase.core_graph.qname(PHENO_ROOT) mroot = graphBase.core_graph.qname(MOD_ROOT) graphBase._predicates, _psupers = getPhenotypePredicates( EXISTING_GRAPH, proot, mroot) g = makeGraph('merged', prefixes={k: str(v) for k, v in EXISTING_GRAPH.namespaces()}, graph=EXISTING_GRAPH) reg_neurons = list(g.g.subjects(rdfs.subClassOf, _NEURON_CLASS)) tc_neurons = [ _ for (_, ) in g.g.query('SELECT DISTINCT ?match WHERE {?match rdfs:subClassOf+ %s}' % g.g.qname(_NEURON_CLASS)) ] def_neurons = g.get_equiv_inter(_NEURON_CLASS) nodef = sorted(set(tc_neurons) - set(def_neurons)) og1 = MeasuredNeuron.out_graph = rdflib.Graph( ) # there is only 1 out_graph at a time, load and switch mns = [MeasuredNeuron(id_=n) for n in nodef] mnsp = [n for n in mns if n.pes] graphBase.out_graph = rdflib.Graph( ) # XXX NEVER DO THIS IT IS EVIL ZALGO WILL EAT YOUR FACE graphBase.ng.g = graphBase.out_graph # and he did, had to swtich to graphBase for exactly this reason >_< dns = [Neuron(id_=n) for n in sorted(def_neurons)] #dns += [Neuron(*m.pes) if m.pes else m.id_ for m in mns] dns += [Neuron(*m.pes) for m in mns if m.pes] # reset everything for export config = Config('phenotype-direct', source_file=relative_path(__file__)) #Neuron.out_graph = graphBase.out_graph # each subclass of graphBase has a distinct out graph IF it was set manually #Neuron.out_graph = rdflib.Graph() #ng = makeGraph('', prefixes={}, graph=Neuron.out_graph) #ng.filename = Neuron.ng.filename Neuron.mro()[1].existing_pes = { } # wow, new adventures in evil python patterns mro()[1] dns = [Neuron(*d.pes) for d in set(dns) ] # TODO remove the set and use this to test existing bags? #from neurons.lang import WRITEPYTHON #WRITEPYTHON(sorted(dns)) #ng.add_ont(TEMP['defined-neurons'], 'Defined Neurons', 'NIFDEFNEU', #'VERY EXPERIMENTAL', '0.0.0.1a') #ng.add_trip(TEMP['defined-neurons'], owl.imports, rdflib.URIRef('file:///home/tom/git/NIF-Ontology/ttl/phenotype-core.ttl')) #ng.add_trip(TEMP['defined-neurons'], owl.imports, rdflib.URIRef('file:///home/tom/git/NIF-Ontology/ttl/phenotypes.ttl')) #ng.write() ontinfo = ( (Neuron.ng.ontid, rdf.type, owl.Ontology), (Neuron.ng.ontid, rdfs.label, rdflib.Literal('phenotype direct neurons')), (Neuron.ng.ontid, rdfs.comment, rdflib.Literal('Neurons derived directly from phenotype definitions') ), ) [Neuron.out_graph.add(t) for t in ontinfo] Neuron.write() Neuron.write_python() bads = [ n for n in Neuron.ng.g.subjects(rdf.type, owl.Class) if len(list(Neuron.ng.g.predicate_objects(n))) == 1 ] if __name__ == '__main__': breakpoint() return config
def annotateServiceDescription(self, desc): desc.add(self.query_predicate, rdflib.Literal(self.get_query()))
def add_word(self, word, depth, relation, target_word, synset_uri=None, comesFrom=None): """Add some tripples to the graph that contains the relation between the word and its target. Args: word (str): The word to add to the graph deepth (int): The deepth of the reccursion relation (str): The relation of the word to the target word. Could be "hyponym", "hypernym", "holonym" or "synonym" target_word (str): The word .. code:: python >>> g = Graph() >>> g.add_root_word('car') >>> print(g) @prefix ns1: <http://www.w3.org/2004/02/skos/core#> . <urn:default:baseUri:#root_word_uri> a <urn:default:baseUri:#root_word> ; ns1:prefLabel "car" . >>> g.add_word('bus', 1, 'synonym', 'car', comesFrom='http://example.com') >>> print(g) @prefix ns1: <http://www.w3.org/2004/02/skos/core#> . @prefix ns2: <urn:default:baseUri:#> . @prefix ns3: <http://taxref.mnhn.fr/lod/property/> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . ns2:bus ns3:isSynonymOf ns2:root_word_uri ; ns1:prefLabel "bus" ; ns2:comesFrom <http://example.com> ; ns2:depth 1 . ns2:root_word_uri a ns2:root_word ; ns1:prefLabel "car" . """ self._check_word_type(word) # to avoid unvalid URI # as some wordnet words do have unwanted characters ss_word = quote(word) ss_target_word = quote(target_word) assert ss_word != ss_target_word base_wn = rdflib.Namespace("http://www.w3.org/2006/03/wn/wn20/schema/") if relation == "hyponym": rela = base_wn.hyponymOf elif relation == "hypernym": rela = base_wn.hypernymOf elif relation == "holonym": rela = base_wn.holonymOf elif relation == "synonym": # word is synonym rela = rdflib.URIRef( "http://taxref.mnhn.fr/lod/property/isSynonymOf") else: raise ValueError( f"The relation '{relation}' is not implemented in the graph") if depth == 1: # the relation is linked to the root word target = rdflib.URIRef(self.root_word_uri) else: target = rdflib.URIRef(self.local_namespace + ss_target_word) # adding the relation word is synonym/hyponym/... of target word self.add(( rdflib.URIRef(self.local_namespace + ss_word), rela, target, )) # adding the depth information self.add(( rdflib.URIRef(self.local_namespace + ss_word), self.base_local.depth, rdflib.Literal(depth), )) # adding the preflabel info self.add(( rdflib.URIRef(self.local_namespace + ss_word), rdflib.URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"), rdflib.Literal(word), )) # adding the synset info if synset_uri: self.add(( rdflib.URIRef(self.local_namespace + ss_word), self.base_local.synsetLink, rdflib.URIRef(synset_uri), )) # adding the website the data is comming from if comesFrom: self.add(( rdflib.URIRef(self.local_namespace + ss_word), self.base_local.comesFrom, rdflib.URIRef(comesFrom), )) assert ("<file:///home/k/Documents/lexicons_builder/" not in self.serialize(format="ttl").decode())
def type_date (cls, date, tz): """input `date` should be interpretable as having a local timezone""" date_tz = dup.parse(date, tzinfos=tz) return rdf.Literal(date_tz, datatype=rdf.XSD.dateTime)
def __init__(self, store="default", identifier=None, namespace_manager=None, base=None): super().__init__( store=store, identifier=identifier, namespace_manager=namespace_manager, base=base, ) # add the root word, self.add(( self.root_word_uriref, rdflib.namespace.RDF.type, rdflib.namespace.RDFS.Class, )) self.add(( self.root_word_uriref, rdflib.namespace.SKOS.definition, rdflib.Literal( "A root word is the term from which all of the words are fetched" ), )) # hyponym self.add(( self.base_local.hyponym, rdflib.namespace.RDF.type, rdflib.namespace.RDFS.Class, )) self.add(( self.base_local.hyponym, rdflib.namespace.SKOS.definition, rdflib.Literal( "Hyponymy is the converse of hypernymy. For example, red is a hyponym of color." ), )) # hypernym self.add(( self.base_local.hypernym, rdflib.namespace.RDF.type, rdflib.namespace.RDFS.Class, )) self.add(( self.base_local.hypernym, rdflib.namespace.SKOS.definition, rdflib.Literal( "a word with a broad meaning constituting a category into which words with more specific meanings fall; a superordinate. For example, colour is a hypernym of red." ), )) # holonym self.add(( self.base_local.holonym, rdflib.namespace.RDF.type, rdflib.namespace.RDFS.Class, )) self.add(( self.base_local.holonym, rdflib.namespace.SKOS.definition, rdflib.Literal( """A term that denotes a whole, a part of which is denoted by a second term. The word "face" is a holonym of the word "eye".""" ), ))
def testSubfieldsGraphGeneration(self): fake_units = 'fake_resources/subfields.yaml' file_contents = rdf_helper.ReadFile(fake_units) yaml_subfields = yaml_handler.ImportYamlFiles(file_contents) # Create global graph graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(graph) namespace_manager.bind('owl', namespace.OWL) namespace_manager.bind('db', constants.DIGITAL_BUILDINGS_NS) graph.namespace_manager = namespace_manager g_subfields = rdflib_subfields_handler.GenerateGraph( yaml_subfields, graph) # Main Types subfield_type = rdflib.URIRef(constants.SUBFIELDS_NS['SubField']) point_type = rdflib.URIRef(constants.SUBFIELDS_NS['Point_type']) capacity = rdflib.URIRef(constants.SUBFIELDS_NS['Capacity']) capacity_description = ( 'A design parameter quantity. Ex: design motor ' 'power capacity. Is always a maximum limit.') point_type_class = (point_type, rdflib.RDF.type, rdflib.OWL.Class) capacity_class = (capacity, rdflib.RDF.type, rdflib.OWL.Class) measurement_descriptor = rdflib.URIRef( constants.SUBFIELDS_NS['Measurement_descriptor']) absolute = rdflib.URIRef(constants.SUBFIELDS_NS['Absolute']) absolute_description = ( 'Quality of media with respect to non-relativistic ' 'boudaries (e.g. absolute temperature).') measurement_descriptor_class = (measurement_descriptor, rdflib.RDF.type, rdflib.OWL.Class) absolute_class = (absolute, rdflib.RDF.type, rdflib.OWL.Class) measurement = rdflib.URIRef(constants.SUBFIELDS_NS['Measurement']) concentration = rdflib.URIRef(constants.SUBFIELDS_NS['Concentration']) concentration_description = ( 'Concentration of chemical (usually in parts ' 'per million or parts per billion).') measurement_class = (measurement, rdflib.RDF.type, rdflib.OWL.Class) concentration_class = (concentration, rdflib.RDF.type, rdflib.OWL.Class) descriptor = rdflib.URIRef(constants.SUBFIELDS_NS['Descriptor']) air = rdflib.URIRef(constants.SUBFIELDS_NS['Air']) air_description = 'Atmospheric air, either conditioned or unconditioned' descriptor_class = (descriptor, rdflib.RDF.type, rdflib.OWL.Class) air_class = (air, rdflib.RDF.type, rdflib.OWL.Class) component = rdflib.URIRef(constants.SUBFIELDS_NS['Component']) coil = rdflib.URIRef(constants.SUBFIELDS_NS['Coil']) coil_description = ('Component that exchanges heat between two media ' 'streams.') component_class = (component, rdflib.RDF.type, rdflib.OWL.Class) coil_class = (coil, rdflib.RDF.type, rdflib.OWL.Class) aggregation = rdflib.URIRef(constants.SUBFIELDS_NS['Aggregation']) max_ref = rdflib.URIRef(constants.SUBFIELDS_NS['Max']) min_ref = rdflib.URIRef(constants.SUBFIELDS_NS['Min']) max_description = 'Maximum value (e.g. Max_Cooling_Air_Flow_Setpoint)' aggregation_class = (aggregation, rdflib.RDF.type, rdflib.OWL.Class) max_class = (max_ref, rdflib.RDF.type, rdflib.OWL.Class) min_class = (min_ref, rdflib.RDF.type, rdflib.OWL.Class) # Assertions self.assertIn(capacity_class, g_subfields) self.assertIn(point_type_class, g_subfields) self.assertIn((capacity, rdflib.RDFS.comment, rdflib.Literal(capacity_description)), g_subfields) self.assertIn(measurement_descriptor_class, g_subfields) self.assertIn(absolute_class, g_subfields) self.assertIn((absolute, rdflib.RDFS.comment, rdflib.Literal(absolute_description)), g_subfields) self.assertIn(measurement_class, g_subfields) self.assertIn(concentration_class, g_subfields) self.assertIn((concentration, rdflib.RDFS.comment, rdflib.Literal(concentration_description)), g_subfields) self.assertIn(descriptor_class, g_subfields) self.assertIn(air_class, g_subfields) self.assertIn( (air, rdflib.RDFS.comment, rdflib.Literal(air_description)), g_subfields) self.assertIn(component_class, g_subfields) self.assertIn(coil_class, g_subfields) self.assertIn( (coil, rdflib.RDFS.comment, rdflib.Literal(coil_description)), g_subfields) self.assertIn(aggregation_class, g_subfields) self.assertIn(min_class, g_subfields) self.assertIn(max_class, g_subfields) self.assertIn( (max_ref, rdflib.RDFS.comment, rdflib.Literal(max_description)), g_subfields) # SubClasses assertions self.assertIn((point_type, rdflib.RDFS.subClassOf, subfield_type), g_subfields) self.assertIn((capacity, rdflib.RDFS.subClassOf, point_type), g_subfields) self.assertIn((aggregation, rdflib.RDFS.subClassOf, subfield_type), g_subfields) self.assertIn((min_ref, rdflib.RDFS.subClassOf, aggregation), g_subfields) self.assertIn((max_ref, rdflib.RDFS.subClassOf, aggregation), g_subfields)
def make_generic_messages(self, datagraph: GraphLike, focus_node, value_node) -> List[rdflib.Literal]: return [ rdflib.Literal("More than one String shares the same Language") ]
def __call__(self, x): return rdflib.Literal(x)
def combind_using_template_context(self, inputModel, used_data_inputs, stored_transformation, template, context_url=None): template_rand_nr = uuid.uuid4().__str__() output_domain_data_type = stored_transformation.get_template_output_domain_data_type( ) for data_obj in used_data_inputs: inputModel.add((data_obj.subject_name, self.PRIVVULN.feeds, data_obj.template_name + template_rand_nr)) inputModel.add((data_obj.domain_data_type, self.PRIVVULN.feeds, data_obj.template_name + template_rand_nr)) self._add_element_data_creates_from_data_types( data_obj.subject_name, output_domain_data_type, inputModel) inputModel.add((data_obj.template_name + template_rand_nr, self.RDF.type, self.PRIVVULN.Transformation)) inputModel.add((data_obj.template_name + template_rand_nr, self.PRIVVULNV2.name, data_obj.template_name)) output_subjet = stored_transformation.get_template_output_subject() output_data_type = stored_transformation.get_template_template_output_data_type( ) self._add_element_data_creates_from_data_types( output_subjet, output_domain_data_type, inputModel) # output_subjet, output_data_type, output_domain_data_type = self._find_output_for_template(template) inputModel.add((output_subjet + template_rand_nr, self.RDF.type, output_domain_data_type)) inputModel.add((output_subjet + template_rand_nr, self.RDF.type, output_data_type)) inputModel.add((output_subjet + template_rand_nr, self.PRIVVULNV2.name, output_subjet)) inputModel.add((data_obj.template_name + template_rand_nr, self.PRIVVULN.feeds, output_subjet + template_rand_nr)) time_resolutions = stored_transformation.get_time_relations_for_transformation( ) time_resultion = self._find_output_time_resultion_for_transformation( time_resolutions, used_data_inputs) if time_resultion is not None: inputModel.add((output_subjet + template_rand_nr, self.PRIVVULNV2.TemporalResolution, rdflib.Literal(time_resultion))) template_count = self._find_template_count(used_data_inputs) inputModel.add( (output_subjet + template_rand_nr, self.PRIVVULNV2.TemplateCount, rdflib.Literal(template_count))) context_types = self._get_context_types(inputModel) self._get_context_data_types(inputModel)[context_url].append( Data( output_domain_data_type, time_resultion, subject_name=output_subjet + template_rand_nr, template_count=template_count, context_subject=context_url, # spatial_resolutions=rdflib.term.URIRef('https://ontology.hviidnet.com/2020/01/03/smartbuildingprivacyvunl.ttl#Room'), context=context_types[context_url])) # domain_data_type, temporal_resolutions,subject_name, template_count, context, context_subject return inputModel
def __call__(self, x): if not self.f: return rdflib.Literal(bool(x)) if callable(self.f): return rdflib.Literal(bool(self.f(x))) raise Exception("Function passed to bool is not callable")
def main(): g = rdflib.Graph() g.bind('', 'http://linked.data.gov.au/dataset/meshblockscatchments/') g.bind('geo', GEO) g.bind('geox', GEOX) g.bind('dbp', DBP) g.bind('qudt', QUDT) g.bind('prov', PROV) g.bind('sss', r_subject) g.bind('ppp', r_predicate) g.bind('ooo', r_object) g.bind('mmm', method) g.bind('www', sfWithin) g.bind('ccc', cc_pre) g.bind('mbb', mb_pre) plan_uri = rdflib.URIRef( "http://linked.data.gov.au/dataset/meshblockscatchments/SpatialIntersection" ) g.add((plan_uri, a, PROV['Plan'])) g.add((plan_uri, RDFS['label'], rdflib.Literal('Spatial Intersection Method'))) g.add((plan_uri, RDFS['seeAlso'], rdflib.URIRef('https://todo.uriref.method.docs'))) #g.bind('i', plan_uri) # todo: wasAttributedTo and generatedAtTime con = pg.connect( "host=localhost dbname=intersect_me user=postgres password=password") cur = con.cursor("cur1") cur.execute(""" SELECT mb.mb_code_2016, cc.hydroid, mb.mb_area, cc.cc_area, mb.i_area, mb.is_overlaps, cc.is_overlaps, mb.is_within, cc.is_within FROM public."mbintersectccareas_classify" as mb INNER JOIN public."ccintersectmbareas_classify" as cc on mb.mb_code_2016 = cc.mb_code_2016 and mb.hydroid = cc.hydroid WHERE (mb.is_overlaps or cc.is_overlaps) and (not mb.is_within) and (not cc.is_within); --LIMIT 10; --ORDER BY mb.mb_code_2016; """) c = 0 intersection_iter = 0 with open("overlaps_all.ttl", "w") as outfile: for record in cur: intersection_iter += 1 c += 1 mb_code_2016 = str(record[0]) #mb_uri = mb_code_to_uri(mb_code_2016) hydroid = str(record[1]) #cc_uri = cc_hydroid_to_uri(hydroid) #g.add((mb_uri, a, Feature)) #g.add((cc_uri, a, Feature)) #mb_area = rdflib.BNode() #cc_area = rdflib.BNode() mb_area_m2 = float(record[2]) cc_area_m2 = float(record[3]) i_area_m2 = float(record[4]) mb_area_m2 = round((mb_area_m2 / 100.0), 7) * 100.0 cc_area_m2 = round((cc_area_m2 / 100.0), 7) * 100.0 i_area_m2 = round((i_area_m2 / 100.0), 7) * 100.0 # g.add((mb_area, numericValue, rdflib.Literal(mb_area_m2, datatype=doubleType))) # g.add((mb_area, unit, sqm)) # g.add((mb_uri, area, mb_area)) # # g.add((cc_area, numericValue, rdflib.Literal(cc_area_m2, datatype=doubleType))) # g.add((cc_area, unit, sqm)) # g.add((cc_uri, area, cc_area)) overlap = rdflib.BNode() g.add((overlap, a, Feature)) overlap_area = rdflib.BNode() g.add((overlap_area, numericValue, rdflib.Literal(i_area_m2, datatype=doubleType))) g.add((overlap_area, unit, sqm)) g.add((overlap, area, overlap_area)) # g.add((mb_uri, sfContains, overlap)) assertion_b1 = rdflib.BNode() g.add((assertion_b1, r_subject, mb_uri)) g.add((assertion_b1, r_predicate, sfContains)) g.add((assertion_b1, r_object, overlap)) g.add((assertion_b1, method, plan_uri)) # g.add((cc_uri, sfContains, overlap)) assertion_b2 = rdflib.BNode() g.add((assertion_b2, r_subject, cc_uri)) g.add((assertion_b2, r_predicate, sfContains)) g.add((assertion_b2, r_object, overlap)) g.add((assertion_b2, method, plan_uri)) assertion_b3 = rdflib.BNode() # g.add((mb_uri, transitiveSfOverlap, cc_uri)) g.add((assertion_b3, r_subject, mb_uri)) g.add((assertion_b3, r_predicate, transitiveSfOverlap)) g.add((assertion_b3, r_object, cc_uri)) g.add((assertion_b3, method, plan_uri)) next_chunk = overlaps_template.format( mb_code_2016=mb_code_2016, hydroid=hydroid, intersection_iter=intersection_iter, mb_area_m2=mb_area_m2, cc_area_m2=cc_area_m2, i_area_m2=i_area_m2) outfile.write(next_chunk)
def __call__(self, x): return rdflib.Literal(datetime.datetime.strptime(x, self.f))
'http://purl.org/vodan/whocovid19crfsemdatamodel/who-covid-19-rapid-crf' )) not in g: g.add(( form, rdflib.RDF.type, rdflib.URIRef( 'http://purl.org/vodan/whocovid19crfsemdatamodel/who-covid-19-rapid-crf' ))) print('new form ' + form.toPython()) group_class_old = 0 module_class_old = 0 questionnaire_subsection_old = 0 column = 0 for item in header: answer = row[column] if item == 'Record Creation Date' or item == 'Report Creation Date': version_info = rdflib.Literal(answer) elif item == 'Report Name Custom': report_name = answer elif item == 'Report Parent': if answer != 'FOLLOW UP' and answer != 'No parent': report_parent = answer elif item in var2class and (answer != ""): #Make instance for question question_class = var2class[item] question = rdflib.BNode() g.add((question, rdflib.RDF.type, question_class)) #print(' question ' + question_class.toPython()) #Get question group, make new if needed, make sure the question is not part of another question group_class = getPartOfClass(question_class) if group_class != group_class_old and n.Question_group in onto.transitive_objects( group_class, rdflib.RDFS.subClassOf):
def convert(self, csvreader): start = time.time() if self.OUT: sys.stderr.write("Output to %s\n" % self.OUT.name) if self.IDENT != "auto" and not isinstance(self.IDENT, tuple): self.IDENT = (self.IDENT, ) if not self.BASE: warnings.warn("No base given, using http://example.org/instances/") self.BASE = rdflib.Namespace("http://example.org/instances/") if not self.PROPBASE: warnings.warn( "No property base given, using http://example.org/property/") self.PROPBASE = rdflib.Namespace("http://example.org/props/") # skip lines at the start for x in range(self.SKIP): next(csvreader) # read header line header_labels = list(next(csvreader)) headers = dict( enumerate([self.PROPBASE[toProperty(x)] for x in header_labels])) # override header properties if some are given for k, v in self.PROPS.items(): headers[k] = v header_labels[k] = split_uri(v)[1] if self.DEFINECLASS: # output class/property definitions self.triple(self.CLASS, RDF.type, RDFS.Class) for i in range(len(headers)): h, l_ = headers[i], header_labels[i] if h == "" or l_ == "": continue if self.COLUMNS.get(i, self.DEFAULT) == "ignore": continue self.triple(h, RDF.type, RDF.Property) self.triple(h, RDFS.label, rdflib.Literal(toPropertyLabel(l_))) self.triple(h, RDFS.domain, self.CLASS) self.triple(h, RDFS.range, self.COLUMNS.get(i, default_node_make).range()) rows = 0 for l_ in csvreader: try: if self.IDENT == "auto": uri = self.BASE["%d" % rows] else: uri = self.BASE["_".join([ quote(x.encode("utf8").replace(" ", "_"), safe="") for x in index(l_, self.IDENT) ])] if self.LABEL: self.triple( uri, RDFS.label, rdflib.Literal(" ".join(index(l_, self.LABEL)))) if self.CLASS: # type triple self.triple(uri, RDF.type, self.CLASS) for i, x in enumerate(l_): x = x.strip() if x != "": if self.COLUMNS.get(i, self.DEFAULT) == "ignore": continue try: o = self.COLUMNS.get(i, rdflib.Literal)(x) if isinstance(o, list): for _o in o: self.triple(uri, headers[i], _o) else: self.triple(uri, headers[i], o) except Exception as e: warnings.warn( "Could not process value for column " + "%d:%s in row %d, ignoring: %s " % (i, headers[i], rows, e.message)) rows += 1 if rows % 100000 == 0: sys.stderr.write("%d rows, %d triples, elapsed %.2fs.\n" % (rows, self.triples, time.time() - start)) except: sys.stderr.write("Error processing line: %d\n" % rows) raise # output types/labels for generated URIs classes = set() for l_, x in uris.items(): u, c = x self.triple(u, RDFS.label, rdflib.Literal(l_)) if c: c = rdflib.URIRef(c) classes.add(c) self.triple(u, RDF.type, c) for c in classes: self.triple(c, RDF.type, RDFS.Class) self.OUT.close() sys.stderr.write("Converted %d rows into %d triples.\n" % (rows, self.triples)) sys.stderr.write("Took %.2f seconds.\n" % (time.time() - start))
def test_repr_apostrophe(self): a = rdflib.Literal("'") b = eval(repr(a)) self.assertEqual(a, b)
def header_graph_description(self): return rdflib.Literal(f'SPARC organization graph for {self.id}')