def test_example_2(self):
        from pyshex.shape_expressions_language.p5_4_node_constraints import nodeSatisfiesValues

        cntxt = setup_context(shex_2, rdf_2)
        nc = cntxt.schema.shapes[0].expression.valueExpr

        focus = cntxt.graph.value(EX.issue3, FOAF.mbox)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesValues(cntxt, focus, nc))

        focus = cntxt.graph.value(EX.issue4, FOAF.mbox)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesValues(cntxt, focus, nc))

        focus = cntxt.graph.value(EX.issue6, FOAF.mbox)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesValues(cntxt, focus, nc))
        self.assertEqual(['Node: "missing" not in value set:\n'
                         '\t {"values": [{"value": "N/A"}, {"stem": "mailto:engineering-"...'],
                         self.fail_reasons(cntxt))

        focus = cntxt.graph.value(EX.issue7, FOAF.mbox)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesValues(cntxt, focus, nc))
        self.assertEqual(['Node: <mailto:[email protected]> not in value set:\n'
                          '\t {"values": [{"value": "N/A"}, {"stem": "mailto:engineering-"...'],
                         self.fail_reasons(cntxt))
    def test_example_1a(self):
        from pyshex.shape_expressions_language.p5_4_node_constraints import nodeSatisfiesDataType

        cntxt = setup_context(shex_1, rdf_1)
        nc = cntxt.schema.shapes[0].expression.valueExpr
        focus = cntxt.graph.value(EX.issue3, EX.submittedOn)
        cntxt.current_node = ParseNode(nodeSatisfiesDataType, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesDataType(cntxt, focus, nc))
        focus = cntxt.graph.value(EX.issue3a, EX.submittedOn)
        cntxt.current_node = ParseNode(nodeSatisfiesDataType, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesDataType(cntxt, focus, nc))
Beispiel #3
0
    def test_example_1(self):
        cntxt = setup_context(shex_1, rdf_1)
        nc = cntxt.schema.shapes[0].expression.valueExpr

        focus = cntxt.graph.value(EX.issue1, EX.submittedBy)
        cntxt.current_node = ParseNode(nodeSatisfiesStringFacet, nc, focus,
                                       cntxt)
        self.assertTrue(nodeSatisfiesStringFacet(cntxt, focus, nc))
        focus = cntxt.graph.value(EX.issue2, EX.submittedBy)
        cntxt.current_node = ParseNode(nodeSatisfiesStringFacet, nc, focus,
                                       cntxt)
        self.assertFalse(nodeSatisfiesStringFacet(cntxt, focus, nc))
        self.assertEqual(['String length violation - minimum: 10 actual: 4'],
                         self.fail_reasons(cntxt))
    def test_example_1(self):
        from pyshex.shape_expressions_language.p5_4_node_constraints import nodeSatisfiesValues

        cntxt = setup_context(shex_1, rdf_1)
        nc = cntxt.schema.shapes[0].expression.valueExpr
        focus = cntxt.graph.value(EX.issue1, EX.state)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesValues(cntxt, focus, nc))

        focus = cntxt.graph.value(EX.issue2, EX.state)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesValues(cntxt, focus, nc))
        self.assertEqual(['Node: :Unresolved not in value set:\n'
                          '\t {"values": ["http://schema.example/Resolved", "http://schema...'],
                         self.fail_reasons(cntxt))
    def test_example_1(self):
        from pyshex.shape_expressions_language.p5_4_node_constraints import nodeSatisfiesNodeKind
        cntxt = setup_context(shex_1, rdf_1)

        nc = cntxt.schema.shapes[0].expression.valueExpr

        focus = cntxt.graph.value(EX.issue1, EX.state)
        cntxt.current_node = ParseNode(nodeSatisfiesNodeKind, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesNodeKind(cntxt, focus, nc))

        focus = cntxt.graph.value(EX.issue3, EX.state)
        cntxt.current_node = ParseNode(nodeSatisfiesNodeKind, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesNodeKind(cntxt, focus, nc))
        self.assertEqual(['Node kind mismatch have: Literal expected: iri'],
                         self.fail_reasons(cntxt))
