Search in sources :

Example 1 with RDFTerm

use of org.apache.commons.rdf.api.RDFTerm in project trellis by trellis-ldp.

the class JenaIOServiceTest method testUpdate.

@Test
void testUpdate() {
    final Graph graph = rdf.createGraph();
    getTriples().forEach(graph::add);
    assertEquals(3L, graph.size(), "Incorrect graph size!");
    service.update(graph, "DELETE WHERE { ?s <http://purl.org/dc/terms/title> ?o }", SPARQL_UPDATE, "test:info");
    assertEquals(2L, graph.size(), "Incorrect graph size, post update!");
    service.update(graph, "INSERT { " + "<> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}", SPARQL_UPDATE, "trellis:data/resource");
    assertEquals(3L, graph.size(), "Incorrect graph size, after adding triple!");
    service.update(graph, "DELETE WHERE { ?s ?p ?o };" + "INSERT { <> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}", SPARQL_UPDATE, "trellis:data/");
    assertEquals(1L, graph.size(), "Incorrect graph size after removing triples!");
    assertEquals("<trellis:data/>", graph.stream().findFirst().map(Triple::getSubject).map(RDFTerm::ntriplesString).get(), "Incorrect graph subject from updates!");
}
Also used : Graph(org.apache.commons.rdf.api.Graph) Factory.createDefaultGraph(org.apache.jena.graph.Factory.createDefaultGraph) RDFTerm(org.apache.commons.rdf.api.RDFTerm) Test(org.junit.jupiter.api.Test)

Example 2 with RDFTerm

use of org.apache.commons.rdf.api.RDFTerm in project trellis by trellis-ldp.

the class TriplestoreResourceService method initialize.

/**
 * This code is equivalent to the SPARQL queries below.
 *
 * <pre><code>
 * SELECT ?object WHERE {
 *   GRAPH trellis:PreferServerManaged { IDENTIFIER rdf:type ?object }
 * }
 * </code></pre>
 *
 * <pre><code>
 * INSERT DATA {
 *   GRAPH trellis:PreferServerManaged {
 *     IDENTIFIER rdf:type ldp:Container ;
 *                dc:modified "NOW"^^xsd:dateTime }
 *   GRAPH IDENTIFIER?ext=audit {
 *     IDENTIFIER prov:wasGeneratedBy [
 *       rdf:type prov:Activity , as:Create ;
 *       prov:wasAssociatedWith trellis:AdministorAgent ;
 *       prov:atTime "TIME"^^xsd:dateTime ] }
 *   GRAPH IDENTIFIER?ext=acl {
 *     IDENTIFIER acl:mode acl.Read , acl:Write , acl:Control ;
 *       acl:agentClass foaf:Agent ;
 *       acl:accessTo IDENTIFIER }
 * }
 *
 * </code></pre>
 */
@PostConstruct
public void initialize() {
    extensions = extensionGraphConfig.map(TriplestoreResourceService::buildExtensionMap).orElseGet(() -> Map.of(ACL_EXT, PreferAccessControl));
    supplier = idService.getSupplier();
    final IRI root = rdf.createIRI(TRELLIS_DATA_PREFIX);
    final Query q = new Query();
    q.setQuerySelectType();
    q.addResultVar(OBJECT);
    final ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(triple(toJena(root), toJena(type), OBJECT));
    final ElementNamedGraph ng = new ElementNamedGraph(toJena(PreferServerManaged), epb);
    final ElementGroup elg = new ElementGroup();
    elg.addElement(ng);
    q.setQueryPattern(elg);
    final Stream.Builder<RDFTerm> builder = builder();
    rdfConnection.querySelect(q, qs -> builder.accept(getObject(qs)));
    if (!builder.build().findFirst().isPresent()) {
        final Literal time = rdf.createLiteral(now().toString(), XSD.dateTime);
        final IRI auth = rdf.createIRI(TRELLIS_DATA_PREFIX + "#auth");
        final UpdateRequest update = new UpdateRequest();
        final QuadDataAcc sink = new QuadDataAcc();
        sink.addQuad(new Quad(toJena(PreferServerManaged), triple(toJena(root), toJena(type), toJena(LDP.BasicContainer))));
        sink.addQuad(new Quad(toJena(PreferServerManaged), triple(toJena(root), toJena(DC.modified), toJena(time))));
        sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.mode), toJena(ACL.Read))));
        sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.mode), toJena(ACL.Write))));
        sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.mode), toJena(ACL.Control))));
        sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.agentClass), toJena(FOAF.Agent))));
        sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.accessTo), toJena(root))));
        update.add(new UpdateDataInsert(sink));
        rdfConnection.update(update);
    }
    LOGGER.info("Initialized Trellis Triplestore Resource Service");
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.jena.sparql.core.Quad) Query(org.apache.jena.query.Query) UpdateRequest(org.apache.jena.update.UpdateRequest) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) Literal(org.apache.commons.rdf.api.Literal) Stream(java.util.stream.Stream) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) PostConstruct(javax.annotation.PostConstruct)

