From 11af0a3de9d65b567c1bdd7ca518d3dc461abedd Mon Sep 17 00:00:00 2001 From: Paul Cuddihy Date: Thu, 11 Nov 2021 10:34:49 -0500 Subject: [PATCH] Fix bug in loading connection with multiple model graphs --- .../semtk/ontologyTools/OntologyInfo.java | 59 +++++++++++-------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/ontologyTools/OntologyInfo.java b/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/ontologyTools/OntologyInfo.java index 99228d1bb..92f213e9c 100644 --- a/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/ontologyTools/OntologyInfo.java +++ b/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/ontologyTools/OntologyInfo.java @@ -214,14 +214,24 @@ public void loadSparqlConnection(SparqlQueryClientConfig clientConfig, SparqlCon } /** - * add a new class to the ontology info object. this includes information on super/sub classes - * being added to a hash of the known entities. + **/ - public void addClass(OntologyClass oClass){ + + /** + * add a new class to the ontology info object. this includes information on super/sub classes + * being added to a hash of the known entities. + * @param oClass + * @throws Exception - if class already exists + */ + public void addClass(OntologyClass oClass) throws Exception { String classnameStr = oClass.getNameString(false); // get the full name of the class and do not strip URI info. this.connHash.clear(); - this.classHash.put(classnameStr, oClass); // silently overwrites if the class is already present. + if (this.classHash.containsKey(classnameStr)) { + throw new Exception("Internal error: class already exists in ontology. Cannot re-add it: " + classnameStr); + } + this.classHash.put(classnameStr, oClass); + // store info on the related subclasses ArrayList superClassNames = oClass.getParentNameStrings(false); // get the parents. there may be more than one. // spin through the list and find the ones that need to be added. @@ -1138,26 +1148,25 @@ public void loadSuperSubProperties(String subPropNames[], String superPropNames[ /** * process the results of the query to get all of the sub- and super-class query and loads * them into the OntologyInfo object. + * + * for unknown historical reasons, the subclass is added along with names of superclasses + * but the superclasses are added later. + * **/ - public void loadSuperSubClasses(String xList[], String yList[]) throws Exception{ + public void loadSuperSubClasses(String subList[], String superList[]) throws Exception{ - HashMap tempClasses = new HashMap(); - - for (int i=0; i < xList.length; i++) { - // check for the existence of the current class. - if(!tempClasses.containsKey(xList[i])){ - OntologyClass c = new OntologyClass(xList[i], null); - tempClasses.put(xList[i], c); - } - // get the current class and add the parent. - OntologyClass c = tempClasses.get(xList[i]); - c.addParentName(yList[i]); - } + for (int i=0; i < subList.length; i++) { + String subClassUri = subList[i]; + String superClassUri = superList[i]; - // call addClass() on the temp list. - for(String keyName : tempClasses.keySet()){ - OntologyClass oClass = tempClasses.get(keyName); - this.addClass(oClass); + // create subclass if it is missing + if (!this.containsClass(subClassUri)) { + OntologyClass subClass = new OntologyClass(subClassUri, null); + this.addClass(subClass); + } + + // get the subclass and add the parent. + this.getClass(subClassUri).addParentName(superClassUri); } } @@ -1188,9 +1197,10 @@ private static String getTopLevelClassQuery(String graphName, String domain){ public void loadTopLevelClasses(String xList[]) throws Exception{ for (int i=0; i < xList.length; i++) { - // add it. - OntologyClass c = new OntologyClass(xList[i], null); - this.addClass(c); + if (!this.containsClass(xList[i])) { + OntologyClass c = new OntologyClass(xList[i], null); + this.addClass(c); + } } } @@ -2316,6 +2326,7 @@ public JSONObject toJson () { // classPropertyRangeList for (String c : this.classHash.keySet()) { + ArrayList propList = this.classHash.get(c).getProperties(); for (int i=0; i < propList.size(); i++) { OntologyProperty oProp = propList.get(i);