Python rdflib.Literal() Examples

The following are 30 code examples of rdflib.Literal(). 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 , or try the search function .
Example #1
Source File: rdf.py    From dgl with Apache License 2.0 6 votes vote down vote up
def parse_entity(self, term):
        if isinstance(term, rdf.Literal):
            return Entity(id=str(term), cls="_Literal")
        elif isinstance(term, rdf.BNode):
            return None
        entstr = str(term)
        if entstr.startswith(self.entity_prefix):
            inst = entstr[len(self.entity_prefix):]
            if self.d_entity.match(inst):
                cls = 'd'
            elif self.bond_entity.match(inst):
                cls = 'bond'
            else:
                cls = None
            return Entity(id=inst, cls=cls)
        else:
            return None 
Example #2
Source File: __init__.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def __init__(self, shape, rule_node):
        """

        :param shape:
        :type shape: Shape
        :param rule_node:
        :type rule_node: rdflib.Identifier
        """
        super(SPARQLRule, self).__init__(shape, rule_node)
        construct_nodes = set(self.shape.sg.objects(self.node, SH_construct))
        if len(construct_nodes) < 1:
            raise RuleLoadError("No sh:construct on SPARQLRule", "https://www.w3.org/TR/shacl-af/#SPARQLRule")
        self._constructs = []
        for c in construct_nodes:
            if not isinstance(c, Literal) or not (c.datatype == XSD_string or c.language is not None or isinstance(c.value, str)):
                raise RuleLoadError("SPARQLRule sh:construct must be an xsd:string", "https://www.w3.org/TR/shacl-af/#SPARQLRule")
            self._constructs.append(str(c.value))
        query_helper = SPARQLQueryHelper(self.shape, self.node, None, deactivated=self._deactivated)
        query_helper.collect_prefixes()
        self._qh = query_helper 
Example #3
Source File: test_dataset.py    From dipper with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_version_level_version_set_explicitly(self):
        self.dataset = Dataset(
            identifier=self.identifier,
            data_release_version=self.data_release_version,
            ingest_name=self.identifier,
            ingest_title=self.ingest_title,
            ingest_url=self.ingest_url,
            ingest_logo=self.ingest_logo_url,
            ingest_description=self.ingest_description,
            license_url=None,
            data_rights=self.data_rights
        )
        triples = list(self.dataset.graph.triples(
            (self.version_level_IRI, self.iri_version, None)))
        self.assertTrue(len(triples) == 1,
                        "didn't get exactly one version level version triple")
        self.assertEqual(triples[0][2],
                         Literal(self.data_release_version, datatype=XSD.date),
                         "version level version triple (set explicitly) is wrong ") 
Example #4
Source File: __init__.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def get_nodes_from_node_expression(self, expr, focus_node, data_graph):
        if expr == SH_this:
            return [focus_node]
        elif isinstance(expr, (rdflib.URIRef, rdflib.Literal)):
            return [expr]
        elif isinstance(expr, rdflib.BNode):
            path_nodes = set(self.shape.sg.objects(expr, SH_path))
            if len(path_nodes) > 0:
                path_results = []
                for p in path_nodes:
                    vals = self.shape.value_nodes_from_path(self.shape.sg, focus_node, p, data_graph)
                    path_results.extend(vals)
                return path_results
            else:
                raise NotImplementedError("Unsupported expression s, p, or o, in SHACL TripleRule")
        else:
            raise NotImplementedError("Unsupported expression s, p, or o, in SHACL TripleRule") 
Example #5
Source File: sparql_based_constraint_components.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def __init__(self, shacl_graph, node, *args, **kwargs):
        super(AskConstraintValidator, self).__init__(shacl_graph, node, **kwargs)
        g = shacl_graph.graph
        ask_vals = set(g.objects(node, SH_ask))
        if len(ask_vals) < 1 or len(ask_vals) > 1:
            # TODO:coverage: No test for this case
            raise ConstraintLoadError(
                "AskValidator must have exactly one value for sh:ask.",
                "https://www.w3.org/TR/shacl/#ConstraintComponent")
        ask_val = next(iter(ask_vals))
        if not (isinstance(ask_val, rdflib.Literal) and
                isinstance(ask_val.value, str)):
            # TODO:coverage: No test for this case
            raise ConstraintLoadError(
                "AskValidator sh:ask must be an RDF Literal of type xsd:string.",
                "https://www.w3.org/TR/shacl/#ConstraintComponent")
        self.query_text = ask_val.value 
