Python rdflib.RDF.type() Examples
The following are 30
code examples of rdflib.RDF.type().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rdflib.RDF
, or try the search function
.
Example #1
Source File: infer.py From Skosify with MIT License | 6 votes |
def rdfs_classes(rdf): """Perform RDFS subclass inference. Mark all resources with a subclass type with the upper class.""" # find out the subclass mappings upperclasses = {} # key: class val: set([superclass1, superclass2..]) for s, o in rdf.subject_objects(RDFS.subClassOf): upperclasses.setdefault(s, set()) for uc in rdf.transitive_objects(s, RDFS.subClassOf): if uc != s: upperclasses[s].add(uc) # set the superclass type information for subclass instances for s, ucs in upperclasses.items(): logging.debug("setting superclass types: %s -> %s", s, str(ucs)) for res in rdf.subjects(RDF.type, s): for uc in ucs: rdf.add((res, RDF.type, uc))
Example #2
Source File: rdf.py From tools-python with Apache License 2.0 | 6 votes |
def handle_pkg_lic(self, p_term, predicate, builder_func): """ Handle package lics concluded or declared. """ try: for _, _, licenses in self.graph.triples((p_term, predicate, None)): if (licenses, RDF.type, self.spdx_namespace['ConjunctiveLicenseSet']) in self.graph: lics = self.handle_conjunctive_list(licenses) builder_func(self.doc, lics) elif (licenses, RDF.type, self.spdx_namespace['DisjunctiveLicenseSet']) in self.graph: lics = self.handle_disjunctive_list(licenses) builder_func(self.doc, lics) else: try: lics = self.handle_lics(licenses) builder_func(self.doc, lics) except SPDXValueError: self.value_error('PKG_SINGLE_LICS', licenses) except CardinalityError: self.more_than_one_error('package {0}'.format(predicate))
Example #3
Source File: rdf.py From tools-python with Apache License 2.0 | 6 votes |
def create_doc(self): """ Add and return the root document node to graph. """ doc_node = URIRef('http://www.spdx.org/tools#SPDXRef-DOCUMENT') # Doc type self.graph.add((doc_node, RDF.type, self.spdx_namespace.SpdxDocument)) # Version vers_literal = Literal(str(self.document.version)) self.graph.add((doc_node, self.spdx_namespace.specVersion, vers_literal)) # Data license data_lics = URIRef(self.document.data_license.url) self.graph.add((doc_node, self.spdx_namespace.dataLicense, data_lics)) if self.document.name: doc_name = URIRef(self.document.name) self.graph.add((doc_node, self.spdx_namespace.name, doc_name)) return doc_node
Example #4
Source File: rdf.py From tools-python with Apache License 2.0 | 6 votes |
def p_file_type(self, f_term, predicate): """ Set file type. """ try: for _, _, ftype in self.graph.triples((f_term, predicate, None)): try: if ftype.endswith('binary'): ftype = 'BINARY' elif ftype.endswith('source'): ftype = 'SOURCE' elif ftype.endswith('other'): ftype = 'OTHER' elif ftype.endswith('archive'): ftype = 'ARCHIVE' self.builder.set_file_type(self.doc, ftype) except SPDXValueError: self.value_error('FILE_TYPE', ftype) except CardinalityError: self.more_than_one_error('file type')
Example #5
Source File: rdf.py From tools-python with Apache License 2.0 | 6 votes |
def package_verif_node(self, package): """ Return a node representing package verification code. """ verif_node = BNode() type_triple = (verif_node, RDF.type, self.spdx_namespace.PackageVerificationCode) self.graph.add(type_triple) value_triple = (verif_node, self.spdx_namespace.packageVerificationCodeValue, Literal(package.verif_code)) self.graph.add(value_triple) excl_file_nodes = map( lambda excl: Literal(excl), package.verif_exc_files) excl_predicate = self.spdx_namespace.packageVerificationCodeExcludedFile excl_file_triples = [(verif_node, excl_predicate, xcl_file) for xcl_file in excl_file_nodes] for trp in excl_file_triples: self.graph.add(trp) return verif_node
Example #6
Source File: rdf.py From tools-python with Apache License 2.0 | 6 votes |
def create_annotation_node(self, annotation): """ Return an annotation node. """ annotation_node = URIRef(str(annotation.spdx_id)) type_triple = (annotation_node, RDF.type, self.spdx_namespace.Annotation) self.graph.add(type_triple) annotator_node = Literal(annotation.annotator.to_value()) self.graph.add((annotation_node, self.spdx_namespace.annotator, annotator_node)) annotation_date_node = Literal(annotation.annotation_date_iso_format) annotation_triple = (annotation_node, self.spdx_namespace.annotationDate, annotation_date_node) self.graph.add(annotation_triple) if annotation.has_comment: comment_node = Literal(annotation.comment) comment_triple = (annotation_node, RDFS.comment, comment_node) self.graph.add(comment_triple) annotation_type_node = Literal(annotation.annotation_type) annotation_type_triple = (annotation_node, self.spdx_namespace.annotationType, annotation_type_node) self.graph.add(annotation_type_triple) return annotation_node
Example #7
Source File: rdf.py From tools-python with Apache License 2.0 | 6 votes |
def create_review_node(self, review): """ Return a review node. """ review_node = BNode() type_triple = (review_node, RDF.type, self.spdx_namespace.Review) self.graph.add(type_triple) reviewer_node = Literal(review.reviewer.to_value()) self.graph.add((review_node, self.spdx_namespace.reviewer, reviewer_node)) reviewed_date_node = Literal(review.review_date_iso_format) reviewed_triple = (review_node, self.spdx_namespace.reviewDate, reviewed_date_node) self.graph.add(reviewed_triple) if review.has_comment: comment_node = Literal(review.comment) comment_triple = (review_node, RDFS.comment, comment_node) self.graph.add(comment_triple) return review_node
Example #8
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def create_extracted_license(self, lic): """ Handle extracted license. Return the license node. """ licenses = list(self.graph.triples((None, self.spdx_namespace.licenseId, lic.identifier))) if len(licenses) != 0: return licenses[0][0] # return subject in first triple else: license_node = BNode() type_triple = (license_node, RDF.type, self.spdx_namespace.ExtractedLicensingInfo) self.graph.add(type_triple) ident_triple = (license_node, self.spdx_namespace.licenseId, Literal(lic.identifier)) self.graph.add(ident_triple) text_triple = (license_node, self.spdx_namespace.extractedText, Literal(lic.text)) self.graph.add(text_triple) if lic.full_name is not None: name_triple = (license_node, self.spdx_namespace.licenseName, self.to_special_value(lic.full_name)) self.graph.add(name_triple) for ref in lic.cross_ref: triple = (license_node, RDFS.seeAlso, URIRef(ref)) self.graph.add(triple) if lic.comment is not None: comment_triple = (license_node, RDFS.comment, Literal(lic.comment)) self.graph.add(comment_triple) return license_node
Example #9
Source File: infer.py From Skosify with MIT License | 5 votes |
def skos_transitive(rdf, narrower=True): """Perform transitive closure inference (S22, S24).""" for conc in rdf.subjects(RDF.type, SKOS.Concept): for bt in rdf.transitive_objects(conc, SKOS.broader): if bt == conc: continue rdf.add((conc, SKOS.broaderTransitive, bt)) if narrower: rdf.add((bt, SKOS.narrowerTransitive, conc))
Example #10
Source File: modify.py From Skosify with MIT License | 5 votes |
def replace_predicate(rdf, fromuri, touri, subjecttypes=None, inverse=False): """Replace occurrences of fromuri as predicate with touri in given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRef, all values will be inserted. If touri is a list of (URIRef, boolean) tuples, the boolean value will be used to determine whether an inverse property is created (if True) or not (if False). If a subjecttypes sequence is given, modify only those triples where the subject is one of the provided types. """ if fromuri == touri: return for s, o in rdf.subject_objects(fromuri): if subjecttypes is not None: typeok = False for t in subjecttypes: if (s, RDF.type, t) in rdf: typeok = True if not typeok: continue rdf.remove((s, fromuri, o)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for val in touri: if not isinstance(val, tuple): val = (val, False) uri, inverse = val if uri is None: continue if inverse: rdf.add((o, uri, s)) else: rdf.add((s, uri, o))
Example #11
Source File: tagsets2entities.py From plastering with MIT License | 5 votes |
def _make_instance_tuple(self, srcid, pred_point): return (URIRef(BASE + srcid), RDF.type, BRICK[pred_point])
Example #12
Source File: virtuoso_wrapper.py From plastering with MIT License | 5 votes |
def add_brick_instance(self, entity_name, tagset): entity = URIRef(BASE + entity_name) tagset = URIRef(BRICK + tagset) triples = [(entity, RDF.type, tagset)] self._add_triples(triples) return str(entity)
Example #13
Source File: rdflib_wrapper.py From plastering with MIT License | 5 votes |
def insert_point(g, name, tagset): triple = (URIRef(name), RDF.type, BRICK[tasget]) g.add(triple)
Example #14
Source File: __init__.py From plastering with MIT License | 5 votes |
def _make_instance_tuple(self, srcid, pred_point): return (URIRef(self.BASE + srcid), RDF.type, self.BRICK[pred_point])
Example #15
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def create_package_external_ref_node(self, pkg_ext_refs): """ Add and return an external package reference node to graph. """ pkg_ext_ref_node = BNode() pkg_ext_ref_triple = (pkg_ext_ref_node, RDF.type, self.spdx_namespace.ExternalRef) self.graph.add(pkg_ext_ref_triple) pkg_ext_ref_category = Literal(pkg_ext_refs.category) pkg_ext_ref_category_triple = ( pkg_ext_ref_node, self.spdx_namespace.referenceCategory, pkg_ext_ref_category) self.graph.add(pkg_ext_ref_category_triple) pkg_ext_ref_type = Literal(pkg_ext_refs.pkg_ext_ref_type) pkg_ext_ref_type_triple = ( pkg_ext_ref_node, self.spdx_namespace.referenceType, pkg_ext_ref_type) self.graph.add(pkg_ext_ref_type_triple) pkg_ext_ref_locator = Literal(pkg_ext_refs.locator) pkg_ext_ref_locator_triple = ( pkg_ext_ref_node, self.spdx_namespace.referenceLocator, pkg_ext_ref_locator) self.graph.add(pkg_ext_ref_locator_triple) pkg_ext_ref_comment = Literal(pkg_ext_refs.comment) pkg_ext_ref_comment_triple = ( pkg_ext_ref_node, RDFS.comment, pkg_ext_ref_comment) self.graph.add(pkg_ext_ref_comment_triple) return pkg_ext_ref_node
Example #16
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def create_external_document_ref_node(self, ext_document_references): """ Add and return a creation info node to graph """ ext_doc_ref_node = BNode() type_triple = (ext_doc_ref_node, RDF.type, self.spdx_namespace.ExternalDocumentRef) self.graph.add(type_triple) ext_doc_id = Literal( ext_document_references.external_document_id) ext_doc_id_triple = ( ext_doc_ref_node, self.spdx_namespace.externalDocumentId, ext_doc_id) self.graph.add(ext_doc_id_triple) doc_uri = Literal( ext_document_references.spdx_document_uri) doc_uri_triple = ( ext_doc_ref_node, self.spdx_namespace.spdxDocument, doc_uri) self.graph.add(doc_uri_triple) checksum_node = self.create_checksum_node( ext_document_references.check_sum) self.graph.add( (ext_doc_ref_node, self.spdx_namespace.checksum, checksum_node)) return ext_doc_ref_node
Example #17
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def get_annotation_type(self, r_term): """ Return annotation type or None if found none or more than one. Report errors on failure. """ for _, _, typ in self.graph.triples(( r_term, self.spdx_namespace['annotationType'], None)): if typ is not None: return six.text_type(typ) else: self.error = True msg = 'Annotation must have exactly one annotation type.' self.logger.log(msg) return
Example #18
Source File: prov.py From grlc with MIT License | 5 votes |
def add_used_entity(self, entity_uri): """ Add the provided URI as a used entity by the logged activity """ entity_o = URIRef(entity_uri) self.prov_g.add((entity_o, RDF.type, self.prov.Entity)) self.prov_g.add((self.activity, self.prov.used, entity_o))
Example #19
Source File: v3migration.py From arches with GNU Affero General Public License v3.0 | 5 votes |
def add_children_to_collection(self, source_graph, out_graph, parent_id, topconcept_id): children = [i for i in source_graph.triples((topconcept_id, SKOS["narrower"], None))] for child in children: out_graph.add((ARCHES[parent_id], SKOS["member"], child[2])) out_graph.add((child[2], RDF.type, SKOS["Concept"])) self.add_children_to_collection(source_graph, out_graph, child[2], child[2]) return out_graph
Example #20
Source File: TestUtils.py From dipper with BSD 3-Clause "New" or "Revised" License | 5 votes |
def remove_ontology_axioms(graph): """ Given an rdflib graph, remove any triples connected to an ontology node: {} a owl:Ontology :param graph: RDFGraph :return: None """ ontology_iri = URIRef("http://www.w3.org/2002/07/owl#Ontology") for subject in graph.subjects(RDF.type, ontology_iri): for predicate, obj in graph.predicate_objects(subject): graph.remove((subject, predicate, obj)) graph.remove((subject, RDF.type, ontology_iri))
Example #21
Source File: yso.py From linkedevents with MIT License | 5 votes |
def save_keywords_in_bulk(self, graph): keywords = [] for subject in graph.subjects(RDF.type, SKOS.Concept): keyword = self.create_keyword(graph, subject) if keyword: keywords.append(keyword) Keyword.objects.bulk_create(keywords, batch_size=1000)
Example #22
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def handle_lics(self, lics): """ Return a License from a `lics` license resource. """ # Handle extracted licensing info type. if (lics, RDF.type, self.spdx_namespace['ExtractedLicensingInfo']) in self.graph: return self.parse_only_extr_license(lics) # Assume resource, hence the path separator ident_start = lics.rfind('/') + 1 if ident_start == 0: # special values such as spdx:noassertion special = self.to_special_value(lics) if special == lics: if self.LICS_REF_REGEX.match(lics): # Is a license ref i.e LicenseRef-1 return document.License.from_identifier(six.text_type(lics)) else: # Not a known license form raise SPDXValueError('License') else: # is a special value return special else: # license url return document.License.from_identifier(lics[ident_start:])
Example #23
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def p_pkg_lics_info_from_files(self, p_term, predicate): for _, _, lics in self.graph.triples((p_term, predicate, None)): try: if (lics, RDF.type, self.spdx_namespace['ExtractedLicensingInfo']) in self.graph: self.builder.set_pkg_license_from_file(self.doc, self.parse_only_extr_license(lics)) else: self.builder.set_pkg_license_from_file(self.doc, self.handle_lics(lics)) except SPDXValueError: self.value_error('PKG_LICS_INFO_FILES', lics)
Example #24
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def p_file_artifact(self, f_term, predicate): """ Handle file artifactOf. Note: does not handle artifact of project URI. """ for _, _, project in self.graph.triples((f_term, predicate, None)): if (project, RDF.type, self.doap_namespace['Project']): self.p_file_project(project) else: self.error = True msg = 'File must be artifact of doap:Project' self.logger.log(msg)
Example #25
Source File: prov.py From grlc with MIT License | 5 votes |
def init_prov_graph(self): """ Initialize PROV graph with all we know at the start of the recording """ try: # Use git2prov to get prov on the repo repo_prov = check_output( ['node_modules/git2prov/bin/git2prov', 'https://github.com/{}/{}/'.format(self.user, self.repo), 'PROV-O']).decode("utf-8") repo_prov = repo_prov[repo_prov.find('@'):] # glogger.debug('Git2PROV output: {}'.format(repo_prov)) glogger.debug('Ingesting Git2PROV output into RDF graph') with open('temp.prov.ttl', 'w') as temp_prov: temp_prov.write(repo_prov) self.prov_g.parse('temp.prov.ttl', format='turtle') except Exception as e: glogger.error(e) glogger.error("Couldn't parse Git2PROV graph, continuing without repo PROV") pass self.prov_g.add((self.agent, RDF.type, self.prov.Agent)) self.prov_g.add((self.entity_d, RDF.type, self.prov.Entity)) self.prov_g.add((self.activity, RDF.type, self.prov.Activity)) # entity_d self.prov_g.add((self.entity_d, self.prov.wasGeneratedBy, self.activity)) self.prov_g.add((self.entity_d, self.prov.wasAttributedTo, self.agent)) # later: entity_d genereated at time (when we know the end time) # activity self.prov_g.add((self.activity, self.prov.wasAssociatedWith, self.agent)) self.prov_g.add((self.activity, self.prov.startedAtTime, Literal(datetime.now()))) # later: activity used entity_o_1 ... entity_o_n # later: activity endedAtTime (when we know the end time)
Example #26
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def parse_pkg_ext_ref(self, pkg_ext_term): """ Parse the category, type, locator, and comment. """ for _s, _p, o in self.graph.triples((pkg_ext_term, self.spdx_namespace['referenceCategory'], None)): try: self.builder.set_pkg_ext_ref_category(self.doc, six.text_type(o)) except SPDXValueError: self.value_error('PKG_EXT_REF_CATEGORY', 'Package External Reference Category') break for _s, _p, o in self.graph.triples((pkg_ext_term, self.spdx_namespace['referenceType'], None)): try: self.builder.set_pkg_ext_ref_type(self.doc, six.text_type(o)) except SPDXValueError: self.value_error('PKG_EXT_REF_TYPE', 'Package External Reference Type') break for _s, _p, o in self.graph.triples((pkg_ext_term, self.spdx_namespace['referenceLocator'], None)): self.builder.set_pkg_ext_ref_locator(self.doc, six.text_type(o)) for _s, _p, o in self.graph.triples((pkg_ext_term, RDFS.comment, None)): try: self.builder.set_pkg_ext_ref_comment(self.doc, six.text_type(o)) except CardinalityError: self.more_than_one_error('Package External Reference Comment') break
Example #27
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def create_checksum_node(self, chksum): """ Return a node representing spdx.checksum. """ chksum_node = BNode() type_triple = (chksum_node, RDF.type, self.spdx_namespace.Checksum) self.graph.add(type_triple) algorithm_triple = (chksum_node, self.spdx_namespace.algorithm, Literal(chksum.identifier)) self.graph.add(algorithm_triple) value_triple = (chksum_node, self.spdx_namespace.checksumValue, Literal(chksum.value)) self.graph.add(value_triple) return chksum_node
Example #28
Source File: rdf.py From tools-python with Apache License 2.0 | 5 votes |
def create_conjunction_node(self, conjunction): """ Return a node representing a conjunction of licenses. """ node = BNode() type_triple = (node, RDF.type, self.spdx_namespace.ConjunctiveLicenseSet) self.graph.add(type_triple) licenses = self.licenses_from_tree(conjunction) for lic in licenses: member_triple = (node, self.spdx_namespace.member, lic) self.graph.add(member_triple) return node
Example #29
Source File: rdf.py From tools-python with Apache License 2.0 | 4 votes |
def create_package_node(self, package): """ Return a Node representing the package. Files must have been added to the graph before this method is called. """ package_node = URIRef('http://www.spdx.org/tools#SPDXRef-Package') type_triple = (package_node, RDF.type, self.spdx_namespace.Package) self.graph.add(type_triple) # Package SPDXID if package.spdx_id: pkg_spdx_id = URIRef(package.spdx_id) pkg_spdx_id_triple = (package_node, self.spdx_namespace.Package, pkg_spdx_id) self.graph.add(pkg_spdx_id_triple) # Handle optional fields: self.handle_pkg_optional_fields(package, package_node) # package name name_triple = (package_node, self.spdx_namespace.name, Literal(package.name)) self.graph.add(name_triple) # Package download location down_loc_node = (package_node, self.spdx_namespace.downloadLocation, self.to_special_value(package.download_location)) self.graph.add(down_loc_node) # Handle package verification verif_node = self.package_verif_node(package) verif_triple = (package_node, self.spdx_namespace.packageVerificationCode, verif_node) self.graph.add(verif_triple) # Handle concluded license conc_lic_node = self.license_or_special(package.conc_lics) conc_lic_triple = (package_node, self.spdx_namespace.licenseConcluded, conc_lic_node) self.graph.add(conc_lic_triple) # Handle declared license decl_lic_node = self.license_or_special(package.license_declared) decl_lic_triple = (package_node, self.spdx_namespace.licenseDeclared, decl_lic_node) self.graph.add(decl_lic_triple) # Package licenses from files licenses_from_files_nodes = map(lambda el: self.license_or_special(el), package.licenses_from_files) lic_from_files_predicate = self.spdx_namespace.licenseInfoFromFiles lic_from_files_triples = [(package_node, lic_from_files_predicate, node) for node in licenses_from_files_nodes] for triple in lic_from_files_triples: self.graph.add(triple) # Copyright Text cr_text_node = self.to_special_value(package.cr_text) cr_text_triple = (package_node, self.spdx_namespace.copyrightText, cr_text_node) self.graph.add(cr_text_triple) # Handle files self.handle_package_has_file(package, package_node) return package_node
Example #30
Source File: rdf.py From tools-python with Apache License 2.0 | 4 votes |
def parse(self, fil): """ Parse a file and returns a document object. fil is a file like object. """ self.error = False self.graph = Graph() self.graph.parse(file=fil, format='xml') self.doc = document.Document() for s, _p, o in self.graph.triples((None, RDF.type, self.spdx_namespace['SpdxDocument'])): self.parse_doc_fields(s) for s, _p, o in self.graph.triples((None, RDF.type, self.spdx_namespace['ExternalDocumentRef'])): self.parse_ext_doc_ref(s) for s, _p, o in self.graph.triples((None, RDF.type, self.spdx_namespace['CreationInfo'])): self.parse_creation_info(s) for s, _p, o in self.graph.triples((None, None, self.spdx_namespace['ExtractedLicensingInfo'])): self.handle_extracted_license(s) for s, _p, o in self.graph.triples((None, RDF.type, self.spdx_namespace['Package'])): self.parse_package(s) for s, _p, o in self.graph.triples((None, RDF.type, self.spdx_namespace['ExternalRef'])): self.parse_pkg_ext_ref(s) for s, _p, o in self.graph.triples((None, self.spdx_namespace['referencesFile'], None)): self.parse_file(o) for s, _p, o in self.graph.triples((None, RDF.type, self.spdx_namespace['Snippet'])): self.parse_snippet(s) for s, _p, o in self.graph.triples((None, self.spdx_namespace['reviewed'], None)): self.parse_review(o) for s, _p, o in self.graph.triples((None, self.spdx_namespace['annotation'], None)): self.parse_annotation(o) validation_messages = [] # Report extra errors if self.error is False otherwise there will be # redundent messages validation_messages = self.doc.validate(validation_messages) if not self.error: if validation_messages: for msg in validation_messages: self.logger.log(msg) self.error = True return self.doc, self.error