Search in sources :

Example 1 with StmtIterator

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);
    }
}
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 2 with StmtIterator

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);
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) TransitiveReasoner(org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) Resource(org.apache.jena.rdf.model.Resource) InfModel(org.apache.jena.rdf.model.InfModel)

Example 3 with StmtIterator

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();
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) Store(org.apache.jena.sdb.Store)

Example 4 with StmtIterator

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()));
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model)

Example 5 with StmtIterator

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) });
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement)

Aggregations

StmtIterator (org.apache.jena.rdf.model.StmtIterator)14 Statement (org.apache.jena.rdf.model.Statement)7 Model (org.apache.jena.rdf.model.Model)6 Resource (org.apache.jena.rdf.model.Resource)5 OntModel (org.apache.jena.ontology.OntModel)2 InfModel (org.apache.jena.rdf.model.InfModel)2 Property (org.apache.jena.rdf.model.Property)2 ThreadInfo (java.lang.management.ThreadInfo)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 ExecutorService (java.util.concurrent.ExecutorService)1 OntClass (org.apache.jena.ontology.OntClass)1 OntResource (org.apache.jena.ontology.OntResource)1 Reasoner (org.apache.jena.reasoner.Reasoner)1 TransitiveReasoner (org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner)1 Store (org.apache.jena.sdb.Store)1 JenaException (org.apache.jena.shared.JenaException)1 ExtendedIterator (org.apache.jena.util.iterator.ExtendedIterator)1