Search in sources :

Example 1 with P_Link

use of org.apache.jena.sparql.path.P_Link in project jena by apache.

the class SparqlValidation method validateMap.

/**
 * return true if the validation is "conforms"
 */
private static boolean validateMap(ValidationContext vCxt, Graph data, Shape shape, Node focusNode, Path path, Node valueNode, Query _query, Map<Parameter, Node> parameterMap, String violationTemplate, Constraint reportConstraint) {
    Model model = ModelFactory.createModelForGraph(data);
    QueryExecution qExec;
    Query query = _query;
    // If path is not a simple link, rewrite the query.
    if (path != null && !(path instanceof P_Link))
        query = QueryTransformOps.transform(query, new ElementTransformPath(SparqlConstraint.varPath, path));
    if (USE_QueryTransformOps) {
        // Done with QueryTransformOps.transform
        Map<Var, Node> substitutions = parameterMapToSyntaxSubstitutions(parameterMap, focusNode, path);
        if (query.isAskType())
            addSubstition(substitutions, "value", valueNode);
        Query query2 = QueryTransformOps.transform(query, substitutions);
        qExec = QueryExecutionFactory.create(query2, model);
    } else {
        // Done with pre-binding.
        QuerySolutionMap qsm = parameterMapToPreBinding(parameterMap, focusNode, path, model);
        if (query.isAskType())
            qsm.add("value", ModelUtils.convertGraphNodeToRDFNode(valueNode, model));
        qExec = QueryExecution.create().query(query).model(model).initialBinding(qsm).build();
    }
    // ASK validator.
    if (qExec.getQuery().isAskType()) {
        boolean b = qExec.execAsk();
        if (!b) {
            String msg = (violationTemplate == null) ? "SPARQL ASK constraint for " + ShLib.displayStr(valueNode) + " returns false" : substitute(violationTemplate, parameterMap, focusNode, path, valueNode);
            vCxt.reportEntry(msg, shape, focusNode, path, valueNode, reportConstraint);
        }
        return b;
    }
    ResultSet rs = qExec.execSelect();
    if (!rs.hasNext())
        return true;
    while (rs.hasNext()) {
        Binding row = rs.nextBinding();
        Node value = row.get(SparqlConstraint.varValue);
        if (value == null)
            value = valueNode;
        String msg;
        if (violationTemplate == null) {
            if (value != null)
                msg = "SPARQL SELECT constraint for " + ShLib.displayStr(valueNode) + " returns " + ShLib.displayStr(value);
            else
                msg = "SPARQL SELECT constraint for " + ShLib.displayStr(valueNode) + " returns row " + row;
        } else {
            msg = substitute(violationTemplate, row);
        }
        Path rPath = path;
        if (rPath == null) {
            Node qPath = row.get(SparqlConstraint.varPath);
            if (qPath != null)
                rPath = PathFactory.pathLink(qPath);
        }
        vCxt.reportEntry(msg, shape, focusNode, rPath, value, reportConstraint);
    }
    return false;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) TriplePath(org.apache.jena.sparql.core.TriplePath) Path(org.apache.jena.sparql.path.Path) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) P_Link(org.apache.jena.sparql.path.P_Link) Model(org.apache.jena.rdf.model.Model)

Example 2 with P_Link

use of org.apache.jena.sparql.path.P_Link in project jena by apache.

the class WhereClauseTest method testAddOptionalStringWithPath.

@ContractTest
public void testAddOptionalStringWithPath() {
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addOptional("<one>", "<two>/<dos>", "three");
    Path path = new P_Seq(new P_Link(NodeFactory.createURI("two")), new P_Link(NodeFactory.createURI("dos")));
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(NodeFactory.createURI("one"), path, NodeFactory.createLiteral("three"));
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : TriplePath(org.apache.jena.sparql.core.TriplePath) Path(org.apache.jena.sparql.path.Path) P_Seq(org.apache.jena.sparql.path.P_Seq) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) P_Link(org.apache.jena.sparql.path.P_Link) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 3 with P_Link

use of org.apache.jena.sparql.path.P_Link in project jena by apache.

the class WhereQuadHolderTest method namedTest_Var.

@Test
public void namedTest_Var() {
    holder = new WhereQuadHolder(new PrefixHandler());
    Node g = NodeFactory.createURI("g");
    List<Triple> tLst = new ArrayList<Triple>();
    Node s = NodeFactory.createURI("s");
    Node p = NodeFactory.createVariable("p");
    Node o = NodeFactory.createURI("o");
    WhereHandler whereHandler = new WhereHandler();
    whereHandler.addWhere(new TriplePath(s, new P_Link(p), o));
    holder.addGraph(g, whereHandler);
    Node s2 = NodeFactory.createURI("s2");
    Node p2 = NodeFactory.createURI("p2");
    Node o2 = NodeFactory.createURI("o2");
    whereHandler = new WhereHandler();
    whereHandler.addWhere(new TriplePath(s2, new P_Link(p2), o2));
    holder.addGraph(g, whereHandler);
    tLst.add(new Triple(s, p2, o));
    tLst.add(new Triple(s2, p2, o2));
    Map<Var, Node> map = new HashMap<>();
    map.put(Var.alloc(p), p2);
    holder.setValues(map);
    List<Quad> lst = holder.getQuads().toList();
    assertEquals(2, lst.size());
    assertEquals(new Quad(g, tLst.get(0)), lst.get(0));
    assertEquals(new Quad(g, tLst.get(1)), lst.get(1));
}
Also used : Quad(org.apache.jena.sparql.core.Quad) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) TriplePath(org.apache.jena.sparql.core.TriplePath) P_Link(org.apache.jena.sparql.path.P_Link) Triple(org.apache.jena.graph.Triple) WhereHandler(org.apache.jena.arq.querybuilder.handlers.WhereHandler) Test(org.junit.Test)

