public void AddClass(OntologyClass oClass) { String classNameStr = oClass.GetNameString(false); // get the full name of the class and do not strip the uri info this.connHash.Clear(); this.classHash.Add(classNameStr, oClass); // store info on the related subclasses List <String> superClassNames = oClass.GetParentNameStrings(false); // get the parents. there may be several. // spin through the list and find the ones that need to be added foreach (String scn in superClassNames) { if (!(this.subclassHash.ContainsKey(scn))) { // this superclass was not previously added. List <OntologyClass> scList = new List <OntologyClass>(); scList.Add(oClass); this.subclassHash.Add(scn, scList); } else { // just add this one this.subclassHash[scn].Add(oClass); } } }
/** for a given class, return all parent classes **/ public List <OntologyClass> GetClassParents(OntologyClass currentClass) { List <OntologyClass> retval = new List <OntologyClass>(); // add each parent foreach (String parentNameString in currentClass.GetParentNameStrings(false)) { retval.Add(this.GetClass(parentNameString)); } // return the collection return(retval); }
// -------------------------------------------------------------------------------------- serialize and deserialize json representation. /* the To and From json methods are adapted from the java code in oInfo. the operative mechanism is different as this code does not * have to be compatible with the loading from sparql methods. instead this goes further, taking advantage of the one-hop information * and the sub-class/super-class info buried in the exported json. */ public JsonObject ToJson() { // based on th advanced client json in the java version. // return the advanced client Json format... JsonObject retval = new JsonObject(); // create all the prefix information... Dictionary <String, String> prefixes = new Dictionary <String, String>(); // create the enumeration information JsonArray enumerations = new JsonArray(); foreach (String key in this.enumerationHash.Keys) { // find each and add them as needed. String uri = Utility.Utility.PrefixUri(key, prefixes); JsonObject currEnumeration = new JsonObject(); JsonArray values = new JsonArray(); // get all the values. foreach (String k in this.enumerationHash[key]) // get eh enumerations we care about. { String kUri = Utility.Utility.PrefixUri(k, prefixes); values.Add(JsonValue.CreateStringValue(kUri)); } currEnumeration.Add("fullUri", JsonValue.CreateStringValue(uri)); currEnumeration.Add("enumeration", values); enumerations.Add(currEnumeration); } // create the propertyList information: JsonArray propertyList = new JsonArray(); foreach (String key in this.propertyHash.Keys) { String uri = Utility.Utility.PrefixUri(key, prefixes); JsonObject currProperty = new JsonObject(); JsonArray domain = new JsonArray(); JsonArray range = new JsonArray(); JsonArray labels = new JsonArray(); JsonArray comments = new JsonArray(); OntologyProperty currProp = this.propertyHash[key]; // get the domain: foreach (String oClassKey in this.classHash.Keys) { // get the classes which are in the domain OntologyClass oClass = this.classHash[oClassKey]; if (oClass.GetProperty(key) != null) { // we found one. as a result, this will be prefixed and added. OntologyProperty currPropInstance = oClass.GetProperty(key); String classId = Utility.Utility.PrefixUri(oClassKey, prefixes); domain.Add(JsonValue.CreateStringValue(classId)); } } // get the range. right now, SemTK core only supports a single value for a range, so it may seems silly/wasteful/stupid // to support these as an array but we intend on changing this limitation one day, so better to be prepared. String rangeId = Utility.Utility.PrefixUri(currProp.GetRange().GetFullName(), prefixes); range.Add(JsonValue.CreateStringValue(rangeId)); // add the labels. foreach (String label in currProp.GetAnnotationLabels()) { labels.Add(JsonValue.CreateStringValue(label)); } // add the comments foreach (String comment in currProp.GetAnnotationComments()) { comments.Add(JsonValue.CreateStringValue(comment)); } // add all the subcomponents of the current property currProperty.Add("fullUri", JsonValue.CreateStringValue(uri)); currProperty.Add("domain", domain); currProperty.Add("range", range); currProperty.Add("labels", labels); currProperty.Add("comments", comments); // add to the outgoing list propertyList.Add(currProperty); } // create the classList information. JsonArray classList = new JsonArray(); foreach (String key in this.classHash.Keys) { String uri = Utility.Utility.PrefixUri(key, prefixes); JsonObject currClass = new JsonObject(); OntologyClass oClass = this.classHash[key]; JsonArray labels = new JsonArray(); JsonArray comments = new JsonArray(); JsonArray superClasses = new JsonArray(); JsonArray subClasses = new JsonArray(); JsonArray directConnections = new JsonArray(); // labels foreach (String label in oClass.GetAnnotationLabels()) { labels.Add(JsonValue.CreateStringValue(label)); } // comments foreach (String comment in oClass.GetAnnotationComments()) { comments.Add(JsonValue.CreateStringValue(comment)); } // superclasses foreach (String parent in oClass.GetParentNameStrings(false)) { String prefixedParent = Utility.Utility.PrefixUri(parent, prefixes); superClasses.Add(JsonValue.CreateStringValue(prefixedParent)); } // subclasses if (this.subclassHash.ContainsKey(key)) { // only do this if any exist. List <OntologyClass> myChildren = this.subclassHash[key]; foreach (OntologyClass currChild in myChildren) { String childId = Utility.Utility.PrefixUri(currChild.GetNameString(false), prefixes); subClasses.Add(JsonValue.CreateStringValue(childId)); } } // direct connections -- generate the connections. foreach (OntologyPath currPath in this.GetConnList(key)) { JsonObject pathJsonObject = new JsonObject(); pathJsonObject.Add("startClass", JsonValue.CreateStringValue(Utility.Utility.PrefixUri(currPath.GetTriple(0).GetSubject(), prefixes))); pathJsonObject.Add("predicate", JsonValue.CreateStringValue(Utility.Utility.PrefixUri(currPath.GetTriple(0).GetPredicate(), prefixes))); pathJsonObject.Add("destinationClass", JsonValue.CreateStringValue(Utility.Utility.PrefixUri(currPath.GetTriple(0).GetObject(), prefixes))); directConnections.Add(pathJsonObject); } // full uri information currClass.Add("fullUri", JsonValue.CreateStringValue(uri)); // add everything else to the outgoing structure. currClass.Add("superClasses", superClasses); currClass.Add("subClasses", subClasses); currClass.Add("comments", comments); currClass.Add("labels", labels); currClass.Add("directConnections", directConnections); classList.Add(currClass); } // create the prefix array JsonArray prefixList = new JsonArray(); foreach (String k in prefixes.Keys) { JsonObject pref = new JsonObject(); pref.Add("prefixId", JsonValue.CreateStringValue(prefixes[k])); pref.Add("prefix", JsonValue.CreateStringValue(k)); } String creationTime = DateTime.Now.ToString("yyyyMMdd_HHmmss"); retval.Add("version", JsonValue.CreateNumberValue(OntologyInfo.JSON_VERSION)); retval.Add("generated", JsonValue.CreateStringValue(creationTime)); if (this.modelConnnection != null) { retval.Add("sparqlConn", this.modelConnnection.ToJson()); } JsonObject jsonOInfo = new JsonObject(); jsonOInfo.Add("prefixes", prefixList); jsonOInfo.Add("enumerations", enumerations); jsonOInfo.Add("propertyList", propertyList); jsonOInfo.Add("classList", classList); // add the oInfo serialization to the outgoing object. retval.Add("ontologyInfo", jsonOInfo); // ship it out. return(retval); }