Search in sources :

Example 1 with ShaclException

use of org.apache.jena.shacl.ShaclException in project jena by apache.

the class EvalSparql method evalSparqlComponent.

public static Collection<Node> evalSparqlComponent(Graph data, Node node, SparqlComponent sparqlComponent) {
    checkForRequiredParams(data, node, sparqlComponent);
    Query query = sparqlComponent.getQuery();
    if (!query.isSelectType())
        throw new ShaclException("Not a SELECT query");
    // Parameters
    if (USE_QueryTransformOps) {
        // Done with QueryTransformOps.transform
        DatasetGraph dsg = DatasetGraphFactory.wrap(data);
        Map<Var, Node> substitutions = parametersToSyntaxSubstitutions(data, node, sparqlComponent.getParams());
        Query query2 = QueryTransformOps.transform(query, substitutions);
        try (QueryExecution qExec = QueryExecutionFactory.create(query2, dsg)) {
            return evalSparqlOneVar(qExec);
        }
    } else {
        // Done with pre-binding.
        Model model = ModelFactory.createModelForGraph(data);
        QuerySolutionMap qsm = parametersToPreBinding(model, node, sparqlComponent.getParams());
        try (QueryExecution qExec = QueryExecution.create().query(query).model(model).initialBinding(qsm).build()) {
            return evalSparqlOneVar(qExec);
        }
    }
}
Also used : Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Model(org.apache.jena.rdf.model.Model) ShaclException(org.apache.jena.shacl.ShaclException) QueryExecution(org.apache.jena.query.QueryExecution) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap)

Example 2 with ShaclException

use of org.apache.jena.shacl.ShaclException 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)

Example 3 with ShaclException

use of org.apache.jena.shacl.ShaclException in project jena by apache.

the class EvalSparql method evalSparqlOneVar.

public static Collection<Node> evalSparqlOneVar(QueryExecution qExec) {
    List<Var> vars = qExec.getQuery().getProjectVars();
    if (vars.size() != 1)
        throw new ShaclException("Except SELECT query with one output variable.");
    Var var = vars.get(0);
    return Iter.iter(qExec.execSelect()).map(row -> row.get(var.getVarName()).asNode()).toSet();
}
Also used : QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) DatasetGraphFactory(org.apache.jena.sparql.core.DatasetGraphFactory) QueryTransformOps(org.apache.jena.sparql.syntax.syntaxtransform.QueryTransformOps) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Collection(java.util.Collection) SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) HashMap(java.util.HashMap) G(org.apache.jena.riot.other.G) ModelUtils(org.apache.jena.sparql.util.ModelUtils) Graph(org.apache.jena.graph.Graph) QueryExecutionFactory(org.apache.jena.query.QueryExecutionFactory) Model(org.apache.jena.rdf.model.Model) List(java.util.List) ShaclException(org.apache.jena.shacl.ShaclException) Parameter(org.apache.jena.shacl.engine.Parameter) QueryExecution(org.apache.jena.query.QueryExecution) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Map(java.util.Map) Query(org.apache.jena.query.Query) Iter(org.apache.jena.atlas.iterator.Iter) ModelFactory(org.apache.jena.rdf.model.ModelFactory) Parameters(org.apache.jena.shacl.parser.Parameters) Var(org.apache.jena.sparql.core.Var) ShaclException(org.apache.jena.shacl.ShaclException)

Example 4 with ShaclException

use of org.apache.jena.shacl.ShaclException in project jena by apache.

the class ShaclCompactParser method rPropertyShape.

// propertyShape: Using a new blank node ?property, produce a triple ?shape
// sh:property ?property. Produce a triple ?property sh:path ?path where
// ?path is the result of path. Use ?property as context shape for
// propertyCount and propertyOr.
protected void rPropertyShape(Path parsedPath) {
    Node path = pathToNode(parsedPath);
    Node b = newBlankNode("propertyShape");
    beginPropertyShape(b);
    triple(currentTripleAcc(), currentNodeShape(), SHACL.property, currentPropertyShape());
    if (path == null)
        throw new ShaclException("Internal error: no path");
    triple(currentTripleAcc(), currentPropertyShape(), SHACL.path, path);
}
Also used : Node(org.apache.jena.graph.Node) ShaclException(org.apache.jena.shacl.ShaclException)

Aggregations

Node (org.apache.jena.graph.Node)4 ShaclException (org.apache.jena.shacl.ShaclException)4 Query (org.apache.jena.query.Query)3 QueryExecution (org.apache.jena.query.QueryExecution)3 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)3 Graph (org.apache.jena.graph.Graph)2 QueryExecutionFactory (org.apache.jena.query.QueryExecutionFactory)2 QuerySolutionMap (org.apache.jena.query.QuerySolutionMap)2 Model (org.apache.jena.rdf.model.Model)2 G (org.apache.jena.riot.other.G)2 SparqlComponent (org.apache.jena.shacl.engine.constraint.SparqlComponent)2 DatasetGraphFactory (org.apache.jena.sparql.core.DatasetGraphFactory)2 Var (org.apache.jena.sparql.core.Var)2 java.util (java.util)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Iter (org.apache.jena.atlas.iterator.Iter)1 CollectionUtils (org.apache.jena.atlas.lib.CollectionUtils)1