Example #6
Source File: sparql_based_constraint_components.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def __init__(self, shacl_graph, node, *args, **kwargs):
        super(SelectConstraintValidator, self).__init__(shacl_graph, node, **kwargs)
        g = shacl_graph.graph
        select_vals = set(g.objects(node, SH_select))
        if len(select_vals) < 1 or len(select_vals) > 1:
            # TODO:coverage: No test for this case, do we need to test this?
            raise ConstraintLoadError(
                "SelectValidator must have exactly one value for sh:select.",
                "https://www.w3.org/TR/shacl/#ConstraintComponent")
        select_val = next(iter(select_vals))
        if not (isinstance(select_val, rdflib.Literal) and
                isinstance(select_val.value, str)):
            # TODO:coverage: No test for the case when sh:select is not a literal of type string
            raise ConstraintLoadError(
                "SelectValidator sh:select must be an RDF Literal of type xsd:string.",
                "https://www.w3.org/TR/shacl/#ConstraintComponent")
        self.query_text = select_val.value 
Example #7
Source File: datatype_to_rdf_tests.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_rdf_concept_list(self):
        dt = self.DT.get_instance("concept-list")
        concept_list = [
            "d75977c1-635b-41d5-b53d-1c82d2237b67",  # junk sculpture@en, has aat identifier
            "4beb7055-8a6e-45a3-9bfb-32984b6f82e0",  # "example document type"@en-us, no ext id}
        ]

        edge_info = {}
        edge_info["range_tile_data"] = concept_list
        edge_info["r_uri"] = URIRef("http://vocab.getty.edu/aat/300047196")
        edge_info["d_uri"] = URIRef("test")
        edge = Mock()
        edge.ontologyproperty = CIDOC_NS["P2_has_type"]
        edge.rangenode.ontologyclass = CIDOC_NS["E55_Type"]
        graph = dt.to_rdf(edge_info, edge)

        edge_info["r_uri"] = ARCHES_NS["concepts/037daf4d-054a-44d2-9c0a-108b59e39109"]
        graph += dt.to_rdf(edge_info, edge)

        self.assertTrue((edge_info["d_uri"], edge.ontologyproperty, URIRef("http://vocab.getty.edu/aat/300047196")) in graph)
        self.assertTrue((URIRef("http://vocab.getty.edu/aat/300047196"), RDFS.label, Literal("junk sculpture")) in graph)
        self.assertTrue((edge_info["d_uri"], edge.ontologyproperty, ARCHES_NS["concepts/037daf4d-054a-44d2-9c0a-108b59e39109"]) in graph)
        self.assertTrue((ARCHES_NS["concepts/037daf4d-054a-44d2-9c0a-108b59e39109"], RDFS.label, Literal("example document type")) in graph) 
Example #8
Source File: test_source_metadata.py    From dipper with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_version_level_source_version_download_timestamp(self):
        path_to_dl_file = '/'.join(
            (self.source.rawdir, self.source.files.get('test_file').get('file')))
        fstat = os.stat(path_to_dl_file)

        self.downloaded_file_timestamp = \
            datetime.utcfromtimestamp(fstat[ST_CTIME]).strftime("%Y%m%d")

        triples = list(self.source.dataset.graph.triples(
            (URIRef(self.theseFiles.get("test_file").get("url")),
             self.iri_retrieved_on,
             None)))
        self.assertTrue(len(triples) == 1,
                        "missing triple for ingest file retrieved on " +
                        "(download timestamp)")
        self.assertEqual(Literal(triples[0][2], datatype=XSD.date),
                         Literal(self.downloaded_file_timestamp, datatype=XSD.date),
                         "version level source version timestamp isn't " +
                         "the same as the download timestamp of the local file") 
Example #9
Source File: test_source_metadata.py    From dipper with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_postgres_version_level_source_version_download_timestamp(self):
        path_to_dl_file = '/'.join(
            (self.pg_source.rawdir, self.pg_source.files.get('test_file').get('file')))
        fstat = os.stat(path_to_dl_file)

        self.downloaded_file_timestamp = \
            datetime.utcfromtimestamp(fstat[ST_CTIME]).strftime("%Y%m%d")

        triples = list(self.pg_source.dataset.graph.triples(
            (URIRef(self.theseFiles.get("test_file").get("url")),
             self.iri_retrieved_on,
             None)))
        self.assertTrue(len(triples) == 1,
                        "missing triple for ingest file retrieved on " +
                        "(download timestamp)")
        self.assertEqual(Literal(triples[0][2], datatype=XSD.date),
                         Literal(self.downloaded_file_timestamp, datatype=XSD.date),
                         "version level source version timestamp isn't " +
                         "the same as the download timestamp of the local file") 
