Search in sources :

Example 1 with OntResource

use of org.apache.jena.ontology.OntResource in project incubator-sdap-mudrod by apache.

the class LocalOntology method subclasses.

/**
 * Retrieve all subclasses of entity(ies) hashed to searchTerm
 * @param entitySearchTerm a query (keywords) for which to obtain
 * subclasses.
 * @return an {@link java.util.Iterator} containing the subclass as Strings.
 */
@Override
public Iterator<String> subclasses(String entitySearchTerm) {
    Map<OntResource, String> classMap = retrieve(entitySearchTerm);
    Map<String, String> subclasses = new HashMap<>();
    Iterator<OntResource> iter = classMap.keySet().iterator();
    while (iter.hasNext()) {
        OntResource resource = iter.next();
        if (resource instanceof OntClass) {
            // need to pass the .listSubClasses(true) boolean parameter.
            for (Iterator<?> i = ((OntClass) resource).listSubClasses(); i.hasNext(); ) {
                OntResource subclass = (OntResource) i.next();
                for (Iterator<?> j = subclass.listLabels(null); j.hasNext(); ) {
                    Literal l = (Literal) j.next();
                    subclasses.put(l.toString(), "1");
                }
            }
            // get individuals
            for (Iterator<?> i = ((OntClass) resource).listInstances(); i.hasNext(); ) {
                OntResource subclass = (OntResource) i.next();
                for (Iterator<?> j = subclass.listLabels(null); j.hasNext(); ) {
                    Literal l = (Literal) j.next();
                    subclasses.put(l.toString(), "1");
                }
            }
        } else if (resource instanceof Individual) {
            for (Iterator<?> i = resource.listSameAs(); i.hasNext(); ) {
                OntResource subclass = (OntResource) i.next();
                for (Iterator<?> j = subclass.listLabels(null); j.hasNext(); ) {
                    Literal l = (Literal) j.next();
                    subclasses.put(l.toString(), "1");
                }
            }
        }
    }
    return subclasses.keySet().iterator();
}
Also used : HashMap(java.util.HashMap) Individual(org.apache.jena.ontology.Individual) Literal(org.apache.jena.rdf.model.Literal) Iterator(java.util.Iterator) OntResource(org.apache.jena.ontology.OntResource) OntClass(org.apache.jena.ontology.OntClass)

Example 2 with OntResource

use of org.apache.jena.ontology.OntResource 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 3 with OntResource

use of org.apache.jena.ontology.OntResource in project incubator-sdap-mudrod by apache.

the class LocalOntology method synonyms.

/**
 * Retreives synonyms for an given phrase if the phrase
 * is present in the ontology
 * @param queryKeyPhrase an input string representing a phrase
 * for which we wish to obtain synonyms.
 * @return an {@link java.util.Iterator} containing synonyms string tokens
 * or an empty if no synonyms exist for the given queryKeyPhrase.
 */
@Override
public Iterator synonyms(String queryKeyPhrase) {
    Map<?, ?> classMap = retrieve(queryKeyPhrase);
    Map<Object, Object> synonyms = new HashMap<>();
    Iterator<?> iter = classMap.keySet().iterator();
    while (iter.hasNext()) {
        OntResource resource = (OntResource) iter.next();
        // listLabels
        for (Iterator<?> i = resource.listLabels(null); i.hasNext(); ) {
            Literal l = (Literal) i.next();
            synonyms.put(l.toString(), "1");
        }
        if (resource instanceof Individual) {
            // get all individuals same as this one
            for (Iterator<?> i = resource.listSameAs(); i.hasNext(); ) {
                Individual individual = (Individual) i.next();
                // add labels
                for (Iterator<?> j = individual.listLabels(null); j.hasNext(); ) {
                    Literal l = (Literal) i.next();
                    synonyms.put(l.toString(), "1");
                }
            }
        } else if (resource instanceof OntClass) {
            // list equivalent classes
            for (Iterator<?> i = ((OntClass) resource).listEquivalentClasses(); i.hasNext(); ) {
                OntClass equivClass = (OntClass) i.next();
                // add labels
                for (Iterator<?> j = equivClass.listLabels(null); j.hasNext(); ) {
                    Literal l = (Literal) j.next();
                    synonyms.put(l.toString(), "1");
                }
            }
        }
    }
    return synonyms.keySet().iterator();
}
Also used : HashMap(java.util.HashMap) Individual(org.apache.jena.ontology.Individual) Literal(org.apache.jena.rdf.model.Literal) Iterator(java.util.Iterator) OntResource(org.apache.jena.ontology.OntResource) OntClass(org.apache.jena.ontology.OntClass)

Aggregations

OntClass (org.apache.jena.ontology.OntClass)3 OntResource (org.apache.jena.ontology.OntResource)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Individual (org.apache.jena.ontology.Individual)2 Literal (org.apache.jena.rdf.model.Literal)2 ThreadInfo (java.lang.management.ThreadInfo)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 ExecutorService (java.util.concurrent.ExecutorService)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1 JenaException (org.apache.jena.shared.JenaException)1 ExtendedIterator (org.apache.jena.util.iterator.ExtendedIterator)1