Beispiel #6
0
    def test_example_2(self):
        cntxt = setup_context(shex_2, rdf_2)
        nc = cntxt.schema.shapes[0].expression.valueExpr

        focus = cntxt.graph.value(EX.issue6, EX.submittedBy)
        cntxt.current_node = ParseNode(nodeSatisfiesStringFacet, nc, focus,
                                       cntxt)
        self.assertTrue(nodeSatisfiesStringFacet(cntxt, focus, nc))
        focus = cntxt.graph.value(EX.issue7, EX.submittedBy)
        cntxt.current_node = ParseNode(nodeSatisfiesStringFacet, nc, focus,
                                       cntxt)
        self.assertFalse(nodeSatisfiesStringFacet(cntxt, focus, nc))
        self.assertEqual([
            'Pattern match failure - pattern: genuser[0-9]+ flags:i string: '
            'http://schema.example/genContact817'
        ], self.fail_reasons(cntxt))
    def test_example_2(self):
        from pyshex.shape_expressions_language.p5_4_node_constraints import nodeSatisfiesDataType

        cntxt = setup_context(shex_2, rdf_2)
        nc = cntxt.schema.shapes[0].expression.valueExpr
        focus = cntxt.graph.value(EX.issue3, RDFS.label)
        cntxt.current_node = ParseNode(nodeSatisfiesDataType, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesDataType(cntxt, focus, nc))

        focus = cntxt.graph.value(EX.issue4, RDFS.label)
        cntxt.current_node = ParseNode(nodeSatisfiesDataType, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesDataType(cntxt, focus, nc))
        self.assertEqual([
            'Datatype mismatch - expected: '
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString actual: '
            'http://www.w3.org/2001/XMLSchema#string'
        ], self.fail_reasons(cntxt))
 def test_trailing_zero(self):
     cntxt = setup_context(shex_2, rdf_2)
     nc = cntxt.schema.shapes[0].expression.valueExpr
     focus = cntxt.graph.value(URIRef("http://a.example/s1"),
                               URIRef("http://a.example/p1"))
     cntxt.current_node = ParseNode(nodeSatisfiesNumericFacet, nc, focus,
                                    cntxt)
     self.assertTrue(nodeSatisfiesNumericFacet(cntxt, focus, nc))
 def test_example_1(self):
     cntxt = setup_context(shex_1, rdf_1)
     nc = cntxt.schema.shapes[0].expression.valueExpr
     focus = cntxt.graph.value(EX.issue1, EX.confirmations)
     cntxt.current_node = ParseNode(nodeSatisfiesNumericFacet, nc, focus,
                                    cntxt)
     self.assertTrue(nodeSatisfiesNumericFacet(cntxt, focus, nc))
     focus = cntxt.graph.value(EX.issue2, EX.confirmations)
     cntxt.current_node = ParseNode(nodeSatisfiesNumericFacet, nc, focus,
                                    cntxt)
     self.assertTrue(nodeSatisfiesNumericFacet(cntxt, focus, nc))
     focus = cntxt.graph.value(EX.issue3, EX.confirmations)
     cntxt.current_node = ParseNode(nodeSatisfiesNumericFacet, nc, focus,
                                    cntxt)
     self.assertFalse(nodeSatisfiesNumericFacet(cntxt, focus, nc))
     self.assertEqual(
         ['Numeric value volation - minimum inclusive: 1.0 actual: 0'],
         self.fail_reasons(cntxt))
Beispiel #10
0
 def test_example_3(self):
     cntxt = setup_context(shex_3, rdf_3)
     nc = cntxt.schema.shapes[0].expression.valueExpr
     focus = cntxt.graph.value(EX.product6, EX.trademark)
     cntxt.current_node = ParseNode(nodeSatisfiesStringFacet, nc, focus,
                                    cntxt)
     self.assertTrue(nodeSatisfiesStringFacet(cntxt, focus, nc))
     focus = cntxt.graph.value(EX.product7, EX.trademark)
     cntxt.current_node = ParseNode(nodeSatisfiesStringFacet, nc, focus,
                                    cntxt)
     self.assertTrue(nodeSatisfiesStringFacet(cntxt, focus, nc))
     focus = cntxt.graph.value(EX.product8, EX.trademark)
     cntxt.current_node = ParseNode(nodeSatisfiesStringFacet, nc, focus,
                                    cntxt)
     self.assertFalse(nodeSatisfiesStringFacet(cntxt, focus, nc))
     self.assertEqual([
         'Pattern match failure - pattern: ^\\t\\\\𝒸\\?$ flags:None string: \t'
         '\\\\U0001D4B8?'
     ], self.fail_reasons(cntxt))
    def test_example_3(self):
        from pyshex.shape_expressions_language.p5_4_node_constraints import nodeSatisfiesValues

        cntxt = setup_context(shex_3, rdf_3)
        nc = cntxt.schema.shapes[0].expression.valueExpr
        focus = cntxt.graph.value(EX.issue8, FOAF.mbox)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesValues(cntxt, focus, nc))

        focus = cntxt.graph.value(EX.issue9, FOAF.mbox)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesValues(cntxt, focus, nc))

        focus = cntxt.graph.value(EX.issue10, FOAF.mbox)
        cntxt.current_node = ParseNode(nodeSatisfiesValues, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesValues(cntxt, focus, nc))
        self.assertEqual(['Node: <mailto:[email protected]> not in value set:\n'
                          '\t {"values": [{"stem": {"type": "Wildcard"}, "exclusions": [{"...'],
                         self.fail_reasons(cntxt))
    def test_example_1(self):
        from pyshex.shape_expressions_language.p5_4_node_constraints import nodeSatisfiesDataType

        cntxt = setup_context(shex_1, rdf_1)
        nc = cntxt.schema.shapes[0].expression.valueExpr
        focus = cntxt.graph.value(EX.issue1, EX.submittedOn)
        cntxt.current_node = ParseNode(nodeSatisfiesDataType, nc, focus, cntxt)
        self.assertTrue(nodeSatisfiesDataType(cntxt, focus, nc))
        focus = cntxt.graph.value(EX.issue2, EX.submittedOn)
        cntxt.current_node = ParseNode(nodeSatisfiesDataType, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesDataType(cntxt, focus, nc))
        self.assertEqual([
            'Datatype mismatch - expected: http://www.w3.org/2001/XMLSchema#dateTime '
            'actual: http://www.w3.org/2001/XMLSchema#date'
        ], self.fail_reasons(cntxt))

        focus = cntxt.graph.value(EX.issue3b, EX.submittedOn)
        cntxt.current_node = ParseNode(nodeSatisfiesDataType, nc, focus, cntxt)
        self.assertFalse(nodeSatisfiesDataType(cntxt, focus, nc))
        self.assertEqual([
            'Datatype mismatch - expected: http://www.w3.org/2001/XMLSchema#dateTime '
            'actual: http://www.w3.org/2001/XMLSchema#date'
        ], self.fail_reasons(cntxt))