Example #10
Source File: string_based_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def _evaluate_string_rule(self, r, target_graph, f_v_dict):
        reports = []
        non_conformant = False
        assert isinstance(r, rdflib.Literal)
        min_len = int(r.value)
        if min_len < 0:
            raise ReportableRuntimeError("Minimum length cannot be less than zero!")
        for f, value_nodes in f_v_dict.items():
            for v in value_nodes:
                flag = False
                if min_len == 0:
                    flag = True  # min len zero always passes
                elif isinstance(v, rdflib.BNode):
                    # blank nodes cannot pass minLen validation
                    pass
                else:
                    v_string = self.value_node_to_string(v)
                    flag = len(v_string) >= min_len
                if not flag:
                    non_conformant = True
                    rept = self.make_v_result(target_graph, f, value_node=v)
                    reports.append(rept)
        return non_conformant, reports 
Example #11
Source File: string_based_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def _evaluate_string_rule(self, r, target_graph, f_v_dict):
        reports = []
        non_conformant = False
        assert isinstance(r, rdflib.Literal)
        max_len = int(r.value)
        if max_len < 0:
            raise ReportableRuntimeError("Maximum length cannot be less than zero!")
        for f, value_nodes in f_v_dict.items():
            for v in value_nodes:
                flag = False
                if isinstance(v, rdflib.BNode):
                    # blank nodes cannot pass minLen validation
                    pass
                else:
                    v_string = self.value_node_to_string(v)
                    flag = len(v_string) <= max_len
                if not flag:
                    non_conformant = True
                    rept = self.make_v_result(target_graph, f, value_node=v)
                    reports.append(rept)
        return non_conformant, reports 
Example #12
Source File: stringify.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def stringify_node(graph, node, ns_manager=None, recursion=0):
    if ns_manager is None:
        ns_manager = graph.namespace_manager
    if isinstance(ns_manager, rdflib.Graph):
        #json-ld loader can set namespace_manager to the conjunctive graph itself.
        ns_manager = ns_manager.namespace_manager
    ns_manager.bind("sh", SH, override=False, replace=False)
    if isinstance(node, rdflib.Literal):
        return stringify_literal(graph, node, ns_manager=ns_manager)
    if isinstance(node, rdflib.BNode):
        if isinstance(graph, (rdflib.ConjunctiveGraph, rdflib.Dataset)):
            graph = find_node_named_graph(graph, node)
        return stringify_blank_node(graph, node, ns_manager=ns_manager,
                                    recursion=recursion+1)
    if isinstance(node, rdflib.URIRef):
        return node.n3(namespace_manager=ns_manager)
    else:
        node_string = str(node)
    return node_string 
Example #13
Source File: string_based_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def __init__(self, shape):
        super(UniqueLangConstraintComponent, self).__init__(shape)
        self.allow_multi_rules = False
        is_unique_lang = set(self.shape.objects(SH_uniqueLang))
        if len(is_unique_lang) < 1:
            raise ConstraintLoadError(
                "UniqueLangConstraintComponent must have at least one sh:uniqueLang predicate.",
                "https://www.w3.org/TR/shacl/#UniqueLangConstraintComponent")
        elif len(is_unique_lang) > 1:
            raise ConstraintLoadError(
                "UniqueLangConstraintComponent must have at most one sh:uniqueLang predicate.",
                "https://www.w3.org/TR/shacl/#UniqueLangConstraintComponent")
        if not shape.is_property_shape:
            raise ConstraintLoadError(
                "UniqueLangConstraintComponent can only be present on a PropertyShape, not a NodeShape.",
                "https://www.w3.org/TR/shacl/#UniqueLangConstraintComponent")
        is_unique_lang = next(iter(is_unique_lang))
        try:
            assert isinstance(is_unique_lang, rdflib.Literal)
            assert isinstance(is_unique_lang.value, bool)
        except (AssertionError, AttributeError):
            raise ConstraintLoadError(
                "UniqueLangConstraintComponent must have an RDF Literal of type boolean as its sh:uniqueLang.",
                "https://www.w3.org/TR/shacl/#UniqueLangConstraintComponent")
        self.string_rules = {is_unique_lang.value} 
