/// <summary> /// Loads the object properties from the given graph into the given ontology /// </summary> internal static void LoadObjectProperties(RDFGraph ontGraph, RDFOntology ontology, RDFGraph rdfType) { #region Explicit ObjectProperty foreach (var t in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.OBJECT_PROPERTY)) { var objProp = new RDFOntologyObjectProperty((RDFResource)t.Subject); ontology.Model.PropertyModel.AddProperty(objProp); #region FunctionalProperty var fp = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value) .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY) .FirstOrDefault(); if (fp != null) { objProp.SetFunctional(); } #endregion #region InverseFunctionalProperty var ifp = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value) .SelectTriplesByObject(RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY) .FirstOrDefault(); if (ifp != null) { objProp.SetInverseFunctional(); } #endregion #region SymmetricProperty var sp = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value) .SelectTriplesByObject(RDFVocabulary.OWL.SYMMETRIC_PROPERTY) .FirstOrDefault(); if (sp != null) { objProp.SetSymmetric(); } #endregion #region TransitiveProperty var tp = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value) .SelectTriplesByObject(RDFVocabulary.OWL.TRANSITIVE_PROPERTY) .FirstOrDefault(); if (tp != null) { objProp.SetTransitive(); } #endregion } #endregion #region Implicit ObjectProperty #region InverseFunctionalProperty foreach (var t in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY)) { var ivfProp = ontology.Model.PropertyModel.SelectProperty(t.Subject.ToString()); if(ivfProp == null) { ivfProp = new RDFOntologyObjectProperty((RDFResource)t.Subject); ontology.Model.PropertyModel.AddProperty(ivfProp); #region FunctionalProperty var fp = rdfType.SelectTriplesBySubject((RDFResource)ivfProp.Value) .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY) .FirstOrDefault(); if (fp != null) { ivfProp.SetFunctional(); } #endregion } ((RDFOntologyObjectProperty)ivfProp).SetInverseFunctional(); } #endregion #region SymmetricProperty foreach (var t in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.SYMMETRIC_PROPERTY)) { var symProp = ontology.Model.PropertyModel.SelectProperty(t.Subject.ToString()); if (symProp == null) { symProp = new RDFOntologyObjectProperty((RDFResource)t.Subject); ontology.Model.PropertyModel.AddProperty(symProp); #region FunctionalProperty var fp = rdfType.SelectTriplesBySubject((RDFResource)symProp.Value) .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY) .FirstOrDefault(); if (fp != null) { symProp.SetFunctional(); } #endregion } ((RDFOntologyObjectProperty)symProp).SetSymmetric(); } #endregion #region TransitiveProperty foreach (var t in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.TRANSITIVE_PROPERTY)) { var trnProp = ontology.Model.PropertyModel.SelectProperty(t.Subject.ToString()); if (trnProp == null) { trnProp = new RDFOntologyObjectProperty((RDFResource)t.Subject); ontology.Model.PropertyModel.AddProperty(trnProp); #region FunctionalProperty var fp = rdfType.SelectTriplesBySubject((RDFResource)trnProp.Value) .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY) .FirstOrDefault(); if (fp != null) { trnProp.SetFunctional(); } #endregion } ((RDFOntologyObjectProperty)trnProp).SetTransitive(); } #endregion #endregion }
/// <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; }