Exemple #1
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
Exemple #2
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
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, []