/// <summary>
 /// Checks if the given "aFact -> transOntProp" assertion links to the given bFact within the given data
 /// </summary>
 public static Boolean IsTransitiveAssertionOf(RDFOntologyFact aFact,
                                               RDFOntologyObjectProperty transOntProp,
                                               RDFOntologyFact bFact,
                                               RDFOntologyData data)
 {
     return(aFact != null && transOntProp != null && transOntProp.IsTransitiveProperty() && bFact != null && data != null ? EnlistTransitiveAssertionsOf(aFact, transOntProp, data).Facts.ContainsKey(bFact.PatternMemberID) : false);
 }
 /// <summary>
 /// Checks if the given "aFact -> objectProperty -> bFact" has transitive assertions
 /// which would cause transitive cycles (unallowed concept in OWL-DL)
 /// </summary>
 internal static Boolean CheckTransitiveAssertionCompatibility(RDFOntologyData ontologyData,
                                                               RDFOntologyFact aFact,
                                                               RDFOntologyObjectProperty objectProperty,
                                                               RDFOntologyFact bFact)
 {
     return(!ontologyData.CheckIsTransitiveAssertionOf(bFact, objectProperty, aFact));
 }
 /// <summary>
 /// Checks if the given fact is member of the given class within the given ontology
 /// </summary>
 public static Boolean IsMemberOf(RDFOntologyFact ontFact,
                                  RDFOntologyClass ontClass,
                                  RDFOntology ontology,
                                  Boolean useBASEOntology = true)
 {
     return(ontFact != null && ontClass != null && ontology != null ? EnlistMembersOf(ontClass, ontology, useBASEOntology).Facts.ContainsKey(ontFact.PatternMemberID) : false);
 }
 /// <summary>
 /// Default-ctor to build an "owl:HasValue" ontology restriction with the given name on the given property and the given requiredValue
 /// </summary>
 public RDFOntologyHasValueRestriction(RDFResource restrictionName, RDFOntologyProperty onProperty, RDFOntologyFact requiredValue): base(restrictionName, onProperty) {
     if (requiredValue     != null) {
         this.RequiredValue = requiredValue;
     }
     else {
         throw new RDFSemanticsException("Cannot create RDFOntologyHasValueRestriction because given \"requiredValue\" parameter is null.");
     }
 }
 /// <summary>
 /// Default-ctor to build an ontology value restriction on the given property and requiring the given fact
 /// </summary>
 public RDFOntologyHasValueRestriction(RDFOntologyProperty onProperty, RDFOntologyFact requiredFact): base(onProperty) {
     if (requiredFact     != null) {
         this.RequiredFact = requiredFact;
     }
     else {
         throw new RDFSemanticsException("Cannot create ontology restriction because given \"requiredFact\" parameter is null.");
     }
 }
        /// <summary>
        /// Enlists the different facts of the given fact within the given data
        /// </summary>
        public static RDFOntologyData EnlistDifferentFactsFrom(RDFOntologyFact ontFact,
                                                               RDFOntologyData data)
        {
            var result = new RDFOntologyData();

            if (ontFact != null && data != null)
            {
                result = RDFSemanticsUtilities.EnlistDifferentFactsFrom_Core(ontFact, data, null)
                         .RemoveFact(ontFact);                          //Safety deletion
            }
            return(result);
        }
        /// <summary>
        /// Enlists the given "aFact -> transOntProp" assertions within the given data
        /// </summary>
        public static RDFOntologyData EnlistTransitiveAssertionsOf(RDFOntologyFact ontFact,
                                                                   RDFOntologyObjectProperty transOntProp,
                                                                   RDFOntologyData data)
        {
            var result = new RDFOntologyData();

            if (ontFact != null && transOntProp != null && transOntProp.IsTransitiveProperty() && data != null)
            {
                result = RDFSemanticsUtilities.EnlistTransitiveAssertionsOf_Core(ontFact, transOntProp, data, null);
            }
            return(result);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Default-ctor to build an "owl:HasValue" ontology restriction with the given name on the given property and the given requiredValue
 /// </summary>
 public RDFOntologyHasValueRestriction(RDFResource restrictionName,
                                       RDFOntologyProperty onProperty,
                                       RDFOntologyFact requiredValue) : base(restrictionName, onProperty)
 {
     if (requiredValue != null)
     {
         this.RequiredValue = requiredValue;
     }
     else
     {
         throw new RDFSemanticsException("Cannot create RDFOntologyHasValueRestriction because given \"requiredValue\" parameter is null.");
     }
 }
        /// <summary>
        /// Adds the given ontology fact to the enumerate members of this
        /// </summary>
        public RDFOntologyEnumerateClass AddEnumerateMember(RDFOntologyFact enumerateMember) {
            if (enumerateMember != null) {

                //Maintain consistency against specified category of members: all resources or all literals
                if( (this.Category == RDFSemanticsEnums.RDFOntologyEnumerateClassCategory.ResourceEnumeration && enumerateMember.IsObjectFact()) ||
                    (this.Category == RDFSemanticsEnums.RDFOntologyEnumerateClassCategory.LiteralEnumeration  && enumerateMember.IsLiteralFact())) {
                        if (!this.EnumerateMembers.ContainsKey(enumerateMember.PatternMemberID)) {
                            this.EnumerateMembers.Add(enumerateMember.PatternMemberID, enumerateMember);
                        }
                }

            }
            return this;
        }
        internal static void LoadOntology(RDFGraph ontGraph, RDFOntology ontology, RDFGraph rdfType,
                                          RDFGraph owlVersInfo, RDFGraph rdfsComment, RDFGraph rdfsLabel,
                                          RDFGraph rdfsSeeAlso, RDFGraph rdfsIsDefBy, RDFGraph customAnnot) {
            var o  = rdfType.SelectTriplesByObject(RDFVocabulary.OWL.ONTOLOGY)
                            .FirstOrDefault();
            if (o != null) {
                ontology.Value           = o.Subject;
                ontology.PatternMemberID = o.Subject.PatternMemberID;

                #region Annotations

                #region VersionInfo
                foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddVersionInfo(fact);
                    }
                }
                #endregion

                #region Comment
                foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddComment(fact);
                    }
                }
                #endregion

                #region Label
                foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddLabel(fact);
                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddSeeAlso(fact);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddIsDefinedBy(fact);
                    }
                }
                #endregion

                #region Custom Annotations
                foreach (var ap in customAnnot) {
                    var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                    foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)ontology.Value)
                                                .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                        var fact                = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact               == null) {
                            if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                fact            = new RDFOntologyFact((RDFResource)t.Object);
                            }
                            else {
                                fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                            }
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddCustomAnnotation(annotationProp, fact);
                    }

                }
                #endregion

                #endregion

                #region Versioning

                #region Imports
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.IMPORTS)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.ImportOntology(new RDFOntology((RDFResource)fact.Value, false));
                    }
                }
                #endregion

                #region BackwardCompatibleWith
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.BACKWARD_COMPATIBLE_WITH)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddBackwardCompatibleWith(fact);
                    }
                }
                #endregion

                #region IncompatibleWith
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INCOMPATIBLE_WITH)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddIncompatibleWith(fact);
                    }
                }
                #endregion

                #region PriorVersion
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.PRIOR_VERSION)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddPriorVersion(fact);
                    }
                }
                #endregion

                #endregion

            }
        }
 /// <summary>
 /// Checks if the given aFact is differentFrom the given bFact within the given data
 /// </summary>
 public static Boolean IsDifferentFactFrom(RDFOntologyFact aFact,
                                           RDFOntologyFact bFact,
                                           RDFOntologyData data)
 {
     return(aFact != null && bFact != null && data != null ? EnlistDifferentFactsFrom(aFact, data).Facts.ContainsKey(bFact.PatternMemberID) : false);
 }
        /// <summary>
        /// Adds the given literal fact to the "owl:versionInfo" annotations about this ontology resource
        /// </summary>
        public RDFOntologyResource AddVersionInfo(RDFOntologyFact versionInfo) {
            if (versionInfo != null && versionInfo.IsLiteralFact()) {

                //Cannot make axioms on annotation properties
                if (!(this is RDFOntologyProperty && ((RDFOntologyProperty)this).IsAnnotationProperty())) {

                    //Cannot assign attributes to literal ontology facts
                    if (!(this is RDFOntologyFact && ((RDFOntologyFact)this).IsLiteralFact())) {
                        if (!this.VersionInfo.ContainsKey(versionInfo.PatternMemberID)) {
                            this.VersionInfo.Add(versionInfo.PatternMemberID, versionInfo);
                        }
                    }

                }

            }
            return this;
        }
 /// <summary>
 /// Removes the given resource fact from the "rdfs:isDefinedBy" annotations about this ontology resource
 /// </summary>
 public RDFOntologyResource RemoveIsDefinedBy(RDFOntologyFact isDefinedBy) {
     if (isDefinedBy != null && isDefinedBy.IsObjectFact()) {
         if (this.IsDefinedBy.ContainsKey(isDefinedBy.PatternMemberID)) {
             this.IsDefinedBy.Remove(isDefinedBy.PatternMemberID);
         }
     }
     return this;
 }
 /// <summary>
 /// Removes the given literal fact from the "rdfs:label" annotations about this ontology resource
 /// </summary>
 public RDFOntologyResource RemoveLabel(RDFOntologyFact label) {
     if (label != null && label.IsLiteralFact()) {
         if (this.Label.ContainsKey(label.PatternMemberID)) {
             this.Label.Remove(label.PatternMemberID);
         }
     }
     return this;
 }
 /// <summary>
 /// Removes the given literal fact from the "owl:versionInfo" annotations about this ontology resource
 /// </summary>
 public RDFOntologyResource RemoveVersionInfo(RDFOntologyFact versionInfo) {
     if (versionInfo != null && versionInfo.IsLiteralFact()) {
         if (this.VersionInfo.ContainsKey(versionInfo.PatternMemberID)) {
             this.VersionInfo.Remove(versionInfo.PatternMemberID);
         }
     }
     return this;
 }
        /// <summary>
        /// Adds the given resource fact to the "rdfs:isDefinedBy" annotations about this ontology resource
        /// </summary>
        public RDFOntologyResource AddIsDefinedBy(RDFOntologyFact isDefinedBy) {
            if (isDefinedBy != null && isDefinedBy.IsObjectFact()) {

                //Cannot make axioms on annotation properties
                if (!(this is RDFOntologyProperty && ((RDFOntologyProperty)this).IsAnnotationProperty())) {

                    //Cannot assign attributes to literal ontology facts
                    if (!(this is RDFOntologyFact && ((RDFOntologyFact)this).IsLiteralFact())) {
                        if (!this.IsDefinedBy.ContainsKey(isDefinedBy.PatternMemberID)) {
                            this.IsDefinedBy.Add(isDefinedBy.PatternMemberID, isDefinedBy);
                        }
                    }

                }

            }
            return this;
        }
 /// <summary>
 /// Checks if the ontology enumerate class contains the given fact in its list of enumerate members
 /// </summary>
 public Boolean ContainsEnumerateMember(RDFOntologyFact enumerateMember) {
     return (enumerateMember != null && this.EnumerateMembers.ContainsKey(enumerateMember.PatternMemberID));
 }
 /// <summary>
 /// Checks if the given fact is member of the given class within the given ontology
 /// </summary>
 public static Boolean IsMemberOf(RDFOntologyFact ontFact, RDFOntologyClass ontClass, RDFOntology ontology) { 
     return(ontFact != null && ontClass != null && ontology != null ? RDFOntologyReasoningHelper.EnlistMembersOf(ontClass, ontology).Facts.ContainsKey(ontFact.PatternMemberID) : false);
 }
 /// <summary>
 /// Enlists the different facts of the given fact within the given data
 /// </summary>
 public static RDFOntologyData EnlistDifferentFactsFrom(RDFOntologyFact ontFact, RDFOntologyData data) {
     var result     = new RDFOntologyData();
     if (ontFact   != null && data != null) {
         result     = RDFSemanticsUtilities.EnlistDifferentFactsFrom_Core(ontFact, data, null).RemoveFact(ontFact);
     }
     return result;
 }
 /// <summary>
 /// Checks if the given aFact is differentFrom the given bFact within the given data
 /// </summary>
 public static Boolean IsDifferentFactFrom(RDFOntologyFact aFact, RDFOntologyFact bFact, RDFOntologyData data) {
     return (aFact != null && bFact != null && data != null ? RDFOntologyReasoningHelper.EnlistDifferentFactsFrom(aFact, data).Facts.ContainsKey(bFact.PatternMemberID) : false);
 }
 /// <summary>
 /// Checks if the given aFact is sameAs the given bFact within the given data
 /// </summary>
 public static Boolean IsSameFactAs(RDFOntologyFact aFact, RDFOntologyFact bFact, RDFOntologyData data) {
     return (aFact != null && bFact != null && data != null ? RDFOntologyReasoningHelper.EnlistSameFactsAs(aFact, data).Facts.ContainsKey(bFact.PatternMemberID) : false);
 }
        /// <summary>
        /// Loads the fact instances from the given graph into the given ontology
        /// </summary>
        internal static void LoadFacts(RDFGraph ontGraph, RDFOntology ontology, RDFGraph rdfType) {

            #region Facts
            foreach (var c in ontology.Model.ClassModel) {
                foreach (var t in rdfType.SelectTriplesByObject((RDFResource)c.Value)) {                    
                    var fct  = ontology.Data.SelectFact(t.Subject.ToString());
                    if (fct == null) {
                        fct  = new RDFOntologyFact((RDFResource)t.Subject);                        
                        ontology.Data.AddFact(fct); 
                    }
                    fct.AddClassType(c);
                }
            }
            #endregion

            #region Attributes
            foreach (var attrProp  in ontology.Model.PropertyModel) {
                if (!attrProp.IsAnnotationProperty()) {
                    foreach (var t in ontGraph.SelectTriplesByPredicate((RDFResource)attrProp.Value)) {

                        //Do not parse attributes of the ontology
                        if (!t.Subject.Equals(ontology.Value)) {

                            var subjFct        = ontology.Data.SelectFact(t.Subject.ToString());
                            if (subjFct       != null) {
                                var objFct     = ontology.Data.SelectFact(t.Object.ToString());
                                if (objFct    != null) {
                                    subjFct.AddOntologyAttribute(attrProp, objFct);
                                }
                                else {
                                    if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                        throw new RDFSemanticsException("Cannot load attributes of fact '" + t.Subject + "' from graph, because it has a range object fact '" + t.Object + "' which has not been found in the ontology data.");
                                    }
                                    else {
                                        //Literal fact must be created on the fly, since they do not exist in ontology data
                                        objFct = new RDFOntologyFact((RDFLiteral)t.Object);
                                        ontology.Data.AddFact(objFct);
                                        subjFct.AddOntologyAttribute(attrProp, objFct);
                                    }
                                }
                            }
                            else {
                                throw new RDFSemanticsException("Cannot load attributes of fact '" + t.Subject + "' from graph, because it has not been found in the ontology data.");
                            }

                        }

                    }
                }
            }
            #endregion

            #region SameAs
            foreach (var t     in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.SAME_AS)) {
                var subjFct     = ontology.Data.SelectFact(t.Subject.ToString());
                if (subjFct    != null) {
                    var objFct  = ontology.Data.SelectFact(t.Object.ToString());
                    if (objFct != null) {
                        subjFct.AddSameAs(objFct);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load taxonomy of fact '" + t.Subject + "' from graph, because its equivalent fact '" + t.Object + "' has not been found in the ontology data.");
                    }
                }
                else {
                    throw new RDFSemanticsException("Cannot load taxonomy of fact '" + t.Subject + "' from graph, because it has not been found in the ontology data.");
                }
            }
            #endregion

            #region DifferentFrom
            foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.DIFFERENT_FROM)) {
                var subjFct     = ontology.Data.SelectFact(t.Subject.ToString());
                if (subjFct    != null) {
                    var objFct  = ontology.Data.SelectFact(t.Object.ToString());
                    if (objFct != null) {
                        subjFct.AddDifferentFrom(objFct);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load taxonomy of fact '" + t.Subject + "' from graph, because its different fact '" + t.Object + "' has not been found in the ontology data.");
                    }
                }
                else {
                    throw new RDFSemanticsException("Cannot load taxonomy of fact '" + t.Subject + "' from graph, because it has not been found in the ontology data.");
                }
            }
            #endregion

        }
        /// <summary>
        /// Adds the given literal fact to the "rdfs:label" annotations about this ontology resource
        /// </summary>
        public RDFOntologyResource AddLabel(RDFOntologyFact label) {
            if (label != null && label.IsLiteralFact()) {

                //Cannot make axioms on annotation properties
                if (!(this is RDFOntologyProperty && ((RDFOntologyProperty)this).IsAnnotationProperty())) {

                    //Cannot assign attributes to literal ontology facts
                    if (!(this is RDFOntologyFact && ((RDFOntologyFact)this).IsLiteralFact())) {
                        if (!this.Label.ContainsKey(label.PatternMemberID)) {
                            this.Label.Add(label.PatternMemberID, label);
                        }
                    }

                }

            }
            return this;
        }
        /// <summary>
        /// Adds the given resource fact to the "rdfs:seeAlso" annotations about this ontology resource
        /// </summary>
        public RDFOntologyResource AddSeeAlso(RDFOntologyFact seeAlso) {
            if (seeAlso != null && seeAlso.IsObjectFact()) {

                //Cannot make axioms on annotation properties
                if (!(this is RDFOntologyProperty && ((RDFOntologyProperty)this).IsAnnotationProperty())) {

                    //Cannot assign attributes to literal ontology facts
                    if (!(this is RDFOntologyFact && ((RDFOntologyFact)this).IsLiteralFact())) {
                        if (!this.SeeAlso.ContainsKey(seeAlso.PatternMemberID)) {
                            this.SeeAlso.Add(seeAlso.PatternMemberID, seeAlso);
                        }
                    }

                }

            }
            return this;
        }
 /// <summary>
 /// Removes the given ontology fact from the enumerate members of this
 /// </summary>
 public RDFOntologyEnumerateClass RemoveEnumerateMember(RDFOntologyFact enumerateMember) {
     if (enumerateMember != null) {
         if (this.EnumerateMembers.ContainsKey(enumerateMember.PatternMemberID)) {
             this.EnumerateMembers.Remove(enumerateMember.PatternMemberID);
         }
     }
     return this;
 }
        /// <summary>
        /// Adds the given custom annotation to this ontology resource
        /// </summary>
        public RDFOntologyResource AddCustomAnnotation(RDFOntologyAnnotationProperty annotationProperty, RDFOntologyFact annotationFact) {
            if (annotationProperty != null && annotationFact != null) {

                //Redirect standard annotation properties to their own methods
                if (annotationProperty.Equals(RDFVocabulary.OWL.VERSION_INFO)) {
                    return this.AddVersionInfo(annotationFact);
                } 
                if (annotationProperty.Equals(RDFVocabulary.RDFS.COMMENT)) {
                    return this.AddComment(annotationFact);
                }
                if (annotationProperty.Equals(RDFVocabulary.RDFS.LABEL)) {
                    return this.AddLabel(annotationFact);
                }
                if (annotationProperty.Equals(RDFVocabulary.RDFS.SEE_ALSO)) {
                    return this.AddSeeAlso(annotationFact);
                }
                if (annotationProperty.Equals(RDFVocabulary.RDFS.IS_DEFINED_BY)) {
                    return this.AddIsDefinedBy(annotationFact);
                }

                //Cannot make axioms on annotation properties
                if (!(this is RDFOntologyProperty && ((RDFOntologyProperty)this).IsAnnotationProperty())) {

                    //Cannot assign attributes to literal ontology facts
                    if (!(this is RDFOntologyFact && ((RDFOntologyFact)this).IsLiteralFact())) {
                        var annotAttr = new RDFOntologyAttribute(annotationProperty, annotationFact);
                        if (!this.CustomAnnotations.ContainsKey(annotAttr.AttributeID)) {
                            this.CustomAnnotations.Add(annotAttr.AttributeID, annotAttr);
                        }
                    }

                }

            }
            return this;
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Gets an ontology representation of the given graph
        /// </summary>
        internal static RDFOntology FromRDFGraph(RDFGraph ontGraph) {
            RDFOntology ontology  = null;
            if (ontGraph         != null) {
                ontology          = new RDFOntology(new RDFResource(ontGraph.Context));

                #region Prefetch
                var versionInfo   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.VERSION_INFO);
                var comment       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.COMMENT);
                var label         = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.LABEL);
                var seeAlso       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.SEE_ALSO);
                var isDefinedBy   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.IS_DEFINED_BY);
                var imports       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.IMPORTS);
                var bcwcompWith   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.BACKWARD_COMPATIBLE_WITH);
                var incompWith    = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INCOMPATIBLE_WITH);
                var priorVersion  = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.PRIOR_VERSION);

                var rdfType       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDF.TYPE);
                var subclassOf    = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.SUB_CLASS_OF);
                var subpropOf     = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.SUB_PROPERTY_OF);
                var equivclassOf  = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.EQUIVALENT_CLASS);
                var equivpropOf   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.EQUIVALENT_PROPERTY);
                var disjclassWith = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.DISJOINT_WITH);
                var domain        = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.DOMAIN);
                var range         = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.RANGE);
                var onProperty    = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.ON_PROPERTY);
                var oneOf         = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.ONE_OF);
                var unionOf       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.UNION_OF);
                var intersectionOf= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INTERSECTION_OF);
                var complementOf  = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.COMPLEMENT_OF);
                var inverseOf     = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INVERSE_OF);
                var allvaluesFrom = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.ALL_VALUES_FROM);
                var somevaluesFrom= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.SOME_VALUES_FROM);
                var hasvalue      = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.HAS_VALUE);
                var cardinality   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.CARDINALITY);
                var mincardinality= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.MIN_CARDINALITY);
                var maxcardinality= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.MAX_CARDINALITY);
                var sameAs        = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.SAME_AS);
                var differentFrom = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.DIFFERENT_FROM);
                #endregion

                #region Load

                #region Ontology
                if (!rdfType.ContainsTriple(new RDFTriple((RDFResource)ontology.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY))) {
                     var ont     = rdfType.SelectTriplesByObject(RDFVocabulary.OWL.ONTOLOGY).FirstOrDefault();
                     if (ont    != null) {
                         ontology.Value           = ont.Subject;
                         ontology.PatternMemberID = ontology.Value.PatternMemberID;
                     }
                }
                #endregion

                #region OntologyModel

                #region PropertyModel

                #region AnnotationProperty
                foreach (var ap in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.ANNOTATION_PROPERTY)) {
                    ontology.Model.PropertyModel.AddProperty(new RDFOntologyAnnotationProperty((RDFResource)ap.Subject));
                }
                #endregion

                #region DatatypeProperty
                foreach (var dp in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.DATATYPE_PROPERTY)) {
                    var dtp   = new RDFOntologyDatatypeProperty((RDFResource)dp.Subject);
                    ontology.Model.PropertyModel.AddProperty(dtp);

                    #region DeprecatedProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)dtp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                        dtp.SetDeprecated(true);
                    }
                    #endregion

                    #region FunctionalProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)dtp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                        dtp.SetFunctional(true);
                    }
                    #endregion

                }
                #endregion

                #region ObjectProperty
                foreach (var op in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.OBJECT_PROPERTY)) {
                    var obp  = new RDFOntologyObjectProperty((RDFResource)op.Subject);
                    ontology.Model.PropertyModel.AddProperty(obp);

                    #region DeprecatedProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                        obp.SetDeprecated(true);
                    }
                    #endregion

                    #region FunctionalProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                        obp.SetFunctional(true);
                    }
                    #endregion

                    #region SymmetricProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.SYMMETRIC_PROPERTY))) {
                        obp.SetSymmetric(true);
                    }
                    #endregion

                    #region TransitiveProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.TRANSITIVE_PROPERTY))) {
                        obp.SetTransitive(true);
                    }
                    #endregion

                    #region InverseFunctionalProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY))) {
                        obp.SetInverseFunctional(true);
                    }
                    #endregion

                }

                #region SymmetricProperty
                foreach (var sp in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.SYMMETRIC_PROPERTY)) {
                    var syp  = ontology.Model.PropertyModel.SelectProperty(sp.Subject.ToString());
                    if (syp == null) {
                        syp  = new RDFOntologyObjectProperty((RDFResource)sp.Subject);
                        ontology.Model.PropertyModel.AddProperty(syp);

                        #region DeprecatedProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)syp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                            syp.SetDeprecated(true);
                        }
                        #endregion

                        #region FunctionalProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)syp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                            syp.SetFunctional(true);
                        }
                        #endregion

                    }
                    ((RDFOntologyObjectProperty)syp).SetSymmetric(true);
                }
                #endregion

                #region TransitiveProperty
                foreach (var tp in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.TRANSITIVE_PROPERTY)) {
                    var trp  = ontology.Model.PropertyModel.SelectProperty(tp.Subject.ToString());
                    if (trp == null) {
                        trp  = new RDFOntologyObjectProperty((RDFResource)tp.Subject);
                        ontology.Model.PropertyModel.AddProperty(trp);

                        #region DeprecatedProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)trp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                            trp.SetDeprecated(true);
                        }
                        #endregion

                        #region FunctionalProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)trp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                            trp.SetFunctional(true);
                        }
                        #endregion

                    }
                    ((RDFOntologyObjectProperty)trp).SetTransitive(true);
                }
                #endregion

                #region InverseFunctionalProperty
                foreach (var ip in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY)) {
                    var ifp  = ontology.Model.PropertyModel.SelectProperty(ip.Subject.ToString());
                    if (ifp == null) {
                        ifp  = new RDFOntologyObjectProperty((RDFResource)ip.Subject);
                        ontology.Model.PropertyModel.AddProperty(ifp);

                        #region DeprecatedProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)ifp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                            ifp.SetDeprecated(true);
                        }
                        #endregion

                        #region FunctionalProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)ifp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                            ifp.SetFunctional(true);
                        }
                        #endregion

                    }
                    ((RDFOntologyObjectProperty)ifp).SetInverseFunctional(true);
                }
                #endregion
                #endregion

                #endregion

                #region ClassModel

                #region Class
                foreach (var c   in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.CLASS)) {
                    var ontClass  = new RDFOntologyClass((RDFResource)c.Subject);
                    ontology.Model.ClassModel.AddClass(ontClass);
                    if   (ontGraph.ContainsTriple(new RDFTriple((RDFResource)ontClass.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_CLASS))) {
                          ontClass.SetDeprecated(true);
                    }
                }
                #endregion

                #region DeprecatedClass
                foreach (var dc  in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.DEPRECATED_CLASS)) {
                    var ontClass  = new RDFOntologyClass((RDFResource)dc.Subject);
                    ontClass.SetDeprecated(true);
                    ontology.Model.ClassModel.AddClass(ontClass);
                }
                #endregion

                #region Restriction
                foreach (var r in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.RESTRICTION)) {

                    #region OnProperty
                    var op   = onProperty.SelectTriplesBySubject((RDFResource)r.Subject).FirstOrDefault();
                    if (op  != null) {
                        var onProp     = ontology.Model.PropertyModel.SelectProperty(op.Object.ToString());
                        if (onProp    != null) {
                            var restr  = new RDFOntologyRestriction((RDFResource)r.Subject, onProp);
                            ontology.Model.ClassModel.AddClass(restr);
                        }
                        else {

                            //Raise warning event to inform the user: restriction cannot be imported from 
                            //graph, because definition of its applied property is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its applied property '{1}' is not found in the model.", r.Subject, op.Object));

                        }
                    }
                    #endregion

                }
                #endregion

                #region DataRange
                foreach (var dr  in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.DATA_RANGE)) {
                    ontology.Model.ClassModel.AddClass(new RDFOntologyDataRangeClass((RDFResource)dr.Subject));
                }
                #endregion

                #region Composite

                #region Union
                foreach (var u in unionOf) {
                    if  (u.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var uc          = ontology.Model.ClassModel.SelectClass(u.Subject.ToString());
                         if (uc         != null) {

                            #region ClassToUnionClass
                            if (!(uc    is RDFOntologyUnionClass)) {
                                  uc     = new RDFOntologyUnionClass((RDFResource)u.Subject);
                                  ontology.Model.ClassModel.Classes[uc.PatternMemberID] = uc;
                            }
                            #endregion

                            #region DeserializeUnionCollection
                            var nilFound = false;
                            var itemRest = (RDFResource)u.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var compClass    = ontology.Model.ClassModel.SelectClass(first.Object.ToString());
                                    if (compClass   != null) {
                                        ontology.Model.ClassModel.AddUnionOfRelation((RDFOntologyUnionClass)uc, compClass);
                                    }
                                    else {

                                        //Raise warning event to inform the user: union class cannot be completely imported
                                        //from graph, because definition of its compositing class is not found in the model
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("UnionClass '{0}' cannot be completely imported from graph, because definition of its compositing class '{1}' is not found in the model.", u.Subject, first.Object));
                                    
                                    }

                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                    }
                }
                #endregion

                #region Intersection
                foreach (var i in intersectionOf) {
                    if  (i.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var ic          = ontology.Model.ClassModel.SelectClass(i.Subject.ToString());
                         if (ic         != null) {

                            #region ClassToIntersectionClass
                            if (!(ic    is RDFOntologyIntersectionClass)) {
                                  ic     = new RDFOntologyIntersectionClass((RDFResource)i.Subject);
                                  ontology.Model.ClassModel.Classes[ic.PatternMemberID] = ic;
                            }
                            #endregion

                            #region DeserializeIntersectionCollection
                            var nilFound   = false;
                            var itemRest   = (RDFResource)i.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var compClass    = ontology.Model.ClassModel.SelectClass(first.Object.ToString());
                                    if (compClass   != null) {
                                        ontology.Model.ClassModel.AddIntersectionOfRelation((RDFOntologyIntersectionClass)ic, compClass);
                                    }
                                    else {

                                        //Raise warning event to inform the user: intersection class cannot be completely imported
                                        //from graph, because definition of its compositing class is not found in the model
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IntersectionClass '{0}' cannot be completely imported from graph, because definition of its compositing class '{1}' is not found in the model.", i.Subject, first.Object));
                                    
                                    }


                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                    }
                }
                #endregion

                #region Complement
                foreach (var c in complementOf) {
                    if  (c.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var cc  = ontology.Model.ClassModel.SelectClass(c.Subject.ToString());
                         if (cc != null) {
                             var compClass  = ontology.Model.ClassModel.SelectClass(c.Object.ToString());
                             if (compClass != null) {
                                 cc         = new RDFOntologyComplementClass((RDFResource)c.Subject, compClass);
                                 ontology.Model.ClassModel.Classes[cc.PatternMemberID] = cc;
                             }
                             else {

                                 //Raise warning event to inform the user: complement class cannot be imported
                                 //from graph, because definition of its complemented class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Class '{0}' cannot be imported from graph, because definition of its complement class '{1}' is not found in the model.", c.Subject, c.Object));

                             }
                         }
                         else {

                             //Raise warning event to inform the user: complement class cannot be imported 
                             //from graph, because its definition is not found in the model
                             RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Class '{0}' cannot be imported from graph, because its definition is not found in the model.", c.Subject));

                         }
                    }
                }
                #endregion

                #endregion

                #endregion

                #endregion

                #region OntologyData

                #region Fact
                foreach (var c  in ontology.Model.ClassModel) {
                    foreach (var t in rdfType.SelectTriplesByObject((RDFResource)c.Value)) {
                        var f    = ontology.Data.SelectFact(t.Subject.ToString());
                        if (f   == null) {
                            f    = new RDFOntologyFact((RDFResource)t.Subject);
                            ontology.Data.AddFact(f);
                        }
                        ontology.Data.AddClassTypeRelation(f, c);
                    }
                }
                #endregion

                #endregion

                #region Finalization

                #region Restriction
                var restrictions = ontology.Model.ClassModel.Where(c => c.IsRestrictionClass()).ToList();
                foreach (var     r in restrictions) {

                    #region Cardinality
                    Int32 exC    = 0;
                    var  crEx    = cardinality.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (crEx    != null   && crEx.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crEx.Object   is RDFPlainLiteral) {
                            if (Regex.IsMatch(crEx.Object.ToString(), @"^[0-9]+$")) {
                                exC        = Int32.Parse(crEx.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crEx.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crEx.Object).Value, @"^[0-9]+$")) {
                                    exC    = Int32.Parse(((RDFTypedLiteral)crEx.Object).Value);
                                }
                            }
                        }
                    }
                    if (exC > 0) {
                        var cardRestr      = new RDFOntologyCardinalityRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, exC, exC);
                        ontology.Model.ClassModel.Classes[r.PatternMemberID] = cardRestr;
                        continue;
                    }

                    Int32 minC   = 0;
                    var  crMin   = mincardinality.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (crMin   != null   && crMin.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crMin.Object  is RDFPlainLiteral) {
                            if (Regex.IsMatch(crMin.Object.ToString(), @"^[0-9]+$")) {
                                minC       = Int32.Parse(crMin.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crMin.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crMin.Object).Value, @"^[0-9]+$")) {
                                    minC   = Int32.Parse(((RDFTypedLiteral)crMin.Object).Value);
                                }
                            }
                        }
                    }

                    Int32 maxC   = 0;
                    var  crMax   = maxcardinality.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (crMax   != null   && crMax.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crMax.Object  is RDFPlainLiteral) {
                            if (Regex.IsMatch(crMax.Object.ToString(), @"^[0-9]+$")) {
                                maxC       = Int32.Parse(crMax.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crMax.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crMax.Object).Value, @"^[0-9]+$")) {
                                    maxC   = Int32.Parse(((RDFTypedLiteral)crMax.Object).Value);
                                }
                            }
                        }
                    }
                    if (minC > 0  ||  maxC > 0) {
                        var cardRestr      = new RDFOntologyCardinalityRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, minC, maxC);
                        ontology.Model.ClassModel.Classes[r.PatternMemberID] = cardRestr;
                        continue;
                    }
                    #endregion

                    #region HasValue
                    var hvRes    = hasvalue.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (hvRes   != null) {
                        if (hvRes.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                            var hvFct             = ontology.Data.SelectFact(hvRes.Object.ToString());
                            if (hvFct            != null) {
                                var hasvalueRestr = new RDFOntologyHasValueRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, hvFct);
                                ontology.Model.ClassModel.Classes[r.PatternMemberID] = hasvalueRestr;
                                continue;
                            }
                            else {

                                //Raise warning event to inform the user: hasvalue restriction cannot be imported
                                //from graph, because definition of its required fact is not found in the data
                                RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its required fact '{1}' is not found in the data.", r.Value, hvRes.Object));

                            }
                        }
                        else {
                            var hasvalueRestr     = new RDFOntologyHasValueRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, new RDFOntologyLiteral((RDFLiteral)hvRes.Object));
                            ontology.Model.ClassModel.Classes[r.PatternMemberID] = hasvalueRestr;
                            continue;
                        }
                    }
                    #endregion

                    #region AllValuesFrom
                    var avfRes       = allvaluesFrom.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (avfRes      != null   && avfRes.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var avfCls   = ontology.Model.ClassModel.SelectClass(avfRes.Object.ToString());
                        if (avfCls  != null) {
                            var allvaluesfromRestr = new RDFOntologyAllValuesFromRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, avfCls);
                            ontology.Model.ClassModel.Classes[r.PatternMemberID] = allvaluesfromRestr;
                            continue;
                        }
                        else {

                            //Raise warning event to inform the user: allvaluesfrom restriction cannot be imported
                            //from graph, because definition of its required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its required class '{1}' is not found in the model.", r.Value, avfRes.Object));

                        }
                    }
                    #endregion

                    #region SomeValuesFrom
                    var svfRes      = somevaluesFrom.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (svfRes     != null   && svfRes.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var svfCls  = ontology.Model.ClassModel.SelectClass(svfRes.Object.ToString());
                        if (svfCls != null) {
                            var somevaluesfromRestr = new RDFOntologySomeValuesFromRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, svfCls);
                            ontology.Model.ClassModel.Classes[r.PatternMemberID] = somevaluesfromRestr;
                            continue;
                        }
                        else {

                            //Raise warning event to inform the user: somevaluesfrom restriction cannot be imported
                            //from graph, because definition of its required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its required class '{1}' is not found in the model.", r.Value, svfRes.Object));

                        }
                    }
                    #endregion

                }
                #endregion

                #region Enumerate
                foreach (var e  in  oneOf) {
                    if  (e.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var ec          = ontology.Model.ClassModel.SelectClass(e.Subject.ToString());
                         if (ec         != null && !ec.IsDataRangeClass()) {

                            #region ClassToEnumerateClass
                            if (!ec.IsEnumerateClass()) {
                                 ec      = new RDFOntologyEnumerateClass((RDFResource)e.Subject);
                                 ontology.Model.ClassModel.Classes[ec.PatternMemberID] = ec;
                            }
                            #endregion

                            #region DeserializeEnumerateCollection
                            var nilFound   = false;
                            var itemRest   = (RDFResource)e.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var enumMember   = ontology.Data.SelectFact(first.Object.ToString());
                                    if (enumMember  != null) {
                                        ontology.Model.ClassModel.AddOneOfRelation((RDFOntologyEnumerateClass)ec, enumMember);
                                    }
									else {

                                        //Raise warning event to inform the user: enumerate class cannot be completely imported
                                        //from graph, because definition of its fact member is not found in the data
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EnumerateClass '{0}' cannot be completely imported from graph, because definition of its fact member '{1}' is not found in the data.", e.Subject, first.Object));

			                        }

                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                         else {
                             if (ec     == null) {

                                 //Raise warning event to inform the user: enumerate class cannot be imported
                                 //from graph, because its definition is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EnumerateClass '{0}' cannot be imported from graph, because its definition is not found in the model.", e.Subject));

                             }
                         }
                    }
                }
                #endregion

                #region DataRange
                foreach (var d in oneOf) {
                    if  (d.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var dr          = ontology.Model.ClassModel.SelectClass(d.Subject.ToString());
                         if (dr         != null && !dr.IsEnumerateClass()) {

                            #region ClassToDataRangeClass
                            if (!dr.IsDataRangeClass()) {
                                 dr      = new RDFOntologyDataRangeClass((RDFResource)d.Subject);
                                 ontology.Model.ClassModel.Classes[dr.PatternMemberID] = dr;
                            }
                            #endregion

                            #region DeserializeDataRangeCollection
                            var nilFound   = false;
                            var itemRest   = (RDFResource)d.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                                    ontology.Model.ClassModel.AddOneOfRelation((RDFOntologyDataRangeClass)dr, new RDFOntologyLiteral((RDFLiteral)first.Object));

                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                         else {
                             if (dr     == null) {

                                 //Raise warning event to inform the user: datarange class cannot be imported from
                                 //graph, because its definition is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DataRangeClass '{0}' cannot be imported from graph, because its definition is not found in the model.", d.Subject));

                             }
                         }
                    }
                }
                #endregion

                #region Domain/Range
                foreach (var p in ontology.Model.PropertyModel) {
                    
                    #region Domain
                    var d   = domain.SelectTriplesBySubject((RDFResource)p.Value).FirstOrDefault();
                    if (d  != null   && d.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var domainClass  = ontology.Model.ClassModel.SelectClass(d.Object.ToString());
                        if (domainClass != null) {
                            p.SetDomain(domainClass);
                        }
                        else {

                            //Raise warning event to inform the user: domain constraint cannot be imported from graph, 
                            //because definition of required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Domain constraint on property '{0}' cannot be imported from graph, because definition of required class '{1}' is not found in the model.", p.Value, d.Object));

                        }
                    }
                    #endregion

                    #region Range
                    var r   = range.SelectTriplesBySubject((RDFResource)p.Value).FirstOrDefault();
                    if (r  != null  && r.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var rangeClass  = ontology.Model.ClassModel.SelectClass(r.Object.ToString());
                        if (rangeClass != null) {
                            p.SetRange(rangeClass);
                        }
                        else {

                            //Raise warning event to inform the user: range constraint cannot be imported from graph, 
                            //because definition of required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Range constraint on property '{0}' cannot be imported from graph, because definition of required class '{1}' is not found in the model.", p.Value, r.Object));

                        }
                    }
                    #endregion

                }
                #endregion

                #region PropertyModel Relations
                foreach (var p in ontology.Model.PropertyModel) {
                    
                    #region SubPropertyOf
                    foreach (var spof in subpropOf.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (spof.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var superProp        = ontology.Model.PropertyModel.SelectProperty(spof.Object.ToString());
                             if (superProp       != null) {
                                 if (p.IsObjectProperty()        && superProp.IsObjectProperty()) {
                                     ontology.Model.PropertyModel.AddSubPropertyOfRelation((RDFOntologyObjectProperty)p,   (RDFOntologyObjectProperty)superProp);
                                 }
                                 else if (p.IsDatatypeProperty() && superProp.IsDatatypeProperty()) {
                                     ontology.Model.PropertyModel.AddSubPropertyOfRelation((RDFOntologyDatatypeProperty)p, (RDFOntologyDatatypeProperty)superProp);
                                 }
                             }
                             else {

                                 //Raise warning event to inform the user: subpropertyof relation cannot be imported
                                 //from graph, because definition of property is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SubPropertyOf relation on property '{0}' cannot be imported from graph, because definition of property '{1}' is not found in the model.", p.Value, spof.Object));

                             }
                        }
                    }
                    #endregion

                    #region EquivalentProperty
                    foreach (var eqpr in equivpropOf.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (eqpr.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var equivProp        = ontology.Model.PropertyModel.SelectProperty(eqpr.Object.ToString());
                             if (equivProp       != null) {
                                 if (p.IsObjectProperty()        && equivProp.IsObjectProperty()) {
                                     ontology.Model.PropertyModel.AddEquivalentPropertyRelation((RDFOntologyObjectProperty)p,   (RDFOntologyObjectProperty)equivProp);
                                 }
                                 else if (p.IsDatatypeProperty() && equivProp.IsDatatypeProperty()) {
                                     ontology.Model.PropertyModel.AddEquivalentPropertyRelation((RDFOntologyDatatypeProperty)p, (RDFOntologyDatatypeProperty)equivProp);
                                 }
                             }
                             else {

                                 //Raise warning event to inform the user: equivalentproperty relation cannot be imported
                                 //from graph, because definition of property is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EquivalentProperty relation on property '{0}' cannot be imported from graph, because definition of property '{1}' is not found in the model.", p.Value, eqpr.Object));

                             }
                        }
                    }
                    #endregion

                    #region InverseOf
                    if (p.IsObjectProperty()) {
                        foreach (var inof in inverseOf.SelectTriplesBySubject((RDFResource)p.Value)) {
                            if  (inof.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                                 var invProp          = ontology.Model.PropertyModel.SelectProperty(inof.Object.ToString());
                                 if (invProp != null && invProp.IsObjectProperty()) {
                                     ontology.Model.PropertyModel.AddInverseOfRelation((RDFOntologyObjectProperty)p, (RDFOntologyObjectProperty)invProp);
                                 }
                                 else {

                                     //Raise warning event to inform the user: inverseof relation cannot be imported
                                     //from graph, because definition of property is not found in the model
                                     RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("InverseOf relation on property '{0}' cannot be imported from graph, because definition of property '{1}' is not found in the model.", p.Value, inof.Object));

                                 }
                            }
                        }
                    }
                    #endregion

                }
                #endregion

                #region ClassModel Relations
                foreach (var c in ontology.Model.ClassModel) {

                    #region SubClassOf
                    foreach (var scof in subclassOf.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (scof.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var superClass       = ontology.Model.ClassModel.SelectClass(scof.Object.ToString());
                             if (superClass      != null) {
                                 ontology.Model.ClassModel.AddSubClassOfRelation(c, superClass);
                             }
                             else {

                                 //Raise warning event to inform the user: subclassof relation cannot be imported
                                 //from graph, because definition of class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SubClassOf relation on class '{0}' cannot be imported from graph, because definition of class '{1}' is not found in the model.", c.Value, scof.Object));

                             }
                        }
                    }
                    #endregion

                    #region EquivalentClass
                    foreach (var eqcl in equivclassOf.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (eqcl.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var equivClass       = ontology.Model.ClassModel.SelectClass(eqcl.Object.ToString());
                             if (equivClass      != null) {
                                 ontology.Model.ClassModel.AddEquivalentClassRelation(c, equivClass);
                             }
                             else {

                                 //Raise warning event to inform the user: equivalentclass relation cannot be imported
                                 //from graph, because definition of class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EquivalentClass relation on class '{0}' cannot be imported from graph, because definition of class '{1}' is not found in the model.", c.Value, eqcl.Object));

                             }
                        }
                    }
                    #endregion

                    #region DisjointWith
                    foreach (var djwt in disjclassWith.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (djwt.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var disjWith         = ontology.Model.ClassModel.SelectClass(djwt.Object.ToString());
                             if (disjWith        != null) {
                                 ontology.Model.ClassModel.AddDisjointWithRelation(c, disjWith);
                             }
                             else {

                                 //Raise warning event to inform the user: disjointwith relation cannot be imported
                                 //from graph, because definition of class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DisjointWith relation on class '{0}' cannot be imported from graph, because definition of class '{1}' is not found in the model.", c.Value, djwt.Object));

                             }
                        }
                    }
                    #endregion

                }
                #endregion

                #region Data Relations

                #region SameAs
                foreach (var t in sameAs) {
                    if  (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var subjFct       = ontology.Data.SelectFact(t.Subject.ToString());
                         if (subjFct      != null) {
                             var objFct    = ontology.Data.SelectFact(t.Object.ToString());
                             if (objFct   != null) {
                                 ontology.Data.AddSameAsRelation(subjFct, objFct);
                             }
                             else {

                                 //Raise warning event to inform the user: sameas relation cannot be imported
                                 //from graph, because definition of fact is not found in the data
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SameAs relation on fact '{0}' cannot be imported from graph, because definition of fact '{1}' is not found in the data.", t.Subject, t.Object));

                             }
                         }
                         else {

                             //Raise warning event to inform the user: sameas relation cannot be imported
                             //from graph, because definition of fact is not found in the data
                             RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SameAs relation on fact '{0}' cannot be imported from graph, because its definition is not found in the data.", t.Subject));

                         }
                    }
                }
                #endregion

                #region DifferentFrom
                foreach (var t in differentFrom) {
                    if  (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var subjFct       = ontology.Data.SelectFact(t.Subject.ToString());
                         if (subjFct      != null) {
                             var objFct    = ontology.Data.SelectFact(t.Object.ToString());
                             if (objFct   != null) {
                                 ontology.Data.AddDifferentFromRelation(subjFct, objFct);
                             }
                             else {

                                 //Raise warning event to inform the user: differentfrom relation cannot be imported
                                 //from graph, because definition of fact is not found in the data
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DifferentFrom relation on fact '{0}' cannot be imported from graph, because definition of fact '{1}' is not found in the data.", t.Subject, t.Object));

                             }
                         }
                         else {

                             //Raise warning event to inform the user: differentfrom relation cannot be imported
                             //from graph, because its definition is not found in the data
                             RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DifferentFrom relation on fact '{0}' cannot be imported from graph, because its definition is not found in the data.", t.Subject));

                         }
                    }
                }
                #endregion

                #region Assertion
                foreach (var p      in ontology.Model.PropertyModel) {
                    foreach (var t  in ontGraph.SelectTriplesByPredicate((RDFResource)p.Value)) {
                        var subjFct  = ontology.Data.SelectFact(t.Subject.ToString());
                        if (subjFct != null) {
                            if (p.IsObjectProperty()) {
                                if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var objFct       = ontology.Data.SelectFact(t.Object.ToString());
                                    if (objFct      != null) {
                                        ontology.Data.AddAssertionRelation(subjFct, (RDFOntologyObjectProperty)p, objFct);
                                    }
                                    else {

                                        //Raise warning event to inform the user: assertion relation cannot be imported
                                        //from graph, because definition of fact is not found in the data
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because definition of fact '{1}' is not found in the data.", t.Subject, t.Object));

                                    }
                                }
                                else {

                                    //Raise warning event to inform the user: assertion relation cannot be imported
                                    //from graph, because object property links to a literal
                                    RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because object property '{1}' links to a literal.", t.Subject, p));

                                }
                            }
                            else if (p.IsDatatypeProperty()) {
                                 if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                                     ontology.Data.AddAssertionRelation(subjFct, (RDFOntologyDatatypeProperty)p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                                 }
                                 else {

                                     //Raise warning event to inform the user: assertion relation cannot be imported
                                     //from graph, because datatype property links to a fact
                                     RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because datatype property '{1}' links to a fact.", t.Subject, p));

                                 }
                            }
                        }
                        else {

                            //Raise warning event to inform the user: assertion relation cannot be imported
                            //from graph, because definition of fact is not found in the data
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because definition of the fact is not found in the data. Ensure its classtype relation is specified.", t.Subject));

                        }
                    }
                }
                #endregion

                #endregion

                #region Annotations

                #region Ontology

                #region VersionInfo
                foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddVersionInfoAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: versioninfo annotation on ontology 
                        //cannot be imported from graph, because it does not link a literal
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on ontology '{0}' cannot be imported from graph, because it does not link a literal.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region Comment
                foreach (var t in comment.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddCommentAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: comment annotation on ontology 
                        //cannot be imported from graph, because it does not link a literal
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on ontology '{0}' cannot be imported from graph, because it does not link a literal.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region Label
                foreach (var t in label.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddLabelAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: label annotation on ontology 
                        //cannot be imported from graph, because it does not link a literal
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on ontology '{0}' cannot be imported from graph, because it does not link a literal.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddSeeAlsoAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {
                        RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                        if (resource         == null) {
                            resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                            if (resource     == null) {
                                resource      = ontology.Data.SelectFact(t.Object.ToString());
                                if (resource == null) {
                                    resource  = new RDFOntologyResource();
                                    resource.Value           = t.Object;
                                    resource.PatternMemberID = t.Object.PatternMemberID;

                                    //Raise warning event to inform the user: seealso annotation on ontology 
                                    //has been imported from graph, but linking an unknown generic resource
                                    RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on ontology '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", ontology.Value, t.Object));

                                }
                            }
                        }
                        ontology.AddSeeAlsoAnnotation(resource);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddIsDefinedByAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {
                        RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                        if (isDefBy         == null) {
                            isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                            if (isDefBy     == null) {
                                isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                if (isDefBy == null) {
                                    isDefBy  = new RDFOntologyResource();
                                    isDefBy.Value           = t.Object;
                                    isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                    //Raise warning event to inform the user: isdefinedby annotation on ontology 
                                    //has been imported from graph, but linking an unknown generic resource
                                    RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on ontology '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", ontology.Value, t.Object));

                                }
                            }
                        }
                        ontology.AddIsDefinedByAnnotation(isDefBy);
                    }
                }
                #endregion

                #region BackwardCompatibleWith
                foreach (var t in bcwcompWith.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         ontology.AddBackwardCompatibleWithAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: backwardcompatiblewith annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("BackwardCompatibleWith annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region IncompatibleWith
                foreach (var t in incompWith.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         ontology.AddIncompatibleWithAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: incompatiblewith annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IncompatibleWith annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region PriorVersion
                foreach (var t in priorVersion.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         ontology.AddPriorVersionAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: priorversion annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("PriorVersion annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region Imports
                foreach(var t in imports.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        ontology.AddImportsAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: imports annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Imports annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region CustomAnnotations
                var annotProps   = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                while (annotProps.MoveNext()) {
                    foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)ontology.Value)
                                              .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                        if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.AddCustomAnnotation((RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (custAnn         == null) {
                                custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (custAnn     == null) {
                                    custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (custAnn == null) {
                                        custAnn  = new RDFOntologyResource();
                                        custAnn.Value           = t.Object;
                                        custAnn.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: custom annotation on ontology 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on ontology '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, ontology.Value, t.Object));

                                    }
                                }
                            }
                            ontology.AddCustomAnnotation((RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                        }

                    }
                }
                #endregion

                #endregion

                #region Classes
                foreach (var c in ontology.Model.ClassModel) {

                    #region VersionInfo
                    foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddVersionInfoAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: versioninfo annotation on class 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on class '{0}' cannot be imported from graph, because it does not link a literal.", c.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in comment.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddCommentAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: comment annotation on class 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on class '{0}' cannot be imported from graph, because it does not link a literal.", c.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in label.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddLabelAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: label annotation on class 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on class '{0}' cannot be imported from graph, because it does not link a literal.", c.Value, t.Object));

                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddSeeAlsoAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (resource         == null) {
                                resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (resource     == null) {
                                    resource      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (resource == null) {
                                        resource  = new RDFOntologyResource();
                                        resource.Value           = t.Object;
                                        resource.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: seealso annotation on class 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on class '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", c.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.ClassModel.AddSeeAlsoAnnotation(c, resource);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddIsDefinedByAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (isDefBy         == null) {
                                isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (isDefBy     == null) {
                                    isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (isDefBy == null) {
                                        isDefBy  = new RDFOntologyResource();
                                        isDefBy.Value           = t.Object;
                                        isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: isdefinedby annotation on class 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on class '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", c.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.ClassModel.AddIsDefinedByAnnotation(c, isDefBy);
                        }
                    }
                    #endregion

                    #region CustomAnnotations
                    annotProps       = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                    while (annotProps.MoveNext()) {
                        foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                                  .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                            if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                                 ontology.Model.ClassModel.AddCustomAnnotation(c, (RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                            }
                            else {
                                RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                                if (custAnn         == null) {
                                    custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                    if (custAnn     == null) {
                                        custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                        if (custAnn == null) {
                                            custAnn  = new RDFOntologyResource();
                                            custAnn.Value           = t.Object;
                                            custAnn.PatternMemberID = t.Object.PatternMemberID;

                                            //Raise warning event to inform the user: custom annotation on class 
                                            //has been imported from graph, but linking an unknown generic resource
                                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on class '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, c.Value, t.Object));

                                        }
                                    }
                                }
                                ontology.Model.ClassModel.AddCustomAnnotation(c, (RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                            }

                        }
                    }
                    #endregion

                }
                #endregion

                #region Properties
                foreach (var p in ontology.Model.PropertyModel) {

                    #region VersionInfo
                    foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddVersionInfoAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: versioninfo annotation on property 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on property '{0}' cannot be imported from graph, because it does not link a literal.", p.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in comment.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddCommentAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: comment annotation on property 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on property '{0}' cannot be imported from graph, because it does not link a literal.", p.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in label.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddLabelAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: label annotation on property 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on property '{0}' cannot be imported from graph, because it does not link a literal.", p.Value, t.Object));

                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddSeeAlsoAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (resource         == null) {
                                resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (resource     == null) {
                                    resource      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (resource == null) {
                                        resource  = new RDFOntologyResource();
                                        resource.Value           = t.Object;
                                        resource.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: seealso annotation on property 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on property '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", p.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.PropertyModel.AddSeeAlsoAnnotation(p, resource);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddIsDefinedByAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (isDefBy         == null) {
                                isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (isDefBy     == null) {
                                    isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (isDefBy == null) {
                                        isDefBy  = new RDFOntologyResource();
                                        isDefBy.Value           = t.Object;
                                        isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: isdefinedby annotation on property 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on property '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", p.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.PropertyModel.AddIsDefinedByAnnotation(p, isDefBy);
                        }
                    }
                    #endregion

                    #region CustomAnnotations
                    annotProps       = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                    while (annotProps.MoveNext()) {
                        foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                                  .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                            if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                                 ontology.Model.PropertyModel.AddCustomAnnotation(p, (RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                            }
                            else {
                                RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                                if (custAnn         == null) {
                                    custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                    if (custAnn     == null) {
                                        custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                        if (custAnn == null) {
                                            custAnn  = new RDFOntologyResource();
                                            custAnn.Value           = t.Object;
                                            custAnn.PatternMemberID = t.Object.PatternMemberID;

                                            //Raise warning event to inform the user: custom annotation on property 
                                            //has been imported from graph, but linking an unknown generic resource
                                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on property '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, p.Value, t.Object));

                                        }
                                    }
                                }
                                ontology.Model.PropertyModel.AddCustomAnnotation(p, (RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                            }

                        }
                    }
                    #endregion

                }
                #endregion

                #region Facts
                foreach (var f in ontology.Data) {

                    #region VersionInfo
                    foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddVersionInfoAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: versioninfo annotation on fact 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on fact '{0}' cannot be imported from graph, because it does not link a literal.", f.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in comment.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddCommentAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: comment annotation on fact 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on fact '{0}' cannot be imported from graph, because it does not link a literal.", f.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in label.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddLabelAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: label annotation on fact 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on fact '{0}' cannot be imported from graph, because it does not link a literal.", f.Value, t.Object));

                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddSeeAlsoAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (resource         == null) {
                                resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (resource     == null) {
                                    resource      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (resource == null) {
                                        resource  = new RDFOntologyResource();
                                        resource.Value           = t.Object;
                                        resource.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: seealso annotation on fact 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on fact '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", f.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Data.AddSeeAlsoAnnotation(f, resource);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddIsDefinedByAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (isDefBy         == null) {
                                isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (isDefBy     == null) {
                                    isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (isDefBy == null) {
                                        isDefBy  = new RDFOntologyResource();
                                        isDefBy.Value           = t.Object;
                                        isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: isdefinedby annotation on fact 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on fact '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", f.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Data.AddIsDefinedByAnnotation(f, isDefBy);
                        }
                    }
                    #endregion

                    #region CustomAnnotations
                    annotProps       = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                    while (annotProps.MoveNext()) {
                        foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)f.Value)
                                                  .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                            if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                                 ontology.Data.AddCustomAnnotation(f, (RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                            }
                            else {
                                RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                                if (custAnn         == null) {
                                    custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                    if (custAnn     == null) {
                                        custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                        if (custAnn == null) {
                                            custAnn  = new RDFOntologyResource();
                                            custAnn.Value           = t.Object;
                                            custAnn.PatternMemberID = t.Object.PatternMemberID;

                                            //Raise warning event to inform the user: custom annotation on fact 
                                            //has been imported from graph, but linking an unknown generic resource
                                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on fact '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, f.Value, t.Object));

                                        }
                                    }
                                }
                                ontology.Data.AddCustomAnnotation(f, (RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                            }

                        }
                    }
                    #endregion

                }
                #endregion

                #endregion

                #endregion

                #endregion

            }
            return ontology;
        }
 /// <summary>
 /// Removes the given literal fact from the "rdfs:comment" annotations about this ontology resource
 /// </summary>
 public RDFOntologyResource RemoveComment(RDFOntologyFact comment) {
     if (comment != null && comment.IsLiteralFact()) {
         if (this.Comment.ContainsKey(comment.PatternMemberID)) {
             this.Comment.Remove(comment.PatternMemberID);
         }
     }
     return this;
 }
Ejemplo n.º 29
0
        /// <summary>
        /// Subsumes the "owl:differentFrom" taxonomy to discover direct and indirect differentFacts of the given facts
        /// </summary>
        internal static RDFOntologyData EnlistDifferentFactsFrom_Core(RDFOntologyFact ontFact,
                                                                      RDFOntologyData data,
                                                                      Dictionary<Int64, RDFOntologyFact> visitContext) {
            var result        = new RDFOntologyData();

            #region visitContext
            if (visitContext == null) {
                visitContext  = new Dictionary<Int64, RDFOntologyFact>() { { ontFact.PatternMemberID, ontFact } };
            }
            else {
                if (!visitContext.ContainsKey(ontFact.PatternMemberID)) {
                     visitContext.Add(ontFact.PatternMemberID, ontFact);
                }
                else {
                    return result;
                }
            }
            #endregion

            // Inference: (A DIFFERENTFROM B  &&  B SAMEAS C         =>  A DIFFERENTFROM C)
            foreach (var      df in data.Relations.DifferentFrom.SelectEntriesBySubject(ontFact)) {
                result.AddFact((RDFOntologyFact)df.TaxonomyObject);
                result        = result.UnionWith(RDFSemanticsUtilities.EnlistSameFactsAs_Core((RDFOntologyFact)df.TaxonomyObject, data, visitContext));
            }

            // Inference: (A SAMEAS B         &&  B DIFFERENTFROM C  =>  A DIFFERENTFROM C)
            foreach (var     sa in RDFOntologyReasoningHelper.EnlistSameFactsAs(ontFact, data)) {
                result        = result.UnionWith(RDFSemanticsUtilities.EnlistDifferentFactsFrom_Core(sa, data, visitContext));
            }

            return result;
        }
 /// <summary>
 /// Removes the given resource fact from the "rdfs:seeAlso" annotations about this ontology resource
 /// </summary>
 public RDFOntologyResource RemoveSeeAlso(RDFOntologyFact seeAlso) {
     if (seeAlso != null && seeAlso.IsObjectFact()) {
         if (this.SeeAlso.ContainsKey(seeAlso.PatternMemberID)) {
             this.SeeAlso.Remove(seeAlso.PatternMemberID);
         }
     }
     return this;
 }
 /// <summary>
 /// Removes the given ontology attribute from the attributes of this
 /// </summary>
 public RDFOntologyFact RemoveOntologyAttribute(RDFOntologyProperty attrProperty, RDFOntologyFact attrValue) {
     if (attrProperty != null && attrValue != null) {
         var attr      = new RDFOntologyAttribute(attrProperty, attrValue);
         if (this.OntologyAttributes.ContainsKey(attr.AttributeID)) {
             this.OntologyAttributes.Remove(attr.AttributeID);
         }
     }
     return this;
 }
        /// <summary>
        /// Removes the given custom annotation from this ontology resource
        /// </summary>
        public RDFOntologyResource RemoveCustomAnnotation(RDFOntologyAnnotationProperty annotationProperty, RDFOntologyFact annotationFact) {
            if (annotationProperty != null && annotationFact != null) {

                //Redirect standard annotation properties to their own methods
                if (annotationProperty.Equals(RDFVocabulary.OWL.VERSION_INFO)) {
                    return this.RemoveVersionInfo(annotationFact);
                }
                if (annotationProperty.Equals(RDFVocabulary.RDFS.COMMENT)) {
                    return this.RemoveComment(annotationFact);
                }
                if (annotationProperty.Equals(RDFVocabulary.RDFS.LABEL)) {
                    return this.RemoveLabel(annotationFact);
                }
                if (annotationProperty.Equals(RDFVocabulary.RDFS.SEE_ALSO)) {
                    return this.RemoveSeeAlso(annotationFact);
                }
                if (annotationProperty.Equals(RDFVocabulary.RDFS.IS_DEFINED_BY)) {
                    return this.RemoveIsDefinedBy(annotationFact);
                }

                var annotAttr = new RDFOntologyAttribute(annotationProperty, annotationFact);
                if (this.CustomAnnotations.ContainsKey(annotAttr.AttributeID)) {
                    this.CustomAnnotations.Remove(annotAttr.AttributeID);
                }

            }
            return this;
        }
Ejemplo n.º 33
0
        /// <summary>
        /// Subsumes the "owl:sameAs" taxonomy to discover direct and indirect samefacts of the given facts
        /// </summary>
        internal static RDFOntologyData EnlistSameFactsAs_Core(RDFOntologyFact ontFact,
                                                               RDFOntologyData data,
                                                               Dictionary<Int64, RDFOntologyFact> visitContext) {
            var result        = new RDFOntologyData();

            #region visitContext
            if (visitContext == null) {
                visitContext  = new Dictionary<Int64, RDFOntologyFact>() { { ontFact.PatternMemberID, ontFact } };
            }
            else {
                if (!visitContext.ContainsKey(ontFact.PatternMemberID)) {
                     visitContext.Add(ontFact.PatternMemberID, ontFact);
                }
                else {
                    return result;
                }
            }
            #endregion

            // Transitivity of "owl:sameAs" taxonomy: ((A SAMEAS B)  &&  (B SAMEAS C))  =>  (A SAMEAS C)
            foreach (var      sf in data.Relations.SameAs.SelectEntriesBySubject(ontFact)) {
                result.AddFact((RDFOntologyFact)sf.TaxonomyObject);
                result        = result.UnionWith(RDFSemanticsUtilities.EnlistSameFactsAs_Core((RDFOntologyFact)sf.TaxonomyObject, data, visitContext));
            }

            return result;
        }
 /// <summary>
 /// Checks if the given aFact is sameAs the given bFact within the given data
 /// </summary>
 public static Boolean IsSameFactAs(RDFOntologyFact aFact,
                                    RDFOntologyFact bFact,
                                    RDFOntologyData data)
 {
     return(aFact != null && bFact != null && data != null ? EnlistSameFactsAs(aFact, data).Facts.ContainsKey(bFact.PatternMemberID) : false);
 }
 /// <summary>
 /// Default-ctor to build an ontology attribute with the given property and given value
 /// </summary>
 internal RDFOntologyAttribute(RDFOntologyProperty attrProperty, RDFOntologyFact attrValue) {
     if (attrProperty  != null) {
         if (attrValue != null) {
             this.AttributeProperty = attrProperty;
             this.AttributeValue    = attrValue;
             this.AttributeID       = RDFModelUtilities.CreateHash(this.ToString());
         }
         else {
             throw new RDFSemanticsException("Cannot create ontology attribute because given \"attrValue\" parameter is null.");
         }
     }
     else {
         throw new RDFSemanticsException("Cannot create ontology attribute because given \"attrProperty\" parameter is null.");
     }
 }
        /// <summary>
        /// Adds the given literal fact to the "rdfs:comment" annotations about this ontology resource
        /// </summary>
        public RDFOntologyResource AddComment(RDFOntologyFact comment) {
            if (comment != null && comment.IsLiteralFact()) {

                //Cannot make axioms on annotation properties
                if (!(this is RDFOntologyProperty && ((RDFOntologyProperty)this).IsAnnotationProperty())) {

                    //Cannot assign attributes to literal ontology facts
                    if (!(this is RDFOntologyFact && ((RDFOntologyFact)this).IsLiteralFact())) {
                        if (!this.Comment.ContainsKey(comment.PatternMemberID)) {
                            this.Comment.Add(comment.PatternMemberID, comment);
                        }
                    }

                }

            }
            return this;
        }
        /// <summary>
        /// Loads the annotations from the given graph into the given ontology
        /// </summary>
        internal static void LoadAnnotations(RDFGraph ontGraph,    RDFOntology ontology, RDFGraph rdfType,
                                             RDFGraph owlVersInfo, RDFGraph rdfsComment, RDFGraph rdfsLabel,
                                             RDFGraph rdfsSeeAlso, RDFGraph rdfsIsDefBy, RDFGraph customAnnot) {

            #region Classes
            foreach (var c in ontology.Model.ClassModel) {

                #region VersionInfo
                foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddVersionInfo(fact);
                    }
                }
                #endregion

                #region Comment
                foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddComment(fact);
                    }
                }
                #endregion

                #region Label
                foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddLabel(fact);
                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddSeeAlso(fact);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddIsDefinedBy(fact);
                    }
                }
                #endregion

                #region Custom Annotations
                foreach (var ap in customAnnot) {
                    var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                    foreach (var t   in ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                                .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                        var fact                = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact               == null) {
                            if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                fact            = new RDFOntologyFact((RDFResource)t.Object);
                            }
                            else {
                                fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                            }
                            ontology.Data.AddFact(fact);
                        }

                        c.AddCustomAnnotation(annotationProp, fact);
                    }

                }
                #endregion

            }
            #endregion

            #region Properties
            foreach (var p in ontology.Model.PropertyModel) {

                #region VersionInfo
                foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddVersionInfo(fact);
                    }
                }
                #endregion

                #region Comment
                foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddComment(fact);
                    }
                }
                #endregion

                #region Label
                foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddLabel(fact);
                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddSeeAlso(fact);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddIsDefinedBy(fact);
                    }
                }
                #endregion

                #region Custom Annotations
                foreach (var ap in customAnnot) {
                    var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                    foreach (var t   in ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                                .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                        var fact                = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact               == null) {
                            if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                fact            = new RDFOntologyFact((RDFResource)t.Object);
                            }
                            else {
                                fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                            }
                            ontology.Data.AddFact(fact);
                        }

                        p.AddCustomAnnotation(annotationProp, fact);
                    }

                }
                #endregion

            }
            #endregion

            #region Facts
            var changeLog = new Dictionary<Int64, RDFOntologyFact>();
            foreach (var  f in ontology.Data) {
                if (f.IsObjectFact()) {

                    #region VersionInfo
                    foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the literal fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddVersionInfo(fact);
                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the literal fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddComment(fact);
                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the literal fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddLabel(fact);
                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the resource fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFResource)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddSeeAlso(fact);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the resource fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFResource)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddIsDefinedBy(fact);
                        }
                    }
                    #endregion

                    #region Custom Annotations
                    foreach (var ap in customAnnot) {
                        var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                        foreach (var t   in ontGraph.SelectTriplesBySubject((RDFResource)f.Value)
                                                    .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                            var fact                = ontology.Data.SelectFact(t.Object.ToString());

                            //If the resource fact is not found in the ontology data, update the ontology data
                            if (fact               == null) {
                                if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    fact            = new RDFOntologyFact((RDFResource)t.Object);
                                }
                                else {
                                    fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                                }
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddCustomAnnotation(annotationProp, fact);
                        }

                    }
                    #endregion

                }
            }
            foreach (var cl in changeLog) {
                ontology.Data.AddFact(cl.Value);
            }
            #endregion

        }
 /// <summary>
 /// Checks if the given afact can be set differentfrom the given bfact
 /// </summary>
 internal static Boolean CheckDifferentFromCompatibility(RDFOntologyData ontologyData,
                                                         RDFOntologyFact aFact,
                                                         RDFOntologyFact bFact)
 {
     return(!ontologyData.CheckIsSameFactAs(aFact, bFact));
 }
 /// <summary>
 /// Removes the given ontology fact from the ontology facts different from this
 /// </summary>
 public RDFOntologyFact RemoveDifferentFrom(RDFOntologyFact differentFrom) {
     if (differentFrom != null) {
         if (this.DifferentFrom.ContainsKey(differentFrom.PatternMemberID)) {
             this.DifferentFrom.Remove(differentFrom.PatternMemberID);
             //Maintain consistence of "owl:differentFrom"
             differentFrom.RemoveDifferentFrom(this);
         }
     }
     return this;
 }