Search in sources :

Example 1 with SparqlComponent

use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.

the class ConstraintComponents method parseSparqlConstraintComponents.

/**
 * Stage 1 : find all Constraint Components
 */
public static ConstraintComponents parseSparqlConstraintComponents(Graph shapesGraph) {
    ConstraintComponents x = new ConstraintComponents();
    // SHACL.SPARQLConstraintComponent is not a subclass of SHACL.ConstraintComponent
    // SHACL.SPARQLConstraintComponent is an instance of SHACL.ConstraintComponent.
    // Need to process by actual property present.
    G.allNodesOfTypeRDFS(shapesGraph, SHACL.ConstraintComponent).forEach(sccNode -> {
        SparqlComponent c = sparqlConstraintComponent(shapesGraph, sccNode);
        if (c != null) {
            for (Parameter p : c.getParams()) {
                x.paramPathToComponents.put(p.getParameterPath(), c);
                x.parameters.add(p);
            }
        }
    });
    return x;
}
Also used : SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) Parameter(org.apache.jena.shacl.engine.Parameter)

Example 2 with SparqlComponent

use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.

the class ConstraintComponents method sparqlConstraintComponent.

/**
 * This handles all ConstraintComponents; only SPARQL ones ar currently supported.
 */
private static SparqlComponent sparqlConstraintComponent(Graph shapesGraph, Node constraintComponentNode) {
    if (SHACL.JSConstraintComponent.equals(constraintComponentNode))
        return null;
    List<Parameter> params = Parameters.parseParameters(shapesGraph, constraintComponentNode);
    // 6.2.3 Validators
    // 
    // For every supported shape type (i.e., property shape or node shape) the constraint component
    // declares a suitable validator. For a given constraint, a validator is selected from the
    // constraint component using the following rules, in order:
    // 
    // 1. For node shapes, use one of the values of sh:nodeValidator, if present.
    // 2. For property shapes, use one of the values of sh:propertyValidator, if present.
    // 3. Otherwise, use one of the values of sh:validator.
    // 
    // If no suitable validator can be found, a SHACL-SPARQL processor ignores the constraint.
    // 
    // SHACL-SPARQL includes two types of validators, based on SPARQL SELECT (for sh:nodeValidator
    // and sh:propertyValidator) or SPARQL ASK queries (for sh:validator).
    SparqlComponent x = possibleValidator(shapesGraph, constraintComponentNode, SHACL.nodeValidator, params);
    if (x == null)
        x = possibleValidator(shapesGraph, constraintComponentNode, SHACL.propertyValidator, params);
    if (x == null)
        x = possibleValidator(shapesGraph, constraintComponentNode, SHACL.validator, params);
    return x;
}
Also used : SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) Parameter(org.apache.jena.shacl.engine.Parameter)

Example 3 with SparqlComponent

use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.

the class ConstraintComponents method possibleSparqlValidator.

private static SparqlComponent possibleSparqlValidator(Graph shapesGraph, Node valNode, List<Parameter> params, /* for reporting */
Node constraintComponentNode) {
    // Check for SHACL-JS
    Node xJSFunctionName = G.getZeroOrOneSP(shapesGraph, valNode, SHACL.jsFunctionName);
    if (xJSFunctionName != null)
        Log.warn(ConstraintComponents.class, "Found javascript validator - ignored (JavaScript not currently supported)");
    // One of sh:select or sh:ask.
    Node xSelect = G.getZeroOrOneSP(shapesGraph, valNode, SHACL.select);
    Node xAsk = G.getZeroOrOneSP(shapesGraph, valNode, SHACL.ask);
    if (xSelect == null && xAsk == null)
        return null;
    if (xSelect != null && xAsk != null)
        throw new ShaclParseException("SparqlConstraintComponent: Multiple SPARQL queries: " + displayStr(constraintComponentNode));
    String prefixes = ShLib.prefixes(shapesGraph, valNode);
    String queryString = firstNonNull(xSelect, xAsk).getLiteralLexicalForm().trim();
    String message = asString(G.getZeroOrOneSP(shapesGraph, valNode, SHACL.message));
    if (!prefixes.isEmpty())
        queryString = prefixes + "\n" + queryString;
    boolean isSelect = (xSelect != null);
    SparqlComponent cs = SparqlComponent.constraintComponent(constraintComponentNode, queryString, params, message);
    if (cs.getQuery().isSelectType() != isSelect)
        throw new ShaclParseException("Query type does not match property");
    return cs;
}
Also used : Node(org.apache.jena.graph.Node) SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent)

Example 4 with SparqlComponent

use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.

the class TargetExtensions method parseSPARQLTargetType.