Example 3 with RDFTerm

use of org.apache.commons.rdf.api.RDFTerm in project minerva by geneontology.

the class ShexValidator method getAllMatchedShapes.

public Set<String> getAllMatchedShapes(RDFTerm value, Typing typing) {
    Set<Label> all_shapes_in_schema = getAllShapesInSchema();
    Set<String> obj_matched_shapes = new HashSet<String>();
    for (Label target_shape_label : all_shapes_in_schema) {
        Pair<RDFTerm, Label> p = new Pair<RDFTerm, Label>(value, target_shape_label);
        Status r = typing.getStatusMap().get(p);
        if (r.equals(Status.CONFORMANT)) {
            obj_matched_shapes.add(getCurie(target_shape_label.stringValue()));
        }
    }
    return obj_matched_shapes;
}
Also used : Status(fr.inria.lille.shexjava.validation.Status) Label(fr.inria.lille.shexjava.schema.Label) RDFTerm(org.apache.commons.rdf.api.RDFTerm) HashSet(java.util.HashSet) Pair(fr.inria.lille.shexjava.util.Pair)

Example 4 with RDFTerm

use of org.apache.commons.rdf.api.RDFTerm in project minerva by geneontology.

the class ShexValidator method runShapeMapValidationWithRecursiveSingleNodeValidation.

