Search in sources :

Example 6 with SparqlComponent

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

the class ConstraintComponents method processShape.

/**
 * Stage 2 :
 * Build all the Constraints for Validators for this shape.
 * Must have all the required parameters (else ignore).
 */
public static List<Constraint> processShape(Graph shapesGraph, ConstraintComponents components, Shape shape) {
    List<Constraint> constraints = new ArrayList<>();
    // Only add set of requires once, not once per parameter.
    Set<Set<Node>> seen = new HashSet<>();
    for (Parameter param : components.parameters) {
        if (param.isOptional())
            continue;
        // The parameter's property node
        Node paramPath = param.getParameterPath();
        // Does the shape use the parameter?
        boolean b = G.contains(shapesGraph, shape.getShapeNode(), paramPath, null);
        if (!b)
            continue;
        // Alternative approach
        // // Find all shapes uses the parameter.
        // List<Node> shapes = G1.find(shapesGraph, null, paramPath, null).mapWith(Triple::getSubject).toList();
        Node shNode = shape.getShapeNode();
        // All components with this parameter.
        Collection<SparqlComponent> sccs = components.paramPathToComponents.get(paramPath);
        // Which shapes conform (have all required parameters)?
        // And we have not seen before for this shape?
        sccs.forEach(scc -> {
            List<Node> required = scc.getRequiredParameters();
            // Check not seen.
            Set<Node> x = Set.copyOf(required);
            if (seen.contains(x)) {
                return;
            }
            seen.add(x);
            if (Parameters.doesShapeHaveAllParameters(shapesGraph, shape.getShapeNode(), required)) {
                // shape -> parameters
                // Is this a cross product and can it be avoided?
                Multimap<Parameter, Node> parameterValues = constraintParameterValues(shapesGraph, shNode, scc);
                if (parameterValues.keySet().size() > 1) {
                    Map<Parameter, Node> parameterMap = new HashMap<>();
                    parameterValues.asMap().forEach((p, values) -> {
                        if (values.size() > 1)
                            throw new ShaclParseException("Multiple values for parameter " + p + " in constraint with multiple parameters");
                    });
                }
                ConstraintComponentSPARQL constraintComponentSPARQL = new ConstraintComponentSPARQL(scc, parameterValues);
                constraints.add(constraintComponentSPARQL);
            } else {
            // Incomplete.
            // System.out.println("shape: no: "+shNode);
            }
        });
    }
    return constraints;
}
Also used : Node(org.apache.jena.graph.Node) SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) ConstraintComponentSPARQL(org.apache.jena.shacl.engine.constraint.ConstraintComponentSPARQL) Parameter(org.apache.jena.shacl.engine.Parameter)

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