Search in sources :

Example 11 with OntClass

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

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

the class LocalOntology method renderHierarchy.

protected static void renderHierarchy(PrintStream out, OntClass cls, List<Object> occurs, int depth) {
    renderClassDescription(out, cls, depth);
    out.println();
    // recurse to the next level down
    if (cls.canAs(OntClass.class) && !occurs.contains(cls)) {
        for (Iterator<?> i = cls.listSubClasses(true); i.hasNext(); ) {
            OntClass sub = (OntClass) i.next();
            // we push this expression on the occurs list before we recurse
            occurs.add(cls);
            renderHierarchy(out, sub, occurs, depth + 1);
            occurs.remove(cls);
        }
        for (Iterator<?> i = cls.listInstances(); i.hasNext(); ) {
            Individual individual = (Individual) i.next();
            renderURI(out, individual.getModel(), individual.getURI());
            out.print(" [");
            for (Iterator<?> j = individual.listLabels(null); j.hasNext(); ) {
                out.print(((Literal) j.next()).getString() + ", ");
            }
            out.print("] ");
            out.println();
        }
    }
}
Also used : Individual(org.apache.jena.ontology.Individual) Literal(org.apache.jena.rdf.model.Literal) OntClass(org.apache.jena.ontology.OntClass)

Example 13 with OntClass

use of org.apache.jena.ontology.OntClass in project jena by apache.

the class TestReasoners method testTransitiveCycleBug.

/**
 * Cycle bug in transitive reasoner
 */
public void testTransitiveCycleBug() {
    Model m = FileManager.getInternal().loadModelInternal("file:testing/reasoners/bugs/unbroken.n3");
    OntModel om = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF, m);
    OntClass rootClass = om.getOntClass(RDFS.Resource.getURI());
    Resource c = m.getResource("c");
    Set<OntClass> direct = rootClass.listSubClasses(true).toSet();
    assertFalse(direct.contains(c));
}
Also used : OntModel(org.apache.jena.ontology.OntModel) InfModel(org.apache.jena.rdf.model.InfModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass)

Example 14 with OntClass

use of org.apache.jena.ontology.OntClass in project jena by apache.

the class TestOntTools method testNamedHierarchyRoots3.

/**
 * Test for no dups in the returned list
 */
public void testNamedHierarchyRoots3() {
    OntClass anon0 = m_model.createClass();
    OntClass anon1 = m_model.createClass();
    anon0.addSubClass(m_a);
    anon1.addSubClass(m_a);
    // only a is root
    m_a.addSubClass(m_b);
    m_a.addSubClass(m_c);
    m_a.addSubClass(m_d);
    m_a.addSubClass(m_e);
    m_a.addSubClass(m_f);
    m_a.addSubClass(m_g);
    List<OntClass> nhr = OntTools.namedHierarchyRoots(m_model);
    assertEquals(1, nhr.size());
    assertTrue(nhr.contains(m_a));
}
Also used : OntClass(org.apache.jena.ontology.OntClass)

Example 15 with OntClass

use of org.apache.jena.ontology.OntClass in project jena by apache.

the class TestOntTools method testNamedHierarchyRoots4.

/**
 * Test for indirect route to a non-root node
 */
public void testNamedHierarchyRoots4() {
    OntClass anon0 = m_model.createClass();
    OntClass anon1 = m_model.createClass();
    anon0.addSubClass(m_a);
    anon1.addSubClass(m_b);
    // only a is root, because b is a subclass of a
    // even though b is a sub-class of an anon root
    m_a.addSubClass(m_b);
    m_a.addSubClass(m_c);
    m_a.addSubClass(m_d);
    m_a.addSubClass(m_e);
    m_a.addSubClass(m_f);
    m_a.addSubClass(m_g);
    List<OntClass> nhr = OntTools.namedHierarchyRoots(m_model);
    assertEquals(1, nhr.size());
    assertTrue(nhr.contains(m_a));
}
Also used : OntClass(org.apache.jena.ontology.OntClass)

Aggregations

OntClass (org.apache.jena.ontology.OntClass)26 Individual (org.apache.jena.ontology.Individual)16 ArrayList (java.util.ArrayList)5 Literal (org.apache.jena.rdf.model.Literal)4 OntResource (org.apache.jena.ontology.OntResource)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 ThreadInfo (java.lang.management.ThreadInfo)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 ExecutorService (java.util.concurrent.ExecutorService)1 OntModel (org.apache.jena.ontology.OntModel)1 InfModel (org.apache.jena.rdf.model.InfModel)1 Model (org.apache.jena.rdf.model.Model)1 Resource (org.apache.jena.rdf.model.Resource)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1 JenaException (org.apache.jena.shared.JenaException)1 ExtendedIterator (org.apache.jena.util.iterator.ExtendedIterator)1