public ShexValidationReport runShapeMapValidationWithRecursiveSingleNodeValidation(Model test_model, boolean stream_output) throws Exception {
    ShexValidationReport r = new ShexValidationReport();
    JenaRDF jr = new JenaRDF();
    // this shex implementation likes to use the commons JenaRDF interface, nothing exciting here
    JenaGraph shexy_graph = jr.asGraph(test_model);
    // recursive only checks the focus node against the chosen shape.
    RecursiveValidationWithMemorization shex_model_validator = new RecursiveValidationWithMemorization(schema, shexy_graph);
    // for each shape in the query map (e.g. MF, BP, CC, etc.)
    boolean all_good = true;
    Map<Resource, Set<String>> node_s_shapes = getShapesToTestForEachResource(test_model);
    for (Resource focus_node_resource : node_s_shapes.keySet()) {
        Set<String> shape_nodes = node_s_shapes.get(focus_node_resource);
        for (String shapelabel : shape_nodes) {
            Label shape_label = new Label(rdfFactory.createIRI(shapelabel));
            if (focus_node_resource == null) {
                System.out.println("null focus node for shape " + shape_label);
                continue;
            }
            // check for use of properties not defined for this shape (okay if OPEN, not if CLOSED)
            Typing typing = validateNodeWithTimeout(shex_model_validator, focus_node_resource, shape_label);
            if (typing != null) {
                Set<ShexViolation> extra_prop_violations = checkForExtraProperties(focus_node_resource, test_model, shape_label, typing);
                if (extra_prop_violations != null && !extra_prop_violations.isEmpty()) {
                    for (Violation v : extra_prop_violations) {
                        r.addViolation(v);
                    }
                    all_good = false;
                }
                // run the validation on the node if possible..
                RDFTerm focus_node = null;
                String focus_node_id = "";
                if (focus_node_resource.isURIResource()) {
                    focus_node = rdfFactory.createIRI(focus_node_resource.getURI());
                    focus_node_id = focus_node_resource.getURI();
                } else {
                    focus_node = rdfFactory.createBlankNode(focus_node_resource.getId().getLabelString());
                    focus_node_id = focus_node_resource.getId().getLabelString();
                }
                // deal with curies for output
                String node = focus_node_id;
                node = getCurie(focus_node_id);
                Status status = typing.getStatus(focus_node, shape_label);
                if (status.equals(Status.CONFORMANT)) {
                    Set<String> shape_ids = r.node_matched_shapes.get(node);
                    if (shape_ids == null) {
                        shape_ids = new HashSet<String>();
                    }
                    shape_ids.add(shapelabel);
                    r.node_matched_shapes.put(node, shape_ids);
                } else if (status.equals(Status.NONCONFORMANT)) {
                    all_good = false;
                    // implementing a start on a generic violation report structure here
                    ShexViolation violation = new ShexViolation(node);
                    ShexExplanation explanation = new ShexExplanation();
                    String shape_curie = getCurie(shapelabel);
                    explanation.setShape(shape_curie);
                    Set<ShexConstraint> unmet_constraints = getUnmetConstraints(focus_node_resource, shape_label, test_model, typing);
                    if (unmet_constraints != null) {
                        for (ShexConstraint constraint : unmet_constraints) {
                            explanation.addConstraint(constraint);
                            violation.addExplanation(explanation);
                        }
                    } else {
                        explanation.setErrorMessage("explanation computation timed out");
                        violation.addExplanation(explanation);
                    }
                    r.addViolation(violation);
                } else if (status.equals(Status.NOTCOMPUTED)) {
                    // if any of these are not computed, there is a problem
                    String error = focus_node_id + " was not tested against " + shapelabel;
                    LOGGER.error(error);
                }
            } else {
                LOGGER.info("shex validation failed for node " + focus_node_resource.getURI());
                all_good = false;
                ShexViolation violation = new ShexViolation(focus_node_resource.getURI());
                ShexExplanation explanation = new ShexExplanation();
                explanation.setErrorMessage("Validating this node was canceled because it took more then " + timeout_mill + " milliseconds");
                String shape_curie = getCurie(shapelabel);
                explanation.setShape(shape_curie);
                violation.addExplanation(explanation);
                r.addViolation(violation);
            }
        }
    }
    if (all_good) {
        r.conformant = true;
    } else {
        r.conformant = false;
    }
    return r;
}
Also used : Status(fr.inria.lille.shexjava.validation.Status) HashSet(java.util.HashSet) Set(java.util.Set) Typing(fr.inria.lille.shexjava.validation.Typing) JenaRDF(org.apache.commons.rdf.jena.JenaRDF) Label(fr.inria.lille.shexjava.schema.Label) RDFTerm(org.apache.commons.rdf.api.RDFTerm) RecursiveValidationWithMemorization(fr.inria.lille.shexjava.validation.RecursiveValidationWithMemorization) JenaGraph(org.apache.commons.rdf.jena.JenaGraph)

Example 5 with RDFTerm

use of org.apache.commons.rdf.api.RDFTerm in project minerva by geneontology.

the class ShexValidator method getUnmetConstraints.

/**
 * We no there is a problem with the focus node and the shape here.  This tries to figure out the constraints that caused the problem and provide some explanation.
 * @param focus_node
 * @param shape_label
 * @param model
 * @param typing
 * @return
 * @throws IOException
 */