Example #14
Source File: property_pair_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def evaluate(self, target_graph, focus_value_nodes, _evaluation_path):
        """

        :type focus_value_nodes: dict
        :type target_graph: rdflib.Graph
        """
        reports = []
        non_conformant = False

        for lt in iter(self.property_compare_set):
            if isinstance(lt, rdflib.Literal) or\
               isinstance(lt, rdflib.BNode):
                raise ReportableRuntimeError(
                    "Value of sh:lessThan MUST be a URI Identifier.")
            _nc, _r = self._evaluate_less_than(lt, target_graph, focus_value_nodes)
            non_conformant = non_conformant or _nc
            reports.extend(_r)
        return (not non_conformant), reports 
Example #15
Source File: property_pair_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def evaluate(self, target_graph, focus_value_nodes, _evaluation_path):
        """

        :type focus_value_nodes: dict
        :type target_graph: rdflib.Graph
        """
        reports = []
        non_conformant = False

        for lt in iter(self.property_compare_set):
            if isinstance(lt, rdflib.Literal) or\
               isinstance(lt, rdflib.BNode):
                raise ReportableRuntimeError(
                    "Value of sh:lessThanOrEquals MUST be a URI Identifier.")
            _nc, _r = self._evaluate_ltoe(lt, target_graph, focus_value_nodes)
            non_conformant = non_conformant or _nc
            reports.extend(_r)
        return (not non_conformant), reports 
Example #16
Source File: other_constraints.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def __init__(self, shape):
        super(ClosedConstraintComponent, self).__init__(shape)
        sg = self.shape.sg.graph
        closed_vals = list(self.shape.objects(SH_closed))
        if len(closed_vals) < 1:
            raise ConstraintLoadError(
                "ClosedConstraintComponent must have at least one sh:closed predicate.",
                "https://www.w3.org/TR/shacl/#ClosedConstraintComponent")
        elif len(closed_vals) > 1:
            raise ConstraintLoadError(
                "ClosedConstraintComponent must have at most one sh:closed predicate.",
                "https://www.w3.org/TR/shacl/#ClosedConstraintComponent")
        assert isinstance(closed_vals[0], rdflib.Literal), "sh:closed must take a xsd:boolean literal."
        self.is_closed = bool(closed_vals[0].value)
        ignored_vals = list(self.shape.objects(SH_ignoredProperties))
        self.ignored_props = set()
        for i in ignored_vals:
            try:
                items = set(sg.items(i))
                for list_item in items:
                    self.ignored_props.add(list_item)
            except ValueError:
                continue
        self.property_shapes = list(self.shape.objects(SH_property)) 
Example #17
Source File: stringify.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def stringify_literal(graph, node, ns_manager=None):
    lit_val_string = str(node.value)
    lex_val_string = str(node)
    if ns_manager is None:  # pragma: no cover
        ns_manager = graph.namespace_manager
        ns_manager.bind("sh", SH)
    if lit_val_string != lex_val_string:
        val_string = "\"{}\" = {}".format(lex_val_string, lit_val_string)
    else:
        val_string = "\"{}\"".format(lex_val_string)
    if node.language:
        lang_string = ", lang={}".format(str(node.language))
    else:
        lang_string = ""
    if node.datatype:
        datatype_uri = stringify_node(graph, node.datatype,
                                      ns_manager=ns_manager)
        datatype_string = ", datatype={}".format(datatype_uri)
    else:
        datatype_string = ""
    node_string = "Literal({}{}{})" \
        .format(val_string,
                lang_string,
                datatype_string)
    return node_string 
Example #18
Source File: rdf.py    From dgl with Apache License 2.0 6 votes vote down vote up
def parse_entity(self, term):
        if isinstance(term, rdf.Literal):
            return None
        elif isinstance(term, rdf.BNode):
            return Entity(id=str(term), cls='_BNode')
        entstr = str(term)
        if entstr.startswith(self.entity_prefix):
            sp = entstr.split('/')
            assert len(sp) == 7, entstr
            spp = sp[6].split('-')
            if len(spp) == 2:
                # instance
                cls, inst = spp
            else:
                cls = 'TYPE'
                inst = spp
            return Entity(id=inst, cls=cls)
        else:
            return None 
Example #19
Source File: rdf.py    From dgl with Apache License 2.0 6 votes vote down vote up
def parse_entity(self, term):
        if isinstance(term, rdf.Literal):
            return None
        elif isinstance(term, rdf.BNode):
            return None
        entstr = str(term)
        if entstr.startswith(self.status_prefix):
            return None
        if entstr.startswith(self.entity_prefix):
            sp = entstr.split('/')
            if len(sp) != 7:
                return None
            # instance
            cls = '%s/%s' % (sp[4], sp[5])
            inst = sp[6]
            return Entity(id=inst, cls=cls)
        else:
            return None 
