use of org.apache.jena.rdf.model.StmtIterator 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.rdf.model.StmtIterator in project jena by apache.
the class TestReasoners method testTransitiveBindReuse.
/**
* The reasoner contract for bind(data) is not quite precise. It allows for
* reasoners which have state so that reusing the same reasoner on a second data
* model might lead to interference. This in fact used to happen with the transitive
* reasoner. This is a test to check that the transitive reasoner state reuse has been fixed at source.
*/
public void testTransitiveBindReuse() {
Reasoner r = ReasonerRegistry.getTransitiveReasoner();
InfModel om1 = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel());
Resource c1 = om1.createResource(PrintUtil.egNS + "Class1");
Resource c2 = om1.createResource(PrintUtil.egNS + "Class2");
Resource c3 = om1.createResource(PrintUtil.egNS + "Class3");
om1.add(c1, RDFS.subClassOf, c2);
om1.add(c2, RDFS.subClassOf, c3);
om1.prepare();
assertFalse(om1.isEmpty());
InfModel om2 = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel());
StmtIterator si = om2.listStatements();
boolean ok = !si.hasNext();
si.close();
assertTrue("Transitive reasoner state leak", ok);
}
use of org.apache.jena.rdf.model.StmtIterator in project jena by apache.
the class ExModelSDB method main.
public static void main(String... argv) {
Store store = StoreFactory.create("sdb.ttl");
Model model = SDBFactory.connectDefaultModel(store);
StmtIterator sIter = model.listStatements();
for (; sIter.hasNext(); ) {
Statement stmt = sIter.nextStatement();
System.out.println(stmt);
}
sIter.close();
store.close();
}
use of org.apache.jena.rdf.model.StmtIterator in project jena by apache.
the class TestModelSetOperations method testUnion.
public void testUnion() {
ModelHelper.modelAdd(model, "a P b; w R x");
ModelHelper.modelAdd(model2, "w R x; y S z");
final Model um = model.union(model2);
Assert.assertFalse(model.containsAll(model2));
Assert.assertFalse(model2.containsAll(model));
Assert.assertTrue(um.containsAll(model));
Assert.assertTrue(um.containsAll(model2));
for (final StmtIterator it = um.listStatements(); it.hasNext(); ) {
final Statement s = it.nextStatement();
Assert.assertTrue(model.contains(s) || model2.contains(s));
}
for (final StmtIterator it = model.listStatements(); it.hasNext(); ) {
Assert.assertTrue(um.contains(it.nextStatement()));
}
for (final StmtIterator it = model2.listStatements(); it.hasNext(); ) {
Assert.assertTrue(um.contains(it.nextStatement()));
}
Assert.assertTrue(um.containsAll(model.listStatements()));
Assert.assertTrue(um.containsAll(model2.listStatements()));
}
use of org.apache.jena.rdf.model.StmtIterator in project jena by apache.
the class TestModelEvents method testDeleteStatementIterator.
public void testDeleteStatementIterator() {
model.register(SL);
final Statement[] sa = ModelHelper.statements(model, "x R y; a P b; x R y");
final StmtIterator it = asIterator(sa);
model.remove(it);
SL.assertHas(new Object[] { "removeIterator", Arrays.asList(sa) });
}
Aggregations