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