Example #20
Source File: value_range_constraints.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def _evaluate_max_rule(self, m_val, target_graph, f_v_dict):
        reports = []
        non_conformant = False
        assert isinstance(m_val, rdflib.Literal)
        max_is_string = isinstance(m_val.value, str)
        for f, value_nodes in f_v_dict.items():
            for v in value_nodes:
                flag = False
                if isinstance(v, rdflib.BNode):
                    # blank nodes cannot pass val comparison
                    pass
                elif isinstance(v, rdflib.URIRef):
                    # TODO: Don't know if URIRefs can be compared here
                    pass
                elif isinstance(v, rdflib.Literal):
                    v_is_string = isinstance(v.value, str)
                    if max_is_string and not v_is_string:
                        pass
                    elif v_is_string and not max_is_string:
                        pass
                    else:
                        try:
                            # pass if v <= m_val
                            cmp = compare_literal(v, m_val)
                            flag = cmp <= 0
                        except (TypeError, NotImplementedError):
                            flag = False
                else:
                    raise ReportableRuntimeError(
                        "Not sure how to compare anything else.")
                if not flag:
                    non_conformant = True
                    rept = self.make_v_result(target_graph, f, value_node=v)
                    reports.append(rept)
        return non_conformant, reports 
Example #21
Source File: connection.py    From sparql-kernel with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def gtype(n):
    """
    Return the a string with the data type of a value, for Graph data
    """
    t = type(n).__name__
    return str(t) if t != 'Literal' else 'Literal, {}'.format(n.language) 
Example #22
Source File: string_based_constraints.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def _evaluate_string_rule(self, r, target_graph, f_v_dict):
        reports = []
        non_conformant = False
        languages_need = set()
        sg = self.shape.sg.graph
        try:
            for l in iter(sg.items(r)):
                try:
                    assert isinstance(l, rdflib.Literal)
                    assert isinstance(l.value, str)
                except (AssertionError, AttributeError):
                    raise ReportableRuntimeError(
                        "All languages in sh:LanugageIn must be a Literal "
                        "with type xsd:string")
                languages_need.add(str(l.value).lower())
        except (KeyError, AttributeError, ValueError):
            raise ReportableRuntimeError("Value of sh:LanguageIn must be a RDF List")
        wildcard = False
        if '*' in languages_need:
            wildcard = True
        for f, value_nodes in f_v_dict.items():
            for v in value_nodes:
                flag = False
                if isinstance(v, rdflib.Literal):
                    lang = v.language
                    if lang:
                        if wildcard:
                            flag = True
                        elif str(lang).lower() in languages_need:
                            flag = True
                        else:
                            lang_parts = str(lang).split('-')
                            first_part = lang_parts[0]
                            if str(first_part).lower() in languages_need:
                                flag = True
                if not flag:
                    non_conformant = True
                    rept = self.make_v_result(target_graph, f, value_node=v)
                    reports.append(rept)
        return non_conformant, reports 
Example #23
Source File: string_based_constraints.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def _evaluate_string_rule(self, r, target_graph, f_v_dict):
        reports = []
        non_conformant = False
        assert isinstance(r, rdflib.Literal)
        re_flags = 0
        if self.flags:
            flags = str(self.flags.value).lower()
            case_insensitive = 'i' in flags
            if case_insensitive:
                re_flags |= re.I
            m = 'm' in flags
            if m:
                re_flags |= re.M
        re_pattern = str(r.value)
        re_matcher = re.compile(re_pattern, re_flags)
        for f, value_nodes in f_v_dict.items():
            for v in value_nodes:
                match = False
                if isinstance(v, rdflib.BNode):
                    # blank nodes cannot pass pattern validation
                    pass
                else:
                    v_string = self.value_node_to_string(v)
                    match = re_matcher.match(v_string)
                    if not match:
                        match = re_matcher.search(v_string)
                if not match:
                    non_conformant = True
                    rept = self.make_v_result(target_graph, f, value_node=v)
                    reports.append(rept)
        return non_conformant, reports 
Example #24
Source File: connection.py    From sparql-kernel with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def lang_match_rdf(triple, accepted_languages):
    '''Find if the RDF triple contains acceptable language data'''
    if not accepted_languages:
        return True
    languages = set([n.language for n in triple if isinstance(n, Literal)])
    return (not languages) or (languages & accepted_languages) 