// SPARQL-based target types.
public static TargetExtensions parseSPARQLTargetType(Graph shapesGraph) {
    TargetExtensions x = new TargetExtensions();
    G.allNodesOfTypeRDFS(shapesGraph, SHACL.SPARQLTargetType).forEach(sttNode -> {
        SparqlComponent c = sparqlTargetType(shapesGraph, sttNode);
        if (c != null) {
            for (Parameter p : c.getParams()) {
                x.paramPathToComponents.put(p.getParameterPath(), c);
                x.parameters.add(p);
            }
        }
    });
    return x;
}
Also used : SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) Parameter(org.apache.jena.shacl.engine.Parameter)

Example 5 with SparqlComponent

use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.

the class TargetOps method focusTargetExt.

public static Collection<Node> focusTargetExt(Graph data, Target target) {
    Graph shapesGraph = Objects.requireNonNull(target.getShapesGraph());
    Node targetArg = target.getObject();
    if (G.hasOneSP(shapesGraph, targetArg, SHACL.select)) {
        // SPARQL-based target -- sh:target [ sh:select ]
        Query query = ShLib.extractSPARQLQuery(shapesGraph, targetArg);
        if (!query.isSelectType())
            throw new ShaclException("Not a SELECT query");
        DatasetGraph dsg = DatasetGraphFactory.wrap(data);
        QueryExecution qExec = QueryExecutionFactory.create(query, dsg);
        return EvalSparql.evalSparqlOneVar(qExec);
    }
    if (G.isOfType(shapesGraph, targetArg, SHACL.Target)) {
        // Now find the type.
        Node type;
        List<Node> types = G.typesOfNodeAsList(shapesGraph, targetArg);
        if (types.size() == 1)
            // It passed the G.isOfType test.
            type = CollectionUtils.oneElt(types);
        else {
            Set<Node> allClasses = G.subClasses(shapesGraph, SHACL.Target);
            // Find any(first) in allClasses
            Optional<Node> x = types.stream().filter(t -> allClasses.contains(t)).findFirst();
            type = x.orElseThrow();
        }
        try {
            // This is also available via the Shapes object.
            // Maybe attach to the target as it is created.
            // But this is at the point of deciding focus nodes so called
            // one (per target shape) not every validation of a focus node.
            SparqlComponent sparqlComponent = TargetExtensions.sparqlTargetType(shapesGraph, type);
            return EvalSparql.evalSparqlComponent(data, target.getObject(), sparqlComponent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    throw new ShaclException("Unknown target extension");
}
Also used : CollectionUtils(org.apache.jena.atlas.lib.CollectionUtils) DatasetGraphFactory(org.apache.jena.sparql.core.DatasetGraphFactory) java.util(java.util) C(org.apache.jena.shacl.sys.C) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) SHACL(org.apache.jena.shacl.vocabulary.SHACL) SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) Triple(org.apache.jena.graph.Triple) G(org.apache.jena.riot.other.G) Graph(org.apache.jena.graph.Graph) QueryExecutionFactory(org.apache.jena.query.QueryExecutionFactory) ShLib(org.apache.jena.shacl.lib.ShLib) ShaclException(org.apache.jena.shacl.ShaclException) QueryExecution(org.apache.jena.query.QueryExecution) Node(org.apache.jena.graph.Node) EvalSparql(org.apache.jena.shacl.validation.EvalSparql) Query(org.apache.jena.query.Query) NodeFormatter(org.apache.jena.riot.out.NodeFormatter) TargetExtensions(org.apache.jena.shacl.parser.TargetExtensions) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) Query(org.apache.jena.query.Query) Node(org.apache.jena.graph.Node) SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) ShaclException(org.apache.jena.shacl.ShaclException) QueryExecution(org.apache.jena.query.QueryExecution) ShaclException(org.apache.jena.shacl.ShaclException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

SparqlComponent (org.apache.jena.shacl.engine.constraint.SparqlComponent)6 Parameter (org.apache.jena.shacl.engine.Parameter)4 Node (org.apache.jena.graph.Node)3 java.util (java.util)1 CollectionUtils (org.apache.jena.atlas.lib.CollectionUtils)1 Graph (org.apache.jena.graph.Graph)1 Triple (org.apache.jena.graph.Triple)1 Query (org.apache.jena.query.Query)1 QueryExecution (org.apache.jena.query.QueryExecution)1 QueryExecutionFactory (org.apache.jena.query.QueryExecutionFactory)1 G (org.apache.jena.riot.other.G)1 NodeFormatter (org.apache.jena.riot.out.NodeFormatter)1 ShaclException (org.apache.jena.shacl.ShaclException)1 ConstraintComponentSPARQL (org.apache.jena.shacl.engine.constraint.ConstraintComponentSPARQL)1 ShLib (org.apache.jena.shacl.lib.ShLib)1 TargetExtensions (org.apache.jena.shacl.parser.TargetExtensions)1 C (org.apache.jena.shacl.sys.C)1 EvalSparql (org.apache.jena.shacl.validation.EvalSparql)1 SHACL (org.apache.jena.shacl.vocabulary.SHACL)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1