Search in sources :

Example 1 with ExtendedIterator

use of org.apache.jena.util.iterator.ExtendedIterator in project jena by apache.

the class StageMatchTriple method accessTriple.

private static Iterator<Binding> accessTriple(Binding binding, Graph graph, Triple pattern, Predicate<Triple> filter, ExecutionContext execCxt) {
    Node s = substituteFlat(pattern.getSubject(), binding);
    Node p = substituteFlat(pattern.getPredicate(), binding);
    Node o = substituteFlat(pattern.getObject(), binding);
    BindingBuilder resultsBuilder = Binding.builder(binding);
    Node s2 = tripleNode(s);
    Node p2 = tripleNode(p);
    Node o2 = tripleNode(o);
    ExtendedIterator<Triple> graphIter = graph.find(s2, p2, o2);
    ExtendedIterator<Binding> iter = graphIter.mapWith(r -> mapper(resultsBuilder, s, p, o, r)).filterDrop(Objects::isNull);
    return iter;
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Objects(java.util.Objects) Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Predicate(java.util.function.Predicate) Triple(org.apache.jena.graph.Triple) ExtendedIterator(org.apache.jena.util.iterator.ExtendedIterator) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Graph(org.apache.jena.graph.Graph) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Node(org.apache.jena.graph.Node) Objects(java.util.Objects)

Example 2 with ExtendedIterator

use of org.apache.jena.util.iterator.ExtendedIterator in project jena by apache.

the class SolverRX3 method rdfStarTripleSub.

private static Iterator<Binding> rdfStarTripleSub(Binding input, Triple xPattern, ExecutionContext execCxt) {
    Triple tPattern = Substitute.substitute(xPattern, input);
    Node s = nodeTopLevel(tPattern.getSubject());
    Node p = nodeTopLevel(tPattern.getPredicate());
    Node o = nodeTopLevel(tPattern.getObject());
    Graph graph = execCxt.getActiveGraph();
    ExtendedIterator<Triple> graphIter = graph.find(s, p, o);
    ExtendedIterator<Binding> matched = graphIter.mapWith(tData -> matchTriple(input, tData, tPattern)).filterDrop(Objects::isNull);
    return matched;
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) SolverLib.nodeTopLevel(org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel) Triple(org.apache.jena.graph.Triple) SolverLib.tripleHasEmbTripleWithVars(org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Graph(org.apache.jena.graph.Graph) Objects(java.util.Objects) SolverLib.sameTermAs(org.apache.jena.sparql.engine.main.solver.SolverLib.sameTermAs) Substitute(org.apache.jena.sparql.core.Substitute) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ExtendedIterator(org.apache.jena.util.iterator.ExtendedIterator) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) Objects(java.util.Objects)

Example 3 with ExtendedIterator

use of org.apache.jena.util.iterator.ExtendedIterator in project jena by apache.

the class ConcurrencyTest method doTestConcurrency.

private void doTestConcurrency(final OntModel model) throws InterruptedException {
    // initialize the model
    final String NS = PrintUtil.egNS;
    model.enterCriticalSection(Lock.WRITE);
    final OntClass Top = model.createClass(NS + "Top");
    for (int i = 0; i < MODEL_SIZE; i++) {
        OntClass C = model.createClass(NS + "C" + i);
        Top.addSubClass(C);
        model.createIndividual(NS + "i" + i, C);
    }
    model.leaveCriticalSection();
    class QueryExecutingRunnable implements Runnable {

        @Override
        @SuppressWarnings("unchecked")
        public void run() {
            // Keep this thread running until the specified duration has expired
            long runStartedAt = System.currentTimeMillis();
            while (System.currentTimeMillis() - runStartedAt < TEST_LENGTH) {
                Thread.yield();
                model.enterCriticalSection(Lock.READ);
                try {
                    // Iterate over all statements
                    StmtIterator it = model.listStatements();
                    while (it.hasNext()) it.nextStatement();
                    it.close();
                    // Check number of instances of Top class
                    int count = 0;
                    ExtendedIterator<OntResource> ei = (ExtendedIterator<OntResource>) Top.listInstances();
                    while (ei.hasNext()) {
                        ei.next();
                        count++;
                    }
                    ei.close();
                    if (MODEL_SIZE != count) {
                        if (FULL_TEST)
                            System.err.println("Failure - found " + count + " instance, expected " + MODEL_SIZE);
                        throw new JenaException("Failure - found " + count + " instance, expected " + MODEL_SIZE);
                    }
                } finally {
                    model.leaveCriticalSection();
                }
            }
        }
    }
    // Start the threads
    ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
    for (int i = 0; i < NUM_THREADS; ++i) {
        executorService.submit(new QueryExecutingRunnable());
    }
    // Wait for threads to finish
    // this will *not* terminate any threads currently running
    executorService.shutdown();
    Thread.sleep(TEST_LENGTH + 50);
    // Possibly in deadlock, wait a little longer to be sure
    for (int i = 0; i < 50 && !executorService.isTerminated(); i++) {
        Thread.sleep(20);
    }
    if (!executorService.isTerminated()) {
        /* uncomment this block to perform deadlock checking, only on java 1.6 */
        // Check for deadlock
        ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
        long[] ids = tmx.findDeadlockedThreads();
        if (ids != null) {
            ThreadInfo[] infos = tmx.getThreadInfo(ids, true, true);
            System.err.println("*** Deadlocked threads");
            for (ThreadInfo ti : infos) {
                System.err.println("Thread \"" + ti.getThreadName() + "\" id=" + ti.getThreadId() + " " + ti.getThreadState().toString());
                System.err.println("Lock name: " + ti.getLockName() + " owned by \"" + ti.getLockOwnerName() + "\" id=" + ti.getLockOwnerId());
                System.err.println("\nStack trace:");
                for (StackTraceElement st : ti.getStackTrace()) System.err.println("   " + st.getClassName() + "." + st.getMethodName() + " (" + st.getFileName() + ":" + st.getLineNumber() + ")");
                System.err.println();
            }
        }
        Assert.assertTrue("Deadlock detected!", false);
        /* end deadlock block */
        assertTrue("Failed to terminate execution", false);
    }
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) ThreadMXBean(java.lang.management.ThreadMXBean) OntResource(org.apache.jena.ontology.OntResource) JenaException(org.apache.jena.shared.JenaException) ThreadInfo(java.lang.management.ThreadInfo) ExtendedIterator(org.apache.jena.util.iterator.ExtendedIterator) ExecutorService(java.util.concurrent.ExecutorService) OntClass(org.apache.jena.ontology.OntClass)

Example 4 with ExtendedIterator

use of org.apache.jena.util.iterator.ExtendedIterator in project jena by apache.

the class GenericPropertyFunction method findAll.

private QueryIterConcat findAll(Graph graph, Node boundNode, Node unboundNode, Binding binding, boolean isSubjectBound, Node predicate, ExecutionContext execCxt) {
    // Prepare the results.
    Var unboundVar = Var.alloc(unboundNode.getName());
    QueryIterConcat queryIterConcat = new QueryIterConcat(execCxt);
    // Search for both Features and Geometry in the Graph. Reliant upon consistent usage of SpatialObject (which is base class of Feature and Geometry) if present.
    ExtendedIterator<Triple> spatialTriples;
    if (graph.contains(null, RDF.type.asNode(), Geo.SPATIAL_OBJECT_NODE)) {
        spatialTriples = graph.find(null, RDF.type.asNode(), Geo.SPATIAL_OBJECT_NODE);
    } else if (graph.contains(null, RDF.type.asNode(), Geo.FEATURE_NODE) || graph.contains(null, RDF.type.asNode(), Geo.GEOMETRY_NODE)) {
        ExtendedIterator<Triple> featureTriples = graph.find(null, RDF.type.asNode(), Geo.FEATURE_NODE);
        ExtendedIterator<Triple> geometryTriples = graph.find(null, RDF.type.asNode(), Geo.GEOMETRY_NODE);
        spatialTriples = featureTriples.andThen(geometryTriples);
    } else {
        // Check for Geo Predicate Features in the Graph if no GeometryLiterals found.
        spatialTriples = graph.find(null, SpatialExtension.GEO_LAT_NODE, null);
    }
    while (spatialTriples.hasNext()) {
        Triple spatialTriple = spatialTriples.next();
        Node spatialNode = spatialTriple.getSubject();
        Binding newBind = BindingFactory.binding(binding, unboundVar, spatialNode);
        QueryIterator queryIter;
        if (isSubjectBound) {
            queryIter = bothBound(newBind, boundNode, predicate, spatialNode, execCxt);
        } else {
            queryIter = bothBound(newBind, spatialNode, predicate, boundNode, execCxt);
        }
        queryIterConcat.add(queryIter);
    }
    return queryIterConcat;
}
Also used : QueryIterConcat(org.apache.jena.sparql.engine.iterator.QueryIterConcat) Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Var(org.apache.jena.sparql.core.Var) ExtendedIterator(org.apache.jena.util.iterator.ExtendedIterator) Node(org.apache.jena.graph.Node)

Example 5 with ExtendedIterator

use of org.apache.jena.util.iterator.ExtendedIterator in project jena by apache.

the class ShapesParser method accTarget.

private static void accTarget(Collection<Target> acc, Graph shapesGraph, Node shape, TargetType targetType) {
    ExtendedIterator<Triple> iter = shapesGraph.find(shape, targetType.predicate, null);
    Graph graph;
    switch(targetType) {
        // Java 14 has "->"
        case targetExtension:
            graph = shapesGraph;
            break;
        // These do not need access to the shapes graph so don't force holding on the the graph reference.
        case implicitClass:
        case targetClass:
        case targetNode:
        case targetObjectsOf:
        case targetSubjectsOf:
        default:
            graph = null;
            break;
    }
    try {
        iter.mapWith(triple -> Target.create(triple.getSubject(), targetType, triple.getObject(), graph)).forEachRemaining(target -> acc.add(target));
    } finally {
        iter.close();
    }
}
Also used : Triple(org.apache.jena.graph.Triple) java.util(java.util) Imports(org.apache.jena.shacl.Imports) JLogConstraint(org.apache.jena.shacl.engine.constraint.JLogConstraint) G(org.apache.jena.riot.other.G) Graph(org.apache.jena.graph.Graph) ShLib(org.apache.jena.shacl.lib.ShLib) RDFDataException(org.apache.jena.riot.other.RDFDataException) Severity(org.apache.jena.shacl.validation.Severity) Path(org.apache.jena.sparql.path.Path) ShLib.displayStr(org.apache.jena.shacl.lib.ShLib.displayStr) SHACL(org.apache.jena.shacl.vocabulary.SHACL) C.rdfsClass(org.apache.jena.shacl.sys.C.rdfsClass) Triple(org.apache.jena.graph.Triple) ShaclSystem(org.apache.jena.shacl.sys.ShaclSystem) Collectors(java.util.stream.Collectors) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) ShaclPaths(org.apache.jena.shacl.engine.ShaclPaths) NodeConst(org.apache.jena.sparql.graph.NodeConst) Node(org.apache.jena.graph.Node) ShaclPaths.pathToString(org.apache.jena.shacl.engine.ShaclPaths.pathToString) XSDDatatype(org.apache.jena.datatypes.xsd.XSDDatatype) Target(org.apache.jena.shacl.engine.Target) Targets(org.apache.jena.shacl.engine.Targets) Pair(org.apache.jena.atlas.lib.Pair) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) TargetType(org.apache.jena.shacl.engine.TargetType) JenaException(org.apache.jena.shared.JenaException) ExtendedIterator(org.apache.jena.util.iterator.ExtendedIterator) Shapes(org.apache.jena.shacl.Shapes) Graph(org.apache.jena.graph.Graph)

Aggregations

ExtendedIterator (org.apache.jena.util.iterator.ExtendedIterator)6 Node (org.apache.jena.graph.Node)5 Triple (org.apache.jena.graph.Triple)5 Graph (org.apache.jena.graph.Graph)4 Var (org.apache.jena.sparql.core.Var)4 Binding (org.apache.jena.sparql.engine.binding.Binding)4 Iterator (java.util.Iterator)2 Objects (java.util.Objects)2 Iter (org.apache.jena.atlas.iterator.Iter)2 JenaException (org.apache.jena.shared.JenaException)2 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)2 BindingBuilder (org.apache.jena.sparql.engine.binding.BindingBuilder)2 ThreadInfo (java.lang.management.ThreadInfo)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 java.util (java.util)1 ExecutorService (java.util.concurrent.ExecutorService)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)1 Pair (org.apache.jena.atlas.lib.Pair)1