Example #25
Source File: value_constraints.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def _evaluate_class_rules(self, target_graph, f_v_dict, class_rule):
        reports = []
        non_conformant = False
        for f, value_nodes in f_v_dict.items():
            for v in value_nodes:
                found = False
                if isinstance(v, Literal):
                    self.shape.logger.debug("Class Constraint won't work with Literals. "
                                   "Attempting to match Literal node {} to class of {} will fail."
                                   .format(v, class_rule))
                else:
                    objs = target_graph.objects(v, RDF_type)
                    for ctype in iter(objs):
                        if ctype == class_rule:
                            found = True
                            break
                        # Note, this only ones _one_ level of subclass traversing.
                        # For more levels, the whole target graph should be put through
                        # a RDFS reasoning engine.
                        subclasses = target_graph.objects(ctype, RDFS_subClassOf)
                        if class_rule in iter(subclasses):
                            found = True
                            break
                if not found:
                    non_conformant = True
                    rept = self.make_v_result(target_graph, f, value_node=v)
                    reports.append(rept)
        return non_conformant, reports 
Example #26
Source File: validate.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def create_validation_report(cls, conforms, target_graph, shacl_graph, results):
        v_text = "Validation Report\nConforms: {}\n".format(str(conforms))
        result_len = len(results)
        if not conforms:
            assert result_len > 0, \
                "A Non-Conformant Validation Report must have at least one result."
        if result_len > 0:
            v_text += "Results ({}):\n".format(str(result_len))
        vg = rdflib.Graph()
        sg = shacl_graph.graph
        for p, n in sg.namespace_manager.namespaces():
            vg.namespace_manager.bind(p, n)
        vr = BNode()
        vg.add((vr, RDF_type, SH_ValidationReport))
        vg.add((vr, SH_conforms, Literal(conforms)))
        for result in iter(results):
            _d, _bn, _tr = result
            v_text += _d
            vg.add((vr, SH_result, _bn))
            for tr in iter(_tr):
                s, p, o = tr
                if isinstance(o, tuple):
                    source = o[0]
                    node = o[1]
                    if source == 'S':
                        o = clone_node(sg, node, vg)
                    elif source == 'D':
                        o = clone_node(target_graph, node, vg)
                    else:  # pragma: no cover
                        raise RuntimeError("Adding node to validation report must have source of either 'D' or 'S'.")
                vg.add((s, p, o))
        return vg, v_text 
Example #27
Source File: shape.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def order(self):
        order_nodes = list(self.objects(SH_order))
        if len(order_nodes) < 1:
            return Decimal("0.0")
        if len(order_nodes) > 1:
            raise ShapeLoadError(
                "A SHACL Shape can have only one sh:order property.",
                "https://www.w3.org/TR/shacl-af/#rules-order")
        order_node = next(iter(order_nodes))
        if not isinstance(order_node, rdflib.Literal):
            raise ShapeLoadError(
                "A SHACL Shape must be a numeric literal.",
                "https://www.w3.org/TR/shacl-af/#rules-order")
        return Decimal(order_node.value) 
Example #28
Source File: clone.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def clone_node(graph, node, target_graph, recursion=0):
    if isinstance(node, rdflib.Literal):
        new_node = clone_literal(graph, node, target_graph)
    elif isinstance(node, rdflib.BNode):
        new_node = clone_blank_node(
                   graph, node, target_graph,
                   recursion=recursion+1)
    elif isinstance(node, rdflib.URIRef):
        new_node = rdflib.URIRef(str(node))
    else:
        new_node = rdflib.term.Identifier(str(node))
    return new_node 
Example #29
Source File: clone.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def clone_literal(graph, node, target_graph):
    lex_val_string = str(node)
    lang = node.language
    datatype = node.datatype
    new_literal = rdflib.Literal(lex_val_string,
                                 lang, datatype)
    return new_literal 
Example #30
Source File: stringify.py    From pySHACL with Apache License 2.0 5 votes vote down vote up
def find_node_named_graph(dataset, node):
    if isinstance(node, rdflib.Literal):
        raise RuntimeError("Cannot search for a Literal node in a dataset.")
    for g in iter(dataset.contexts()):
        try:
             first = next(iter(g.predicate_objects(node)))
             return g
        except StopIteration:
            continue
    raise RuntimeError("Cannot find that node in any named graph.")