Exemple #1
0
        public OntologyProperty GetProperty(String propertyName)
        {
            OntologyProperty retval = null;

            // find it if we can...
            foreach (OntologyProperty op in this.properties)
            {
                if (op.GetNameStr(false).ToLower().Equals(propertyName.ToLower()))
                {
                    retval = op;
                    break;
                }
            }
            return(retval);
        }
        public OntologyProperty GetInheritedPropertyByKeyName(OntologyClass ontClass, String propName)
        {
            OntologyProperty        retval = null;
            List <OntologyProperty> props  = this.GetInheritedProperies(ontClass);

            foreach (OntologyProperty i in props)
            {
                if (i.GetNameStr(true).Equals(propName))
                {
                    retval = i;
                    break;
                }
            }

            return(retval);
        }
        public void AddJson(JsonObject encodedOInfo)
        {
            // check the version:
            long version = 0;

            if (encodedOInfo.ContainsKey("version"))
            {
                version = (long)encodedOInfo.GetNamedNumber("version");
            }
            // check the version information now
            if (version > OntologyInfo.JSON_VERSION)
            {   // fail gracelessly
                throw new Exception("Can't decode OntologyInfo JSON with newer version > " + OntologyInfo.JSON_VERSION + " : found " + version);
            }

            // get the connection, if it exists and we care.
            if (encodedOInfo.ContainsKey("sparqlConn"))
            {   // set it up.
                JsonObject       connObj = encodedOInfo.GetNamedObject("sparqlConn");
                SparqlConnection connVal = new SparqlConnection(connObj.ToString());
                this.modelConnnection = connVal;
            }

            // get the oInfo block
            if (!encodedOInfo.ContainsKey("ontologyInfo"))
            {   // panic
                throw new Exception("encoded group does not include the ontologyInfo block and cannot be used.");
            }
            JsonObject oInfoBlock = encodedOInfo.GetNamedObject("ontologyInfo");

            // unpack the prefixews
            Dictionary <String, String> prefixHash = new Dictionary <String, String>();
            JsonArray prefixes = oInfoBlock.GetNamedArray("prefixes");

            for (int i = 0; i < prefixes.Count; i++)
            {
                JsonObject currPrefix = prefixes.GetObjectAt((uint)i);
                prefixHash.Add(currPrefix.GetNamedString("prefixId"), currPrefix.GetNamedString("prefix"));
            }

            // unpack everything else. this is a little different than in the java because we do not have to use the table load
            // logic. as a result, this may be more straightforward.

            // unpack the classes:

            JsonArray classArr = oInfoBlock.GetNamedArray("classList");

            for (int m = 0; m < classArr.Count; m++)
            {   // get each class and add them... and related values...
                JsonObject currClass = classArr.GetObjectAt((uint)m);
                String     fullUri   = Utility.Utility.UnPrefixUri(currClass.GetNamedString("fullUri"), prefixHash);
                JsonArray  subC      = currClass.GetNamedArray("subClasses");
                JsonArray  superC    = currClass.GetNamedArray("superClasses");
                JsonArray  comments  = currClass.GetNamedArray("comments");
                JsonArray  labels    = currClass.GetNamedArray("labels");
                JsonArray  direct    = currClass.GetNamedArray("directConnections");

                // find or create the class.
                OntologyClass curr = null;

                if (this.classHash.ContainsKey(fullUri))
                {   // get that one.
                    curr = classHash[fullUri];
                }
                else
                {   // create a new one
                    curr = new OntologyClass(fullUri);
                    this.classHash.Add(fullUri, curr);
                }

                // create the List of parent names
                for (int pi = 0; pi < superC.Count; pi++)
                {   // check and add the superclasses
                    String pn = Utility.Utility.UnPrefixUri(superC.GetStringAt((uint)pi), prefixHash);
                    // check that the parent exists. if not, make it.
                    if (!this.classHash.ContainsKey(pn))
                    {
                        OntologyClass parentClass = new OntologyClass(pn);
                        classHash.Add(pn, parentClass);
                    }

                    // add the parent.
                    curr.AddParentName(pn);
                }

                // get the comments.
                for (int com = 0; com < comments.Count; com++)
                {
                    String c = comments.GetStringAt((uint)com);
                    curr.AddAnnotationComment(c);
                }

                // get the labels.
                for (int lab = 0; lab < labels.Count; lab++)
                {
                    String l = labels.GetStringAt((uint)lab);
                    curr.AddAnnotationLabel(l);
                }

                // add the subclasses as well...
                List <OntologyClass> children = new List <OntologyClass>();
                for (int h = 0; h < subC.Count; h++)
                {   // check and add the subclasses
                    String        cn    = Utility.Utility.UnPrefixUri(subC.GetStringAt((uint)h), prefixHash);
                    OntologyClass child = null;
                    // check that it exists. if not, make it.
                    if (!this.classHash.ContainsKey(cn))
                    {
                        OntologyClass childClass = new OntologyClass(cn);
                        classHash.Add(cn, childClass);
                        child = childClass;
                    }
                    else
                    {
                        child = this.classHash[cn];
                    }
                    children.Add(child);
                }

                // if there were any entries, add this.
                if (children.Count > 0)
                {
                    this.subclassHash.Add(fullUri, children);
                }

                // handle the connections.
                List <OntologyPath> paths = new List <OntologyPath>();
                for (int b = 0; b < direct.Count; b++)
                {   // all the one-hop connections.
                    JsonObject jdir = direct.GetObjectAt((uint)b);
                    String     destinationClassUri = Utility.Utility.UnPrefixUri(jdir.GetNamedString("destinationClass"), prefixHash);
                    String     predicateUri        = Utility.Utility.UnPrefixUri(jdir.GetNamedString("predicate"), prefixHash);
                    String     startClassUri       = Utility.Utility.UnPrefixUri(jdir.GetNamedString("startClass"), prefixHash);

                    OntologyPath op = new OntologyPath(startClassUri);
                    op.AddTriple(startClassUri, predicateUri, destinationClassUri);

                    // add to the list
                    paths.Add(op);
                }

                if (paths.Count > 0)
                {   // we have some paths. add it.
                    this.connHash.Add(fullUri, paths);
                }

                // done with the classes.
            }

            // unpack the properties:

            JsonArray propertyArr = oInfoBlock.GetNamedArray("propertyList");

            for (int j = 0; j < propertyArr.Count; j++)
            {   // add each entry to the property list.
                JsonObject currPropJson = propertyArr.GetObjectAt((uint)j);

                String    fullUri  = Utility.Utility.UnPrefixUri(currPropJson.GetNamedString("fullUri"), prefixHash);
                JsonArray comments = currPropJson.GetNamedArray("comments");
                JsonArray labels   = currPropJson.GetNamedArray("labels");
                JsonArray domain   = currPropJson.GetNamedArray("domain");
                JsonArray range    = currPropJson.GetNamedArray("range");

                // get the (currently) single range.
                String rangeUri = Utility.Utility.UnPrefixUri(range.GetStringAt(0), prefixHash);
                // create the property
                OntologyProperty oProp = new OntologyProperty(fullUri, rangeUri);

                for (int di = 0; di < domain.Count; di++)
                {   // get the domain info and add it.
                    String domainUri = Utility.Utility.UnPrefixUri(domain.GetStringAt((uint)di), prefixHash);
                    // get the class in the domain.
                    OntologyClass domClass = this.classHash[domainUri];
                    domClass.AddProperty(oProp);
                }

                // add the labels.
                for (int li = 0; li < labels.Count; li++)
                {
                    String label = labels.GetStringAt((uint)li);
                    oProp.AddAnnotationLabel(label);
                }

                // add the comments
                for (int ci = 0; ci < comments.Count; ci++)
                {
                    String comment = comments.GetStringAt((uint)ci);
                    oProp.AddAnnotationComment(comment);
                }

                this.propertyHash.Add(fullUri, oProp);
            }

            // add the enumerations
            JsonArray enumerationArr = oInfoBlock.GetNamedArray("enumerations");

            for (int ei = 0; ei < enumerationArr.Count; ei++)
            {
                JsonObject enumVal = enumerationArr.GetObjectAt((uint)ei);

                String    fullUri  = Utility.Utility.UnPrefixUri(enumVal.GetNamedString("fullUri"), prefixHash);
                JsonArray children = enumVal.GetNamedArray("enumeration");

                List <String> childUris = new List <String>();
                for (int ci = 0; ci < children.Count; ci++)
                {
                    childUris.Add(Utility.Utility.UnPrefixUri(children.GetStringAt((uint)ci), prefixHash));
                }
                // add the list
                if (childUris.Count > 0)
                {
                    this.enumerationHash.Add(fullUri, childUris);
                }
            }
        }
        // -------------------------------------------------------------------------------------- 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);
        }
Exemple #5
0
 public void AddProperty(OntologyProperty op)
 {
     this.properties.Add(op);
 }