def testHTML(): l1=Literal('<msg>hello</msg>', datatype=RDF.XMLLiteral) assert l1.value is not None, 'xml must have been parsed' assert l1.datatype==RDF.XMLLiteral, 'literal must have right datatype' l2=Literal('<msg>hello</msg>', datatype=RDF.HTML) assert l2.value is not None, 'xml must have been parsed' assert l2.datatype==RDF.HTML, 'literal must have right datatype' assert l1!=l2 assert not l1.eq(l2)
def parseTerm(element): """rdflib object (Literal, URIRef, BNode) for the given elementtree element""" tag, text = element.tag, element.text if tag == RESULTS_NS_ET + 'literal': if text is None: text = '' ret = Literal(text) if element.get('datatype', None): ret.datatype = URIRef(element.get('datatype')) return ret elif tag == RESULTS_NS_ET + 'uri': return URIRef(text) elif tag == RESULTS_NS_ET + 'bnode': return BNode(text) else: raise TypeError("unknown binding type %r" % element)
def testPythonRoundtrip(): l1=Literal('<msg>hello</msg>', datatype=RDF.XMLLiteral) assert l1.value is not None, 'xml must have been parsed' assert l1.datatype==RDF.XMLLiteral, 'literal must have right datatype' l2=Literal('<msg>good morning</msg>', datatype=RDF.XMLLiteral) assert l2.value is not None, 'xml must have been parsed' assert not l1.eq(l2), 'literals must NOT be equal' l3=Literal(l1.value) assert l1.eq(l3), 'roundtripped literals must be equal' assert l3.datatype==RDF.XMLLiteral, 'literal must have right datatype' l4=Literal('<msg >hello</msg>', datatype=RDF.XMLLiteral) assert l1==l4 assert l1.eq(l4) rdflib.NORMALIZE_LITERALS=False l4=Literal('<msg >hello</msg>', datatype=RDF.XMLLiteral) assert l1!=l4 assert l1.eq(l4) rdflib.NORMALIZE_LITERALS=True
def query_cache_for_linkset(current_uri, g): query = """ PREFIX dct: <http://purl.org/dc/terms/> PREFIX loci: <http://linked.data.gov.au/def/loci#> PREFIX o: <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> PREFIX p: <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> PREFIX s: <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> SELECT ?stmt ?to ?from ?pred ?linksetInstance ?stmt_pred ?stmt_value WHERE {{ ?stmt dct:isPartOf ?linksetInstance . ?linksetInstance a loci:Linkset . ?stmt o: ?to . ?stmt p: ?pred . ?stmt s: ?from . ?stmt ?x ?y FILTER(?from = <{current_uri}>) OPTIONAL {{ ?stmt ?stmt_pred ?stmt_value }} }} """.format(current_uri=current_uri) sparql = SPARQLWrapper2(SPARQL_ENDPOINT) sparql.setQuery(query) for result in sparql.query().bindings: stmt = result["stmt"] from_uri = result["from"] to_uri = result["to"] pred = result["pred"] stmt_pred = None if "stmt_pred" in result: stmt_pred = result["stmt_pred"] stmt_value = None if "stmt_value" in result: stmt_value = result["stmt_value"] linksetInstance = result["linksetInstance"] partOf = URIRef("http://purl.org/dc/terms/isPartOf") stmt_node = None if (stmt.type == 'uri'): stmt_node = URIRef(stmt.value) else: stmt_node = BNode() # a GUID is generated to_node = URIRef(to_uri.value) from_node = URIRef(from_uri.value) pred_node = URIRef(pred.value) linkset_node = URIRef(linksetInstance.value) g.add((stmt_node, RDF.object, to_node)) g.add((stmt_node, RDF.subject, from_node)) g.add((stmt_node, RDF.predicate, pred_node)) g.add((stmt_node, partOf, linkset_node)) g.add((linkset_node, RDF.type, URIRef("http://linked.data.gov.au/def/loci#Linkset"))) stmt_value_node = None if (stmt_value != None and stmt_value.type == 'literal'): stmt_value_node = Literal(stmt_value.value, datatype=stmt_value.datatype) if (stmt_value != None and stmt_value.type == 'uri'): stmt_value_node = URIRef(stmt_value.value) if (stmt_value_node != None and stmt_pred != None): g.add((stmt_node, URIRef(stmt_pred.value), stmt_value_node)) return g
g.add( (place_uri, RDF.type, schema.Place)) #place_sameas(place) same_as = place.get('sameAs').split() i = 0 while i < len(same_as): same_as_uri = URIRef(same_as[i]) g.add( (place_uri, OWL.sameAs, same_as_uri)) i += 1 #placename(place) placename = place.find('./tei:placeName', tei) label = placename.text label_lang = placename.get('{http://www.w3.org/XML/1998/namespace}lang') if label_lang is not None: g.add( (place_uri, RDFS.label, Literal(label, lang=label_lang))) else: g.add( (place_uri, RDFS.label, Literal(label))) # value value = etree.tostring(place, pretty_print=True, method="xml") g.add( (place_uri, RDF.value, Literal(value, datatype=RDF.XMLLiteral)) ) #referenced_place(place_id) ref = './/tei:placeName[@ref="#' + place_id + '"]' for referenced_place in root.findall(ref, tei): parent = referenced_place.getparent() parent_id = parent.get('{http://www.w3.org/XML/1998/namespace}id') parent_uri = URIRef(base_uri + '/text/' + parent_id) g.add( (place_uri, DCTERMS.isReferencedBy, parent_uri)) g.add( (parent_uri, RDF.type, frbroo.F23_Expression_Fragment))
def version(self, value): if not isinstance(value, Literal): value = Literal(value) self.graph.set((self.asNode(), RDF_NAMESPACES.DTS.version, value))
def define_classes(definitions, parent, pun_classes=False): """ Generates triples for the hierarchy given by 'definitions', rooted at the class given by 'parent' - class hierarchy ('subclasses') - tag mappings - substance + quantity modeling If pun_classes is True, then create punned instances of the classes """ for classname, defn in definitions.items(): classname = BRICK[classname] # class is a owl:Class G.add((classname, A, OWL.Class)) # subclass of parent G.add((classname, RDFS.subClassOf, parent)) # add label class_label = classname.split("#")[-1].replace("_", " ") G.add((classname, RDFS.label, Literal(class_label))) if pun_classes: G.add((classname, A, classname)) # define mapping to tags if it exists # "tags" property is a list of URIs naming Tags taglist = defn.get("tags", []) assert isinstance(taglist, list) if len(taglist) == 0: logging.warning(f"Property 'tags' not defined for {classname}") add_tags(classname, taglist) # define mapping to substances + quantities if it exists # "substances" property is a list of (predicate, object) pairs substancedef = defn.get("substances", []) assert isinstance(substancedef, list) add_restriction(classname, substancedef) # define class structure # this is a nested dictionary subclassdef = defn.get("subclasses", {}) assert isinstance(subclassdef, dict) define_classes(subclassdef, classname, pun_classes=pun_classes) # handle 'parents' subclasses (links outside of tree-based hierarchy) parents = defn.get("parents", []) assert isinstance(parents, list) for _parent in parents: G.add((classname, RDFS.subClassOf, _parent)) # all other key-value pairs in the definition are # property-object pairs expected_properties = ["parents", "tags", "substances", "subclasses"] other_properties = [ prop for prop in defn.keys() if prop not in expected_properties ] for propname in other_properties: propval = defn[propname] if isinstance(propval, list): for pv in propval: G.add((classname, propname, pv)) else: G.add((classname, propname, propval))
import rdflib from rdflib import Graph, Literal, BNode, Namespace, RDF, URIRef from rdflib.namespace import DC from rdflib.namespace import XSD FAIRURL = "" EMPTY_STRING = "unk" # what to do with empty strings? FAIR = Namespace(FAIRURL) g = Graph() # Set up the Principles (columns of the curated spreadsheet) # Findable g.add((Literal("FAIR.findablePrinciple"), RDF.type, Literal("FAIR.principle"))) ## requirements g.add((Literal("FAIR.F1Requirement"), RDF.type, Literal("FAIR.requirement"))) g.add((Literal("FAIR.F2Requirement"), RDF.type, Literal("FAIR.requirement"))) g.add((Literal("FAIR.F3Requirement"), RDF.type, Literal("FAIR.requirement"))) g.add((Literal("FAIR.F4Requirement"), RDF.type, Literal("FAIR.requirement"))) ## set forward and backwards pointers g.add((Literal("FAIR.findablePrinciple"), DC.hasPart, Literal("FAIR.F1Requirement"))) g.add((Literal("FAIR.F1Requirement"), DC.isPartOf, Literal("FAIR.findablePrinciple"))) g.add((Literal("FAIR.findablePrinciple"), DC.hasPart, Literal("FAIR.F2Requirement"))) g.add((Literal("FAIR.F2Requirement"), DC.isPartOf, Literal("FAIR.findablePrinciple"))) g.add((Literal("FAIR.findablePrinciple"), DC.hasPart,
def versionEndIncluding(self, cm): return [(XSD.maxInclusive, Literal(i)) for i in all("$.versionEndIncluding", cm)]
def add_tags(klass, definition): """ Adds the definition of tags to the given class. This method adds two groups of triples. The first group of triples uses the BRICK.hasAssociatedTag property to associate the tags with this class. While this is duplicate information, it is much easier to query for. The second group of triples uses SHACL-AF rules to generate the appropriate Brick class from a set of tags. Strict equality of the tag set is required: if two classes which are *not* related by a subclass relationship exist, but one class's tags are a strict subset of the other, then under this regime the subsumed class will *not* be inferred for instances of the class with more tags. Args: klass: the URI of the Brick class to be modeled definition: a list of BRICK.Tag instances (e.g. TAG.Air) """ if len(definition) == 0: return for tag in definition: G.add((klass, BRICK.hasAssociatedTag, tag)) G.add((tag, A, BRICK.Tag)) # make sure the tag is declared as such G.add( (tag, RDFS.label, Literal(tag.split("#")[-1])) ) # make sure the tag is declared as such all_restrictions = [] equivalent_class = BNode() list_name = BNode() # add SHACL shape sc = BSH[klass.split("#")[-1] + "_TagShape"] shaclGraph.add((sc, A, SH.NodeShape)) # G.add((sc, SH.targetSubjectsOf, BRICK.hasTag)) rule = BNode(str(klass) + "TagInferenceRule") shaclGraph.add((sc, SH.rule, rule)) # define rule shaclGraph.add((rule, A, SH.TripleRule)) shaclGraph.add((rule, SH.subject, SH.this)) shaclGraph.add((rule, SH.predicate, RDF.type)) shaclGraph.add((rule, SH.object, klass)) # conditions for tag in definition: if tag not in has_tag_restriction_class: restriction = BNode(f"has_{tag.split('#')[-1]}") G.add((restriction, A, OWL.Restriction)) G.add((restriction, OWL.onProperty, BRICK.hasTag)) G.add((restriction, OWL.hasValue, tag)) has_tag_restriction_class[tag] = restriction all_restrictions.append(has_tag_restriction_class[tag]) if tag not in shacl_tag_property_shapes: cond = BNode(f"has_{tag.split('#')[-1]}_condition") prop = BNode(f"has_{tag.split('#')[-1]}_tag") tagshape = BNode() shaclGraph.add((rule, SH.condition, cond)) shaclGraph.add((cond, SH.property, prop)) shaclGraph.add((prop, SH.path, BRICK.hasTag)) shaclGraph.add((prop, SH.qualifiedValueShape, tagshape)) shaclGraph.add((tagshape, SH.hasValue, tag)) shaclGraph.add( (prop, SH.qualifiedMinCount, Literal(1, datatype=XSD.integer)) ) # probably don't need the Max count here; addition of duplicate tags should be idempotent # shaclGraph.add((prop, SH.qualifiedMaxCount, Literal(1))) shacl_tag_property_shapes[tag] = cond shaclGraph.add((rule, SH.condition, shacl_tag_property_shapes[tag])) num_tags = len(definition) if len(definition) not in has_exactly_n_tags_shapes: # tag count condition cond = BNode(f"has_exactly_{num_tags}_tags_condition") prop = BNode(f"has_exactly_{num_tags}_tags") shaclGraph.add((cond, SH.property, prop)) shaclGraph.add((prop, SH.path, BRICK.hasTag)) shaclGraph.add((prop, SH.minCount, Literal(len(definition)))) shaclGraph.add((prop, SH.maxCount, Literal(len(definition)))) has_exactly_n_tags_shapes[len(definition)] = cond shaclGraph.add((rule, SH.condition, has_exactly_n_tags_shapes[len(definition)])) # ensure that the rule applies to at least one of the base tags that should be on # most Brick classes # base_tags = [TAG.Equipment, TAG.Point, TAG.Location, TAG.System, TAG.Solid, TAG.Fluid] # target_class_tag = [t for t in base_tags if t in definition] # assert len(target_class_tag) > 0, klass # shaclGraph.add((sc, SH.targetClass, has_tag_restriction_class[target_class_tag[0]])) shaclGraph.add((sc, SH.targetSubjectsOf, BRICK.hasTag)) # if we've already mapped this class, don't map it again if klass in intersection_classes: return if len(all_restrictions) == 1: G.add((klass, RDFS.subClassOf, all_restrictions[0])) if len(all_restrictions) > 1: G.add((klass, RDFS.subClassOf, equivalent_class)) G.add((equivalent_class, OWL.intersectionOf, list_name)) Collection(G, list_name, all_restrictions) intersection_classes[klass] = tuple(sorted(definition))
def test_by_attr_and_find_by(self): context = QueryContext(testgraph, 'en', queries=[Item]) found = context.Item.find_by(name=Literal(u'Item X')) assert list(found)[0].uri == itemX
def versionStartIncluding(self, cm): return [(XSD.minInclusive, Literal(i)) for i in all("$.versionStartIncluding", cm)]
def assert_item_facts(item): assert item.uri == itemX, \ "Unexpected uri: %r" % item.uri assert item.title == Literal(u'Example Item', 'en'), \ "Unexpected title: %r" % item.title
if (subjectURI, RDFS.subClassOf, objectURI) in graph: print subject, "already added" else: graph.add((subjectURI, RDF.type, OWL.Class)) graph.add((subjectURI, RDFS.subClassOf, objectURI)) graph.add((subjectURI, RDFS.label, Literal(subject))) graph.add((subjectURI, RDFS.comment, Literal(subject))) DBfN = Namespace( "http://maven.renci.org/ontologies/databridgeforneuroscience/") graph = Graph() #Clinical = URIRef("Clinical") graph.add((DBfN.Clinical, RDF.type, OWL.Class)) graph.add((DBfN.Clinical, RDFS.subClassOf, OWL.Thing)) graph.add((DBfN.Clinical, RDFS.label, Literal("Clinical"))) graph.add((DBfN.Clinical, RDFS.comment, Literal("Highest level of ontology"))) workbook = sys.argv[1] outFile = sys.argv[2] lastRow = int(sys.argv[3]) sheet = sys.argv[4] wb = open_workbook(workbook) for s in wb.sheets(): # if (s.name == 'SCZ Clin Model Groups'): if (s.name == sheet): for row in range(4, lastRow): if (s.cell(row, 1).value != ""): thisValue = (s.cell(row, 0).value) theseValues = thisValue.split('>') addANode(graph, theseValues[1], "Clinical")
from rdflib import Dataset from rdflib import URIRef, Literal r1 = Recipe() r1.title = "Foo" i1_1 = Ingredient() i1_1.name = "Foo_1" t1 = Tag("Baz") r1.ingredients.append(i1_1) r1.tags.append(t1) r1.add_prov("wasDerivedFrom", URIRef("http://recipes.com/r/Foo")) r1.add_pub_info("wasAttributedTo", Literal("Jeff the Data Guy")) summed = Dataset() for quad in r1.__publish__(): summed.add(quad) summed.namespace_manager.bind("np", data.NP, True) summed.namespace_manager.bind("recipe-kb", data.BASE, True) summed.namespace_manager.bind("prov", data.PROV, True) print(summed.serialize(format="trig").decode("utf-8")) u1 = data.USDAEntry(12345, "CHEESE,SERIOUSLY SPICY", []) l1 = data.Linkage(data.IngredientName(i1_1.name), u1)
g.add((shape_uri, URIRef("http://www.w3.org/ns/shacl#targetClass"), class_uri)) for j in range(2, r_count): p1 = df.iloc[j, 0] p1 = p1.replace("sh:", "http://www.w3.org/ns/shacl#") p1 = URIRef(p1) blank = BNode() g.add((shape_uri, p1, blank)) g.add((blank, URIRef("http://www.w3.org/ns/shacl#order"), Literal(j - 1))) for i in map: value = df.iloc[j, i] if not pd.isnull(value) and value != 0: obj = map[i] if pd.isnull(obj["uri"]): continue p = URIRef(obj["uri"]) if str(value).startswith("http") and obj[ "uri"] != "http://www.w3.org/2004/02/skos/core#example":
def allDateTime(p, q, o): return [(p, Literal(dt, datatype=XSD.dateTime)) for dt in all(q, o)]
def crosswalk(self, orcid_profile, person_uri, graph): if orcid_profile["orcid-profile"]["orcid-activities"] \ and "funding-list" in orcid_profile["orcid-profile"]["orcid-activities"] \ and orcid_profile["orcid-profile"]["orcid-activities"]["funding-list"] \ and "funding" in orcid_profile["orcid-profile"]["orcid-activities"]["funding-list"]: #Funding fundings = orcid_profile["orcid-profile"]["orcid-activities"]["funding-list"]["funding"] for funding in fundings: if funding["funding-type"] == "GRANT": title = funding["funding-title"]["title"]["value"] grant_uri = self.identifier_strategy.to_uri(VIVO.Grant, {"title": title}) #Type graph.add((grant_uri, RDF.type, VIVO.Grant)) #Person graph.add((grant_uri, VIVO.relates, person_uri)) #Title graph.add((grant_uri, RDFS.label, Literal(title))) #Role role_uri = self.identifier_strategy.to_uri(VIVO.PrincipalInvestigatorRole, {"grant_uri": grant_uri}) graph.add((role_uri, RDF.type, VIVO.PrincipalInvestigatorRole)) #Inheres in graph.add((role_uri, OBO.RO_0000052, person_uri)) graph.add((role_uri, VIVO.relatedBy, grant_uri)) #Date interval (start_year, start_month, start_day) = FundingCrosswalk._get_date_parts("start-date", funding) (end_year, end_month, end_day) = FundingCrosswalk._get_date_parts("end-date", funding) add_date_interval(grant_uri, graph, self.identifier_strategy, add_date(start_year, graph, self.identifier_strategy, start_month, start_day), add_date(end_year, graph, self.identifier_strategy, end_month, end_day)) #Award amount funding_amount = funding.get("amount") if funding_amount is not None: value = funding_amount.get("value") if value is not None: award_amount = "${:,}".format(int(value)) graph.add((grant_uri, VIVO.totalAwardAmount, Literal(award_amount))) #Awarded by if "organization" in funding: organization_name = funding["organization"]["name"] organization_uri = self.identifier_strategy.to_uri(FOAF.Organization, {"name": organization_name}) graph.add((grant_uri, VIVO.assignedBy, organization_uri)) if self.create_strategy.should_create(FOAF.Organization, organization_uri): graph.add((organization_uri, RDF.type, FOAF.Organization)) graph.add((organization_uri, RDFS.label, Literal(organization_name))) #Identifiers if "funding-external-identifiers" in funding and funding.get("funding-external-identifiers"): for external_identifier in funding["funding-external-identifiers"]["funding-external-identifier"]: if "funding-external-identifier-value" in external_identifier: graph.add((grant_uri, VIVO.sponsorAwardId, Literal(external_identifier["funding-external-identifier-value"]))) identifier_url = (external_identifier.get("funding-external-identifier-url", {}) or {}).get("value") if identifier_url: vcard_uri = self.identifier_strategy.to_uri(VCARD.Kind, {"url": identifier_url}) graph.add((vcard_uri, RDF.type, VCARD.Kind)) #Has contact info graph.add((grant_uri, OBO.ARG_2000028, vcard_uri)) #Url vcard vcard_url_uri = self.identifier_strategy.to_uri(VCARD.URL, {"vcard_uri": vcard_uri}) graph.add((vcard_url_uri, RDF.type, VCARD.URL)) graph.add((vcard_uri, VCARD.hasURL, vcard_url_uri)) graph.add((vcard_url_uri, VCARD.url, Literal(identifier_url, datatype=XSD.anyURI)))
def allFloat(p, q, o): return [(p, Literal(dt, datatype=XSD.decimal)) for dt in all(q, o)]
def define_concept_hierarchy(definitions, typeclasses, broader=None, related=None): """ Generates triples to define the SKOS hierarchy of concepts given by 'definitions', which are all instances of the class given by 'typeclass'. 'broader', if provided, is the skos:broader concept 'related', if provided, is the skos:related concept Currently this is used for Brick Quantities """ for concept, defn in definitions.items(): concept = BRICK[concept] for typeclass in typeclasses: G.add((concept, A, typeclass)) # mark broader concept if one exists if broader is not None: G.add((concept, SKOS.broader, broader)) # mark related concept if one exists if related is not None: G.add((concept, SKOS.related, related)) # add label class_label = concept.split("#")[-1].replace("_", " ") G.add((concept, RDFS.label, Literal(class_label))) # define mapping to substances + quantities if it exists # "substances" property is a list of (predicate, object) pairs substancedef = defn.get("substances", []) assert isinstance(substancedef, list) add_restriction(concept, substancedef) # define concept hierarchy # this is a nested dictionary narrower_defs = defn.get(SKOS.narrower, {}) if narrower_defs is not None and isinstance(narrower_defs, dict): define_concept_hierarchy( narrower_defs, [BRICK.Quantity, QUDT.QuantityKind], broader=concept ) related_defs = defn.get(SKOS.related, {}) if related_defs is not None and isinstance(related_defs, dict): define_concept_hierarchy( related_defs, [BRICK.Quantity, QUDT.QuantityKind], related=concept ) # handle 'parents' subconcepts (links outside of tree-based hierarchy) parents = defn.get("parents", []) assert isinstance(parents, list) for _parent in parents: G.add((concept, SKOS.broader, _parent)) # all other key-value pairs in the definition are # property-object pairs expected_properties = ["parents", "tags", "substances"] other_properties = [ prop for prop in defn.keys() if prop not in expected_properties ] for propname in other_properties: propval = defn[propname] if isinstance(propval, list): for pv in propval: G.add((concept, propname, pv)) elif not isinstance(propval, dict): G.add((concept, propname, propval))
def allString(p, q, o): return [(p, Literal(s)) for s in all(q, o)]
def add_definitions(): """ Adds definitions for Brick subclasses through SKOS.definitions. This parses the definitions from ./bricksrc/definitions.csv and adds it to the graph. If available, adds the source information of through RDFS.seeAlso. """ with open("./bricksrc/definitions.csv", encoding="utf-8") as dictionary_file: dictionary = csv.reader(dictionary_file) # skip the header next(dictionary) # add definitions, citations to the graph for definition in dictionary: term = URIRef(definition[0]) if len(definition[1]): G.add((term, SKOS.definition, Literal(definition[1], lang="en"))) if len(definition[2]): G.add((term, RDFS.seeAlso, URIRef(definition[2]))) qstr = """ select ?param where { ?param rdfs:subClassOf* brick:Limit. } """ limit_def_template = "A parameter that places {direction} bound on the range of permitted values of a {setpoint}." params = [row["param"] for row in G.query(qstr)] for param in params: words = param.split("#")[-1].split("_") prefix = words[0] # define "direction" component of Limit definition if prefix == "Min": direction = "a lower" elif prefix == "Max": direction = "an upper" else: prefix = None direction = "a lower or upper" # define the "setpoint" component of a Limit definition if param.split("#")[-1] in ["Max_Limit", "Min_Limit", "Limit"]: setpoint = "Setpoint" else: if prefix: setpoint = "_".join(words[1:-1]) else: setpoint = "_".join(words[:-1]) if setpoint.split("_")[-1] != "Setpoint": # While Limits are a boundary of a Setpoint, the associated # Setpoint names are not explicit in class's names. Thus needs # to be explicily added for the definition text. setpoint = setpoint + "_Setpoint" logging.info(f"Inferred setpoint: {setpoint}") limit_def = limit_def_template.format(direction=direction, setpoint=setpoint) G.add((param, SKOS.definition, Literal(limit_def, lang="en"))) class_exists = G.query( f"""select ?class where {{ BIND(brick:{setpoint} as ?class) ?class rdfs:subClassOf* brick:Class. }} """ ).bindings if not class_exists: logging.warning(f"WARNING: {setpoint} does not exist in Brick for {param}.")
def allLangString(p, q, o): return [(p, Literal(all("$.value", s)[0], lang=all("$.lang", s)[0])) for s in all(q, o)]
hostBirthPlace = hostname + "birthplace" hostAffliattionName = hostname + "affliation_name" counter = 0 for (id, object_id, first_name, last_name, birthplace, affiliation_name) in cursor: # print "{}, {} decription is {}".format(entity_type, name, description) # subject = rdflib.term.URIRef(hostname+ str(first_name.encode('ascii','ignore').replace("20%"," ")+"20%"+last_name.encode('ascii','ignore').replace("20%"," "))) subject = rdflib.term.URIRef(hostname + urllib.quote( first_name.encode('ascii', 'ignore') + " " + last_name.encode('ascii', 'ignore'))) # predicateEntityType = rdflib.term.URIRef("http://www.example.org/cb_objects/entity_type") predicateObjectId = rdflib.term.URIRef(hostObjectId) g.add((subject, predicateObjectId, Literal(object_id))) # predicateEntityId = rdflib.term.URIRef("http://www.example.org/cb_objects/entity_id") predicateFirstName = rdflib.term.URIRef(hostName) g.add((subject, predicateFirstName, Literal(first_name + " " + last_name))) # predicateEntityname = rdflib.term.URIRef("http://www.example.org/cb_objects/entity_id") # predicateLastName = rdflib.term.URIRef(hostLastName) # g.add((subject , predicateLastName, Literal(last_name))) # predicateEntityCategoryCode = rdflib.term.URIRef("http://www.example.org/cb_objects/category_code") predicateBirthPlace = rdflib.term.URIRef(hostBirthPlace) g.add((subject, predicateBirthPlace, Literal(birthplace))) # predicateEntityTwitterUsername = rdflib.term.URIRef("http://www.example.org/cb_objects/twitter_username") predicateAffiliationName = rdflib.term.URIRef(hostAffliattionName) g.add((subject, predicateAffiliationName, Literal(affiliation_name))) counter += 1
def allURL(p, q, o): return [(p, Literal(u, datatype=XSD.anyURI)) for u in all(q, o)]
"itilrole", "http://eden.dei.uc.pt/~jcardoso/rdf/itil/roles.ttl#") g.namespace_manager.bind( "itilproc", "http://eden.dei.uc.pt/~jcardoso/rdf/itil/processes.ttl#") g.namespace_manager.bind( "itilglos", "http://eden.dei.uc.pt/~jcardoso/rdf/itil/glossary.ttl#") g.namespace_manager.bind("foaf", FOAF) g.namespace_manager.bind("skos", SKOS) g.namespace_manager.bind("dc", DC) itilglos = Namespace("http://eden.dei.uc.pt/~jcardoso/rdf/itil/glossary.ttl#") itilproc = Namespace("http://eden.dei.uc.pt/~jcardoso/rdf/itil/processes.ttl#") g.add((URIRef("http://eden.dei.uc.pt/~jcardoso/rdf/itil/glossary.ttl"), RDF.type, OWL.ontology)) g.add((itilglos["jcardoso"], RDF.type, FOAF.Person)) g.add((itilglos["jcardoso"], FOAF.name, Literal("Jorge Cardoso"))) g.add((itilglos["jcardoso"], FOAF.homepage, URIRef("http://eden.dei.uc.pt/~jcardoso/"))) g.add((itilglos[""], DC.creator, itilglos["jcardoso"])) g.add((itilglos["Service_Strategy"], RDF.type, SKOS.concept)) g.add((itilglos["Service_Design"], RDF.type, SKOS.concept)) g.add((itilglos["Service_Transition"], RDF.type, SKOS.concept)) g.add((itilglos["Service_Operation"], RDF.type, SKOS.concept)) g.add((itilglos["Continual_Service_Improvement"], RDF.type, SKOS.concept)) print "" print "===== Parsing ITIL Glossary \"ITIL.org - Glossary Complete.html\" ======" i = 0 for row in soup.findAll('tr'):
def creators(self): """ Return a list of creator nodes. Note: Does not add anything to the graph. """ return map(lambda c: Literal(c.to_value()), self.document.creation_info.creators)
path = "data/authors/list/{}.html".format(i) soup = BeautifulSoup(open(path), "lxml") divs = soup.find_all(class_="name") for div in divs: suffix = div.find(class_="name2").text.replace(" ", "") prefix = "chname" subject = URIRef("https://shibusawa-dlab.github.io/lab1/api/" + prefix + "/" + suffix) print(subject) stmt = (subject, RDFS.label, Literal(suffix)) all.add(stmt) stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Agent")) all.add(stmt) stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Person")) all.add(stmt) name1s = div.find_all(class_="name1") for name1 in name1s: stmt = (subject, URIRef("http://schema.org/name"), Literal(name1.text.replace("・", " "), lang="ja-kana")) all.add(stmt)
def processObservation(self, rules, table, observation, description): """ Process a specific source observation and add to the graph the harmonized version of it, using the rules from self.rules """ # Check that the observation is an int, skip it otherwise (a bit hacky now) #http://example.org/resource/BRT_1920_01_S1_marked/populationSize pop_size = URIRef("http://example.org/resource/"+table+"_marked/populationSize") has_pop_size = False pop_size_val = Literal("") for (p, o) in description: if p == pop_size: has_pop_size = True pop_size_val = o if not has_pop_size or type(pop_size_val.toPython()) is not long: return # Get all the mapped dimensions harmonized_po = [] for (p,o) in description: if o in rules: # These are rules for column headers rules_set = rules[o] for r in rules_set: if r['type'] == 'IgnoreObservation': # We need to ignore the observation, it's a sum of something return elif r['type'] == 'AddDimensionValue': # Got a p,o pair to bind to the thing harmonized_po.append(r['dimval']) elif r['type'] == 'SetDimension': # should not happen, maybe raise an exception pass else: # Unknown rule ?! pass elif p in rules: # if there is a total, return if type(o.toPython) == unicode: if 'totaal' in self._clean_string(o.toPython()): return # These are rules that applies to row properties rules_set = rules[p] for r in rules_set: if r['type'] == 'SetDimension': raw_value = o dim = r['dimension'] if codes.no_codes_for(dim): # If there is no code just put the raw value harmonized_po.append((dim, raw_value)) elif type(raw_value.toPython()) == unicode: # try to map the value to a code cleaned_raw_value = self._clean_string(raw_value.toPython()) c = codes.get_code(dim, cleaned_raw_value) if c != None: harmonized_po.append((dim, c)) else: # Just ignore this dimension pass elif r['type'] == 'IgnoreObservation' or r['type'] == 'AddDimensionValue': # should not happen, maybe raise an exception pass else: # Unknown rule ?! pass else: # No rule that apply to either the p or the o # no worries, that can happen pass if len(harmonized_po) > 0: # Add the new observation to the graph resource = URIRef(self.namespaces['cedardata'] + self.cube_name + "/" + str(self.nb_obs)) self.nb_obs = self.nb_obs + 1 self.graph.add((resource, RDF.type, self.namespaces['qb']['Observation'])) self.graph.add((resource, self.namespaces['cedar']['sourceObservation'], observation)) self.graph.add((resource, self.namespaces['cedar']['populationSize'], pop_size_val)) self.graph.add((resource, self.namespaces['qb']['dataSet'], self.dataset_resource)) for (p,o) in harmonized_po: self.graph.add((resource, p, o))
from rdflib import Namespace, URIRef, Graph, Literal from rdflib.namespace import RDF, FOAF import rdflib # https://www.youtube.com/watch?v=5DCS9LE-8rE g = Graph() subject = rdflib.term.URIRef("http://www.example.org/cb_objects/c:1") predicate = rdflib.term.URIRef("http://www.example.org/cb_objects/entity_type") g.add((subject, predicate, Literal("Company"))) predicate = rdflib.term.URIRef("http://www.example.org/cb_objects/name") g.add((subject, predicate, Literal('WetName'))) subject = rdflib.term.URIRef("http://www.example.org/cb_objects/c:2") predicate = rdflib.term.URIRef("http://www.example.org/cb_objects/entity_type") g.add((subject, predicate, Literal('Company'))) s = g.serialize(format='pretty-xml') outfile = open("test.xml", "w") outfile.write(s)
def fileTimeStatements(self, uri, filename): dt = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) dt = dt.replace(tzinfo=tzlocal()) dateLit = Literal(dt.isoformat(), datatype=XS.dateTime) return [(uri, PHO.fileTime, dateLit), (uri, DC.date, Literal(dateLit.split("T")[0], datatype=XS.date))]
CyberSecurityKG.add((Weakness, attributed_to, ApplicablePlatform)) CyberSecurityKG.add((IntrusionSet, attributed_to, Campaign)) CyberSecurityKG.add((Malware, downloads, Malware)) CyberSecurityKG.add((Malware, downloads, Tool)) # CyberSecurityKG.add((CyberAction,installs,Malware)) # CyberSecurityKG.add((CyberAction,installs,Malware)) CyberSecurityKG.add((CyberAction, delivers, Malware)) CyberSecurityKG.add((Malware, execute, CyberAction)) CyberSecurityKG.add((IntrusionSet, execute, CyberAction)) CyberSecurityKG.add((CyberAction, is_Instance_Of, AttackPattern)) CyberSecurityKG.add((CyberAction, intervalBefore, CyberAction)) CyberSecurityKG.add((AttackPattern, name, Literal(""))) CyberSecurityKG.add((IntrusionSet, name, Literal(""))) CyberSecurityKG.add((Malware, name, Literal(""))) CyberSecurityKG.add((Tool, name, Literal(""))) CyberSecurityKG.add((Weakness, name, Literal(""))) CyberSecurityKG.add((Vulnerability, name, Literal(""))) CyberSecurityKG.add((Indicator, name, Literal(""))) CyberSecurityKG.add((CourseOfAction, name, Literal(""))) CyberSecurityKG.add((IntrusionSet, aliases, Literal(""))) CyberSecurityKG.add((Malware, aliases, Literal(""))) CyberSecurityKG.add((Tool, aliases, Literal(""))) CyberSecurityKG.add((Weakness, aliases, Literal(""))) CyberSecurityKG.add((AttackPattern, description, Literal(""))) CyberSecurityKG.add((IntrusionSet, description, Literal("")))
def main(): outfile = sys.argv[1] if not os.path.exists('CONTRIBUTING.md') and os.path.exists('ecmascript-testcases'): sys.stderr.write('Invalid CWD, must be in Duktape root with dist/ built') sys.exit(1) os.chdir('dist') if not os.path.exists('Makefile.cmdline'): sys.stderr.write('Invalid CWD, must be in Duktape root with dist/ built') sys.exit(1) duktape_version = getDuktapeVersion() duktape_pkgname = 'duktape-' + duktape_version + '.tar.xz' now = datetime.datetime.utcnow() now = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second) creation_date = Literal(now.isoformat() + 'Z', datatype=XSD.dateTime) duktape_org = Literal('Organization: duktape.org') mit_license = URIRef('http://spdx.org/licenses/MIT') duktape_copyright = Literal('Copyright 2013-2014 Duktape authors (see AUTHORS.rst in the Duktape distributable)') g = rdflib.Graph() crea_node = BNode() g.add((crea_node, RDF.type, SPDX.CreationInfo)) g.add((crea_node, RDFS.comment, Literal(''))) g.add((crea_node, SPDX.creator, duktape_org)) g.add((crea_node, SPDX.created, creation_date)) g.add((crea_node, SPDX.licenseListVersion, Literal('1.20'))) # http://spdx.org/licenses/ # 'name' should not include a version number (see best practices) pkg_node = BNode() g.add((pkg_node, RDF.type, SPDX.Package)) g.add((pkg_node, SPDX.name, Literal('Duktape'))) g.add((pkg_node, SPDX.versionInfo, Literal(duktape_version))) g.add((pkg_node, SPDX.packageFileName, Literal(duktape_pkgname))) g.add((pkg_node, SPDX.supplier, duktape_org)) g.add((pkg_node, SPDX.originator, duktape_org)) g.add((pkg_node, SPDX.downloadLocation, Literal('http://duktape.org/' + duktape_pkgname, datatype=XSD.anyURI))) g.add((pkg_node, SPDX.homePage, Literal('http://duktape.org/', datatype=XSD.anyURI))) verify_node = computePackageVerification(g, '.', [ './license.spdx' ]) g.add((pkg_node, SPDX.packageVerificationCode, verify_node)) # SPDX.checksum: omitted because license is inside the package g.add((pkg_node, SPDX.sourceInfo, Literal('Official duktape.org release built from GitHub repo https://github.com/svaarala/duktape.'))) # NOTE: MIT license alone is sufficient fornow, because Duktape, MurmurHash2 and # CommonJS (though probably not even relevant for licensing) are all MIT. g.add((pkg_node, SPDX.licenseConcluded, mit_license)) g.add((pkg_node, SPDX.licenseInfoFromFiles, mit_license)) g.add((pkg_node, SPDX.licenseDeclared, mit_license)) g.add((pkg_node, SPDX.licenseComments, Literal('Duktape is copyrighted by its authors and licensed under the MIT license. MurmurHash2 is used internally, it is also under the MIT license. Duktape module loader is based on the CommonJS module loading specification (without sharing any code), CommonJS is under the MIT license.'))) g.add((pkg_node, SPDX.copyrightText, duktape_copyright)) g.add((pkg_node, SPDX.summary, Literal('Duktape Ecmascript interpreter'))) g.add((pkg_node, SPDX.description, Literal('Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint'))) # hasFile properties added separately below #reviewed_node = BNode() #g.add((reviewed_node, RDF.type, SPDX.Review)) #g.add((reviewed_node, SPDX.reviewer, XXX)) #g.add((reviewed_node, SPDX.reviewDate, XXX)) #g.add((reviewed_node, RDFS.comment, '')) spdx_doc = BNode() g.add((spdx_doc, RDF.type, SPDX.SpdxDocument)) g.add((spdx_doc, SPDX.specVersion, Literal('SPDX-1.2'))) g.add((spdx_doc, SPDX.dataLicense, URIRef('http://spdx.org/licenses/CC0-1.0'))) g.add((spdx_doc, RDFS.comment, Literal('SPDX license for Duktape ' + duktape_version))) g.add((spdx_doc, SPDX.creationInfo, crea_node)) g.add((spdx_doc, SPDX.describesPackage, pkg_node)) # SPDX.hasExtractedLicensingInfo # SPDX.reviewed # SPDX.referencesFile: added below for dirpath, dirnames, filenames in os.walk('.'): for fn in filenames: full_fn = os.path.join(dirpath, fn) #print('# file: ' + full_fn) file_node = BNode() g.add((file_node, RDF.type, SPDX.File)) g.add((file_node, SPDX.fileName, Literal(full_fn))) g.add((file_node, SPDX.fileType, fileType(full_fn))) g.add((file_node, SPDX.checksum, checksumFile(g, full_fn))) # Here we assume that LICENSE.txt provides the actual "in file" # licensing information, and everything else is implicitly under # MIT license. g.add((file_node, SPDX.licenseConcluded, mit_license)) if full_fn == './LICENSE.txt': g.add((file_node, SPDX.licenseInfoInFile, mit_license)) else: g.add((file_node, SPDX.licenseInfoInFile, URIRef(SPDX.none))) # SPDX.licenseComments g.add((file_node, SPDX.copyrightText, duktape_copyright)) # SPDX.noticeText # SPDX.artifactOf # SPDX.fileDependency # SPDX.fileContributor # XXX: should referencesFile include all files? g.add((spdx_doc, SPDX.referencesFile, file_node)) g.add((pkg_node, SPDX.hasFile, file_node)) # Serialize into RDF/XML directly. We could also serialize into # N-Triples and use external tools (like 'rapper') to get cleaner, # abbreviated output. #print('# Duktape SPDX license file (autogenerated)') #print(g.serialize(format='turtle')) #print(g.serialize(format='nt')) f = open(outfile, 'wb') f.write(g.serialize(format='rdf/xml')) f.close()
g.bind('owl', OWL) g.bind('foaf', FOAF) g.bind('xsd', XSD) g.bind('rdfs', RDFS) g.bind('doap', DOAP) g.bind('dc', DC) g.bind('prov', prov) g.bind('dcat', dcat) g.bind('mexalgo', mexalgo) g.bind('mexperf', mexperf) g.bind('mexcore', mexcore) g.bind('this', this) g.add((this.pooja_task3, mexcore.Experiment, prov.Entity)) g.add((this.pooja_task3, mexcore.ApplicationContext, prov.Entity)) g.add((this.pooja_task3, DCTERMS.date, Literal('2018-07-22', datatype=XSD.date))) g.add((this.pooja_task3, FOAF.givenName, Literal('Pooja Bhatia'))) g.add((this.pooja_task3, FOAF.mbox, Literal('*****@*****.**'))) #Configuration of Model 1 g.add((this.configuration1, RDF.type, mexcore.ExperimentConfiguration)) g.add((this.configuration1, prov.used, this.model1)) g.add((this.configuration1, prov.wasStartedBy, this.pooja_task3)) #Configuration of Model 2 g.add((this.configuration2, RDF.type, mexcore.ExperimentConfiguration)) g.add((this.configuration2, prov.used, this.model2)) g.add((this.configuration2, prov.wasStartedBy, this.pooja_task3)) g.add( (this.hyerparameter_model1, mexalgo.HyperParameterCollection, prov.Entity))
# handle ontology definition define_ontology(G) # Declare root classes G.add((BRICK.Class, A, OWL.Class)) G.add((BRICK.Tag, A, OWL.Class)) roots = { "Equipment": {"tags": [TAG.Equipment]}, "Location": {"tags": [TAG.Location]}, "Point": {"tags": [TAG.Point]}, "Measurable": {}, "System": { SKOS.definition: Literal( "A System is a combination of equipment and auxiliary devices (e.g., controls, accessories, interconnecting means, and terminal elements) by which energy is transformed so it performs a specific function such as HVAC, service water heating, or lighting. (ASHRAE Dictionary)." ), "tags": [TAG.System], }, } define_classes(roots, BRICK.Class) logging.info("Defining properties") # define BRICK properties define_properties(properties) logging.info("Defining Point subclasses") # define Point subclasses define_classes(setpoint_definitions, BRICK.Point) define_classes(sensor_definitions, BRICK.Point) define_classes(alarm_definitions, BRICK.Point)
def createBikeGraph(arg, g): nspaces = readDict() schema = Namespace(nspaces.get('schema')) naptan = Namespace(nspaces.get('naptan')) owl = Namespace(nspaces.get('owl')) xsd = Namespace(nspaces.get('xsd')) rdfs = Namespace(nspaces.get('rdfs')) vcard = Namespace(nspaces.get('vcard')) locationOnt = Namespace(nspaces.get('locationOnt')) geom = Namespace(nspaces.get('geom')) geo = Namespace(nspaces.get('geo')) geosparql = Namespace(nspaces.get('geosparql')) rdf = Namespace(nspaces.get('rdf')) dcterms = Namespace(nspaces.get('dcterms')) dul = Namespace(nspaces.get('dul')) locn = Namespace(nspaces.get('locn')) dc = Namespace(nspaces.get('dc')) bikeid = arg[0].split('_')[1].encode('utf-8') bikeGUID = getUid(bikeid, naptan) bikeLat, bikeLong = float(arg[8]), float(arg[9]) bikeLats = str('{:f}'.format(bikeLat)) bikeLongs = str('{:f}'.format(bikeLong)) nTotalDocks = str(arg[7].encode('utf-8')) address = arg[2].split(',') bikeLabel = address[len(address) - 1].lstrip() + ' ' + str(bikeid) bikeGeometry = "POINT (" + str(bikeLat) + " " + str(bikeLong) + ")" bikeAddress = Literal(re.sub(r'&(?![A-Za-z]+[0-9]*;|#[0-9]+;|#x[0-9a-fA-F]+;)', r'and',arg[2])) bikeAddressSplit = Literal(bikeAddress.split(',', 1)[-1]) bikeAddressLocality = Literal(bikeAddressSplit.replace(' ', '',1)) bikeCreatedDate = arg[5] singleBike = createBikeParkID(bikeGUID) singleAddress = createAddress(bikeGUID) singleGeometry = createGeometry(bikeGUID) bikePublisher = URIRef('https://api.tfl.gov.uk/#BikePoint') bikeBusinessType = URIRef('http://data.linkedevents.org/kos/3cixty/bikestation') g.add((singleBike, rdf.type, dul.Place)) g.add((singleBike, rdf.type, locationOnt.bikePark)) g.add((singleBike, dcterms.identifier, Literal(bikeLabel))) g.add((singleBike, dcterms.description, Literal("London TFL Bike hire docks"))) g.add((singleBike, schema.dateCreated, Literal(bikeCreatedDate, datatype=xsd.dateTime))) g.add((singleBike, locationOnt.nTotalDocks, Literal(nTotalDocks, datatype=xsd.int))) g.add((singleBike, dc.publisher, bikePublisher)) g.add((singleBike, locationOnt.businessType, bikeBusinessType)) g.add((singleBike, geom.geometry, singleGeometry)) g.add((singleBike, schema.geo, singleGeometry)) g.add((singleBike, geosparql.hasGeometry, singleGeometry)) g.add((singleBike, locn.geometry, singleGeometry)) g.add((singleBike, vcard.hasAddress, singleAddress)) g.add((singleBike, locn.addresss, singleAddress)) g.add((singleBike, schema.location, singleAddress)) g.add((singleGeometry, rdf.type, geosparql.hasGeometry)) g.add((singleGeometry, rdf.type, geom.geometry)) g.add((singleGeometry, rdf.type, locn.geometry)) g.add((singleGeometry, rdf.type, schema.geo)) g.add((singleGeometry, geo.geometry, Literal(bikeGeometry, datatype=geosparql.wktLiteral))) g.add((singleGeometry, geo.lat, Literal(bikeLats, datatype=xsd.double))) g.add((singleGeometry, geo.long, Literal(bikeLongs, datatype=xsd.double))) g.add((singleGeometry, schema.latitude, Literal(bikeLats, datatype=xsd.double))) g.add((singleGeometry, schema.longitude, Literal(bikeLongs, datatype=xsd.double))) g.add((singleAddress, rdf.type, locn.address)) g.add((singleAddress, rdf.type, schema.location)) g.add((singleAddress, rdf.type, vcard.hasAddress)) g.add((singleAddress, dcterms.title, bikeAddress)) g.add((singleAddress, schema.streetAddress, bikeAddress)) g.add((singleAddress, locn.address, bikeAddress)) g.add((singleAddress, vcard.street_address, bikeAddress)) g.add((singleAddress, schema.addressLocality, bikeAddressLocality)) return g
def commentStatements(user, commentUri, realComment): # here you can put more processing on the comment text realComment = Literal(realComment.replace("\r", ""), datatype=realComment.datatype) # rdflib n3 can't read these back return [(commentUri, CONTENT.encoded, realComment)]