private Set<ShexConstraint> getUnmetConstraints(Resource focus_node, Label shape_label, Model model, Typing typing) throws IOException {
    Set<ShexConstraint> unmet_constraints = new HashSet<ShexConstraint>();
    Set<String> node_types = getNodeTypes(model, focus_node.getURI());
    Map<String, Set<String>> expected_property_ranges = shape_expected_property_ranges.get(shape_label);
    // check for assertions with properties in the target shape
    for (String prop_uri : expected_property_ranges.keySet()) {
        Property prop = model.getProperty(prop_uri);
        Interval cardinality = shape_expected_property_cardinality.get(shape_label).get(prop_uri);
        // checking on objects of this property for the problem node.
        int n_objects = 0;
        for (StmtIterator i = focus_node.listProperties(prop); i.hasNext(); ) {
            while (i.hasNext()) {
                n_objects++;
                RDFNode obj = i.nextStatement().getObject();
                // check the computed shapes for this individual
                if (!obj.isResource()) {
                    continue;
                // no checks on literal values at this time
                } else if (prop_uri.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") && obj.asResource().getURI().equals("http://www.w3.org/2002/07/owl#NamedIndividual")) {
                    // ignore type owl individual
                    continue;
                }
                RDFTerm range_obj = rdfFactory.createIRI(obj.asResource().getURI());
                // does it hit any allowable shapes?
                boolean good = false;
                // TODO many property ranges are MISSING from previous step
                // e.g. any OR will not show up here.
                Set<String> expected_ranges = expected_property_ranges.get(prop_uri);
                for (String target_shape_uri : expected_ranges) {
                    if (target_shape_uri.equals(".")) {
                        // anything is fine
                        good = true;
                    // break;
                    } else if (target_shape_uri.trim().equals("<http://www.w3.org/2001/XMLSchema#string>")) {
                        // ignore syntax type checking for now
                        good = true;
                    // break;
                    }
                    Label target_shape_label = new Label(rdfFactory.createIRI(target_shape_uri));
                    // Typing typing = validateNodeWithTimeout(shex_model_validator, obj.asResource(), shape_label);
                    if (typing != null) {
                        // capture the result
                        // Typing shape_test = shex_model_validator.getTyping();
                        // Pair<RDFTerm, Label> p = new Pair<RDFTerm, Label>(range_obj, target_shape_label);
                        // Status r = shape_test.getStatusMap().get(p);
                        Status r = typing.getStatus(range_obj, target_shape_label);
                        if (r != null && r.equals(Status.CONFORMANT)) {
                            good = true;
                        // break;
                        }
                    } else {
                        good = false;
                    }
                }
                if (!good) {
                    // add violated range constraint to explanation
                    if (obj.isURIResource()) {
                        String object = obj.toString();
                        Set<String> object_types = getNodeTypes(model, obj.toString());
                        String property = prop.toString();
                        object = getCurie(object);
                        property = getCurie(property);
                        Set<String> expected = new HashSet<String>();
                        for (String e : expected_property_ranges.get(prop_uri)) {
                            String curie_e = getCurie(e);
                            expected.add(curie_e);
                        }
                        ShexConstraint constraint = new ShexConstraint(object, property, expected, node_types, object_types);
                        // return all shapes that are matched by this node for explanation
                        Set<String> obj_matched_shapes = getAllMatchedShapes(range_obj, typing);
                        constraint.setMatched_range_shapes(obj_matched_shapes);
                        unmet_constraints.add(constraint);
                    } else {
                        ShexConstraint constraint = new ShexConstraint(obj.toString(), getCurie(prop.toString()), null, node_types, null);
                        // return all shapes that are matched by this node for explanation
                        Set<String> obj_matched_shapes = getAllMatchedShapes(range_obj, typing);
                        constraint.setMatched_range_shapes(obj_matched_shapes);
                        unmet_constraints.add(constraint);
                    }
                }
            }
        }
        // check for cardinality violations
        if (!prop_uri.contentEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) {
            // skip types - should always allow multiple..
            if (!cardinality.contains(n_objects)) {
                System.out.println("cardinality violation!");
                System.out.println("problem node " + focus_node);
                System.out.println("prop " + prop);
                System.out.println("Intended Interval " + cardinality.toString());
                System.out.println("Actual " + n_objects);
                ShexConstraint constraint = new ShexConstraint(getCurie(prop.toString()), cardinality.toString(), n_objects);
                unmet_constraints.add(constraint);
            }
        }
    }
    return unmet_constraints;
}
Also used : Status(fr.inria.lille.shexjava.validation.Status) HashSet(java.util.HashSet) Set(java.util.Set) Label(fr.inria.lille.shexjava.schema.Label) RDFTerm(org.apache.commons.rdf.api.RDFTerm) HashSet(java.util.HashSet) Interval(fr.inria.lille.shexjava.util.Interval)

Aggregations

RDFTerm (org.apache.commons.rdf.api.RDFTerm)37 IRI (org.apache.commons.rdf.api.IRI)18 Test (org.junit.Test)18 Quad (org.apache.commons.rdf.api.Quad)14 BlankNodeOrIRI (org.apache.commons.rdf.api.BlankNodeOrIRI)11 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)7 Label (fr.inria.lille.shexjava.schema.Label)5 Status (fr.inria.lille.shexjava.validation.Status)5 Set (java.util.Set)5 BlankNode (org.apache.commons.rdf.api.BlankNode)5 Graph (org.apache.commons.rdf.api.Graph)5 Literal (org.apache.commons.rdf.api.Literal)5 Typing (fr.inria.lille.shexjava.validation.Typing)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 PrintStream (java.io.PrintStream)4 Triple (org.apache.commons.rdf.api.Triple)4 Stream (java.util.stream.Stream)3 SimpleRDFTerm (org.apache.commons.rdf.simple.SimpleRDF.SimpleRDFTerm)3