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;
}
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;
}
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);
}
}
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;
}
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();
}
}
Aggregations