Example 4 with P_Link

use of org.apache.jena.sparql.path.P_Link in project jena by apache.

the class WhereQuadHolderTest method mixedTest_Var.

@Test
public void mixedTest_Var() {
    holder = new WhereQuadHolder(new PrefixHandler());
    List<Triple> tLst = new ArrayList<Triple>();
    Node s = NodeFactory.createURI("s");
    Node p = NodeFactory.createVariable("p");
    Node o = NodeFactory.createURI("o");
    holder.addWhere(new TriplePath(s, new P_Link(p), o));
    Node g = NodeFactory.createURI("g");
    Node s2 = NodeFactory.createURI("s2");
    Node p2 = NodeFactory.createURI("p2");
    Node o2 = NodeFactory.createURI("o2");
    tLst.add(new Triple(s, p2, o));
    tLst.add(new Triple(s2, p2, o2));
    WhereHandler whereHandler = new WhereHandler();
    whereHandler.addWhere(new TriplePath(s2, new P_Link(p2), o2));
    holder.addGraph(g, whereHandler);
    Map<Var, Node> map = new HashMap<>();
    map.put(Var.alloc(p), p2);
    holder.setValues(map);
    List<Quad> lst = holder.getQuads().toList();
    assertEquals(2, lst.size());
    assertEquals(new Quad(Quad.defaultGraphNodeGenerated, tLst.get(0)), lst.get(0));
    assertEquals(new Quad(g, tLst.get(1)), lst.get(1));
}
Also used : Quad(org.apache.jena.sparql.core.Quad) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) TriplePath(org.apache.jena.sparql.core.TriplePath) P_Link(org.apache.jena.sparql.path.P_Link) Triple(org.apache.jena.graph.Triple) WhereHandler(org.apache.jena.arq.querybuilder.handlers.WhereHandler) Test(org.junit.Test)

Example 5 with P_Link

use of org.apache.jena.sparql.path.P_Link in project jena by apache.

the class WhereQuadHolderTest method mixedTest.

@Test
public void mixedTest() {
    holder = new WhereQuadHolder(new PrefixHandler());
    List<Triple> tLst = new ArrayList<Triple>();
    Node s = NodeFactory.createURI("s");
    Node p = NodeFactory.createURI("p");
    Node o = NodeFactory.createURI("o");
    tLst.add(new Triple(s, p, o));
    holder.addWhere(new TriplePath(s, new P_Link(p), o));
    Node g = NodeFactory.createURI("g");
    Node s2 = NodeFactory.createURI("s2");
    Node p2 = NodeFactory.createURI("p2");
    Node o2 = NodeFactory.createURI("o2");
    tLst.add(new Triple(s2, p2, o2));
    WhereHandler whereHandler = new WhereHandler();
    whereHandler.addWhere(new TriplePath(s2, new P_Link(p2), o2));
    holder.addGraph(g, whereHandler);
    List<Quad> lst = holder.getQuads().toList();
    assertEquals(2, lst.size());
    assertEquals(new Quad(Quad.defaultGraphNodeGenerated, tLst.get(0)), lst.get(0));
    assertEquals(new Quad(g, tLst.get(1)), lst.get(1));
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) WhereHandler(org.apache.jena.arq.querybuilder.handlers.WhereHandler) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) TriplePath(org.apache.jena.sparql.core.TriplePath) P_Link(org.apache.jena.sparql.path.P_Link) Test(org.junit.Test)

Aggregations

P_Link (org.apache.jena.sparql.path.P_Link)13 Node (org.apache.jena.graph.Node)11 TriplePath (org.apache.jena.sparql.core.TriplePath)11 Test (org.junit.Test)9 Triple (org.apache.jena.graph.Triple)8 ArrayList (java.util.ArrayList)6 Quad (org.apache.jena.sparql.core.Quad)6 Var (org.apache.jena.sparql.core.Var)5 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)4 WhereHandler (org.apache.jena.arq.querybuilder.handlers.WhereHandler)4 Path (org.apache.jena.sparql.path.Path)4 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)4 HashMap (java.util.HashMap)3 ContractTest (org.xenei.junit.contract.ContractTest)3 ElementOptional (org.apache.jena.sparql.syntax.ElementOptional)2 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)1 FrontsTriple (org.apache.jena.graph.FrontsTriple)1 Query (org.apache.jena.query.Query)1 Model (org.apache.jena.rdf.model.Model)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1