Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classtree generation code #444

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
<artifactId>owlapi-distribution</artifactId>
<version>5.1.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20201115</version>
</dependency>

<dependency>
<groupId>com.googlecode.json-simple </groupId>
Expand Down
279 changes: 279 additions & 0 deletions src/main/java/widoco/ClassTreeParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
package widoco;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONObject;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubPropertyAxiom;

public class ClassTreeParser {

static JSONObject getClassHierarchyTree( final OWLOntology model) {
Set<String> seenResources=new TreeSet<String>();
Set<String> workedResources=new TreeSet<String>();
JSONObject result=new JSONObject();
result.put("classes",new JSONArray());
result.put("objprop",new JSONArray());
result.put("dataprop",new JSONArray());
result.put("annoprop",new JSONArray());
result.put("individuals",new JSONArray());
System.out.println("SeenResources: "+seenResources);
System.out.println("WorkedResources: "+workedResources);
Iterator<OWLSubClassOfAxiom> sciter = model.axioms(AxiomType.SUBCLASS_OF).iterator();
System.out.println("Hasnext? "+sciter.hasNext());
while (sciter.hasNext())
{
OWLSubClassOfAxiom subClasse=sciter.next();
if (subClasse.getSuperClass() instanceof OWLClass
&& subClasse.getSubClass() instanceof OWLClass)
{
System.out.println(subClasse.getSuperClass().toString()+" - "+subClasse.getSubClass().toString());
if(!seenResources.contains(subClasse.getSubClass().toString()) || (seenResources.contains(subClasse.getSubClass().toString()) && !workedResources.contains(subClasse.getSubClass().toString())) ){
seenResources.add(subClasse.getSuperClass().toString());
seenResources.add(subClasse.getSubClass().toString());
JSONObject treeelem=new JSONObject();
treeelem.put("id",subClasse.getSubClass().toString().replace("<", "").replace(">", ""));
treeelem.put("parent",subClasse.getSuperClass().toString().replace("<", "").replace(">", ""));
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/Classes.gif");
if(subClasse.getSubClass().toString().contains("#")) {
treeelem.put("text",subClasse.getSubClass().toString().substring(subClasse.getSubClass().toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",subClasse.getSubClass().toString().substring(subClasse.getSubClass().toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("classes").put(treeelem);
workedResources.add(subClasse.getSubClass().toString());
}
}
}
System.out.println("SeenResources: "+seenResources);
System.out.println("WorkedResources: "+workedResources);
for(String res:seenResources) {
if(!workedResources.contains(res)) {
System.out.println("Missing Class: "+res);
JSONObject treeelem=new JSONObject();
treeelem.put("id",res.replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/Classes.gif");
if(res.toString().contains("#")) {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("classes").put(treeelem);
}
}
Iterator<OWLClass> classpropiter = model.classesInSignature().iterator();
System.out.println("Hasnext? "+sciter.hasNext());
while (classpropiter.hasNext()) {
OWLClass c=classpropiter.next();
if(!seenResources.contains(c.toString())) {
JSONObject treeelem=new JSONObject();
treeelem.put("id",c.toString().replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/Classes.gif");
if(c.toString().contains("#")) {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("dataprop").put(treeelem);
}
}
//System.out.println(result.getJSONArray("classes").toString(2));
seenResources.clear();
workedResources.clear();
for (OWLClass c : model.getClassesInSignature()) {
for (OWLNamedIndividual i :c.getIndividualsInSignature()) {
JSONObject treeelem=new JSONObject();
treeelem.put("id",i.toString().replace("<", "").replace(">", ""));
treeelem.put("parent",c.toString().replace("<", "").replace(">", ""));
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/OWLIndividual.gif");
if(i.toString().contains("#")) {
treeelem.put("text",i.toString().substring(i.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",i.toString().substring(i.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("individuals").put(treeelem);
}
}
Iterator<OWLSubObjectPropertyOfAxiom> sciter2 = model.axioms(AxiomType.SUB_OBJECT_PROPERTY).iterator();
System.out.println("Hasnext? "+sciter.hasNext());
while (sciter2.hasNext())
{
OWLSubObjectPropertyOfAxiom subClasse = sciter2.next();
if(!seenResources.contains(subClasse.getSuperProperty().toString()) || (seenResources.contains(subClasse.getSubProperty().toString()) && !workedResources.contains(subClasse.getSubProperty().toString()))){
seenResources.add(subClasse.getSuperProperty().toString());
seenResources.add(subClasse.getSubProperty().toString());
JSONObject treeelem=new JSONObject();
treeelem.put("id",subClasse.getSubProperty().toString().replace("<", "").replace(">", ""));
treeelem.put("parent",subClasse.getSuperProperty().toString().replace("<", "").replace(">", ""));
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/OWLObjectProperty.gif");
if(subClasse.getSubProperty().toString().contains("#")) {
treeelem.put("text",subClasse.getSubProperty().toString().substring(subClasse.getSubProperty().toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",subClasse.getSubProperty().toString().substring(subClasse.getSubProperty().toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("objprop").put(treeelem);
workedResources.add(subClasse.getSubProperty().toString());
}
}
System.out.println(seenResources);
System.out.println(workedResources);
for(String res:seenResources) {
if(!workedResources.contains(res)) {
System.out.println("Missing ObjProp: "+res);
JSONObject treeelem=new JSONObject();
treeelem.put("id",res.replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/OWLObjectProperty.gif");
if(res.toString().contains("#")) {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("objprop").put(treeelem);
}
}
Iterator<OWLObjectProperty> objpropiter = model.objectPropertiesInSignature().iterator();
System.out.println("Hasnext? "+sciter.hasNext());
while (objpropiter.hasNext()) {
OWLObjectProperty c=objpropiter.next();
if(!seenResources.contains(c.toString())) {
JSONObject treeelem=new JSONObject();
treeelem.put("id",c.toString().replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/OWLObjectProperty.gif");
if(c.toString().contains("#")) {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("objprop").put(treeelem);
}
}
seenResources.clear();
workedResources.clear();
for (final OWLSubPropertyAxiom subClasse : model.getAxioms(AxiomType.SUB_DATA_PROPERTY))
{
if(!seenResources.contains(subClasse.getSuperProperty().toString()) || (seenResources.contains(subClasse.getSubProperty().toString()) && !workedResources.contains(subClasse.getSubProperty().toString()))){
seenResources.add(subClasse.getSuperProperty().toString());
seenResources.add(subClasse.getSubProperty().toString());
JSONObject treeelem=new JSONObject();
treeelem.put("id",subClasse.getSubProperty().toString().replace("<", "").replace(">", ""));
treeelem.put("parent",subClasse.getSuperProperty().toString().replace("<", "").replace(">", ""));
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/OWLDatatypeProperty.gif");
if(subClasse.getSubProperty().toString().contains("#")) {
treeelem.put("text",subClasse.getSubProperty().toString().substring(subClasse.getSubProperty().toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",subClasse.getSubProperty().toString().substring(subClasse.getSubProperty().toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("dataprop").put(treeelem);
workedResources.add(subClasse.getSubProperty().toString());
}
}
System.out.println("SeenResources: "+seenResources);
System.out.println("WorkedResources: "+workedResources);
for(String res:seenResources) {
if(!workedResources.contains(res)) {
System.out.println("Missing Dataprop: "+res);
JSONObject treeelem=new JSONObject();
treeelem.put("id",res.replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/OWLDatatypeProperty.gif");
if(res.toString().contains("#")) {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("dataprop").put(treeelem);
}
}
Iterator<OWLDataProperty> datapropiter = model.dataPropertiesInSignature().iterator();
System.out.println("Hasnext? "+sciter.hasNext());
while (datapropiter.hasNext()) {
OWLDataProperty c=datapropiter.next();
if(!seenResources.contains(c.toString())) {
JSONObject treeelem=new JSONObject();
treeelem.put("id",c.toString().replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/OWLDatatypeProperty.gif");
if(c.toString().contains("#")) {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("dataprop").put(treeelem);
}
}
seenResources.clear();
workedResources.clear();
for (final OWLSubAnnotationPropertyOfAxiom subClasse : model.getAxioms(AxiomType.SUB_ANNOTATION_PROPERTY_OF))
{
if(!seenResources.contains(subClasse.getSuperProperty().toString()) || (seenResources.contains(subClasse.getSubProperty().toString()) && !workedResources.contains(subClasse.getSubProperty().toString()))){
seenResources.add(subClasse.getSuperProperty().toString());
seenResources.add(subClasse.getSubProperty().toString());
JSONObject treeelem=new JSONObject();
treeelem.put("id",subClasse.getSubProperty().toString().replace("<", "").replace(">", ""));
treeelem.put("parent",subClasse.getSuperProperty().toString().replace("<", "").replace(">", ""));
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/Annotation.gif");
if(subClasse.getSubProperty().toString().contains("#")) {
treeelem.put("text",subClasse.getSubProperty().toString().substring(subClasse.getSubProperty().toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",subClasse.getSubProperty().toString().substring(subClasse.getSubProperty().toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("annoprop").put(treeelem);
workedResources.add(subClasse.getSubProperty().toString());
}
}
System.out.println("SeenResources: "+seenResources);
System.out.println("WorkedResources: "+workedResources);
for(String res:seenResources) {
if(!workedResources.contains(res)) {
System.out.println("Missing Annoprop: "+res);
JSONObject treeelem=new JSONObject();
treeelem.put("id",res.replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/Annotation.gif");
if(res.toString().contains("#")) {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",res.toString().substring(res.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("annoprop").put(treeelem);
}
}
Iterator<OWLAnnotationProperty> annopropiter = model.annotationPropertiesInSignature().iterator();
System.out.println("Hasnext? "+sciter.hasNext());
while (annopropiter.hasNext()) {
OWLAnnotationProperty c=annopropiter.next();
if(!seenResources.contains(c.toString())) {
JSONObject treeelem=new JSONObject();
treeelem.put("id",c.toString().replace("<", "").replace(">", ""));
treeelem.put("parent","#");
treeelem.put("icon","https://raw.githubusercontent.com/protegeproject/protege/master/protege-editor-owl/src/main/resources/Annotation.gif");
if(c.toString().contains("#")) {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('#')+1).replace("<", "").replace(">", ""));
}else {
treeelem.put("text",c.toString().substring(c.toString().lastIndexOf('/')+1).replace("<", "").replace(">", ""));
}
result.getJSONArray("annoprop").put(treeelem);
}
}
seenResources.clear();
workedResources.clear();
return result;
}

}
2 changes: 1 addition & 1 deletion src/main/java/widoco/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public static String getIndexDocument(String resourcesFolderName, Configuration

// JSON-LD snippet
document += getJSONLDSnippet(c);
document += "<script src=\"" + resourcesFolderName + "/jquery.js\"></script> \n" + "<script src=\""
document += "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.9/jstree.min.js\"></script><script src=\"" + resourcesFolderName + "/classtree.js\"></script><script src=\"" + resourcesFolderName + "/jquery.js\"></script> \n" + "<script src=\""
+ resourcesFolderName + "/marked.min.js\"></script> \n" + " <script> \n" + "function loadHash() {\n"
+ " jQuery(\".markdown\").each(function(el){jQuery(this).after(marked(jQuery(this).text())).remove()});\n"
+ " var hash = location.hash;\n" + " if($(hash).offset()!=null){\n"
Expand Down
1 change: 1 addition & 0 deletions src/main/java/widoco/CreateResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static void generateDocumentation(String outFolder, Configuration c, File
// serialize the model in different serializations.
OWLOntologyManager om = c.getMainOntology().getOWLAPIOntologyManager();
OWLOntology o = c.getMainOntology().getOWLAPIModel();
WidocoUtils.writeClassTreeJSON(o, folderOut + File.separator + "resources"+File.separator+"classtree.js");
WidocoUtils.writeModel(om, o, new RDFXMLDocumentFormat(), folderOut + File.separator + "ontology.xml");
WidocoUtils.writeModel(om, o, new TurtleDocumentFormat(), folderOut + File.separator + "ontology.ttl");
WidocoUtils.writeModel(om, o, new NTriplesDocumentFormat(), folderOut + File.separator + "ontology.nt");
Expand Down
Loading