def isValid(cntxt: Context, m: FixedShapeMap) -> Tuple[bool, List[str]]:
    """`5.2 Validation Definition <http://shex.io/shex-semantics/#validation>`_

    The expression isValid(G, m) indicates that for every nodeSelector/shapeLabel pair (n, s) in m, s has a
        corresponding shape expression se and satisfies(n, se, G, m). satisfies is defined below for each form
        of shape expression

    :param cntxt: evaluation context - includes graph and schema
    :param m: list of NodeShape pairs to test
    :return: Success/failure indicator and, if fail, a list of failure reasons
    """
    if not cntxt.is_valid:
        return False, cntxt.error_list
    parse_nodes = []
    for nodeshapepair in m:
        n = nodeshapepair.nodeSelector
        if not isinstance_(n, Node):
            #return False, [f"{n}: Triple patterns are not implemented"]
            return False, [n + ":Triple patterns are not implemented"]
        # The third test below is because the spec asserts that completely empty graphs pass in certain circumstances
        elif not (next(
                cntxt.graph.predicate_objects(nodeshapepair.nodeSelector),
                None) or next(
                    cntxt.graph.subject_predicates(nodeshapepair.nodeSelector),
                    None) or not next(cntxt.graph.triples(
                        (None, None, None)), None)):
            #return False, [f"Focus: {nodeshapepair.nodeSelector} not in graph"]
            return False, [
                "Focus: " + nodeshapepair.nodeSelector + " not in graph"
            ]
        else:
            s = cntxt.shapeExprFor(START if nodeshapepair.shapeLabel is None
                                   or nodeshapepair.shapeLabel is START else
                                   nodeshapepair.shapeLabel)
            cntxt.current_node = ParseNode(satisfies, s, n, cntxt)
            if not s:
                if nodeshapepair.shapeLabel is START or nodeshapepair.shapeLabel is None:
                    cntxt.fail_reason = "START node is not specified or is invalid"
                else:
                    #cntxt.fail_reason = f"Shape: {nodeshapepair.shapeLabel} not found in Schema"
                    cntxt.fail.reason = "Shape: " + nodeshapepair.shapeLabel + " not found in Schema"
                return False, cntxt.process_reasons()
            parse_nodes.append(cntxt.current_node)
            if not satisfies(cntxt, n, s):
                cntxt.current_node.result = False
                return False, cntxt.process_reasons()
            else:
                cntxt.current_node.result = True
    return True, []
Beispiel #14
0
 def wrapper(cntxt: Context, T: RDFGraph, expr: JSGObject) -> bool:
     parent_parse_node = cntxt.current_node
     cntxt.current_node = ParseNode(f, expr, T, cntxt)
     parent_parse_node.nodes.append(cntxt.current_node)
     c = cntxt.debug_context
     c.splus()
     if c.debug:
         c.print(c.i(0, f'--> {f.__name__} {c.d()}'), not newline)
     rval = f(cntxt, T, expr, c)
     if c.debug:
         c.print(c.i(0, f'<-- {f.__name__} {c.d()} {rval}'))
     c.sminus()
     cntxt.current_node.result = rval
     cntxt.current_node = parent_parse_node
     return rval
Beispiel #15
0
 def wrapper(cntxt: Context, n: Node, expr: JSGObject) -> bool:
     parent_parse_node = cntxt.current_node
     cntxt.current_node = ParseNode(f, expr, n, cntxt)
     parent_parse_node.nodes.append(cntxt.current_node)
     c = cntxt.debug_context
     c.splus()
     if c.debug and not skip_trace(expr):
         c.print(
             c.i(
                 0, '--> ' + f.__name__ + ' ' + c.d() + ' node: ' +
                 cntxt.n3_mapper.n3(n)), not newline)
     rval = f(cntxt, n, expr, c)
     if c.debug and not skip_trace(expr):
         c.print(
             c.i(
                 0, '<-- ' + f.__name__ + ' ' + c.d() + ' node: ' +
                 cntxt.n3_mapper.n3(n) + ':' + rval))
     c.sminus()
     cntxt.current_node.set_result(rval)
     cntxt.current_node = parent_parse_node
     return rval