diff --git a/OWLSharp.Test/Ontology/OWLOntologyTest.cs b/OWLSharp.Test/Ontology/OWLOntologyTest.cs index d06f2ff6..0b67d65a 100644 --- a/OWLSharp.Test/Ontology/OWLOntologyTest.cs +++ b/OWLSharp.Test/Ontology/OWLOntologyTest.cs @@ -407,7 +407,7 @@ public void ShouldDeserializeOntology() } [TestMethod] - public void ShouldConvertOntologyWithPrefixAndImportAndAnnotationToGraph() + public async Task ShouldConvertOntologyWithPrefixAndImportAndAnnotationToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -423,7 +423,7 @@ public void ShouldConvertOntologyWithPrefixAndImportAndAnnotationToGraph() v1.0 "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.Context.Equals(new Uri("ex:ont"))); @@ -443,7 +443,7 @@ public void ShouldConvertOntologyWithPrefixAndImportAndAnnotationToGraph() } [TestMethod] - public void ShouldConvertOntologyWithDeclarationToGraph() + public async Task ShouldConvertOntologyWithDeclarationToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -453,7 +453,7 @@ public void ShouldConvertOntologyWithDeclarationToGraph() "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.TriplesCount == 2); @@ -462,7 +462,7 @@ public void ShouldConvertOntologyWithDeclarationToGraph() } [TestMethod] - public void ShouldConvertOntologyWithClassAxiomToGraph() + public async Task ShouldConvertOntologyWithClassAxiomToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -473,7 +473,7 @@ public void ShouldConvertOntologyWithClassAxiomToGraph() "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.TriplesCount == 4); @@ -484,7 +484,7 @@ public void ShouldConvertOntologyWithClassAxiomToGraph() } [TestMethod] - public void ShouldConvertOntologyWithObjectPropertyAxiomToGraph() + public async Task ShouldConvertOntologyWithObjectPropertyAxiomToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -495,7 +495,7 @@ public void ShouldConvertOntologyWithObjectPropertyAxiomToGraph() "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.TriplesCount == 4); @@ -506,7 +506,7 @@ public void ShouldConvertOntologyWithObjectPropertyAxiomToGraph() } [TestMethod] - public void ShouldConvertOntologyWithDataPropertyAxiomToGraph() + public async Task ShouldConvertOntologyWithDataPropertyAxiomToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -517,7 +517,7 @@ public void ShouldConvertOntologyWithDataPropertyAxiomToGraph() "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.TriplesCount == 4); @@ -528,7 +528,7 @@ public void ShouldConvertOntologyWithDataPropertyAxiomToGraph() } [TestMethod] - public void ShouldConvertOntologyWithDatatypeDefinitionAxiomToGraph() + public async Task ShouldConvertOntologyWithDatatypeDefinitionAxiomToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -550,7 +550,7 @@ public void ShouldConvertOntologyWithDatatypeDefinitionAxiomToGraph() "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.Context.Equals(RDFNamespaceRegister.DefaultNamespace.NamespaceUri)); @@ -569,7 +569,7 @@ public void ShouldConvertOntologyWithDatatypeDefinitionAxiomToGraph() } [TestMethod] - public void ShouldConvertOntologyWithHasKeyAxiomToGraph() + public async Task ShouldConvertOntologyWithHasKeyAxiomToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -582,7 +582,7 @@ public void ShouldConvertOntologyWithHasKeyAxiomToGraph() "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.TriplesCount == 11); @@ -598,7 +598,7 @@ public void ShouldConvertOntologyWithHasKeyAxiomToGraph() } [TestMethod] - public void ShouldConvertOntologyWithAssertionAxiomToGraph() + public async Task ShouldConvertOntologyWithAssertionAxiomToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -613,7 +613,7 @@ public void ShouldConvertOntologyWithAssertionAxiomToGraph() "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.TriplesCount == 4); @@ -624,7 +624,7 @@ public void ShouldConvertOntologyWithAssertionAxiomToGraph() } [TestMethod] - public void ShouldConvertOntologyWithAnnotationAxiomToGraph() + public async Task ShouldConvertOntologyWithAnnotationAxiomToGraphAsync() { OWLOntology ontology = OWLSerializer.DeserializeOntology( @" @@ -637,7 +637,7 @@ public void ShouldConvertOntologyWithAnnotationAxiomToGraph() States the age of a person "); - RDFGraph graph = ontology.ToRDFGraph(); + RDFGraph graph = await ontology.ToRDFGraphAsync(); Assert.IsNotNull(graph); Assert.IsTrue(graph.TriplesCount == 3); @@ -647,7 +647,7 @@ public void ShouldConvertOntologyWithAnnotationAxiomToGraph() } [TestMethod] - public void ShouldWriteOntologyToFile() + public async Task ShouldWriteOntologyToFileAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.Prefixes.Add( @@ -692,11 +692,11 @@ public void ShouldWriteOntologyToFile() new OWLAnnotationAssertion(new OWLAnnotationProperty(RDFVocabulary.RDFS.COMMENT),new RDFResource("ex:Mark"),new OWLLiteral(new RDFPlainLiteral("This is Mark")))); //Write to file - ontology.ToFile(OWLEnums.OWLFormats.OWL2XML, Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldWriteOntologyToFile.owx")); - Assert.IsTrue(File.Exists(Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldWriteOntologyToFile.owx"))); + await ontology.ToFileAsync(OWLEnums.OWLFormats.OWL2XML, Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldWriteOntologyToFileAsync.owx")); + Assert.IsTrue(File.Exists(Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldWriteOntologyToFileAsync.owx"))); //Read from file and deserialize to test content - OWLOntology ontology2 = OWLSerializer.DeserializeOntology(File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldWriteOntologyToFile.owx"))); + OWLOntology ontology2 = OWLSerializer.DeserializeOntology(File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldWriteOntologyToFileAsync.owx"))); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -726,11 +726,11 @@ public void ShouldWriteOntologyToFile() } [TestMethod] - public void ShouldThrowExceptionOnWritingOntologyToFileBecauseNullPath() - => Assert.ThrowsException(() => new OWLOntology().ToFile(OWLEnums.OWLFormats.OWL2XML, null)); + public async Task ShouldThrowExceptionOnWritingOntologyToFileBecauseNullPathAsync() + => await Assert.ThrowsExceptionAsync(async() => await new OWLOntology().ToFileAsync(OWLEnums.OWLFormats.OWL2XML, null)); [TestMethod] - public void ShouldWriteOntologyToStream() + public async Task ShouldWriteOntologyToStreamAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.Prefixes.Add( @@ -776,7 +776,7 @@ public void ShouldWriteOntologyToStream() //Write to stream MemoryStream stream = new MemoryStream(); - ontology.ToStream(OWLEnums.OWLFormats.OWL2XML, stream); + await ontology.ToStreamAsync(OWLEnums.OWLFormats.OWL2XML, stream); //Read from stream and deserialize to test content string fileContent; @@ -812,11 +812,11 @@ public void ShouldWriteOntologyToStream() } [TestMethod] - public void ShouldThrowExceptionOnWritingOntologyToStreamBecauseNullStream() - => Assert.ThrowsException(() => new OWLOntology().ToStream(OWLEnums.OWLFormats.OWL2XML, null)); + public async Task ShouldThrowExceptionOnWritingOntologyToStreamBecauseNullStreamAsync() + => await Assert.ThrowsExceptionAsync(async() => await new OWLOntology().ToStreamAsync(OWLEnums.OWLFormats.OWL2XML, null)); [TestMethod] - public void ShouldReadOntologyFromFile() + public async Task ShouldReadOntologyFromFileAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.Prefixes.Add( @@ -861,10 +861,10 @@ public void ShouldReadOntologyFromFile() new OWLAnnotationAssertion(new OWLAnnotationProperty(RDFVocabulary.RDFS.COMMENT),new RDFResource("ex:Mark"),new OWLLiteral(new RDFPlainLiteral("This is Mark")))); //Write to file - ontology.ToFile(OWLEnums.OWLFormats.OWL2XML, Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldReadOntologyFromFile.owx")); + await ontology.ToFileAsync(OWLEnums.OWLFormats.OWL2XML, Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldReadOntologyFromFileAsync.owx")); //Read from file - OWLOntology ontology2 = OWLOntology.FromFile(OWLEnums.OWLFormats.OWL2XML, Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldReadOntologyFromFile.owx")); + OWLOntology ontology2 = await OWLOntology.FromFileAsync(OWLEnums.OWLFormats.OWL2XML, Path.Combine(Environment.CurrentDirectory, "OWLOntologyTest_ShouldReadOntologyFromFileAsync.owx")); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -894,15 +894,15 @@ public void ShouldReadOntologyFromFile() } [TestMethod] - public void ShouldThrowExceptionOnReadingOntologyFromFileBecauseNullPath() - => Assert.ThrowsException(() => OWLOntology.FromFile(OWLEnums.OWLFormats.OWL2XML, null)); + public async Task ShouldThrowExceptionOnReadingOntologyFromFileBecauseNullPathAsync() + => await Assert.ThrowsExceptionAsync(async () => await OWLOntology.FromFileAsync(OWLEnums.OWLFormats.OWL2XML, null)); [TestMethod] - public void ShouldThrowExceptionOnReadingOntologyFromFileBecauseUnexistingPath() - => Assert.ThrowsException(() => OWLOntology.FromFile(OWLEnums.OWLFormats.OWL2XML, "test/test")); + public async Task ShouldThrowExceptionOnReadingOntologyFromFileBecauseUnexistingPathAsync() + => await Assert.ThrowsExceptionAsync(async () => await OWLOntology.FromFileAsync(OWLEnums.OWLFormats.OWL2XML, "test/test")); [TestMethod] - public void ShouldReadOntologyFromStream() + public async Task ShouldReadOntologyFromStreamAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.Prefixes.Add( @@ -948,10 +948,10 @@ public void ShouldReadOntologyFromStream() //Write to stream MemoryStream stream = new MemoryStream(); - ontology.ToStream(OWLEnums.OWLFormats.OWL2XML, stream); + await ontology.ToStreamAsync(OWLEnums.OWLFormats.OWL2XML, stream); //Read from stream - OWLOntology ontology2 = OWLOntology.FromStream(OWLEnums.OWLFormats.OWL2XML, new MemoryStream(stream.ToArray())); + OWLOntology ontology2 = await OWLOntology.FromStreamAsync(OWLEnums.OWLFormats.OWL2XML, new MemoryStream(stream.ToArray())); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -981,16 +981,16 @@ public void ShouldReadOntologyFromStream() } [TestMethod] - public void ShouldThrowExceptionOnReadingOntologyFromStreamBecauseNullStream() - => Assert.ThrowsException(() => OWLOntology.FromStream(OWLEnums.OWLFormats.OWL2XML, null)); + public async Task ShouldThrowExceptionOnReadingOntologyFromStreamBecauseNullStreamAsync() + => await Assert.ThrowsExceptionAsync(async() => await OWLOntology.FromStreamAsync(OWLEnums.OWLFormats.OWL2XML, null)); [TestMethod] - public void ShouldReadOntologyHeaderFromGraph() + public async Task ShouldReadOntologyHeaderFromGraphAsync() { RDFGraph graph = new RDFGraph(); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.OWL.VERSION_IRI, new RDFResource("ex:ont/v1"))); - OWLOntology ontology = OWLOntology.FromRDFGraph(graph); + OWLOntology ontology = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology); Assert.IsTrue(string.Equals(ontology.IRI, "ex:ont")); @@ -998,12 +998,12 @@ public void ShouldReadOntologyHeaderFromGraph() } [TestMethod] - public void ShouldReadOntologyHeaderWithInvalidVersionIRIFromGraph() + public async Task ShouldReadOntologyHeaderWithInvalidVersionIRIFromGraphAsync() { RDFGraph graph = new RDFGraph(); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.OWL.VERSION_IRI, new RDFPlainLiteral("ex:ont/v1"))); - OWLOntology ontology = OWLOntology.FromRDFGraph(graph); + OWLOntology ontology = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology); Assert.IsTrue(string.Equals(ontology.IRI, "ex:ont")); @@ -1011,20 +1011,20 @@ public void ShouldReadOntologyHeaderWithInvalidVersionIRIFromGraph() } [TestMethod] - public void ShouldCrashOnReadingOntologyHeaderFromGraphBecauseNoOntologyDeclaration() + public async Task ShouldCrashOnReadingOntologyHeaderFromGraphBecauseNoOntologyDeclarationAsync() { RDFGraph graph = new RDFGraph(); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.OWL.VERSION_IRI, new RDFResource("ex:ont/v1"))); - Assert.ThrowsException(() => OWLOntology.FromRDFGraph(graph)); + await Assert.ThrowsExceptionAsync(async() => await OWLOntology.FromRDFGraphAsync(graph)); } [TestMethod] - public void ShouldReadImportsFromGraph() + public async Task ShouldReadImportsFromGraphAsync() { RDFGraph graph = new RDFGraph(); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.OWL.IMPORTS, new RDFResource("ex:ont2"))); - OWLOntology ontology = OWLOntology.FromRDFGraph(graph); + OWLOntology ontology = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology); Assert.IsTrue(string.Equals(ontology.IRI, "ex:ont")); @@ -1034,12 +1034,12 @@ public void ShouldReadImportsFromGraph() } [TestMethod] - public void ShouldReadPrefixesFromGraph() + public async Task ShouldReadPrefixesFromGraphAsync() { RDFGraph graph = new RDFGraph(); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.FOAF.MAKER, new RDFResource("ex:Mark"))); - OWLOntology ontology = OWLOntology.FromRDFGraph(graph); + OWLOntology ontology = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology); Assert.IsTrue(string.Equals(ontology.IRI, "ex:ont")); @@ -1053,7 +1053,7 @@ public void ShouldReadPrefixesFromGraph() } [TestMethod] - public void ShouldReadDeclarationsFromGraph() + public async Task ShouldReadDeclarationsFromGraphAsync() { RDFGraph graph = new RDFGraph(); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); @@ -1064,7 +1064,7 @@ public void ShouldReadDeclarationsFromGraph() graph.AddTriple(new RDFTriple(RDFVocabulary.FOAF.NAME, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DATATYPE_PROPERTY)); graph.AddTriple(new RDFTriple(RDFVocabulary.FOAF.MAKER, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY)); graph.AddTriple(new RDFTriple(new RDFResource("ex:Alice"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NAMED_INDIVIDUAL)); - OWLOntology ontology = OWLOntology.FromRDFGraph(graph); + OWLOntology ontology = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology); Assert.IsTrue(string.Equals(ontology.IRI, "ex:ont")); @@ -1087,7 +1087,7 @@ public void ShouldReadDeclarationsFromGraph() } [TestMethod] - public void ShouldReadOntologyAnnotationsFromGraph() + public async Task ShouldReadOntologyAnnotationsFromGraphAsync() { RDFGraph graph = new RDFGraph(); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont2"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); @@ -1103,7 +1103,7 @@ public void ShouldReadOntologyAnnotationsFromGraph() graph.AddTriple(new RDFTriple(new RDFResource("ex:ont2"), RDFVocabulary.RDFS.IS_DEFINED_BY, new RDFResource("ex:ont2"))); graph.AddTriple(new RDFTriple(RDFVocabulary.DC.CREATOR, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY)); graph.AddTriple(new RDFTriple(new RDFResource("ex:ont2"), RDFVocabulary.DC.CREATOR, new RDFResource("ex:Test"))); - OWLOntology ontology = OWLOntology.FromRDFGraph(graph); + OWLOntology ontology = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology); Assert.IsTrue(string.Equals(ontology.IRI, "ex:ont2")); @@ -1122,7 +1122,7 @@ public void ShouldReadOntologyAnnotationsFromGraph() } [TestMethod] - public void ShouldReadOntologyNestedAnnotationsFromGraph() + public async Task ShouldReadOntologyNestedAnnotationsFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.Annotations.Add( @@ -1133,8 +1133,8 @@ public void ShouldReadOntologyNestedAnnotationsFromGraph() Annotation = new OWLAnnotation(new OWLAnnotationProperty(RDFVocabulary.DC.DESCRIPTION), new RDFResource("ex:ann")) } }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1149,7 +1149,7 @@ public void ShouldReadOntologyNestedAnnotationsFromGraph() } [TestMethod] - public void ShouldReadAsymmetricObjectPropertyFromGraph() + public async Task ShouldReadAsymmetricObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1178,8 +1178,8 @@ public void ShouldReadAsymmetricObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1208,7 +1208,7 @@ public void ShouldReadAsymmetricObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadSymmetricObjectPropertyFromGraph() + public async Task ShouldReadSymmetricObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1237,8 +1237,8 @@ public void ShouldReadSymmetricObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1267,7 +1267,7 @@ public void ShouldReadSymmetricObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadIrreflexiveObjectPropertyFromGraph() + public async Task ShouldReadIrreflexiveObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1296,8 +1296,8 @@ public void ShouldReadIrreflexiveObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1326,7 +1326,7 @@ public void ShouldReadIrreflexiveObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadReflexiveObjectPropertyFromGraph() + public async Task ShouldReadReflexiveObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1355,8 +1355,8 @@ public void ShouldReadReflexiveObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1385,7 +1385,7 @@ public void ShouldReadReflexiveObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadTransitiveObjectPropertyFromGraph() + public async Task ShouldReadTransitiveObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1414,8 +1414,8 @@ public void ShouldReadTransitiveObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1444,7 +1444,7 @@ public void ShouldReadTransitiveObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadInverseFunctionalObjectPropertyFromGraph() + public async Task ShouldReadInverseFunctionalObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1473,8 +1473,8 @@ public void ShouldReadInverseFunctionalObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1503,7 +1503,7 @@ public void ShouldReadInverseFunctionalObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadFunctionalObjectPropertyFromGraph() + public async Task ShouldReadFunctionalObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1532,8 +1532,8 @@ public void ShouldReadFunctionalObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1562,7 +1562,7 @@ public void ShouldReadFunctionalObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadInverseObjectPropertyFromGraph() + public async Task ShouldReadInverseObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1616,8 +1616,8 @@ public void ShouldReadInverseObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1670,7 +1670,7 @@ public void ShouldReadInverseObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadEquivalentObjectPropertyFromGraph() + public async Task ShouldReadEquivalentObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1720,8 +1720,8 @@ public void ShouldReadEquivalentObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1774,7 +1774,7 @@ public void ShouldReadEquivalentObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadDisjointObjectPropertyFromGraph() + public async Task ShouldReadDisjointObjectPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -1851,8 +1851,8 @@ public void ShouldReadDisjointObjectPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -1927,7 +1927,7 @@ public void ShouldReadDisjointObjectPropertyFromGraph() } [TestMethod] - public void ShouldReadSubObjectPropertyOfFromGraph() + public async Task ShouldReadSubObjectPropertyOfFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -2020,8 +2020,8 @@ public void ShouldReadSubObjectPropertyOfFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2110,7 +2110,7 @@ public void ShouldReadSubObjectPropertyOfFromGraph() } [TestMethod] - public void ShouldReadObjectPropertyDomainGraph() + public async Task ShouldReadObjectPropertyDomainGraph() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -2195,8 +2195,8 @@ public void ShouldReadObjectPropertyDomainGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2294,7 +2294,7 @@ public void ShouldReadObjectPropertyDomainGraph() } [TestMethod] - public void ShouldReadObjectPropertyRangeGraph() + public async Task ShouldReadObjectPropertyRangeGraph() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ObjectPropertyAxioms.Add( @@ -2378,8 +2378,8 @@ public void ShouldReadObjectPropertyRangeGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2474,7 +2474,7 @@ public void ShouldReadObjectPropertyRangeGraph() } [TestMethod] - public void ShouldReadFunctionalDataPropertyFromGraph() + public async Task ShouldReadFunctionalDataPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DataPropertyAxioms.Add( @@ -2503,8 +2503,8 @@ public void ShouldReadFunctionalDataPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2531,7 +2531,7 @@ public void ShouldReadFunctionalDataPropertyFromGraph() } [TestMethod] - public void ShouldReadEquivalentDataPropertyFromGraph() + public async Task ShouldReadEquivalentDataPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DataPropertyAxioms.Add( @@ -2560,8 +2560,8 @@ public void ShouldReadEquivalentDataPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2594,7 +2594,7 @@ public void ShouldReadEquivalentDataPropertyFromGraph() } [TestMethod] - public void ShouldReadDisjointDataPropertyFromGraph() + public async Task ShouldReadDisjointDataPropertyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DataPropertyAxioms.Add( @@ -2626,8 +2626,8 @@ public void ShouldReadDisjointDataPropertyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2657,7 +2657,7 @@ public void ShouldReadDisjointDataPropertyFromGraph() } [TestMethod] - public void ShouldReadSubDataPropertyOfFromGraph() + public async Task ShouldReadSubDataPropertyOfFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DataPropertyAxioms.Add( @@ -2688,8 +2688,8 @@ public void ShouldReadSubDataPropertyOfFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2718,7 +2718,7 @@ public void ShouldReadSubDataPropertyOfFromGraph() } [TestMethod] - public void ShouldReadSubClassOfFromGraph() + public async Task ShouldReadSubClassOfFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ClassAxioms.Add( @@ -2733,8 +2733,8 @@ public void ShouldReadSubClassOfFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2751,7 +2751,7 @@ public void ShouldReadSubClassOfFromGraph() } [TestMethod] - public void ShouldReadEquivalentClassesGraph() + public async Task ShouldReadEquivalentClassesGraph() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ClassAxioms.Add( @@ -2766,8 +2766,8 @@ public void ShouldReadEquivalentClassesGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2784,7 +2784,7 @@ public void ShouldReadEquivalentClassesGraph() } [TestMethod] - public void ShouldReadDisjointClassesFromGraph() + public async Task ShouldReadDisjointClassesFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ClassAxioms.Add( @@ -2812,8 +2812,8 @@ public void ShouldReadDisjointClassesFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2844,7 +2844,7 @@ public void ShouldReadDisjointClassesFromGraph() } [TestMethod] - public void ShouldReadDisjointUnionFromGraph() + public async Task ShouldReadDisjointUnionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.ClassAxioms.Add( @@ -2859,8 +2859,8 @@ public void ShouldReadDisjointUnionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2880,7 +2880,7 @@ public void ShouldReadDisjointUnionFromGraph() } [TestMethod] - public void ShouldReadHasKeyFromGraph() + public async Task ShouldReadHasKeyFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.KeyAxioms.Add( @@ -2896,8 +2896,8 @@ public void ShouldReadHasKeyFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2918,7 +2918,7 @@ public void ShouldReadHasKeyFromGraph() } [TestMethod] - public void ShouldReadDatatypeDefinitionFromGraph() + public async Task ShouldReadDatatypeDefinitionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DatatypeDefinitionAxioms.Add( @@ -2948,8 +2948,8 @@ [new OWLFacetRestriction(new OWLLiteral(new RDFTypedLiteral("6", RDFModelEnums.R } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -2978,7 +2978,7 @@ [new OWLFacetRestriction(new OWLLiteral(new RDFTypedLiteral("6", RDFModelEnums.R } [TestMethod] - public void ShouldReadSameIndividualFromGraph() + public async Task ShouldReadSameIndividualFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AssertionAxioms.Add( @@ -3041,8 +3041,8 @@ public void ShouldReadSameIndividualFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3095,7 +3095,7 @@ public void ShouldReadSameIndividualFromGraph() } [TestMethod] - public void ShouldReadDifferentIndividualsFromGraph() + public async Task ShouldReadDifferentIndividualsFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AssertionAxioms.Add( @@ -3172,8 +3172,8 @@ public void ShouldReadDifferentIndividualsFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3248,7 +3248,7 @@ public void ShouldReadDifferentIndividualsFromGraph() } [TestMethod] - public void ShouldReadObjectPropertyAssertionFromGraph() + public async Task ShouldReadObjectPropertyAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AssertionAxioms.Add( @@ -3315,8 +3315,8 @@ public void ShouldReadObjectPropertyAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3385,7 +3385,7 @@ public void ShouldReadObjectPropertyAssertionFromGraph() } [TestMethod] - public void ShouldReadNegativeObjectPropertyAssertionFromGraph() + public async Task ShouldReadNegativeObjectPropertyAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AssertionAxioms.Add( @@ -3452,8 +3452,8 @@ public void ShouldReadNegativeObjectPropertyAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3522,7 +3522,7 @@ public void ShouldReadNegativeObjectPropertyAssertionFromGraph() } [TestMethod] - public void ShouldReadDataPropertyAssertionFromGraph() + public async Task ShouldReadDataPropertyAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AssertionAxioms.Add( @@ -3555,8 +3555,8 @@ public void ShouldReadDataPropertyAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3589,7 +3589,7 @@ public void ShouldReadDataPropertyAssertionFromGraph() } [TestMethod] - public void ShouldReadNegativeDataPropertyAssertionFromGraph() + public async Task ShouldReadNegativeDataPropertyAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AssertionAxioms.Add( @@ -3622,8 +3622,8 @@ public void ShouldReadNegativeDataPropertyAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3656,7 +3656,7 @@ public void ShouldReadNegativeDataPropertyAssertionFromGraph() } [TestMethod] - public void ShouldReadDataPropertyDomainGraph() + public async Task ShouldReadDataPropertyDomainGraph() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DataPropertyAxioms.Add( @@ -3671,8 +3671,8 @@ public void ShouldReadDataPropertyDomainGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3690,7 +3690,7 @@ public void ShouldReadDataPropertyDomainGraph() } [TestMethod] - public void ShouldReadDataPropertyRangeGraph() + public async Task ShouldReadDataPropertyRangeGraph() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DataPropertyAxioms.Add( @@ -3705,8 +3705,8 @@ public void ShouldReadDataPropertyRangeGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3724,7 +3724,7 @@ public void ShouldReadDataPropertyRangeGraph() } [TestMethod] - public void ShouldReadClassAssertionFromGraph() + public async Task ShouldReadClassAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AssertionAxioms.Add( @@ -3770,8 +3770,8 @@ [new OWLFacetRestriction(new OWLLiteral(new RDFTypedLiteral("6", RDFModelEnums.R } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3828,7 +3828,7 @@ [new OWLFacetRestriction(new OWLLiteral(new RDFTypedLiteral("6", RDFModelEnums.R } [TestMethod] - public void ShouldReadSubAnnotationPropertyOfFromGraph() + public async Task ShouldReadSubAnnotationPropertyOfFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AnnotationAxioms.Add( @@ -3843,8 +3843,8 @@ public void ShouldReadSubAnnotationPropertyOfFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3861,7 +3861,7 @@ public void ShouldReadSubAnnotationPropertyOfFromGraph() } [TestMethod] - public void ShouldReadAnnotationPropertyDomainFromGraph() + public async Task ShouldReadAnnotationPropertyDomainFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AnnotationAxioms.Add( @@ -3888,8 +3888,8 @@ public void ShouldReadAnnotationPropertyDomainFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3914,7 +3914,7 @@ public void ShouldReadAnnotationPropertyDomainFromGraph() } [TestMethod] - public void ShouldReadAnnotationPropertyRangeFromGraph() + public async Task ShouldReadAnnotationPropertyRangeFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.AnnotationAxioms.Add( @@ -3941,8 +3941,8 @@ public void ShouldReadAnnotationPropertyRangeFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -3967,7 +3967,7 @@ public void ShouldReadAnnotationPropertyRangeFromGraph() } [TestMethod] - public void ShouldReadClassAnnotationAssertionFromGraph() + public async Task ShouldReadClassAnnotationAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DeclarationAxioms.Add(new OWLDeclaration(new OWLClass(RDFVocabulary.FOAF.PERSON))); @@ -3997,8 +3997,8 @@ public void ShouldReadClassAnnotationAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -4025,7 +4025,7 @@ public void ShouldReadClassAnnotationAssertionFromGraph() } [TestMethod] - public void ShouldReadDatatypeAnnotationAssertionFromGraph() + public async Task ShouldReadDatatypeAnnotationAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DeclarationAxioms.Add(new OWLDeclaration(new OWLDatatype(RDFVocabulary.XSD.INTEGER))); @@ -4055,8 +4055,8 @@ public void ShouldReadDatatypeAnnotationAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -4083,7 +4083,7 @@ public void ShouldReadDatatypeAnnotationAssertionFromGraph() } [TestMethod] - public void ShouldReadObjectPropertyAnnotationAssertionFromGraph() + public async Task ShouldReadObjectPropertyAnnotationAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DeclarationAxioms.Add(new OWLDeclaration(new OWLObjectProperty(RDFVocabulary.FOAF.KNOWS))); @@ -4113,8 +4113,8 @@ public void ShouldReadObjectPropertyAnnotationAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -4141,7 +4141,7 @@ public void ShouldReadObjectPropertyAnnotationAssertionFromGraph() } [TestMethod] - public void ShouldReadDataPropertyAnnotationAssertionFromGraph() + public async Task ShouldReadDataPropertyAnnotationAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DeclarationAxioms.Add(new OWLDeclaration(new OWLDataProperty(RDFVocabulary.FOAF.AGE))); @@ -4171,8 +4171,8 @@ public void ShouldReadDataPropertyAnnotationAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -4199,7 +4199,7 @@ public void ShouldReadDataPropertyAnnotationAssertionFromGraph() } [TestMethod] - public void ShouldReadAnnotationPropertyAnnotationAssertionFromGraph() + public async Task ShouldReadAnnotationPropertyAnnotationAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DeclarationAxioms.Add(new OWLDeclaration(new OWLAnnotationProperty(RDFVocabulary.RDFS.COMMENT))); @@ -4229,8 +4229,8 @@ public void ShouldReadAnnotationPropertyAnnotationAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -4257,7 +4257,7 @@ public void ShouldReadAnnotationPropertyAnnotationAssertionFromGraph() } [TestMethod] - public void ShouldReadIndividualAnnotationAssertionFromGraph() + public async Task ShouldReadIndividualAnnotationAssertionFromGraphAsync() { OWLOntology ontology = new OWLOntology(new Uri("ex:ont"), new Uri("ex:ont/v1")); ontology.DeclarationAxioms.Add(new OWLDeclaration(new OWLNamedIndividual(new RDFResource("ex:Idv1")))); @@ -4287,8 +4287,8 @@ public void ShouldReadIndividualAnnotationAssertionFromGraph() } ] }); - RDFGraph graph = ontology.ToRDFGraph(); - OWLOntology ontology2 = OWLOntology.FromRDFGraph(graph); + RDFGraph graph = await ontology.ToRDFGraphAsync(); + OWLOntology ontology2 = await OWLOntology.FromRDFGraphAsync(graph); Assert.IsNotNull(ontology2); Assert.IsTrue(string.Equals(ontology2.IRI, "ex:ont")); @@ -4322,7 +4322,7 @@ public async Task ShouldImportOntologyAsync() Assert.IsTrue(ontology.Annotations.Count == 1); Assert.IsTrue(ontology.Annotations.Single().AnnotationProperty.GetIRI().Equals(RDFVocabulary.OWL.IMPORTS) - && string.Equals(ontology.Annotations.Single().ValueIRI, "http://www.w3.org/2004/02/skos/core")); + && string.Equals(ontology.Annotations.Single().ValueIRI, "http://www.w3.org/2004/02/skos/core")); Assert.IsTrue(ontology.AnnotationAxioms.Count == 51); Assert.IsTrue(ontology.AnnotationAxioms.TrueForAll(ax => ax.IsImport)); Assert.IsTrue(ontology.ClassAxioms.Count == 4); diff --git a/OWLSharp/Ontology/OWLOntology.cs b/OWLSharp/Ontology/OWLOntology.cs index 4427e83a..9be67bb4 100644 --- a/OWLSharp/Ontology/OWLOntology.cs +++ b/OWLSharp/Ontology/OWLOntology.cs @@ -163,2290 +163,2287 @@ public OWLOntology(OWLOntology ontology) #endregion #region Methods - public RDFGraph ToRDFGraph() - { - RDFGraph graph = new RDFGraph(); + public Task ToRDFGraphAsync() + => Task.Run(() => + { + RDFGraph graph = new RDFGraph(); + + //IRI + RDFResource ontologyIRI = new RDFResource(); + if (!string.IsNullOrWhiteSpace(IRI)) + ontologyIRI = new RDFResource(IRI); + graph.AddTriple(new RDFTriple(ontologyIRI, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); + + //VersionIRI + if (!string.IsNullOrWhiteSpace(VersionIRI)) + graph.AddTriple(new RDFTriple(ontologyIRI, RDFVocabulary.OWL.VERSION_IRI, new RDFResource(VersionIRI))); + + //Imports + foreach (OWLImport import in Imports) + graph.AddTriple(new RDFTriple(ontologyIRI, RDFVocabulary.OWL.IMPORTS, new RDFResource(import.IRI))); + + //Annotations + foreach (OWLAnnotation annotation in Annotations) + graph = graph.UnionWith(annotation.ToRDFGraphInternal(ontologyIRI)); + + //Axioms + foreach (OWLDeclaration declarationAxiom in DeclarationAxioms) + graph = graph.UnionWith(declarationAxiom.ToRDFGraph()); + foreach (OWLClassAxiom classAxiom in ClassAxioms) + graph = graph.UnionWith(classAxiom.ToRDFGraph()); + foreach (OWLObjectPropertyAxiom objectPropertyAxiom in ObjectPropertyAxioms) + graph = graph.UnionWith(objectPropertyAxiom.ToRDFGraph()); + foreach (OWLDataPropertyAxiom dataPropertyAxiom in DataPropertyAxioms) + graph = graph.UnionWith(dataPropertyAxiom.ToRDFGraph()); + foreach (OWLDatatypeDefinition datatypeDefinitionAxiom in DatatypeDefinitionAxioms) + graph = graph.UnionWith(datatypeDefinitionAxiom.ToRDFGraph()); + foreach (OWLHasKey keyAxiom in KeyAxioms) + graph = graph.UnionWith(keyAxiom.ToRDFGraph()); + foreach (OWLAssertionAxiom assertionAxiom in AssertionAxioms) + graph = graph.UnionWith(assertionAxiom.ToRDFGraph()); + foreach (OWLAnnotationAxiom annotationAxiom in AnnotationAxioms) + graph = graph.UnionWith(annotationAxiom.ToRDFGraph()); + + //IRI => Context + if (!ontologyIRI.IsBlank) + graph.SetContext(ontologyIRI.URI); + + return graph; + }); + + public Task ToFileAsync(OWLEnums.OWLFormats owlFormat, string outputFile) + { + if (string.IsNullOrWhiteSpace(outputFile)) + throw new OWLException("Cannot write ontology to file because given \"outputFile\" parameter is null or empty"); - //IRI - RDFResource ontologyIRI = new RDFResource(); - if (!string.IsNullOrWhiteSpace(IRI)) - ontologyIRI = new RDFResource(IRI); - graph.AddTriple(new RDFTriple(ontologyIRI, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY)); + return ToStreamAsync(owlFormat, new FileStream(outputFile, FileMode.Create)); + } - //VersionIRI - if (!string.IsNullOrWhiteSpace(VersionIRI)) - graph.AddTriple(new RDFTriple(ontologyIRI, RDFVocabulary.OWL.VERSION_IRI, new RDFResource(VersionIRI))); + public Task ToStreamAsync(OWLEnums.OWLFormats owlFormat, Stream outputStream) + => Task.Run(() => + { + if (outputStream == null) + throw new OWLException("Cannot write ontology to stream because given \"outputStream\" parameter is null"); - //Imports - foreach (OWLImport import in Imports) - graph.AddTriple(new RDFTriple(ontologyIRI, RDFVocabulary.OWL.IMPORTS, new RDFResource(import.IRI))); + try + { + switch (owlFormat) + { + case OWLEnums.OWLFormats.OWL2XML: + default: + string ontology = OWLSerializer.SerializeOntology(this); + using (StreamWriter streamWriter = new StreamWriter(outputStream, RDFModelUtilities.UTF8_NoBOM)) + streamWriter.Write(ontology); + break; + } + } + catch(Exception ex) + { + throw new OWLException($"Cannot write ontology to stream because: {ex.Message}", ex); + } + }); - //Annotations - foreach (OWLAnnotation annotation in Annotations) - graph = graph.UnionWith(annotation.ToRDFGraphInternal(ontologyIRI)); + public static Task FromRDFGraphAsync(RDFGraph graph) + => Task.Run(() => + { + if (graph == null) + throw new OWLException("Cannot read ontology from graph because: given \"graph\" parameter is null"); + + RDFGraph typeGraph = graph[null, RDFVocabulary.RDF.TYPE, null, null]; + + #region Utilities + //Ontology + void LoadOntology(out OWLOntology ont) + { + string ontIRI = typeGraph[null, null, RDFVocabulary.OWL.ONTOLOGY, null] + .FirstOrDefault()?.Subject.ToString() ?? throw new OWLException("Cannot find an owl:Ontology definition in the given graph!"); + ont = new OWLOntology() + { + IRI = ontIRI, + VersionIRI = (graph[new RDFResource(ontIRI), RDFVocabulary.OWL.VERSION_IRI, null, null] + .FirstOrDefault()?.Object as RDFResource)?.ToString() + }; + } + void LoadImports(OWLOntology ont) + { + foreach (RDFTriple imports in graph[new RDFResource(ont.IRI), RDFVocabulary.OWL.IMPORTS, null, null] + .Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO)) + ont.Imports.Add(new OWLImport((RDFResource)imports.Object)); + } + void LoadDeclarations(OWLOntology ont) + { + HashSet namedIndividuals = new HashSet(); - //Axioms - foreach (OWLDeclaration declarationAxiom in DeclarationAxioms) - graph = graph.UnionWith(declarationAxiom.ToRDFGraph()); - foreach (OWLClassAxiom classAxiom in ClassAxioms) - graph = graph.UnionWith(classAxiom.ToRDFGraph()); - foreach (OWLObjectPropertyAxiom objectPropertyAxiom in ObjectPropertyAxioms) - graph = graph.UnionWith(objectPropertyAxiom.ToRDFGraph()); - foreach (OWLDataPropertyAxiom dataPropertyAxiom in DataPropertyAxioms) - graph = graph.UnionWith(dataPropertyAxiom.ToRDFGraph()); - foreach (OWLDatatypeDefinition datatypeDefinitionAxiom in DatatypeDefinitionAxioms) - graph = graph.UnionWith(datatypeDefinitionAxiom.ToRDFGraph()); - foreach (OWLHasKey keyAxiom in KeyAxioms) - graph = graph.UnionWith(keyAxiom.ToRDFGraph()); - foreach (OWLAssertionAxiom assertionAxiom in AssertionAxioms) - graph = graph.UnionWith(assertionAxiom.ToRDFGraph()); - foreach (OWLAnnotationAxiom annotationAxiom in AnnotationAxioms) - graph = graph.UnionWith(annotationAxiom.ToRDFGraph()); - - if (!ontologyIRI.IsBlank) - graph.SetContext(ontologyIRI.URI); - return graph; - } + //Class, Datatype, ObjectProperty, DataProperty, AnnotationProperty, NamedIndividual + foreach (RDFTriple typeTriple in typeGraph.Where(t => !((RDFResource)t.Subject).IsBlank)) + { + if (typeTriple.Object.Equals(RDFVocabulary.OWL.CLASS) || typeTriple.Object.Equals(RDFVocabulary.RDFS.CLASS)) + { + ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLClass((RDFResource)typeTriple.Subject))); + continue; + } + + if (typeTriple.Object.Equals(RDFVocabulary.RDFS.DATATYPE)) + { + ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLDatatype((RDFResource)typeTriple.Subject))); + continue; + } + + if (typeTriple.Object.Equals(RDFVocabulary.OWL.OBJECT_PROPERTY)) + { + ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLObjectProperty((RDFResource)typeTriple.Subject))); + continue; + } + + if (typeTriple.Object.Equals(RDFVocabulary.OWL.DATATYPE_PROPERTY)) + { + ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLDataProperty((RDFResource)typeTriple.Subject))); + continue; + } + + if (typeTriple.Object.Equals(RDFVocabulary.OWL.ANNOTATION_PROPERTY)) + { + ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLAnnotationProperty((RDFResource)typeTriple.Subject))); + continue; + } + + if (typeTriple.Object.Equals(RDFVocabulary.OWL.NAMED_INDIVIDUAL)) + { + namedIndividuals.Add(typeTriple.Subject.ToString()); + continue; + } + } - public void ToFile(OWLEnums.OWLFormats owlFormat, string outputFile) - { - #region Guards - if (string.IsNullOrWhiteSpace(outputFile)) - throw new OWLException("Cannot write ontology to file because given \"outputFile\" parameter is null or empty"); - #endregion + //NamedIndividual (undeclared, type-inferred via SPARQL) + RDFSelectQuery namedIdvQuery = new RDFSelectQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS).UnionWithNext()) + .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.RESTRICTION)) + .AddPattern(new RDFPattern(new RDFVariable("?NIDV"), RDFVocabulary.RDF.TYPE, new RDFVariable("?CLS"))) + .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?NIDV"), new RDFVariable("?CLS")))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?NIDV")))) + .AddProjectionVariable(new RDFVariable("?NIDV")); + RDFSelectQueryResult namedIdvQueryResult = namedIdvQuery.ApplyToGraph(typeGraph); + foreach (DataRow nidvRow in namedIdvQueryResult.SelectResults.Rows) + namedIndividuals.Add(nidvRow["?NIDV"].ToString()); + foreach (string namedIndividual in namedIndividuals.Distinct()) + ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLNamedIndividual(new RDFResource(namedIndividual)))); + } + void PrefetchAnnotationAxioms(OWLOntology ont, out RDFGraph annAxiomsGraph) + { + RDFConstructQuery query = new RDFConstructQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, new RDFVariable("?ANNOTATED_SOURCE"))) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, new RDFVariable("?ANNOTATED_PROPERTY"))) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, new RDFVariable("?ANNOTATED_TARGET"))) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) + .AddPattern(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY))) + .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) + .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, new RDFVariable("?ANNOTATED_SOURCE"))) + .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, new RDFVariable("?ANNOTATED_PROPERTY"))) + .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, new RDFVariable("?ANNOTATED_TARGET"))) + .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) + .AddTemplate(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY)); + RDFConstructQueryResult result = query.ApplyToGraph(graph); + annAxiomsGraph = result.ToRDFGraph(); + } + void LoadOntologyAnnotations(OWLOntology ont, RDFGraph annAxiomsGraph) + { + LoadIRIAnnotations(ont, new List() { + RDFVocabulary.OWL.BACKWARD_COMPATIBLE_WITH, + RDFVocabulary.OWL.INCOMPATIBLE_WITH, + RDFVocabulary.OWL.PRIOR_VERSION, + RDFVocabulary.OWL.VERSION_INFO, + RDFVocabulary.OWL.DEPRECATED, + RDFVocabulary.RDFS.COMMENT, + RDFVocabulary.RDFS.LABEL, + RDFVocabulary.RDFS.SEE_ALSO, + RDFVocabulary.RDFS.IS_DEFINED_BY + }, new RDFResource(ont.IRI), annAxiomsGraph, out List ontologyAnnotations); + ont.Annotations = ontologyAnnotations; + } + //Axioms + void LoadFunctionalObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple funcPropTriple in typeGraph[null, null, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)funcPropTriple.Subject, out OWLObjectPropertyExpression opex); + if (opex != null) + { + OWLFunctionalObjectProperty functionalObjectProperty = new OWLFunctionalObjectProperty() { + ObjectPropertyExpression = opex }; - ToStream(owlFormat, new FileStream(outputFile, FileMode.Create)); - } + LoadAxiomAnnotations(ont, funcPropTriple, functionalObjectProperty, annAxiomsGraph); - public void ToStream(OWLEnums.OWLFormats owlFormat, Stream outputStream) - { - #region Guards - if (outputStream == null) - throw new OWLException("Cannot write ontology to stream because given \"outputStream\" parameter is null"); - #endregion - - try - { - switch (owlFormat) - { - case OWLEnums.OWLFormats.OWL2XML: - default: - string ontology = OWLSerializer.SerializeOntology(this); - using (StreamWriter streamWriter = new StreamWriter(outputStream, RDFModelUtilities.UTF8_NoBOM)) - streamWriter.Write(ontology); - break; - } - } - catch(Exception ex) - { - throw new OWLException($"Cannot write ontology to stream because: {ex.Message}", ex); - } - } + ont.ObjectPropertyAxioms.Add(functionalObjectProperty); + } + } + } + void LoadInverseFunctionalObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple invfuncPropTriple in typeGraph[null, null, RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)invfuncPropTriple.Subject, out OWLObjectPropertyExpression opex); + if (opex != null) + { + OWLInverseFunctionalObjectProperty inverseFunctionalObjectProperty = new OWLInverseFunctionalObjectProperty() { + ObjectPropertyExpression = opex }; - public static OWLOntology FromRDFGraph(RDFGraph graph) - { - #region Guards - if (graph == null) - throw new OWLException("Cannot read ontology from graph because: given \"graph\" parameter is null"); - #endregion + LoadAxiomAnnotations(ont, invfuncPropTriple, inverseFunctionalObjectProperty, annAxiomsGraph); - RDFGraph typeGraph = graph[null, RDFVocabulary.RDF.TYPE, null, null]; + ont.ObjectPropertyAxioms.Add(inverseFunctionalObjectProperty); + } + } + } + void LoadSymmetricObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple symPropTriple in typeGraph[null, null, RDFVocabulary.OWL.SYMMETRIC_PROPERTY, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)symPropTriple.Subject, out OWLObjectPropertyExpression opex); + if (opex != null) + { + OWLSymmetricObjectProperty symmetricObjectProperty = new OWLSymmetricObjectProperty() { + ObjectPropertyExpression = opex }; - #region Utilities - //Ontology - void LoadOntology(out OWLOntology ont) - { - string ontIRI = typeGraph[null, null, RDFVocabulary.OWL.ONTOLOGY, null] - .FirstOrDefault()?.Subject.ToString() ?? throw new OWLException("Cannot find an owl:Ontology definition in the given graph!"); - ont = new OWLOntology() - { - IRI = ontIRI, - VersionIRI = (graph[new RDFResource(ontIRI), RDFVocabulary.OWL.VERSION_IRI, null, null] - .FirstOrDefault()?.Object as RDFResource)?.ToString() - }; - } - void LoadImports(OWLOntology ont) - { - foreach (RDFTriple imports in graph[new RDFResource(ont.IRI), RDFVocabulary.OWL.IMPORTS, null, null] - .Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO)) - ont.Imports.Add(new OWLImport((RDFResource)imports.Object)); - } - void LoadDeclarations(OWLOntology ont) - { - HashSet namedIndividuals = new HashSet(); + LoadAxiomAnnotations(ont, symPropTriple, symmetricObjectProperty, annAxiomsGraph); - //Class, Datatype, ObjectProperty, DataProperty, AnnotationProperty, NamedIndividual - foreach (RDFTriple typeTriple in typeGraph.Where(t => !((RDFResource)t.Subject).IsBlank)) - { - if (typeTriple.Object.Equals(RDFVocabulary.OWL.CLASS) || typeTriple.Object.Equals(RDFVocabulary.RDFS.CLASS)) + ont.ObjectPropertyAxioms.Add(symmetricObjectProperty); + } + } + } + void LoadAsymmetricObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) { - ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLClass((RDFResource)typeTriple.Subject))); - continue; + foreach (RDFTriple asymPropTriple in typeGraph[null, null, RDFVocabulary.OWL.ASYMMETRIC_PROPERTY, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)asymPropTriple.Subject, out OWLObjectPropertyExpression opex); + if (opex != null) + { + OWLAsymmetricObjectProperty asymmetricObjectProperty = new OWLAsymmetricObjectProperty() { + ObjectPropertyExpression = opex }; + + LoadAxiomAnnotations(ont, asymPropTriple, asymmetricObjectProperty, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(asymmetricObjectProperty); + } + } } + void LoadReflexiveObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple refPropTriple in typeGraph[null, null, RDFVocabulary.OWL.REFLEXIVE_PROPERTY, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)refPropTriple.Subject, out OWLObjectPropertyExpression opex); + if (opex != null) + { + OWLReflexiveObjectProperty reflexiveObjectProperty = new OWLReflexiveObjectProperty() { + ObjectPropertyExpression = opex }; + + LoadAxiomAnnotations(ont, refPropTriple, reflexiveObjectProperty, annAxiomsGraph); - if (typeTriple.Object.Equals(RDFVocabulary.RDFS.DATATYPE)) + ont.ObjectPropertyAxioms.Add(reflexiveObjectProperty); + } + } + } + void LoadIrreflexiveObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) { - ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLDatatype((RDFResource)typeTriple.Subject))); - continue; + foreach (RDFTriple irrefPropTriple in typeGraph[null, null, RDFVocabulary.OWL.IRREFLEXIVE_PROPERTY, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)irrefPropTriple.Subject, out OWLObjectPropertyExpression opex); + if (opex != null) + { + OWLIrreflexiveObjectProperty irreflexiveObjectProperty = new OWLIrreflexiveObjectProperty() { + ObjectPropertyExpression = opex }; + + LoadAxiomAnnotations(ont, irrefPropTriple, irreflexiveObjectProperty, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(irreflexiveObjectProperty); + } + } } + void LoadTransitiveObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple transPropTriple in typeGraph[null, null, RDFVocabulary.OWL.TRANSITIVE_PROPERTY, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)transPropTriple.Subject, out OWLObjectPropertyExpression opex); + if (opex != null) + { + OWLTransitiveObjectProperty transitiveObjectProperty = new OWLTransitiveObjectProperty() { + ObjectPropertyExpression = opex }; - if (typeTriple.Object.Equals(RDFVocabulary.OWL.OBJECT_PROPERTY)) + LoadAxiomAnnotations(ont, transPropTriple, transitiveObjectProperty, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(transitiveObjectProperty); + } + } + } + void LoadInverseObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) { - ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLObjectProperty((RDFResource)typeTriple.Subject))); - continue; + RDFSelectQuery query = new RDFSelectQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) + .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) + .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("OO")), new RDFVariable("?CASE"))) + .UnionWithNext()) + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPL"))) + .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) + .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPL"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) + .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?OPL"), new RDFVariable("?OPR")))) + .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("IO")), new RDFVariable("?CASE"))) + .UnionWithNext()) + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?IOPR"))) + .AddPattern(new RDFPattern(new RDFVariable("?IOPR"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) + .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPR"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) + .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?OPL"), new RDFVariable("?OPR")))) + .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("OI")), new RDFVariable("?CASE"))) + .UnionWithNext()) + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPL"))) + .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?IOPR"))) + .AddPattern(new RDFPattern(new RDFVariable("?IOPR"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) + .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPL"))) + .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPR"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) + .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?OPL"), new RDFVariable("?OPR")))) + .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("II")), new RDFVariable("?CASE")))) + .AddProjectionVariable(new RDFVariable("?IOPL")) + .AddProjectionVariable(new RDFVariable("?OPL")) + .AddProjectionVariable(new RDFVariable("?IOPR")) + .AddProjectionVariable(new RDFVariable("?OPR")) + .AddProjectionVariable(new RDFVariable("?CASE")) + .AddModifier(new RDFOrderByModifier(new RDFVariable("?CASE"), RDFQueryEnums.RDFOrderByFlavors.DESC)); + RDFSelectQueryResult result = query.ApplyToGraph(graph); + + HashSet ioplLookup = new HashSet(); + foreach (DataRow resultRow in result.SelectResults.Rows) + { + OWLInverseObjectProperties inverseObjectProperties = new OWLInverseObjectProperties(); + + RDFResource IOPL/*InverseOfPropertyLeft*/, OPL/*ObjectPropertyLeft*/, IOPR/*InverseOfPropertyRight*/, OPR/*ObjectPropertyRight*/; + switch (resultRow["?CASE"].ToString()) + { + case "OO": + OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); + OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); + inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectProperty(OPL); + inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectProperty(OPR); + LoadAxiomAnnotations(ont, new RDFTriple(OPL, RDFVocabulary.OWL.INVERSE_OF, OPR), inverseObjectProperties, annAxiomsGraph); + break; + case "IO": + IOPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPL"].ToString()); + #region IOPL Guard + if (ioplLookup.Contains(IOPL.PatternMemberID)) continue; + ioplLookup.Add(IOPL.PatternMemberID); + #endregion + OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); + OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); + inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPL)); + inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectProperty(OPR); + LoadAxiomAnnotations(ont, new RDFTriple(IOPL, RDFVocabulary.OWL.INVERSE_OF, OPR), inverseObjectProperties, annAxiomsGraph); + break; + case "OI": + OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); + IOPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPR"].ToString()); + OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); + inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectProperty(OPL); + inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPR)); + LoadAxiomAnnotations(ont, new RDFTriple(OPL, RDFVocabulary.OWL.INVERSE_OF, IOPR), inverseObjectProperties, annAxiomsGraph); + break; + case "II": + IOPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPL"].ToString()); + OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); + IOPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPR"].ToString()); + OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); + inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPL)); + inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPR)); + LoadAxiomAnnotations(ont, new RDFTriple(IOPL, RDFVocabulary.OWL.INVERSE_OF, IOPR), inverseObjectProperties, annAxiomsGraph); + break; + } + + ont.ObjectPropertyAxioms.Add(inverseObjectProperties); + } } + void LoadEquivalentObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple equivPropTriple in graph[null, RDFVocabulary.OWL.EQUIVALENT_PROPERTY, null, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)equivPropTriple.Subject, out OWLObjectPropertyExpression leftOPE); + LoadObjectPropertyExpression(ont, (RDFResource)equivPropTriple.Object, out OWLObjectPropertyExpression rightOPE); + + if (leftOPE != null && rightOPE != null) + { + OWLEquivalentObjectProperties equivalentObjectProperties = new OWLEquivalentObjectProperties() { + ObjectPropertyExpressions = new List() { leftOPE, rightOPE } }; + + LoadAxiomAnnotations(ont, equivPropTriple, equivalentObjectProperties, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(equivalentObjectProperties); + } + } + } + void LoadDisjointObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + //Load axioms built with owl:propertyDisjointWith + foreach (RDFTriple propDisjointWithTriple in graph[null, RDFVocabulary.OWL.PROPERTY_DISJOINT_WITH, null, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Subject, out OWLObjectPropertyExpression leftOPE); + LoadObjectPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Object, out OWLObjectPropertyExpression rightOPE); + + if (leftOPE != null && rightOPE != null) + { + OWLDisjointObjectProperties disjointObjectProperties = new OWLDisjointObjectProperties() { + ObjectPropertyExpressions = new List() { leftOPE, rightOPE } }; - if (typeTriple.Object.Equals(RDFVocabulary.OWL.DATATYPE_PROPERTY)) + LoadAxiomAnnotations(ont, propDisjointWithTriple, disjointObjectProperties, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(disjointObjectProperties); + } + } + + //Load axioms built with owl:AllDisjointProperties + foreach (RDFTriple allDisjointPropertiesTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DISJOINT_PROPERTIES, null]) + if (graph[(RDFResource)allDisjointPropertiesTriple.Subject, RDFVocabulary.OWL.MEMBERS, null, null] + .FirstOrDefault()?.Object is RDFResource adjpCollectionRepresentative) + { + List adjpMembers = new List(); + + RDFCollection adjpCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adjpCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource adjpMember in adjpCollection.Items.Cast()) + { + LoadObjectPropertyExpression(ont, adjpMember, out OWLObjectPropertyExpression opex); + if (opex != null) + adjpMembers.Add(opex); + } + + if (adjpMembers.Count >= 2) + { + OWLDisjointObjectProperties disjointObjectProperties = new OWLDisjointObjectProperties() { + ObjectPropertyExpressions = adjpMembers }; + + LoadIRIAnnotations(ont, new List() { + RDFVocabulary.OWL.DEPRECATED, + RDFVocabulary.RDFS.COMMENT, + RDFVocabulary.RDFS.LABEL, + RDFVocabulary.RDFS.SEE_ALSO, + RDFVocabulary.RDFS.IS_DEFINED_BY + }, (RDFResource)allDisjointPropertiesTriple.Subject, annAxiomsGraph, out List adjpAnnotations); + disjointObjectProperties.Annotations = adjpAnnotations; + + ont.ObjectPropertyAxioms.Add(disjointObjectProperties); + } + } + } + void LoadSubObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) { - ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLDataProperty((RDFResource)typeTriple.Subject))); - continue; + //Load axioms built with owl:propertyChainAxiom + foreach (RDFTriple propertyChainAxiomTriple in graph[null, RDFVocabulary.OWL.PROPERTY_CHAIN_AXIOM, null, null]) + { + OWLObjectPropertyChain objectPropertyChain = new OWLObjectPropertyChain() { + ObjectPropertyExpressions = new List() }; + + //Left + RDFCollection chainAxiomMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)propertyChainAxiomTriple.Object, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource chainAxiomMember in chainAxiomMembers.Items.Cast()) + { + LoadObjectPropertyExpression(ont, chainAxiomMember, out OWLObjectPropertyExpression opex); + if (opex != null) + objectPropertyChain.ObjectPropertyExpressions.Add(opex); + } + + //Right + LoadObjectPropertyExpression(ont, (RDFResource)propertyChainAxiomTriple.Subject, out OWLObjectPropertyExpression rightOPE); + + if (objectPropertyChain.ObjectPropertyExpressions.Count >= 2 && rightOPE != null) + { + OWLSubObjectPropertyOf subObjectPropertyOf = new OWLSubObjectPropertyOf() { + SubObjectPropertyChain = objectPropertyChain, SuperObjectPropertyExpression = rightOPE }; + + LoadAxiomAnnotations(ont, propertyChainAxiomTriple, subObjectPropertyOf, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(subObjectPropertyOf); + } + } + + //Load axioms built with rdfs:subPropertyOf + foreach (RDFTriple subPropTriple in graph[null, RDFVocabulary.RDFS.SUB_PROPERTY_OF, null, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)subPropTriple.Subject, out OWLObjectPropertyExpression leftOPE); + LoadObjectPropertyExpression(ont, (RDFResource)subPropTriple.Object, out OWLObjectPropertyExpression rightOPE); + + if (leftOPE != null && rightOPE != null) + { + OWLSubObjectPropertyOf subObjectPropertyOf = new OWLSubObjectPropertyOf() { + SubObjectPropertyExpression = leftOPE, SuperObjectPropertyExpression = rightOPE }; + + LoadAxiomAnnotations(ont, subPropTriple, subObjectPropertyOf, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(subObjectPropertyOf); + } + } } + void LoadObjectPropertyDomain(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple domainTriple in graph[null, RDFVocabulary.RDFS.DOMAIN, null, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)domainTriple.Subject, out OWLObjectPropertyExpression objEXP); + LoadClassExpression(ont, (RDFResource)domainTriple.Object, out OWLClassExpression clsEXP); + + if (objEXP != null && clsEXP != null) + { + OWLObjectPropertyDomain objectPropertyDomain = new OWLObjectPropertyDomain() { + ObjectPropertyExpression = objEXP, ClassExpression = clsEXP }; - if (typeTriple.Object.Equals(RDFVocabulary.OWL.ANNOTATION_PROPERTY)) + LoadAxiomAnnotations(ont, domainTriple, objectPropertyDomain, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(objectPropertyDomain); + } + } + } + void LoadObjectPropertyRange(OWLOntology ont, RDFGraph annAxiomsGraph) { - ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLAnnotationProperty((RDFResource)typeTriple.Subject))); - continue; + foreach (RDFTriple rangeTriple in graph[null, RDFVocabulary.RDFS.RANGE, null, null]) + { + LoadObjectPropertyExpression(ont, (RDFResource)rangeTriple.Subject, out OWLObjectPropertyExpression objEXP); + LoadClassExpression(ont, (RDFResource)rangeTriple.Object, out OWLClassExpression clsEXP); + + if (objEXP != null && clsEXP != null) + { + OWLObjectPropertyRange objectPropertyRange = new OWLObjectPropertyRange() { + ObjectPropertyExpression = objEXP, ClassExpression = clsEXP }; + + LoadAxiomAnnotations(ont, rangeTriple, objectPropertyRange, annAxiomsGraph); + + ont.ObjectPropertyAxioms.Add(objectPropertyRange); + } + } } + void LoadFunctionalDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple funcPropTriple in typeGraph[null, null, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY, null]) + { + LoadDataPropertyExpression(ont, (RDFResource)funcPropTriple.Subject, out OWLDataPropertyExpression dpex); + if (dpex is OWLDataProperty dp) + { + OWLFunctionalDataProperty functionalDataProperty = new OWLFunctionalDataProperty(dp); + + LoadAxiomAnnotations(ont, funcPropTriple, functionalDataProperty, annAxiomsGraph); - if (typeTriple.Object.Equals(RDFVocabulary.OWL.NAMED_INDIVIDUAL)) + ont.DataPropertyAxioms.Add(functionalDataProperty); + } + } + } + void LoadEquivalentDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) { - namedIndividuals.Add(typeTriple.Subject.ToString()); - continue; + foreach (RDFTriple equivPropTriple in graph[null, RDFVocabulary.OWL.EQUIVALENT_PROPERTY, null, null]) + { + LoadDataPropertyExpression(ont, (RDFResource)equivPropTriple.Subject, out OWLDataPropertyExpression leftDPex); + LoadDataPropertyExpression(ont, (RDFResource)equivPropTriple.Object, out OWLDataPropertyExpression rightDPex); + + if (leftDPex is OWLDataProperty leftDP && rightDPex is OWLDataProperty rightDP) + { + OWLEquivalentDataProperties equivalentDataProperties = new OWLEquivalentDataProperties() { + DataProperties = new List() { leftDP, rightDP } }; + + LoadAxiomAnnotations(ont, equivPropTriple, equivalentDataProperties, annAxiomsGraph); + + ont.DataPropertyAxioms.Add(equivalentDataProperties); + } + } } - } + void LoadDisjointDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + //Load axioms built with owl:propertyDisjointWith + foreach (RDFTriple propDisjointWithTriple in graph[null, RDFVocabulary.OWL.PROPERTY_DISJOINT_WITH, null, null]) + { + LoadDataPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Subject, out OWLDataPropertyExpression leftDPex); + LoadDataPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Object, out OWLDataPropertyExpression rightDPex); - //NamedIndividual (undeclared, type-inferred via SPARQL) - RDFSelectQuery namedIdvQuery = new RDFSelectQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS).UnionWithNext()) - .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.RESTRICTION)) - .AddPattern(new RDFPattern(new RDFVariable("?NIDV"), RDFVocabulary.RDF.TYPE, new RDFVariable("?CLS"))) - .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?NIDV"), new RDFVariable("?CLS")))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?NIDV")))) - .AddProjectionVariable(new RDFVariable("?NIDV")); - RDFSelectQueryResult namedIdvQueryResult = namedIdvQuery.ApplyToGraph(typeGraph); - foreach (DataRow nidvRow in namedIdvQueryResult.SelectResults.Rows) - namedIndividuals.Add(nidvRow["?NIDV"].ToString()); - foreach (string namedIndividual in namedIndividuals.Distinct()) - ont.DeclarationAxioms.Add(new OWLDeclaration(new OWLNamedIndividual(new RDFResource(namedIndividual)))); - } - void PrefetchAnnotationAxioms(OWLOntology ont, out RDFGraph annAxiomsGraph) - { - RDFConstructQuery query = new RDFConstructQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, new RDFVariable("?ANNOTATED_SOURCE"))) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, new RDFVariable("?ANNOTATED_PROPERTY"))) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, new RDFVariable("?ANNOTATED_TARGET"))) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) - .AddPattern(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY))) - .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) - .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, new RDFVariable("?ANNOTATED_SOURCE"))) - .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, new RDFVariable("?ANNOTATED_PROPERTY"))) - .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, new RDFVariable("?ANNOTATED_TARGET"))) - .AddTemplate(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) - .AddTemplate(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY)); - RDFConstructQueryResult result = query.ApplyToGraph(graph); - annAxiomsGraph = result.ToRDFGraph(); - } - void LoadOntologyAnnotations(OWLOntology ont, RDFGraph annAxiomsGraph) - { - LoadIRIAnnotations(ont, new List() { - RDFVocabulary.OWL.BACKWARD_COMPATIBLE_WITH, - RDFVocabulary.OWL.INCOMPATIBLE_WITH, - RDFVocabulary.OWL.PRIOR_VERSION, - RDFVocabulary.OWL.VERSION_INFO, - RDFVocabulary.OWL.DEPRECATED, - RDFVocabulary.RDFS.COMMENT, - RDFVocabulary.RDFS.LABEL, - RDFVocabulary.RDFS.SEE_ALSO, - RDFVocabulary.RDFS.IS_DEFINED_BY - }, new RDFResource(ont.IRI), annAxiomsGraph, out List ontologyAnnotations); - ont.Annotations = ontologyAnnotations; - } - //Axioms - void LoadFunctionalObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple funcPropTriple in typeGraph[null, null, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)funcPropTriple.Subject, out OWLObjectPropertyExpression opex); - if (opex != null) + if (leftDPex is OWLDataProperty leftDP && rightDPex is OWLDataProperty rightDP) + { + OWLDisjointDataProperties disjointDataProperties = new OWLDisjointDataProperties() { + DataProperties = new List() { leftDP, rightDP } }; + + LoadAxiomAnnotations(ont, propDisjointWithTriple, disjointDataProperties, annAxiomsGraph); + + ont.DataPropertyAxioms.Add(disjointDataProperties); + } + } + + //Load axioms built with owl:AllDisjointProperties + foreach (RDFTriple allDisjointPropertiesTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DISJOINT_PROPERTIES, null]) + if (graph[(RDFResource)allDisjointPropertiesTriple.Subject, RDFVocabulary.OWL.MEMBERS, null, null] + .FirstOrDefault()?.Object is RDFResource adjpCollectionRepresentative) + { + List adjpMembers = new List(); + + RDFCollection adjpCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adjpCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource adjpMember in adjpCollection.Items.Cast()) + { + LoadDataPropertyExpression(ont, adjpMember, out OWLDataPropertyExpression dpex); + if (dpex is OWLDataProperty dp) + adjpMembers.Add(dp); + } + + if (adjpMembers.Count >= 2) + { + OWLDisjointDataProperties disjointDataProperties = new OWLDisjointDataProperties() { + DataProperties = adjpMembers }; + + LoadIRIAnnotations(ont, new List() { + RDFVocabulary.OWL.DEPRECATED, + RDFVocabulary.RDFS.COMMENT, + RDFVocabulary.RDFS.LABEL, + RDFVocabulary.RDFS.SEE_ALSO, + RDFVocabulary.RDFS.IS_DEFINED_BY + }, (RDFResource)allDisjointPropertiesTriple.Subject, annAxiomsGraph, out List adjpAnnotations); + disjointDataProperties.Annotations = adjpAnnotations; + + ont.DataPropertyAxioms.Add(disjointDataProperties); + } + } + } + void LoadSubDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) { - OWLFunctionalObjectProperty functionalObjectProperty = new OWLFunctionalObjectProperty() { - ObjectPropertyExpression = opex }; + foreach (RDFTriple subPropTriple in graph[null, RDFVocabulary.RDFS.SUB_PROPERTY_OF, null, null]) + { + LoadDataPropertyExpression(ont, (RDFResource)subPropTriple.Subject, out OWLDataPropertyExpression leftDPex); + LoadDataPropertyExpression(ont, (RDFResource)subPropTriple.Object, out OWLDataPropertyExpression rightDPex); + + if (leftDPex is OWLDataProperty leftDP && rightDPex is OWLDataProperty rightDP) + { + OWLSubDataPropertyOf subDataPropertyOf = new OWLSubDataPropertyOf() { + SubDataProperty = leftDP, SuperDataProperty = rightDP }; - LoadAxiomAnnotations(ont, funcPropTriple, functionalObjectProperty, annAxiomsGraph); + LoadAxiomAnnotations(ont, subPropTriple, subDataPropertyOf, annAxiomsGraph); - ont.ObjectPropertyAxioms.Add(functionalObjectProperty); + ont.DataPropertyAxioms.Add(subDataPropertyOf); + } + } } - } - } - void LoadInverseFunctionalObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple invfuncPropTriple in typeGraph[null, null, RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)invfuncPropTriple.Subject, out OWLObjectPropertyExpression opex); - if (opex != null) - { - OWLInverseFunctionalObjectProperty inverseFunctionalObjectProperty = new OWLInverseFunctionalObjectProperty() { - ObjectPropertyExpression = opex }; - - LoadAxiomAnnotations(ont, invfuncPropTriple, inverseFunctionalObjectProperty, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(inverseFunctionalObjectProperty); - } - } - } - void LoadSymmetricObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple symPropTriple in typeGraph[null, null, RDFVocabulary.OWL.SYMMETRIC_PROPERTY, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)symPropTriple.Subject, out OWLObjectPropertyExpression opex); - if (opex != null) - { - OWLSymmetricObjectProperty symmetricObjectProperty = new OWLSymmetricObjectProperty() { - ObjectPropertyExpression = opex }; - - LoadAxiomAnnotations(ont, symPropTriple, symmetricObjectProperty, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(symmetricObjectProperty); - } - } - } - void LoadAsymmetricObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple asymPropTriple in typeGraph[null, null, RDFVocabulary.OWL.ASYMMETRIC_PROPERTY, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)asymPropTriple.Subject, out OWLObjectPropertyExpression opex); - if (opex != null) - { - OWLAsymmetricObjectProperty asymmetricObjectProperty = new OWLAsymmetricObjectProperty() { - ObjectPropertyExpression = opex }; - - LoadAxiomAnnotations(ont, asymPropTriple, asymmetricObjectProperty, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(asymmetricObjectProperty); - } - } - } - void LoadReflexiveObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple refPropTriple in typeGraph[null, null, RDFVocabulary.OWL.REFLEXIVE_PROPERTY, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)refPropTriple.Subject, out OWLObjectPropertyExpression opex); - if (opex != null) - { - OWLReflexiveObjectProperty reflexiveObjectProperty = new OWLReflexiveObjectProperty() { - ObjectPropertyExpression = opex }; - - LoadAxiomAnnotations(ont, refPropTriple, reflexiveObjectProperty, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(reflexiveObjectProperty); - } - } - } - void LoadIrreflexiveObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple irrefPropTriple in typeGraph[null, null, RDFVocabulary.OWL.IRREFLEXIVE_PROPERTY, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)irrefPropTriple.Subject, out OWLObjectPropertyExpression opex); - if (opex != null) - { - OWLIrreflexiveObjectProperty irreflexiveObjectProperty = new OWLIrreflexiveObjectProperty() { - ObjectPropertyExpression = opex }; - - LoadAxiomAnnotations(ont, irrefPropTriple, irreflexiveObjectProperty, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(irreflexiveObjectProperty); - } - } - } - void LoadTransitiveObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple transPropTriple in typeGraph[null, null, RDFVocabulary.OWL.TRANSITIVE_PROPERTY, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)transPropTriple.Subject, out OWLObjectPropertyExpression opex); - if (opex != null) - { - OWLTransitiveObjectProperty transitiveObjectProperty = new OWLTransitiveObjectProperty() { - ObjectPropertyExpression = opex }; - - LoadAxiomAnnotations(ont, transPropTriple, transitiveObjectProperty, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(transitiveObjectProperty); - } - } - } - void LoadInverseObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - RDFSelectQuery query = new RDFSelectQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) - .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) - .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("OO")), new RDFVariable("?CASE"))) - .UnionWithNext()) - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPL"))) - .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) - .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPL"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) - .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?OPL"), new RDFVariable("?OPR")))) - .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("IO")), new RDFVariable("?CASE"))) - .UnionWithNext()) - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?IOPR"))) - .AddPattern(new RDFPattern(new RDFVariable("?IOPR"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) - .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPR"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) - .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?OPL"), new RDFVariable("?OPR")))) - .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("OI")), new RDFVariable("?CASE"))) - .UnionWithNext()) - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPL"))) - .AddPattern(new RDFPattern(new RDFVariable("?IOPL"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?IOPR"))) - .AddPattern(new RDFPattern(new RDFVariable("?IOPR"), RDFVocabulary.OWL.INVERSE_OF, new RDFVariable("?OPR"))) - .AddPattern(new RDFPattern(new RDFVariable("?OPL"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddPattern(new RDFPattern(new RDFVariable("?OPR"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPL"))) - .AddFilter(new RDFIsBlankFilter(new RDFVariable("?IOPR"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPL"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OPR"))) - .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?OPL"), new RDFVariable("?OPR")))) - .AddBind(new RDFBind(new RDFConstantExpression(new RDFPlainLiteral("II")), new RDFVariable("?CASE")))) - .AddProjectionVariable(new RDFVariable("?IOPL")) - .AddProjectionVariable(new RDFVariable("?OPL")) - .AddProjectionVariable(new RDFVariable("?IOPR")) - .AddProjectionVariable(new RDFVariable("?OPR")) - .AddProjectionVariable(new RDFVariable("?CASE")) - .AddModifier(new RDFOrderByModifier(new RDFVariable("?CASE"), RDFQueryEnums.RDFOrderByFlavors.DESC)); - RDFSelectQueryResult result = query.ApplyToGraph(graph); - - HashSet ioplLookup = new HashSet(); - foreach (DataRow resultRow in result.SelectResults.Rows) - { - OWLInverseObjectProperties inverseObjectProperties = new OWLInverseObjectProperties(); - - RDFResource IOPL/*InverseOfPropertyLeft*/, OPL/*ObjectPropertyLeft*/, IOPR/*InverseOfPropertyRight*/, OPR/*ObjectPropertyRight*/; - switch (resultRow["?CASE"].ToString()) - { - case "OO": - OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); - OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); - inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectProperty(OPL); - inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectProperty(OPR); - LoadAxiomAnnotations(ont, new RDFTriple(OPL, RDFVocabulary.OWL.INVERSE_OF, OPR), inverseObjectProperties, annAxiomsGraph); - break; - case "IO": - IOPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPL"].ToString()); - #region IOPL Guard - if (ioplLookup.Contains(IOPL.PatternMemberID)) continue; - ioplLookup.Add(IOPL.PatternMemberID); - #endregion - OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); - OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); - inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPL)); - inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectProperty(OPR); - LoadAxiomAnnotations(ont, new RDFTriple(IOPL, RDFVocabulary.OWL.INVERSE_OF, OPR), inverseObjectProperties, annAxiomsGraph); - break; - case "OI": - OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); - IOPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPR"].ToString()); - OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); - inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectProperty(OPL); - inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPR)); - LoadAxiomAnnotations(ont, new RDFTriple(OPL, RDFVocabulary.OWL.INVERSE_OF, IOPR), inverseObjectProperties, annAxiomsGraph); - break; - case "II": - IOPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPL"].ToString()); - OPL = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPL"].ToString()); - IOPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IOPR"].ToString()); - OPR = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OPR"].ToString()); - inverseObjectProperties.LeftObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPL)); - inverseObjectProperties.RightObjectPropertyExpression = new OWLObjectInverseOf(new OWLObjectProperty(OPR)); - LoadAxiomAnnotations(ont, new RDFTriple(IOPL, RDFVocabulary.OWL.INVERSE_OF, IOPR), inverseObjectProperties, annAxiomsGraph); - break; - } - - ont.ObjectPropertyAxioms.Add(inverseObjectProperties); - } - } - void LoadEquivalentObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple equivPropTriple in graph[null, RDFVocabulary.OWL.EQUIVALENT_PROPERTY, null, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)equivPropTriple.Subject, out OWLObjectPropertyExpression leftOPE); - LoadObjectPropertyExpression(ont, (RDFResource)equivPropTriple.Object, out OWLObjectPropertyExpression rightOPE); - - if (leftOPE != null && rightOPE != null) - { - OWLEquivalentObjectProperties equivalentObjectProperties = new OWLEquivalentObjectProperties() { - ObjectPropertyExpressions = new List() { leftOPE, rightOPE } }; - - LoadAxiomAnnotations(ont, equivPropTriple, equivalentObjectProperties, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(equivalentObjectProperties); - } - } - } - void LoadDisjointObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - //Load axioms built with owl:propertyDisjointWith - foreach (RDFTriple propDisjointWithTriple in graph[null, RDFVocabulary.OWL.PROPERTY_DISJOINT_WITH, null, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Subject, out OWLObjectPropertyExpression leftOPE); - LoadObjectPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Object, out OWLObjectPropertyExpression rightOPE); - - if (leftOPE != null && rightOPE != null) - { - OWLDisjointObjectProperties disjointObjectProperties = new OWLDisjointObjectProperties() { - ObjectPropertyExpressions = new List() { leftOPE, rightOPE } }; - - LoadAxiomAnnotations(ont, propDisjointWithTriple, disjointObjectProperties, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(disjointObjectProperties); - } - } - - //Load axioms built with owl:AllDisjointProperties - foreach (RDFTriple allDisjointPropertiesTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DISJOINT_PROPERTIES, null]) - if (graph[(RDFResource)allDisjointPropertiesTriple.Subject, RDFVocabulary.OWL.MEMBERS, null, null] - .FirstOrDefault()?.Object is RDFResource adjpCollectionRepresentative) - { - List adjpMembers = new List(); - - RDFCollection adjpCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adjpCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource adjpMember in adjpCollection.Items.Cast()) - { - LoadObjectPropertyExpression(ont, adjpMember, out OWLObjectPropertyExpression opex); - if (opex != null) - adjpMembers.Add(opex); - } - - if (adjpMembers.Count >= 2) - { - OWLDisjointObjectProperties disjointObjectProperties = new OWLDisjointObjectProperties() { - ObjectPropertyExpressions = adjpMembers }; - - LoadIRIAnnotations(ont, new List() { - RDFVocabulary.OWL.DEPRECATED, - RDFVocabulary.RDFS.COMMENT, - RDFVocabulary.RDFS.LABEL, - RDFVocabulary.RDFS.SEE_ALSO, - RDFVocabulary.RDFS.IS_DEFINED_BY - }, (RDFResource)allDisjointPropertiesTriple.Subject, annAxiomsGraph, out List adjpAnnotations); - disjointObjectProperties.Annotations = adjpAnnotations; + void LoadDataPropertyDomain(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple domainTriple in graph[null, RDFVocabulary.RDFS.DOMAIN, null, null]) + { + LoadDataPropertyExpression(ont, (RDFResource)domainTriple.Subject, out OWLDataPropertyExpression dtEXP); + LoadClassExpression(ont, (RDFResource)domainTriple.Object, out OWLClassExpression clsEXP); + + if (dtEXP is OWLDataProperty dp && clsEXP != null) + { + OWLDataPropertyDomain dataPropertyDomain = new OWLDataPropertyDomain() { + DataProperty = dp, ClassExpression = clsEXP }; - ont.ObjectPropertyAxioms.Add(disjointObjectProperties); + LoadAxiomAnnotations(ont, domainTriple, dataPropertyDomain, annAxiomsGraph); + + ont.DataPropertyAxioms.Add(dataPropertyDomain); + } } } - } - void LoadSubObjectProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - //Load axioms built with owl:propertyChainAxiom - foreach (RDFTriple propertyChainAxiomTriple in graph[null, RDFVocabulary.OWL.PROPERTY_CHAIN_AXIOM, null, null]) - { - OWLObjectPropertyChain objectPropertyChain = new OWLObjectPropertyChain() { - ObjectPropertyExpressions = new List() }; - - //Left - RDFCollection chainAxiomMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)propertyChainAxiomTriple.Object, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource chainAxiomMember in chainAxiomMembers.Items.Cast()) - { - LoadObjectPropertyExpression(ont, chainAxiomMember, out OWLObjectPropertyExpression opex); - if (opex != null) - objectPropertyChain.ObjectPropertyExpressions.Add(opex); - } - - //Right - LoadObjectPropertyExpression(ont, (RDFResource)propertyChainAxiomTriple.Subject, out OWLObjectPropertyExpression rightOPE); - - if (objectPropertyChain.ObjectPropertyExpressions.Count >= 2 && rightOPE != null) - { - OWLSubObjectPropertyOf subObjectPropertyOf = new OWLSubObjectPropertyOf() { - SubObjectPropertyChain = objectPropertyChain, SuperObjectPropertyExpression = rightOPE }; - - LoadAxiomAnnotations(ont, propertyChainAxiomTriple, subObjectPropertyOf, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(subObjectPropertyOf); - } - } - - //Load axioms built with rdfs:subPropertyOf - foreach (RDFTriple subPropTriple in graph[null, RDFVocabulary.RDFS.SUB_PROPERTY_OF, null, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)subPropTriple.Subject, out OWLObjectPropertyExpression leftOPE); - LoadObjectPropertyExpression(ont, (RDFResource)subPropTriple.Object, out OWLObjectPropertyExpression rightOPE); - - if (leftOPE != null && rightOPE != null) - { - OWLSubObjectPropertyOf subObjectPropertyOf = new OWLSubObjectPropertyOf() { - SubObjectPropertyExpression = leftOPE, SuperObjectPropertyExpression = rightOPE }; - - LoadAxiomAnnotations(ont, subPropTriple, subObjectPropertyOf, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(subObjectPropertyOf); - } - } - } - void LoadObjectPropertyDomain(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple domainTriple in graph[null, RDFVocabulary.RDFS.DOMAIN, null, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)domainTriple.Subject, out OWLObjectPropertyExpression objEXP); - LoadClassExpression(ont, (RDFResource)domainTriple.Object, out OWLClassExpression clsEXP); - - if (objEXP != null && clsEXP != null) - { - OWLObjectPropertyDomain objectPropertyDomain = new OWLObjectPropertyDomain() { - ObjectPropertyExpression = objEXP, ClassExpression = clsEXP }; - - LoadAxiomAnnotations(ont, domainTriple, objectPropertyDomain, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(objectPropertyDomain); - } - } - } - void LoadObjectPropertyRange(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple rangeTriple in graph[null, RDFVocabulary.RDFS.RANGE, null, null]) - { - LoadObjectPropertyExpression(ont, (RDFResource)rangeTriple.Subject, out OWLObjectPropertyExpression objEXP); - LoadClassExpression(ont, (RDFResource)rangeTriple.Object, out OWLClassExpression clsEXP); - - if (objEXP != null && clsEXP != null) - { - OWLObjectPropertyRange objectPropertyRange = new OWLObjectPropertyRange() { - ObjectPropertyExpression = objEXP, ClassExpression = clsEXP }; - - LoadAxiomAnnotations(ont, rangeTriple, objectPropertyRange, annAxiomsGraph); - - ont.ObjectPropertyAxioms.Add(objectPropertyRange); - } - } - } - void LoadFunctionalDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple funcPropTriple in typeGraph[null, null, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY, null]) - { - LoadDataPropertyExpression(ont, (RDFResource)funcPropTriple.Subject, out OWLDataPropertyExpression dpex); - if (dpex is OWLDataProperty dp) - { - OWLFunctionalDataProperty functionalDataProperty = new OWLFunctionalDataProperty(dp); - - LoadAxiomAnnotations(ont, funcPropTriple, functionalDataProperty, annAxiomsGraph); - - ont.DataPropertyAxioms.Add(functionalDataProperty); - } - } - } - void LoadEquivalentDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple equivPropTriple in graph[null, RDFVocabulary.OWL.EQUIVALENT_PROPERTY, null, null]) - { - LoadDataPropertyExpression(ont, (RDFResource)equivPropTriple.Subject, out OWLDataPropertyExpression leftDPex); - LoadDataPropertyExpression(ont, (RDFResource)equivPropTriple.Object, out OWLDataPropertyExpression rightDPex); - - if (leftDPex is OWLDataProperty leftDP && rightDPex is OWLDataProperty rightDP) - { - OWLEquivalentDataProperties equivalentDataProperties = new OWLEquivalentDataProperties() { - DataProperties = new List() { leftDP, rightDP } }; - - LoadAxiomAnnotations(ont, equivPropTriple, equivalentDataProperties, annAxiomsGraph); - - ont.DataPropertyAxioms.Add(equivalentDataProperties); - } - } - } - void LoadDisjointDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - //Load axioms built with owl:propertyDisjointWith - foreach (RDFTriple propDisjointWithTriple in graph[null, RDFVocabulary.OWL.PROPERTY_DISJOINT_WITH, null, null]) - { - LoadDataPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Subject, out OWLDataPropertyExpression leftDPex); - LoadDataPropertyExpression(ont, (RDFResource)propDisjointWithTriple.Object, out OWLDataPropertyExpression rightDPex); - - if (leftDPex is OWLDataProperty leftDP && rightDPex is OWLDataProperty rightDP) - { - OWLDisjointDataProperties disjointDataProperties = new OWLDisjointDataProperties() { - DataProperties = new List() { leftDP, rightDP } }; - - LoadAxiomAnnotations(ont, propDisjointWithTriple, disjointDataProperties, annAxiomsGraph); - - ont.DataPropertyAxioms.Add(disjointDataProperties); - } - } - - //Load axioms built with owl:AllDisjointProperties - foreach (RDFTriple allDisjointPropertiesTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DISJOINT_PROPERTIES, null]) - if (graph[(RDFResource)allDisjointPropertiesTriple.Subject, RDFVocabulary.OWL.MEMBERS, null, null] - .FirstOrDefault()?.Object is RDFResource adjpCollectionRepresentative) - { - List adjpMembers = new List(); - - RDFCollection adjpCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adjpCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource adjpMember in adjpCollection.Items.Cast()) - { - LoadDataPropertyExpression(ont, adjpMember, out OWLDataPropertyExpression dpex); - if (dpex is OWLDataProperty dp) - adjpMembers.Add(dp); - } - - if (adjpMembers.Count >= 2) - { - OWLDisjointDataProperties disjointDataProperties = new OWLDisjointDataProperties() { - DataProperties = adjpMembers }; - - LoadIRIAnnotations(ont, new List() { - RDFVocabulary.OWL.DEPRECATED, - RDFVocabulary.RDFS.COMMENT, - RDFVocabulary.RDFS.LABEL, - RDFVocabulary.RDFS.SEE_ALSO, - RDFVocabulary.RDFS.IS_DEFINED_BY - }, (RDFResource)allDisjointPropertiesTriple.Subject, annAxiomsGraph, out List adjpAnnotations); - disjointDataProperties.Annotations = adjpAnnotations; - - ont.DataPropertyAxioms.Add(disjointDataProperties); - } - } - } - void LoadSubDataProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple subPropTriple in graph[null, RDFVocabulary.RDFS.SUB_PROPERTY_OF, null, null]) - { - LoadDataPropertyExpression(ont, (RDFResource)subPropTriple.Subject, out OWLDataPropertyExpression leftDPex); - LoadDataPropertyExpression(ont, (RDFResource)subPropTriple.Object, out OWLDataPropertyExpression rightDPex); - - if (leftDPex is OWLDataProperty leftDP && rightDPex is OWLDataProperty rightDP) - { - OWLSubDataPropertyOf subDataPropertyOf = new OWLSubDataPropertyOf() { - SubDataProperty = leftDP, SuperDataProperty = rightDP }; - - LoadAxiomAnnotations(ont, subPropTriple, subDataPropertyOf, annAxiomsGraph); - - ont.DataPropertyAxioms.Add(subDataPropertyOf); - } - } - } - void LoadDataPropertyDomain(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple domainTriple in graph[null, RDFVocabulary.RDFS.DOMAIN, null, null]) - { - LoadDataPropertyExpression(ont, (RDFResource)domainTriple.Subject, out OWLDataPropertyExpression dtEXP); - LoadClassExpression(ont, (RDFResource)domainTriple.Object, out OWLClassExpression clsEXP); - - if (dtEXP is OWLDataProperty dp && clsEXP != null) - { - OWLDataPropertyDomain dataPropertyDomain = new OWLDataPropertyDomain() { - DataProperty = dp, ClassExpression = clsEXP }; - - LoadAxiomAnnotations(ont, domainTriple, dataPropertyDomain, annAxiomsGraph); - - ont.DataPropertyAxioms.Add(dataPropertyDomain); - } - } - } - void LoadDataPropertyRange(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple rangeTriple in graph[null, RDFVocabulary.RDFS.RANGE, null, null]) - { - LoadDataPropertyExpression(ont, (RDFResource)rangeTriple.Subject, out OWLDataPropertyExpression dtEXP); - LoadDataRangeExpression(ont, (RDFResource)rangeTriple.Object, out OWLDataRangeExpression drEXP); - - if (dtEXP is OWLDataProperty dp && drEXP != null) - { - OWLDataPropertyRange dataPropertyRange = new OWLDataPropertyRange() { - DataProperty = dp, DataRangeExpression = drEXP }; - - LoadAxiomAnnotations(ont, rangeTriple, dataPropertyRange, annAxiomsGraph); - - ont.DataPropertyAxioms.Add(dataPropertyRange); - } - } - } - void LoadSubClassOf(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple subClassTriple in graph[null, RDFVocabulary.RDFS.SUB_CLASS_OF, null, null]) - { - LoadClassExpression(ont, (RDFResource)subClassTriple.Subject, out OWLClassExpression leftCLEX); - LoadClassExpression(ont, (RDFResource)subClassTriple.Object, out OWLClassExpression rightCLEX); - - if (leftCLEX != null && rightCLEX != null) - { - OWLSubClassOf subClassOf = new OWLSubClassOf() - { - SubClassExpression = leftCLEX, - SuperClassExpression = rightCLEX - }; - - LoadAxiomAnnotations(ont, subClassTriple, subClassOf, annAxiomsGraph); - - ont.ClassAxioms.Add(subClassOf); - } - } - } - void LoadEquivalentClasses(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple equivClassTriple in graph[null, RDFVocabulary.OWL.EQUIVALENT_CLASS, null, null]) - { - LoadClassExpression(ont, (RDFResource)equivClassTriple.Subject, out OWLClassExpression leftCLex); - LoadClassExpression(ont, (RDFResource)equivClassTriple.Object, out OWLClassExpression rightCLex); - - if (leftCLex != null && rightCLex != null) - { - OWLEquivalentClasses equivalentClasses = new OWLEquivalentClasses() { - ClassExpressions = new List() { leftCLex, rightCLex } }; - - LoadAxiomAnnotations(ont, equivClassTriple, equivalentClasses, annAxiomsGraph); - - ont.ClassAxioms.Add(equivalentClasses); - } - } - } - void LoadDisjointClasses(OWLOntology ont, RDFGraph annAxiomsGraph) - { - //Load axioms built with owl:disjointWith - foreach (RDFTriple disjointWithTriple in graph[null, RDFVocabulary.OWL.DISJOINT_WITH, null, null]) - { - LoadClassExpression(ont, (RDFResource)disjointWithTriple.Subject, out OWLClassExpression leftCLE); - LoadClassExpression(ont, (RDFResource)disjointWithTriple.Object, out OWLClassExpression rightCLE); - - if (leftCLE != null && rightCLE != null) - { - OWLDisjointClasses disjointClasses = new OWLDisjointClasses() { - ClassExpressions = new List() { leftCLE, rightCLE } }; - - LoadAxiomAnnotations(ont, disjointWithTriple, disjointClasses, annAxiomsGraph); - - ont.ClassAxioms.Add(disjointClasses); - } - } - - //Load axioms built with owl:AllDisjointClasses - foreach (RDFTriple allDisjointClassesTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DISJOINT_CLASSES, null]) - if (graph[(RDFResource)allDisjointClassesTriple.Subject, RDFVocabulary.OWL.MEMBERS, null, null] - .FirstOrDefault()?.Object is RDFResource adjcCollectionRepresentative) - { - List adjcMembers = new List(); - - RDFCollection adjcCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adjcCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource adjcMember in adjcCollection.Items.Cast()) - { - LoadClassExpression(ont, adjcMember, out OWLClassExpression clex); - if (clex != null) - adjcMembers.Add(clex); - } - - if (adjcMembers.Count >= 2) - { - OWLDisjointClasses disjointClasses = new OWLDisjointClasses() { - ClassExpressions = adjcMembers }; - - LoadIRIAnnotations(ont, new List() { - RDFVocabulary.OWL.DEPRECATED, - RDFVocabulary.RDFS.COMMENT, - RDFVocabulary.RDFS.LABEL, - RDFVocabulary.RDFS.SEE_ALSO, - RDFVocabulary.RDFS.IS_DEFINED_BY - }, (RDFResource)allDisjointClassesTriple.Subject, annAxiomsGraph, out List adjcAnnotations); - disjointClasses.Annotations = adjcAnnotations; - - ont.ClassAxioms.Add(disjointClasses); - } - } - } - void LoadDisjointUnion(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple disjointUnionOfTriple in graph[null, RDFVocabulary.OWL.DISJOINT_UNION_OF, null, null]) - { - LoadClassExpression(ont, (RDFResource)disjointUnionOfTriple.Subject, out OWLClassExpression clsExp); - if (!(clsExp is OWLClass classIRI)) - continue; - - List disjointUnionMembers = new List(); - RDFCollection disjointUnionMembersCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)disjointUnionOfTriple.Object, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource disjointUnionMember in disjointUnionMembersCollection.Items.Cast()) - { - LoadClassExpression(ont, disjointUnionMember, out OWLClassExpression clsMemberExp); - if (clsMemberExp != null) - disjointUnionMembers.Add(clsMemberExp); - } - - if (disjointUnionMembers.Count >= 2) - { - OWLDisjointUnion disjointUnion = new OWLDisjointUnion() { - ClassIRI = classIRI, ClassExpressions = disjointUnionMembers }; - - LoadAxiomAnnotations(ont, disjointUnionOfTriple, disjointUnion, annAxiomsGraph); - - ont.ClassAxioms.Add(disjointUnion); - } - } - } - void LoadHasKey(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple hasKeyTriple in graph[null, RDFVocabulary.OWL.HAS_KEY, null, null]) - { - LoadClassExpression(ont, (RDFResource)hasKeyTriple.Subject, out OWLClassExpression clsExp); - if (!(clsExp is OWLClass classIRI)) - continue; - - List haskeyOPMembers = new List(); - List haskeyDPMembers = new List(); - RDFCollection haskeyMembersCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)hasKeyTriple.Object, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource hasKeyMember in haskeyMembersCollection.Items.Cast()) - { - LoadObjectPropertyExpression(ont, hasKeyMember, out OWLObjectPropertyExpression objPropMember); - if (objPropMember != null) - { - haskeyOPMembers.Add(objPropMember); - continue; - } - LoadDataPropertyExpression(ont, hasKeyMember, out OWLDataPropertyExpression dtPropMember); - if (dtPropMember is OWLDataProperty dtProp) - haskeyDPMembers.Add(dtProp); - } - - OWLHasKey hasKey = new OWLHasKey() { - ClassExpression = clsExp, ObjectPropertyExpressions = haskeyOPMembers, DataProperties = haskeyDPMembers }; - - LoadAxiomAnnotations(ont, hasKeyTriple, hasKey, annAxiomsGraph); - - ont.KeyAxioms.Add(hasKey); - } - } - void LoadDatatypeDefinition(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple datatypeTriple in typeGraph[null, null, RDFVocabulary.RDFS.DATATYPE, null] - .Where(t => !((RDFResource)t.Subject).IsBlank)) - { - if (!(graph[(RDFResource)datatypeTriple.Subject, RDFVocabulary.OWL.EQUIVALENT_CLASS, null, null] - .FirstOrDefault()?.Object is RDFResource equivalentDatatype)) - continue; - - LoadDataRangeExpression(ont, equivalentDatatype, out OWLDataRangeExpression drex); - if (drex == null) - continue; - - OWLDatatypeDefinition datatypeDefinition = new OWLDatatypeDefinition() - { - Datatype = new OWLDatatype((RDFResource)datatypeTriple.Subject), - DataRangeExpression = drex - }; - - LoadAxiomAnnotations(ont, datatypeTriple, datatypeDefinition, annAxiomsGraph); - - ont.DatatypeDefinitionAxioms.Add(datatypeDefinition); - } - } - void LoadSameIndividual(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple sameAsTriple in graph[null, RDFVocabulary.OWL.SAME_AS, null, null]) - { - LoadIndividualExpression(ont, (RDFResource)sameAsTriple.Subject, out OWLIndividualExpression leftIE); - LoadIndividualExpression(ont, (RDFResource)sameAsTriple.Object, out OWLIndividualExpression rightIE); - - if (leftIE != null && rightIE != null) - { - OWLSameIndividual sameIndividual = new OWLSameIndividual() { - IndividualExpressions = new List() { leftIE, rightIE } }; - - LoadAxiomAnnotations(ont, sameAsTriple, sameIndividual, annAxiomsGraph); - - ont.AssertionAxioms.Add(sameIndividual); - } - } - } - void LoadDifferentIndividuals(OWLOntology ont, RDFGraph annAxiomsGraph) - { - //Load axioms built with owl:differentFrom - foreach (RDFTriple differentFromTriple in graph[null, RDFVocabulary.OWL.DIFFERENT_FROM, null, null]) - { - LoadIndividualExpression(ont, (RDFResource)differentFromTriple.Subject, out OWLIndividualExpression leftIE); - LoadIndividualExpression(ont, (RDFResource)differentFromTriple.Object, out OWLIndividualExpression rightIE); - - if (leftIE != null && rightIE != null) - { - OWLDifferentIndividuals differentIndividuals = new OWLDifferentIndividuals() { - IndividualExpressions = new List() { leftIE, rightIE } }; - - LoadAxiomAnnotations(ont, differentFromTriple, differentIndividuals, annAxiomsGraph); - - ont.AssertionAxioms.Add(differentIndividuals); - } - } - - //Load axioms built with owl:AllDifferent - foreach (RDFTriple allDifferentTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DIFFERENT, null]) - if (graph[(RDFResource)allDifferentTriple.Subject, RDFVocabulary.OWL.DISTINCT_MEMBERS, null, null] - .FirstOrDefault()?.Object is RDFResource adiffCollectionRepresentative) - { - List adiffMembers = new List(); - - RDFCollection adiffCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adiffCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource adiffMember in adiffCollection.Items.Cast()) - { - LoadIndividualExpression(ont, adiffMember, out OWLIndividualExpression idvex); - if (idvex != null) - adiffMembers.Add(idvex); - } - - if (adiffMembers.Count >= 2) - { - OWLDifferentIndividuals differentIndividuals = new OWLDifferentIndividuals() { - IndividualExpressions = adiffMembers }; - - LoadIRIAnnotations(ont, new List() { - RDFVocabulary.OWL.DEPRECATED, - RDFVocabulary.RDFS.COMMENT, - RDFVocabulary.RDFS.LABEL, - RDFVocabulary.RDFS.SEE_ALSO, - RDFVocabulary.RDFS.IS_DEFINED_BY - }, (RDFResource)allDifferentTriple.Subject, annAxiomsGraph, out List adjpAnnotations); - differentIndividuals.Annotations = adjpAnnotations; - - ont.AssertionAxioms.Add(differentIndividuals); - } - } - } - void LoadObjectPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple objPropTriple in typeGraph[null, null, RDFVocabulary.OWL.OBJECT_PROPERTY, null]) - { - OWLObjectProperty objProp = new OWLObjectProperty((RDFResource)objPropTriple.Subject); - foreach (RDFTriple objPropAsnTriple in graph[null, (RDFResource)objPropTriple.Subject, null, null]) + void LoadDataPropertyRange(OWLOntology ont, RDFGraph annAxiomsGraph) { - LoadIndividualExpression(ont, (RDFResource)objPropAsnTriple.Subject, out OWLIndividualExpression leftIE); - LoadIndividualExpression(ont, (RDFResource)objPropAsnTriple.Object, out OWLIndividualExpression rightIE); + foreach (RDFTriple rangeTriple in graph[null, RDFVocabulary.RDFS.RANGE, null, null]) + { + LoadDataPropertyExpression(ont, (RDFResource)rangeTriple.Subject, out OWLDataPropertyExpression dtEXP); + LoadDataRangeExpression(ont, (RDFResource)rangeTriple.Object, out OWLDataRangeExpression drEXP); + + if (dtEXP is OWLDataProperty dp && drEXP != null) + { + OWLDataPropertyRange dataPropertyRange = new OWLDataPropertyRange() { + DataProperty = dp, DataRangeExpression = drEXP }; - if (leftIE != null && rightIE != null) + LoadAxiomAnnotations(ont, rangeTriple, dataPropertyRange, annAxiomsGraph); + + ont.DataPropertyAxioms.Add(dataPropertyRange); + } + } + } + void LoadSubClassOf(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple subClassTriple in graph[null, RDFVocabulary.RDFS.SUB_CLASS_OF, null, null]) { - OWLObjectPropertyAssertion objPropAsn = new OWLObjectPropertyAssertion() { - ObjectPropertyExpression = objProp, - SourceIndividualExpression = leftIE, - TargetIndividualExpression = rightIE }; + LoadClassExpression(ont, (RDFResource)subClassTriple.Subject, out OWLClassExpression leftCLEX); + LoadClassExpression(ont, (RDFResource)subClassTriple.Object, out OWLClassExpression rightCLEX); + + if (leftCLEX != null && rightCLEX != null) + { + OWLSubClassOf subClassOf = new OWLSubClassOf() + { + SubClassExpression = leftCLEX, + SuperClassExpression = rightCLEX + }; - LoadAxiomAnnotations(ont, objPropAsnTriple, objPropAsn, annAxiomsGraph); + LoadAxiomAnnotations(ont, subClassTriple, subClassOf, annAxiomsGraph); - ont.AssertionAxioms.Add(objPropAsn); + ont.ClassAxioms.Add(subClassOf); + } } } - } - } - void LoadNegativeObjectPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) - { - RDFSelectQuery query = new RDFSelectQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION)) - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.SOURCE_INDIVIDUAL, new RDFVariable("?SIDV"))) - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.ASSERTION_PROPERTY, new RDFVariable("?OBJP"))) - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.TARGET_INDIVIDUAL, new RDFVariable("?TIDV"))) - .AddPattern(new RDFPattern(new RDFVariable("?OBJP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?SIDV"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?OBJP"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?TIDV")))); - RDFSelectQueryResult result = query.ApplyToGraph(graph); - foreach (DataRow resultRow in result.SelectResults.Rows) - { - LoadIndividualExpression(ont, (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?SIDV"].ToString()), out OWLIndividualExpression leftIE); - LoadIndividualExpression(ont, (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?TIDV"].ToString()), out OWLIndividualExpression rightIE); - - if (leftIE != null && rightIE != null) - { - RDFResource axiomIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?NASN"].ToString()); - OWLObjectProperty objProp = new OWLObjectProperty((RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OBJP"].ToString())); - - OWLNegativeObjectPropertyAssertion negObjPropAsn = new OWLNegativeObjectPropertyAssertion() - { - ObjectPropertyExpression = objProp, - SourceIndividualExpression = leftIE, - TargetIndividualExpression = rightIE - }; - - LoadIRIAnnotations(ont, new List() { - RDFVocabulary.OWL.DEPRECATED, - RDFVocabulary.RDFS.COMMENT, - RDFVocabulary.RDFS.LABEL, - RDFVocabulary.RDFS.SEE_ALSO, - RDFVocabulary.RDFS.IS_DEFINED_BY - }, axiomIRI, annAxiomsGraph, out List nasnAnnotations); - negObjPropAsn.Annotations = nasnAnnotations; - - ont.AssertionAxioms.Add(negObjPropAsn); - } - } - } - void LoadDataPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple dtPropTriple in typeGraph[null, null, RDFVocabulary.OWL.DATATYPE_PROPERTY, null]) - { - OWLDataProperty dtProp = new OWLDataProperty((RDFResource)dtPropTriple.Subject); - foreach (RDFTriple dtPropAsnTriple in graph[null, (RDFResource)dtPropTriple.Subject, null, null]) - { - LoadIndividualExpression(ont, (RDFResource)dtPropAsnTriple.Subject, out OWLIndividualExpression leftIE); - - if (leftIE != null) - { - OWLDataPropertyAssertion dtPropAsn = new OWLDataPropertyAssertion() - { - DataProperty = dtProp, - IndividualExpression = leftIE, - Literal = new OWLLiteral((RDFLiteral)dtPropAsnTriple.Object) - }; - - LoadAxiomAnnotations(ont, dtPropAsnTriple, dtPropAsn, annAxiomsGraph); - - ont.AssertionAxioms.Add(dtPropAsn); - } - } - } - } - void LoadClassAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) - { - RDFSelectQuery query = new RDFSelectQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?IDV"), RDFVocabulary.RDF.TYPE, new RDFVariable("?CLS"))) - .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS).UnionWithNext()) - .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS).UnionWithNext()) - .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_CLASS).UnionWithNext()) - .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.RESTRICTION)) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?IDV"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?CLS"))) - .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?IDV"), new RDFVariable("?CLS")))) - .AddFilter(new RDFBooleanNotFilter(new RDFInFilter(new RDFVariable("?CLS"), new List() { - RDFVocabulary.RDF.LIST, RDFVocabulary.RDFS.CLASS, RDFVocabulary.OWL.CLASS, RDFVocabulary.OWL.DEPRECATED_CLASS, RDFVocabulary.OWL.RESTRICTION })))); - RDFSelectQueryResult result = query.ApplyToGraph(typeGraph); - foreach (DataRow resultRow in result.SelectResults.Rows) - { - RDFResource clsIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?CLS"].ToString()); - RDFResource idvIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IDV"].ToString()); - LoadClassExpression(ont, clsIRI, out OWLClassExpression clsEx); - LoadIndividualExpression(ont, idvIRI, out OWLIndividualExpression idvEx); - - if (idvEx != null && clsEx != null) - { - OWLClassAssertion classAssertion = new OWLClassAssertion() { - ClassExpression = clsEx, IndividualExpression = idvEx }; - - LoadAxiomAnnotations(ont, new RDFTriple(idvIRI, RDFVocabulary.RDF.TYPE, clsIRI), classAssertion, annAxiomsGraph); - - ont.AssertionAxioms.Add(classAssertion); - } - } - } - void LoadNegativeDataPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) - { - RDFSelectQuery query = new RDFSelectQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION)) - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.SOURCE_INDIVIDUAL, new RDFVariable("?SIDV"))) - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.ASSERTION_PROPERTY, new RDFVariable("?DTP"))) - .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.TARGET_VALUE, new RDFVariable("?TVAL"))) - .AddPattern(new RDFPattern(new RDFVariable("?DTP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DATATYPE_PROPERTY)) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?SIDV"))) - .AddFilter(new RDFIsUriFilter(new RDFVariable("?DTP"))) - .AddFilter(new RDFIsLiteralFilter(new RDFVariable("?TVAL")))); - RDFSelectQueryResult result = query.ApplyToGraph(graph); - foreach (DataRow resultRow in result.SelectResults.Rows) - { - LoadIndividualExpression(ont, (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?SIDV"].ToString()), out OWLIndividualExpression leftIE); - - if (leftIE != null) - { - RDFResource axiomIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?NASN"].ToString()); - OWLDataProperty dtProp = new OWLDataProperty((RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?DTP"].ToString())); - OWLLiteral litVal = new OWLLiteral((RDFLiteral)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?TVAL"].ToString())); - - OWLNegativeDataPropertyAssertion negDtPropAsn = new OWLNegativeDataPropertyAssertion() - { - DataProperty = dtProp, - IndividualExpression = leftIE, - Literal = litVal - }; - - LoadIRIAnnotations(ont, new List() { - RDFVocabulary.OWL.DEPRECATED, - RDFVocabulary.RDFS.COMMENT, - RDFVocabulary.RDFS.LABEL, - RDFVocabulary.RDFS.SEE_ALSO, - RDFVocabulary.RDFS.IS_DEFINED_BY - }, axiomIRI, annAxiomsGraph, out List nasnAnnotations); - negDtPropAsn.Annotations = nasnAnnotations; - - ont.AssertionAxioms.Add(negDtPropAsn); - } - } - } - void LoadAnnotationAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) - { - #region Fetch Declarations - List declaredClasses = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLClass) - .Select(dax => (OWLClass)dax.Expression) - .ToList(); - List declaredDatatypes = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLDatatype) - .Select(dax => (OWLDatatype)dax.Expression) - .ToList(); - List declaredObjectProperties = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLObjectProperty) - .Select(dax => (OWLObjectProperty)dax.Expression) - .ToList(); - List declaredDataProperties = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLDataProperty) - .Select(dax => (OWLDataProperty)dax.Expression) - .ToList(); - List declaredAnnotationProperties = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLAnnotationProperty) - .Select(dax => (OWLAnnotationProperty)dax.Expression) - .ToList(); - List declaredIndividuals = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLNamedIndividual) - .Select(dax => (OWLNamedIndividual)dax.Expression) - .ToList(); - #endregion - - declaredAnnotationProperties.ForEach(annProp => - { - RDFResource annPropIRI = annProp.GetIRI(); - RDFGraph annPropGraph = graph[null, annPropIRI, null, null]; - - //Class Annotations - declaredClasses.ForEach(cls => - { - RDFResource clsIRI = cls.GetIRI(); - foreach (RDFTriple clsAnnPropTriple in annPropGraph[clsIRI, null, null, null]) - { - OWLAnnotationAssertion annAsn; - if (clsAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) - annAsn = new OWLAnnotationAssertion(annProp, clsIRI, (RDFResource)clsAnnPropTriple.Object); - else - annAsn = new OWLAnnotationAssertion(annProp, clsIRI, new OWLLiteral((RDFLiteral)clsAnnPropTriple.Object)); - - LoadAxiomAnnotations(ont, clsAnnPropTriple, annAsn, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annAsn); - } - }); - - //Datatype Annotations - declaredDatatypes.ForEach(dt => - { - RDFResource dtIRI = dt.GetIRI(); - foreach (RDFTriple dtAnnPropTriple in annPropGraph[dtIRI, null, null, null]) - { - OWLAnnotationAssertion annAsn; - if (dtAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) - annAsn = new OWLAnnotationAssertion(annProp, dtIRI, (RDFResource)dtAnnPropTriple.Object); - else - annAsn = new OWLAnnotationAssertion(annProp, dtIRI, new OWLLiteral((RDFLiteral)dtAnnPropTriple.Object)); - - LoadAxiomAnnotations(ont, dtAnnPropTriple, annAsn, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annAsn); - } - }); - - //ObjectProperty Annotations - declaredObjectProperties.ForEach(op => - { - RDFResource opIRI = op.GetIRI(); - foreach (RDFTriple opAnnPropTriple in annPropGraph[opIRI, null, null, null]) - { - OWLAnnotationAssertion annAsn; - if (opAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) - annAsn = new OWLAnnotationAssertion(annProp, opIRI, (RDFResource)opAnnPropTriple.Object); - else - annAsn = new OWLAnnotationAssertion(annProp, opIRI, new OWLLiteral((RDFLiteral)opAnnPropTriple.Object)); - - LoadAxiomAnnotations(ont, opAnnPropTriple, annAsn, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annAsn); - } - }); - - //DataProperty Annotations - declaredDataProperties.ForEach(dp => - { - RDFResource dpIRI = dp.GetIRI(); - foreach (RDFTriple dpAnnPropTriple in annPropGraph[dpIRI, null, null, null]) - { - OWLAnnotationAssertion annAsn; - if (dpAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) - annAsn = new OWLAnnotationAssertion(annProp, dpIRI, (RDFResource)dpAnnPropTriple.Object); - else - annAsn = new OWLAnnotationAssertion(annProp, dpIRI, new OWLLiteral((RDFLiteral)dpAnnPropTriple.Object)); - - LoadAxiomAnnotations(ont, dpAnnPropTriple, annAsn, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annAsn); - } - }); - - //AnnotationProperty Annotations - declaredAnnotationProperties.ForEach(ap => - { - RDFResource apIRI = ap.GetIRI(); - foreach (RDFTriple apAnnPropTriple in annPropGraph[apIRI, null, null, null]) - { - OWLAnnotationAssertion annAsn; - if (apAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) - annAsn = new OWLAnnotationAssertion(annProp, apIRI, (RDFResource)apAnnPropTriple.Object); - else - annAsn = new OWLAnnotationAssertion(annProp, apIRI, new OWLLiteral((RDFLiteral)apAnnPropTriple.Object)); - - LoadAxiomAnnotations(ont, apAnnPropTriple, annAsn, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annAsn); - } - }); - - //Individual Annotations - declaredIndividuals.ForEach(idv => - { - RDFResource idvIRI = idv.GetIRI(); - foreach (RDFTriple idvAnnPropTriple in annPropGraph[idvIRI, null, null, null]) - { - OWLAnnotationAssertion annAsn; - if (idvAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) - annAsn = new OWLAnnotationAssertion(annProp, idvIRI, (RDFResource)idvAnnPropTriple.Object); - else - annAsn = new OWLAnnotationAssertion(annProp, idvIRI, new OWLLiteral((RDFLiteral)idvAnnPropTriple.Object)); - - LoadAxiomAnnotations(ont, idvAnnPropTriple, annAsn, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annAsn); - } - }); - }); - } - void LoadSubAnnotationProperties(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple subPropTriple in graph[null, RDFVocabulary.RDFS.SUB_PROPERTY_OF, null, null]) - { - LoadAnnotationPropertyExpression(ont, (RDFResource)subPropTriple.Subject, out OWLAnnotationPropertyExpression leftAPex); - LoadAnnotationPropertyExpression(ont, (RDFResource)subPropTriple.Object, out OWLAnnotationPropertyExpression rightAPex); - - if (leftAPex is OWLAnnotationProperty leftAP && rightAPex is OWLAnnotationProperty rightAP) - { - OWLSubAnnotationPropertyOf subAnnotationPropertyOf = new OWLSubAnnotationPropertyOf() { - SubAnnotationProperty = leftAP, SuperAnnotationProperty = rightAP }; - - LoadAxiomAnnotations(ont, subPropTriple, subAnnotationPropertyOf, annAxiomsGraph); - - ont.AnnotationAxioms.Add(subAnnotationPropertyOf); - } - } - } - void LoadAnnotationPropertyDomain(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple domainTriple in graph[null, RDFVocabulary.RDFS.DOMAIN, null, null]) - { - LoadAnnotationPropertyExpression(ont, (RDFResource)domainTriple.Subject, out OWLAnnotationPropertyExpression annEXP); - - if (annEXP is OWLAnnotationProperty annProp && domainTriple.Object is RDFResource domainObject) - { - OWLAnnotationPropertyDomain annotationPropertyDomain = new OWLAnnotationPropertyDomain() { - AnnotationProperty = annProp, IRI = domainObject.ToString() }; - - LoadAxiomAnnotations(ont, domainTriple, annotationPropertyDomain, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annotationPropertyDomain); - } - } - } - void LoadAnnotationPropertyRange(OWLOntology ont, RDFGraph annAxiomsGraph) - { - foreach (RDFTriple rangeTriple in graph[null, RDFVocabulary.RDFS.RANGE, null, null]) - { - LoadAnnotationPropertyExpression(ont, (RDFResource)rangeTriple.Subject, out OWLAnnotationPropertyExpression annEXP); - - if (annEXP is OWLAnnotationProperty annProp && rangeTriple.Object is RDFResource rangeObject) - { - OWLAnnotationPropertyRange annotationPropertyRange = new OWLAnnotationPropertyRange() { - AnnotationProperty = annProp, IRI = rangeObject.ToString() }; - - LoadAxiomAnnotations(ont, rangeTriple, annotationPropertyRange, annAxiomsGraph); - - ont.AnnotationAxioms.Add(annotationPropertyRange); - } - } - } - //Annotations - void LoadAxiomAnnotations(OWLOntology ont, RDFTriple axiomTriple, OWLAxiom axiom, RDFGraph annAxiomsGraph) - { - #region Guards - if (annAxiomsGraph.TriplesCount == 0) - return; - #endregion - - RDFSelectQuery query = new RDFSelectQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, axiomTriple.Subject)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, axiomTriple.Predicate)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, axiomTriple.Object)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) - .AddPattern(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY))); - RDFSelectQueryResult result = query.ApplyToGraph(annAxiomsGraph); - foreach (DataRow resultRow in result.SelectResults.Rows) - { - RDFPatternMember axiomIRI = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?AXIOM"].ToString()); - RDFPatternMember annProp = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNPROP"].ToString()); - RDFPatternMember annVal = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNVAL"].ToString()); - RDFTriple annotationTriple = annVal is RDFResource annValRes - ? new RDFTriple((RDFResource)axiomIRI, (RDFResource)annProp, annValRes) - : new RDFTriple((RDFResource)axiomIRI, (RDFResource)annProp, (RDFLiteral)annVal); - OWLAnnotation annotation = annVal is RDFResource annValRes2 - ? new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), annValRes2) - : new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), new OWLLiteral((RDFLiteral)annVal)); - - LoadNestedAnnotation(ont, annotationTriple, annotation, annAxiomsGraph); - - axiom.Annotations.Add(annotation); - } - } - void LoadIRIAnnotations(OWLOntology ont, List annotationProperties, RDFResource iri, RDFGraph annAxiomsGraph, out List annotations) - { - annotations = new List(); - - foreach (RDFTriple annPropTriple in typeGraph[null, null, RDFVocabulary.OWL.ANNOTATION_PROPERTY, null] - .Where(ap => !ap.Equals(RDFVocabulary.OWL.VERSION_IRI))) - { - RDFResource annPropIRI = (RDFResource)annPropTriple.Subject; - if (!annotationProperties.Any(ap => ap.Equals(annPropIRI))) - annotationProperties.Add(annPropIRI); - } - - foreach (RDFResource workingAnnotationProperty in annotationProperties) - { - OWLAnnotationProperty annotationProperty = new OWLAnnotationProperty(workingAnnotationProperty); - foreach (RDFTriple annotationTriple in graph[iri, workingAnnotationProperty, null, null]) - { - OWLAnnotation annotation = annotationTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO - ? new OWLAnnotation(annotationProperty, (RDFResource)annotationTriple.Object) - : new OWLAnnotation(annotationProperty, new OWLLiteral((RDFLiteral)annotationTriple.Object)); - - LoadNestedAnnotation(ont, annotationTriple, annotation, annAxiomsGraph); - - annotations.Add(annotation); - } - } - } - void LoadNestedAnnotation(OWLOntology ont, RDFTriple annotationTriple, OWLAnnotation annotation, RDFGraph annAxiomsGraph) - { - #region Guards - if (annAxiomsGraph.TriplesCount == 0) - return; - #endregion - - RDFSelectQuery query = new RDFSelectQuery() - .AddPatternGroup(new RDFPatternGroup() - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, annotationTriple.Subject)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, annotationTriple.Predicate)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, annotationTriple.Object)) - .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) - .AddPattern(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY))) - .AddModifier(new RDFLimitModifier(1)); - RDFSelectQueryResult result = query.ApplyToGraph(annAxiomsGraph); - if (result.SelectResultsCount > 0) - { - DataRow resultRow = result.SelectResults.Rows[0]; - RDFPatternMember axiom = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?AXIOM"].ToString()); - RDFPatternMember annProp = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNPROP"].ToString()); - RDFPatternMember annVal = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNVAL"].ToString()); - RDFTriple nestedAnnotationTriple = annVal is RDFResource annValRes - ? new RDFTriple((RDFResource)axiom, (RDFResource)annProp, annValRes) - : new RDFTriple((RDFResource)axiom, (RDFResource)annProp, (RDFLiteral)annVal); - OWLAnnotation nestedAnnotation = annVal is RDFResource annValRes2 - ? new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), annValRes2) - : new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), new OWLLiteral((RDFLiteral)annVal)); - annotation.Annotation = nestedAnnotation; - - LoadNestedAnnotation(ont, nestedAnnotationTriple, annotation.Annotation, annAxiomsGraph); - } - } - //Expressions - void LoadAnnotationPropertyExpression(OWLOntology ont, RDFResource apIRI, out OWLAnnotationPropertyExpression apex) - { - apex = null; - if (typeGraph[apIRI, null, RDFVocabulary.OWL.ANNOTATION_PROPERTY, null].TriplesCount > 0) - apex = new OWLAnnotationProperty(apIRI); - } - void LoadObjectPropertyExpression(OWLOntology ont, RDFResource opIRI, out OWLObjectPropertyExpression opex) - { - opex = null; - if (typeGraph[opIRI, null, RDFVocabulary.OWL.OBJECT_PROPERTY, null].TriplesCount > 0) - opex = new OWLObjectProperty(opIRI); - else if ((graph[opIRI, RDFVocabulary.OWL.INVERSE_OF, null, null].FirstOrDefault()?.Object) is RDFResource objectProperty) - opex = new OWLObjectInverseOf(new OWLObjectProperty(objectProperty)); - } - void LoadDataPropertyExpression(OWLOntology ont, RDFResource dpIRI, out OWLDataPropertyExpression dpex) - { - dpex = null; - if (typeGraph[dpIRI, null, RDFVocabulary.OWL.DATATYPE_PROPERTY, null].TriplesCount > 0) - dpex = new OWLDataProperty(dpIRI); - } - void LoadIndividualExpression(OWLOntology ont, RDFResource idvIRI, out OWLIndividualExpression idvex) - { - idvex = null; - if (idvIRI.IsBlank) - idvex = new OWLAnonymousIndividual(idvIRI.ToString().Substring(6)); - else if (typeGraph[idvIRI, null, null, null].TriplesCount > 0) - idvex = new OWLNamedIndividual(idvIRI); - } - void LoadClassExpression(OWLOntology ont, RDFResource clsIRI, out OWLClassExpression clex) - { - clex = null; - RDFGraph clsGraph = graph[clsIRI, null, null, null]; - - #region Restriction - if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.RESTRICTION, null].TriplesCount > 0) - { - #region AllValuesFrom - if (clsGraph[null, RDFVocabulary.OWL.ALL_VALUES_FROM, null, null].FirstOrDefault()?.Object is RDFResource allValuesFrom) - { - LoadObjectAllValuesFrom(ont, clsIRI, allValuesFrom, out OWLObjectAllValuesFrom objAVF); - if (objAVF != null) - { - clex = objAVF; - return; - } - LoadDataAllValuesFrom(ont, clsIRI, allValuesFrom, out OWLDataAllValuesFrom dtAVF); - if (dtAVF != null) - { - clex = dtAVF; - return; - } - } - #endregion - - #region SomeValuesFrom - if (clsGraph[null, RDFVocabulary.OWL.SOME_VALUES_FROM, null, null].FirstOrDefault()?.Object is RDFResource someValuesFrom) - { - LoadObjectSomeValuesFrom(ont, clsIRI, someValuesFrom, out OWLObjectSomeValuesFrom objSVF); - if (objSVF != null) - { - clex = objSVF; - return; - } - LoadDataSomeValuesFrom(ont, clsIRI, someValuesFrom, out OWLDataSomeValuesFrom dtSVF); - if (dtSVF != null) - { - clex = dtSVF; - return; - } - } - #endregion - - #region HasSelf - if (clsGraph[null, RDFVocabulary.OWL.HAS_SELF, null, RDFTypedLiteral.True].TriplesCount > 0) - { - LoadObjectHasSelf(ont, clsIRI, out OWLObjectHasSelf objHS); - if (objHS != null) - { - clex = objHS; - return; - } - } - #endregion - - #region HasValue - if (clsGraph[null, RDFVocabulary.OWL.HAS_VALUE, null, null].TriplesCount > 0) - { - LoadObjectHasValue(ont, clsIRI, out OWLObjectHasValue objHV); - if (objHV != null) - { - clex = objHV; - return; - } - LoadDataHasValue(ont, clsIRI, out OWLDataHasValue dtHV); - if (dtHV != null) - { - clex = dtHV; - return; - } - } - #endregion - - #region ExactCardinality - if (clsGraph[null, RDFVocabulary.OWL.CARDINALITY, null, null].TriplesCount > 0 - || clsGraph[null, RDFVocabulary.OWL.QUALIFIED_CARDINALITY, null, null].TriplesCount > 0) - { - LoadObjectExactCardinality(ont, clsIRI, out OWLObjectExactCardinality objEXCR); - if (objEXCR != null) - { - clex = objEXCR; - return; - } - LoadDataExactCardinality(ont, clsIRI, out OWLDataExactCardinality dtEXCR); - if (dtEXCR != null) - { - clex = dtEXCR; - return; - } - } - #endregion - - #region MinCardinality - if (clsGraph[null, RDFVocabulary.OWL.MIN_CARDINALITY, null, null].TriplesCount > 0 - || clsGraph[null, RDFVocabulary.OWL.MIN_QUALIFIED_CARDINALITY, null, null].TriplesCount > 0) - { - LoadObjectMinCardinality(ont, clsIRI, out OWLObjectMinCardinality objMINCR); - if (objMINCR != null) - { - clex = objMINCR; - return; - } - LoadDataMinCardinality(ont, clsIRI, out OWLDataMinCardinality dtMINCR); - if (dtMINCR != null) - { - clex = dtMINCR; - return; - } - } - #endregion - - #region MaxCardinality - if (clsGraph[null, RDFVocabulary.OWL.MAX_CARDINALITY, null, null].TriplesCount > 0 - || clsGraph[null, RDFVocabulary.OWL.MAX_QUALIFIED_CARDINALITY, null, null].TriplesCount > 0) - { - LoadObjectMaxCardinality(ont, clsIRI, out OWLObjectMaxCardinality objMAXCR); - if (objMAXCR != null) - { - clex = objMAXCR; - return; - } - LoadDataMaxCardinality(ont, clsIRI, out OWLDataMaxCardinality dtMAXCR); - if (dtMAXCR != null) - { - clex = dtMAXCR; - return; - } - } - #endregion - } - #endregion - - #region Composite - if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS, null].TriplesCount > 0 - && (clsGraph[null, RDFVocabulary.OWL.UNION_OF, null, null].TriplesCount > 0 - || clsGraph[null, RDFVocabulary.OWL.INTERSECTION_OF, null, null].TriplesCount > 0 - || clsGraph[null, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].TriplesCount > 0)) - { - #region UnionOf - LoadObjectUnionOf(ont, clsIRI, out OWLObjectUnionOf objUNOF); - if (objUNOF != null) + void LoadEquivalentClasses(OWLOntology ont, RDFGraph annAxiomsGraph) { - clex = objUNOF; - return; + foreach (RDFTriple equivClassTriple in graph[null, RDFVocabulary.OWL.EQUIVALENT_CLASS, null, null]) + { + LoadClassExpression(ont, (RDFResource)equivClassTriple.Subject, out OWLClassExpression leftCLex); + LoadClassExpression(ont, (RDFResource)equivClassTriple.Object, out OWLClassExpression rightCLex); + + if (leftCLex != null && rightCLex != null) + { + OWLEquivalentClasses equivalentClasses = new OWLEquivalentClasses() { + ClassExpressions = new List() { leftCLex, rightCLex } }; + + LoadAxiomAnnotations(ont, equivClassTriple, equivalentClasses, annAxiomsGraph); + + ont.ClassAxioms.Add(equivalentClasses); + } + } } - #endregion + void LoadDisjointClasses(OWLOntology ont, RDFGraph annAxiomsGraph) + { + //Load axioms built with owl:disjointWith + foreach (RDFTriple disjointWithTriple in graph[null, RDFVocabulary.OWL.DISJOINT_WITH, null, null]) + { + LoadClassExpression(ont, (RDFResource)disjointWithTriple.Subject, out OWLClassExpression leftCLE); + LoadClassExpression(ont, (RDFResource)disjointWithTriple.Object, out OWLClassExpression rightCLE); + + if (leftCLE != null && rightCLE != null) + { + OWLDisjointClasses disjointClasses = new OWLDisjointClasses() { + ClassExpressions = new List() { leftCLE, rightCLE } }; + + LoadAxiomAnnotations(ont, disjointWithTriple, disjointClasses, annAxiomsGraph); - #region IntersectionOf - LoadObjectIntersectionOf(ont, clsIRI, out OWLObjectIntersectionOf objINTOF); - if (objINTOF != null) + ont.ClassAxioms.Add(disjointClasses); + } + } + + //Load axioms built with owl:AllDisjointClasses + foreach (RDFTriple allDisjointClassesTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DISJOINT_CLASSES, null]) + if (graph[(RDFResource)allDisjointClassesTriple.Subject, RDFVocabulary.OWL.MEMBERS, null, null] + .FirstOrDefault()?.Object is RDFResource adjcCollectionRepresentative) + { + List adjcMembers = new List(); + + RDFCollection adjcCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adjcCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource adjcMember in adjcCollection.Items.Cast()) + { + LoadClassExpression(ont, adjcMember, out OWLClassExpression clex); + if (clex != null) + adjcMembers.Add(clex); + } + + if (adjcMembers.Count >= 2) + { + OWLDisjointClasses disjointClasses = new OWLDisjointClasses() { + ClassExpressions = adjcMembers }; + + LoadIRIAnnotations(ont, new List() { + RDFVocabulary.OWL.DEPRECATED, + RDFVocabulary.RDFS.COMMENT, + RDFVocabulary.RDFS.LABEL, + RDFVocabulary.RDFS.SEE_ALSO, + RDFVocabulary.RDFS.IS_DEFINED_BY + }, (RDFResource)allDisjointClassesTriple.Subject, annAxiomsGraph, out List adjcAnnotations); + disjointClasses.Annotations = adjcAnnotations; + + ont.ClassAxioms.Add(disjointClasses); + } + } + } + void LoadDisjointUnion(OWLOntology ont, RDFGraph annAxiomsGraph) { - clex = objINTOF; - return; + foreach (RDFTriple disjointUnionOfTriple in graph[null, RDFVocabulary.OWL.DISJOINT_UNION_OF, null, null]) + { + LoadClassExpression(ont, (RDFResource)disjointUnionOfTriple.Subject, out OWLClassExpression clsExp); + if (!(clsExp is OWLClass classIRI)) + continue; + + List disjointUnionMembers = new List(); + RDFCollection disjointUnionMembersCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)disjointUnionOfTriple.Object, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource disjointUnionMember in disjointUnionMembersCollection.Items.Cast()) + { + LoadClassExpression(ont, disjointUnionMember, out OWLClassExpression clsMemberExp); + if (clsMemberExp != null) + disjointUnionMembers.Add(clsMemberExp); + } + + if (disjointUnionMembers.Count >= 2) + { + OWLDisjointUnion disjointUnion = new OWLDisjointUnion() { + ClassIRI = classIRI, ClassExpressions = disjointUnionMembers }; + + LoadAxiomAnnotations(ont, disjointUnionOfTriple, disjointUnion, annAxiomsGraph); + + ont.ClassAxioms.Add(disjointUnion); + } + } } - #endregion + void LoadHasKey(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple hasKeyTriple in graph[null, RDFVocabulary.OWL.HAS_KEY, null, null]) + { + LoadClassExpression(ont, (RDFResource)hasKeyTriple.Subject, out OWLClassExpression clsExp); + if (!(clsExp is OWLClass classIRI)) + continue; + + List haskeyOPMembers = new List(); + List haskeyDPMembers = new List(); + RDFCollection haskeyMembersCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)hasKeyTriple.Object, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource hasKeyMember in haskeyMembersCollection.Items.Cast()) + { + LoadObjectPropertyExpression(ont, hasKeyMember, out OWLObjectPropertyExpression objPropMember); + if (objPropMember != null) + { + haskeyOPMembers.Add(objPropMember); + continue; + } + LoadDataPropertyExpression(ont, hasKeyMember, out OWLDataPropertyExpression dtPropMember); + if (dtPropMember is OWLDataProperty dtProp) + haskeyDPMembers.Add(dtProp); + } + + OWLHasKey hasKey = new OWLHasKey() { + ClassExpression = clsExp, ObjectPropertyExpressions = haskeyOPMembers, DataProperties = haskeyDPMembers }; + + LoadAxiomAnnotations(ont, hasKeyTriple, hasKey, annAxiomsGraph); + + ont.KeyAxioms.Add(hasKey); + } + } + void LoadDatatypeDefinition(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple datatypeTriple in typeGraph[null, null, RDFVocabulary.RDFS.DATATYPE, null] + .Where(t => !((RDFResource)t.Subject).IsBlank)) + { + if (!(graph[(RDFResource)datatypeTriple.Subject, RDFVocabulary.OWL.EQUIVALENT_CLASS, null, null] + .FirstOrDefault()?.Object is RDFResource equivalentDatatype)) + continue; + + LoadDataRangeExpression(ont, equivalentDatatype, out OWLDataRangeExpression drex); + if (drex == null) + continue; + + OWLDatatypeDefinition datatypeDefinition = new OWLDatatypeDefinition() + { + Datatype = new OWLDatatype((RDFResource)datatypeTriple.Subject), + DataRangeExpression = drex + }; + + LoadAxiomAnnotations(ont, datatypeTriple, datatypeDefinition, annAxiomsGraph); + + ont.DatatypeDefinitionAxioms.Add(datatypeDefinition); + } + } + void LoadSameIndividual(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple sameAsTriple in graph[null, RDFVocabulary.OWL.SAME_AS, null, null]) + { + LoadIndividualExpression(ont, (RDFResource)sameAsTriple.Subject, out OWLIndividualExpression leftIE); + LoadIndividualExpression(ont, (RDFResource)sameAsTriple.Object, out OWLIndividualExpression rightIE); - #region ComplementOf - LoadObjectComplementOf(ont, clsIRI, out OWLObjectComplementOf objCMPOF); - if (objCMPOF != null) + if (leftIE != null && rightIE != null) + { + OWLSameIndividual sameIndividual = new OWLSameIndividual() { + IndividualExpressions = new List() { leftIE, rightIE } }; + + LoadAxiomAnnotations(ont, sameAsTriple, sameIndividual, annAxiomsGraph); + + ont.AssertionAxioms.Add(sameIndividual); + } + } + } + void LoadDifferentIndividuals(OWLOntology ont, RDFGraph annAxiomsGraph) { - clex = objCMPOF; - return; + //Load axioms built with owl:differentFrom + foreach (RDFTriple differentFromTriple in graph[null, RDFVocabulary.OWL.DIFFERENT_FROM, null, null]) + { + LoadIndividualExpression(ont, (RDFResource)differentFromTriple.Subject, out OWLIndividualExpression leftIE); + LoadIndividualExpression(ont, (RDFResource)differentFromTriple.Object, out OWLIndividualExpression rightIE); + + if (leftIE != null && rightIE != null) + { + OWLDifferentIndividuals differentIndividuals = new OWLDifferentIndividuals() { + IndividualExpressions = new List() { leftIE, rightIE } }; + + LoadAxiomAnnotations(ont, differentFromTriple, differentIndividuals, annAxiomsGraph); + + ont.AssertionAxioms.Add(differentIndividuals); + } + } + + //Load axioms built with owl:AllDifferent + foreach (RDFTriple allDifferentTriple in typeGraph[null, null, RDFVocabulary.OWL.ALL_DIFFERENT, null]) + if (graph[(RDFResource)allDifferentTriple.Subject, RDFVocabulary.OWL.DISTINCT_MEMBERS, null, null] + .FirstOrDefault()?.Object is RDFResource adiffCollectionRepresentative) + { + List adiffMembers = new List(); + + RDFCollection adiffCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, adiffCollectionRepresentative, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource adiffMember in adiffCollection.Items.Cast()) + { + LoadIndividualExpression(ont, adiffMember, out OWLIndividualExpression idvex); + if (idvex != null) + adiffMembers.Add(idvex); + } + + if (adiffMembers.Count >= 2) + { + OWLDifferentIndividuals differentIndividuals = new OWLDifferentIndividuals() { + IndividualExpressions = adiffMembers }; + + LoadIRIAnnotations(ont, new List() { + RDFVocabulary.OWL.DEPRECATED, + RDFVocabulary.RDFS.COMMENT, + RDFVocabulary.RDFS.LABEL, + RDFVocabulary.RDFS.SEE_ALSO, + RDFVocabulary.RDFS.IS_DEFINED_BY + }, (RDFResource)allDifferentTriple.Subject, annAxiomsGraph, out List adjpAnnotations); + differentIndividuals.Annotations = adjpAnnotations; + + ont.AssertionAxioms.Add(differentIndividuals); + } + } } - #endregion - } - #endregion + void LoadObjectPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple objPropTriple in typeGraph[null, null, RDFVocabulary.OWL.OBJECT_PROPERTY, null]) + { + OWLObjectProperty objProp = new OWLObjectProperty((RDFResource)objPropTriple.Subject); + foreach (RDFTriple objPropAsnTriple in graph[null, (RDFResource)objPropTriple.Subject, null, null]) + { + LoadIndividualExpression(ont, (RDFResource)objPropAsnTriple.Subject, out OWLIndividualExpression leftIE); + LoadIndividualExpression(ont, (RDFResource)objPropAsnTriple.Object, out OWLIndividualExpression rightIE); + + if (leftIE != null && rightIE != null) + { + OWLObjectPropertyAssertion objPropAsn = new OWLObjectPropertyAssertion() { + ObjectPropertyExpression = objProp, + SourceIndividualExpression = leftIE, + TargetIndividualExpression = rightIE }; + + LoadAxiomAnnotations(ont, objPropAsnTriple, objPropAsn, annAxiomsGraph); + + ont.AssertionAxioms.Add(objPropAsn); + } + } + } + } + void LoadNegativeObjectPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) + { + RDFSelectQuery query = new RDFSelectQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION)) + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.SOURCE_INDIVIDUAL, new RDFVariable("?SIDV"))) + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.ASSERTION_PROPERTY, new RDFVariable("?OBJP"))) + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.TARGET_INDIVIDUAL, new RDFVariable("?TIDV"))) + .AddPattern(new RDFPattern(new RDFVariable("?OBJP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.OBJECT_PROPERTY)) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?SIDV"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?OBJP"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?TIDV")))); + RDFSelectQueryResult result = query.ApplyToGraph(graph); + foreach (DataRow resultRow in result.SelectResults.Rows) + { + LoadIndividualExpression(ont, (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?SIDV"].ToString()), out OWLIndividualExpression leftIE); + LoadIndividualExpression(ont, (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?TIDV"].ToString()), out OWLIndividualExpression rightIE); + + if (leftIE != null && rightIE != null) + { + RDFResource axiomIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?NASN"].ToString()); + OWLObjectProperty objProp = new OWLObjectProperty((RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?OBJP"].ToString())); + + OWLNegativeObjectPropertyAssertion negObjPropAsn = new OWLNegativeObjectPropertyAssertion() + { + ObjectPropertyExpression = objProp, + SourceIndividualExpression = leftIE, + TargetIndividualExpression = rightIE + }; + + LoadIRIAnnotations(ont, new List() { + RDFVocabulary.OWL.DEPRECATED, + RDFVocabulary.RDFS.COMMENT, + RDFVocabulary.RDFS.LABEL, + RDFVocabulary.RDFS.SEE_ALSO, + RDFVocabulary.RDFS.IS_DEFINED_BY + }, axiomIRI, annAxiomsGraph, out List nasnAnnotations); + negObjPropAsn.Annotations = nasnAnnotations; + + ont.AssertionAxioms.Add(negObjPropAsn); + } + } + } + void LoadDataPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple dtPropTriple in typeGraph[null, null, RDFVocabulary.OWL.DATATYPE_PROPERTY, null]) + { + OWLDataProperty dtProp = new OWLDataProperty((RDFResource)dtPropTriple.Subject); + foreach (RDFTriple dtPropAsnTriple in graph[null, (RDFResource)dtPropTriple.Subject, null, null]) + { + LoadIndividualExpression(ont, (RDFResource)dtPropAsnTriple.Subject, out OWLIndividualExpression leftIE); + + if (leftIE != null) + { + OWLDataPropertyAssertion dtPropAsn = new OWLDataPropertyAssertion() + { + DataProperty = dtProp, + IndividualExpression = leftIE, + Literal = new OWLLiteral((RDFLiteral)dtPropAsnTriple.Object) + }; + + LoadAxiomAnnotations(ont, dtPropAsnTriple, dtPropAsn, annAxiomsGraph); + + ont.AssertionAxioms.Add(dtPropAsn); + } + } + } + } + void LoadClassAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) + { + RDFSelectQuery query = new RDFSelectQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?IDV"), RDFVocabulary.RDF.TYPE, new RDFVariable("?CLS"))) + .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS).UnionWithNext()) + .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS).UnionWithNext()) + .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_CLASS).UnionWithNext()) + .AddPattern(new RDFPattern(new RDFVariable("?CLS"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.RESTRICTION)) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?IDV"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?CLS"))) + .AddFilter(new RDFBooleanNotFilter(new RDFSameTermFilter(new RDFVariable("?IDV"), new RDFVariable("?CLS")))) + .AddFilter(new RDFBooleanNotFilter(new RDFInFilter(new RDFVariable("?CLS"), new List() { + RDFVocabulary.RDF.LIST, RDFVocabulary.RDFS.CLASS, RDFVocabulary.OWL.CLASS, RDFVocabulary.OWL.DEPRECATED_CLASS, RDFVocabulary.OWL.RESTRICTION })))); + RDFSelectQueryResult result = query.ApplyToGraph(typeGraph); + foreach (DataRow resultRow in result.SelectResults.Rows) + { + RDFResource clsIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?CLS"].ToString()); + RDFResource idvIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?IDV"].ToString()); + LoadClassExpression(ont, clsIRI, out OWLClassExpression clsEx); + LoadIndividualExpression(ont, idvIRI, out OWLIndividualExpression idvEx); - #region Enumerate - if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS, null].TriplesCount > 0 - && clsGraph[null, RDFVocabulary.OWL.ONE_OF, null, null].TriplesCount > 0) - { - #region OneOf - LoadObjectOneOf(ont, clsIRI, out OWLObjectOneOf objONEOF); - if (objONEOF != null) + if (idvEx != null && clsEx != null) + { + OWLClassAssertion classAssertion = new OWLClassAssertion() { + ClassExpression = clsEx, IndividualExpression = idvEx }; + + LoadAxiomAnnotations(ont, new RDFTriple(idvIRI, RDFVocabulary.RDF.TYPE, clsIRI), classAssertion, annAxiomsGraph); + + ont.AssertionAxioms.Add(classAssertion); + } + } + } + void LoadNegativeDataPropertyAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) + { + RDFSelectQuery query = new RDFSelectQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION)) + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.SOURCE_INDIVIDUAL, new RDFVariable("?SIDV"))) + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.ASSERTION_PROPERTY, new RDFVariable("?DTP"))) + .AddPattern(new RDFPattern(new RDFVariable("?NASN"), RDFVocabulary.OWL.TARGET_VALUE, new RDFVariable("?TVAL"))) + .AddPattern(new RDFPattern(new RDFVariable("?DTP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DATATYPE_PROPERTY)) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?SIDV"))) + .AddFilter(new RDFIsUriFilter(new RDFVariable("?DTP"))) + .AddFilter(new RDFIsLiteralFilter(new RDFVariable("?TVAL")))); + RDFSelectQueryResult result = query.ApplyToGraph(graph); + foreach (DataRow resultRow in result.SelectResults.Rows) + { + LoadIndividualExpression(ont, (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?SIDV"].ToString()), out OWLIndividualExpression leftIE); + + if (leftIE != null) + { + RDFResource axiomIRI = (RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?NASN"].ToString()); + OWLDataProperty dtProp = new OWLDataProperty((RDFResource)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?DTP"].ToString())); + OWLLiteral litVal = new OWLLiteral((RDFLiteral)RDFQueryUtilities.ParseRDFPatternMember(resultRow["?TVAL"].ToString())); + + OWLNegativeDataPropertyAssertion negDtPropAsn = new OWLNegativeDataPropertyAssertion() + { + DataProperty = dtProp, + IndividualExpression = leftIE, + Literal = litVal + }; + + LoadIRIAnnotations(ont, new List() { + RDFVocabulary.OWL.DEPRECATED, + RDFVocabulary.RDFS.COMMENT, + RDFVocabulary.RDFS.LABEL, + RDFVocabulary.RDFS.SEE_ALSO, + RDFVocabulary.RDFS.IS_DEFINED_BY + }, axiomIRI, annAxiomsGraph, out List nasnAnnotations); + negDtPropAsn.Annotations = nasnAnnotations; + + ont.AssertionAxioms.Add(negDtPropAsn); + } + } + } + void LoadAnnotationAssertions(OWLOntology ont, RDFGraph annAxiomsGraph) + { + #region Fetch Declarations + List declaredClasses = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLClass) + .Select(dax => (OWLClass)dax.Expression) + .ToList(); + List declaredDatatypes = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLDatatype) + .Select(dax => (OWLDatatype)dax.Expression) + .ToList(); + List declaredObjectProperties = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLObjectProperty) + .Select(dax => (OWLObjectProperty)dax.Expression) + .ToList(); + List declaredDataProperties = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLDataProperty) + .Select(dax => (OWLDataProperty)dax.Expression) + .ToList(); + List declaredAnnotationProperties = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLAnnotationProperty) + .Select(dax => (OWLAnnotationProperty)dax.Expression) + .ToList(); + List declaredIndividuals = ont.DeclarationAxioms.Where(dax => dax.Expression is OWLNamedIndividual) + .Select(dax => (OWLNamedIndividual)dax.Expression) + .ToList(); + #endregion + + declaredAnnotationProperties.ForEach(annProp => + { + RDFResource annPropIRI = annProp.GetIRI(); + RDFGraph annPropGraph = graph[null, annPropIRI, null, null]; + + //Class Annotations + declaredClasses.ForEach(cls => + { + RDFResource clsIRI = cls.GetIRI(); + foreach (RDFTriple clsAnnPropTriple in annPropGraph[clsIRI, null, null, null]) + { + OWLAnnotationAssertion annAsn; + if (clsAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) + annAsn = new OWLAnnotationAssertion(annProp, clsIRI, (RDFResource)clsAnnPropTriple.Object); + else + annAsn = new OWLAnnotationAssertion(annProp, clsIRI, new OWLLiteral((RDFLiteral)clsAnnPropTriple.Object)); + + LoadAxiomAnnotations(ont, clsAnnPropTriple, annAsn, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annAsn); + } + }); + + //Datatype Annotations + declaredDatatypes.ForEach(dt => + { + RDFResource dtIRI = dt.GetIRI(); + foreach (RDFTriple dtAnnPropTriple in annPropGraph[dtIRI, null, null, null]) + { + OWLAnnotationAssertion annAsn; + if (dtAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) + annAsn = new OWLAnnotationAssertion(annProp, dtIRI, (RDFResource)dtAnnPropTriple.Object); + else + annAsn = new OWLAnnotationAssertion(annProp, dtIRI, new OWLLiteral((RDFLiteral)dtAnnPropTriple.Object)); + + LoadAxiomAnnotations(ont, dtAnnPropTriple, annAsn, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annAsn); + } + }); + + //ObjectProperty Annotations + declaredObjectProperties.ForEach(op => + { + RDFResource opIRI = op.GetIRI(); + foreach (RDFTriple opAnnPropTriple in annPropGraph[opIRI, null, null, null]) + { + OWLAnnotationAssertion annAsn; + if (opAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) + annAsn = new OWLAnnotationAssertion(annProp, opIRI, (RDFResource)opAnnPropTriple.Object); + else + annAsn = new OWLAnnotationAssertion(annProp, opIRI, new OWLLiteral((RDFLiteral)opAnnPropTriple.Object)); + + LoadAxiomAnnotations(ont, opAnnPropTriple, annAsn, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annAsn); + } + }); + + //DataProperty Annotations + declaredDataProperties.ForEach(dp => + { + RDFResource dpIRI = dp.GetIRI(); + foreach (RDFTriple dpAnnPropTriple in annPropGraph[dpIRI, null, null, null]) + { + OWLAnnotationAssertion annAsn; + if (dpAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) + annAsn = new OWLAnnotationAssertion(annProp, dpIRI, (RDFResource)dpAnnPropTriple.Object); + else + annAsn = new OWLAnnotationAssertion(annProp, dpIRI, new OWLLiteral((RDFLiteral)dpAnnPropTriple.Object)); + + LoadAxiomAnnotations(ont, dpAnnPropTriple, annAsn, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annAsn); + } + }); + + //AnnotationProperty Annotations + declaredAnnotationProperties.ForEach(ap => + { + RDFResource apIRI = ap.GetIRI(); + foreach (RDFTriple apAnnPropTriple in annPropGraph[apIRI, null, null, null]) + { + OWLAnnotationAssertion annAsn; + if (apAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) + annAsn = new OWLAnnotationAssertion(annProp, apIRI, (RDFResource)apAnnPropTriple.Object); + else + annAsn = new OWLAnnotationAssertion(annProp, apIRI, new OWLLiteral((RDFLiteral)apAnnPropTriple.Object)); + + LoadAxiomAnnotations(ont, apAnnPropTriple, annAsn, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annAsn); + } + }); + + //Individual Annotations + declaredIndividuals.ForEach(idv => + { + RDFResource idvIRI = idv.GetIRI(); + foreach (RDFTriple idvAnnPropTriple in annPropGraph[idvIRI, null, null, null]) + { + OWLAnnotationAssertion annAsn; + if (idvAnnPropTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) + annAsn = new OWLAnnotationAssertion(annProp, idvIRI, (RDFResource)idvAnnPropTriple.Object); + else + annAsn = new OWLAnnotationAssertion(annProp, idvIRI, new OWLLiteral((RDFLiteral)idvAnnPropTriple.Object)); + + LoadAxiomAnnotations(ont, idvAnnPropTriple, annAsn, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annAsn); + } + }); + }); + } + void LoadSubAnnotationProperties(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple subPropTriple in graph[null, RDFVocabulary.RDFS.SUB_PROPERTY_OF, null, null]) + { + LoadAnnotationPropertyExpression(ont, (RDFResource)subPropTriple.Subject, out OWLAnnotationPropertyExpression leftAPex); + LoadAnnotationPropertyExpression(ont, (RDFResource)subPropTriple.Object, out OWLAnnotationPropertyExpression rightAPex); + + if (leftAPex is OWLAnnotationProperty leftAP && rightAPex is OWLAnnotationProperty rightAP) + { + OWLSubAnnotationPropertyOf subAnnotationPropertyOf = new OWLSubAnnotationPropertyOf() { + SubAnnotationProperty = leftAP, SuperAnnotationProperty = rightAP }; + + LoadAxiomAnnotations(ont, subPropTriple, subAnnotationPropertyOf, annAxiomsGraph); + + ont.AnnotationAxioms.Add(subAnnotationPropertyOf); + } + } + } + void LoadAnnotationPropertyDomain(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple domainTriple in graph[null, RDFVocabulary.RDFS.DOMAIN, null, null]) + { + LoadAnnotationPropertyExpression(ont, (RDFResource)domainTriple.Subject, out OWLAnnotationPropertyExpression annEXP); + + if (annEXP is OWLAnnotationProperty annProp && domainTriple.Object is RDFResource domainObject) + { + OWLAnnotationPropertyDomain annotationPropertyDomain = new OWLAnnotationPropertyDomain() { + AnnotationProperty = annProp, IRI = domainObject.ToString() }; + + LoadAxiomAnnotations(ont, domainTriple, annotationPropertyDomain, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annotationPropertyDomain); + } + } + } + void LoadAnnotationPropertyRange(OWLOntology ont, RDFGraph annAxiomsGraph) + { + foreach (RDFTriple rangeTriple in graph[null, RDFVocabulary.RDFS.RANGE, null, null]) + { + LoadAnnotationPropertyExpression(ont, (RDFResource)rangeTriple.Subject, out OWLAnnotationPropertyExpression annEXP); + + if (annEXP is OWLAnnotationProperty annProp && rangeTriple.Object is RDFResource rangeObject) + { + OWLAnnotationPropertyRange annotationPropertyRange = new OWLAnnotationPropertyRange() { + AnnotationProperty = annProp, IRI = rangeObject.ToString() }; + + LoadAxiomAnnotations(ont, rangeTriple, annotationPropertyRange, annAxiomsGraph); + + ont.AnnotationAxioms.Add(annotationPropertyRange); + } + } + } + //Annotations + void LoadAxiomAnnotations(OWLOntology ont, RDFTriple axiomTriple, OWLAxiom axiom, RDFGraph annAxiomsGraph) + { + #region Guards + if (annAxiomsGraph.TriplesCount == 0) + return; + #endregion + + RDFSelectQuery query = new RDFSelectQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, axiomTriple.Subject)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, axiomTriple.Predicate)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, axiomTriple.Object)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) + .AddPattern(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY))); + RDFSelectQueryResult result = query.ApplyToGraph(annAxiomsGraph); + foreach (DataRow resultRow in result.SelectResults.Rows) + { + RDFPatternMember axiomIRI = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?AXIOM"].ToString()); + RDFPatternMember annProp = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNPROP"].ToString()); + RDFPatternMember annVal = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNVAL"].ToString()); + RDFTriple annotationTriple = annVal is RDFResource annValRes + ? new RDFTriple((RDFResource)axiomIRI, (RDFResource)annProp, annValRes) + : new RDFTriple((RDFResource)axiomIRI, (RDFResource)annProp, (RDFLiteral)annVal); + OWLAnnotation annotation = annVal is RDFResource annValRes2 + ? new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), annValRes2) + : new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), new OWLLiteral((RDFLiteral)annVal)); + + LoadNestedAnnotation(ont, annotationTriple, annotation, annAxiomsGraph); + + axiom.Annotations.Add(annotation); + } + } + void LoadIRIAnnotations(OWLOntology ont, List annotationProperties, RDFResource iri, RDFGraph annAxiomsGraph, out List annotations) + { + annotations = new List(); + + foreach (RDFTriple annPropTriple in typeGraph[null, null, RDFVocabulary.OWL.ANNOTATION_PROPERTY, null] + .Where(ap => !ap.Equals(RDFVocabulary.OWL.VERSION_IRI))) + { + RDFResource annPropIRI = (RDFResource)annPropTriple.Subject; + if (!annotationProperties.Any(ap => ap.Equals(annPropIRI))) + annotationProperties.Add(annPropIRI); + } + + foreach (RDFResource workingAnnotationProperty in annotationProperties) + { + OWLAnnotationProperty annotationProperty = new OWLAnnotationProperty(workingAnnotationProperty); + foreach (RDFTriple annotationTriple in graph[iri, workingAnnotationProperty, null, null]) + { + OWLAnnotation annotation = annotationTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO + ? new OWLAnnotation(annotationProperty, (RDFResource)annotationTriple.Object) + : new OWLAnnotation(annotationProperty, new OWLLiteral((RDFLiteral)annotationTriple.Object)); + + LoadNestedAnnotation(ont, annotationTriple, annotation, annAxiomsGraph); + + annotations.Add(annotation); + } + } + } + void LoadNestedAnnotation(OWLOntology ont, RDFTriple annotationTriple, OWLAnnotation annotation, RDFGraph annAxiomsGraph) + { + #region Guards + if (annAxiomsGraph.TriplesCount == 0) + return; + #endregion + + RDFSelectQuery query = new RDFSelectQuery() + .AddPatternGroup(new RDFPatternGroup() + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.AXIOM)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_SOURCE, annotationTriple.Subject)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_PROPERTY, annotationTriple.Predicate)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), RDFVocabulary.OWL.ANNOTATED_TARGET, annotationTriple.Object)) + .AddPattern(new RDFPattern(new RDFVariable("?AXIOM"), new RDFVariable("?ANNPROP"), new RDFVariable("?ANNVAL"))) + .AddPattern(new RDFPattern(new RDFVariable("?ANNPROP"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ANNOTATION_PROPERTY))) + .AddModifier(new RDFLimitModifier(1)); + RDFSelectQueryResult result = query.ApplyToGraph(annAxiomsGraph); + if (result.SelectResultsCount > 0) + { + DataRow resultRow = result.SelectResults.Rows[0]; + RDFPatternMember axiom = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?AXIOM"].ToString()); + RDFPatternMember annProp = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNPROP"].ToString()); + RDFPatternMember annVal = RDFQueryUtilities.ParseRDFPatternMember(resultRow["?ANNVAL"].ToString()); + RDFTriple nestedAnnotationTriple = annVal is RDFResource annValRes + ? new RDFTriple((RDFResource)axiom, (RDFResource)annProp, annValRes) + : new RDFTriple((RDFResource)axiom, (RDFResource)annProp, (RDFLiteral)annVal); + OWLAnnotation nestedAnnotation = annVal is RDFResource annValRes2 + ? new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), annValRes2) + : new OWLAnnotation(new OWLAnnotationProperty((RDFResource)annProp), new OWLLiteral((RDFLiteral)annVal)); + annotation.Annotation = nestedAnnotation; + + LoadNestedAnnotation(ont, nestedAnnotationTriple, annotation.Annotation, annAxiomsGraph); + } + } + //Expressions + void LoadAnnotationPropertyExpression(OWLOntology ont, RDFResource apIRI, out OWLAnnotationPropertyExpression apex) + { + apex = null; + if (typeGraph[apIRI, null, RDFVocabulary.OWL.ANNOTATION_PROPERTY, null].TriplesCount > 0) + apex = new OWLAnnotationProperty(apIRI); + } + void LoadObjectPropertyExpression(OWLOntology ont, RDFResource opIRI, out OWLObjectPropertyExpression opex) + { + opex = null; + if (typeGraph[opIRI, null, RDFVocabulary.OWL.OBJECT_PROPERTY, null].TriplesCount > 0) + opex = new OWLObjectProperty(opIRI); + else if ((graph[opIRI, RDFVocabulary.OWL.INVERSE_OF, null, null].FirstOrDefault()?.Object) is RDFResource objectProperty) + opex = new OWLObjectInverseOf(new OWLObjectProperty(objectProperty)); + } + void LoadDataPropertyExpression(OWLOntology ont, RDFResource dpIRI, out OWLDataPropertyExpression dpex) + { + dpex = null; + if (typeGraph[dpIRI, null, RDFVocabulary.OWL.DATATYPE_PROPERTY, null].TriplesCount > 0) + dpex = new OWLDataProperty(dpIRI); + } + void LoadIndividualExpression(OWLOntology ont, RDFResource idvIRI, out OWLIndividualExpression idvex) + { + idvex = null; + if (idvIRI.IsBlank) + idvex = new OWLAnonymousIndividual(idvIRI.ToString().Substring(6)); + else if (typeGraph[idvIRI, null, null, null].TriplesCount > 0) + idvex = new OWLNamedIndividual(idvIRI); + } + void LoadClassExpression(OWLOntology ont, RDFResource clsIRI, out OWLClassExpression clex) + { + clex = null; + RDFGraph clsGraph = graph[clsIRI, null, null, null]; + + #region Restriction + if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.RESTRICTION, null].TriplesCount > 0) + { + #region AllValuesFrom + if (clsGraph[null, RDFVocabulary.OWL.ALL_VALUES_FROM, null, null].FirstOrDefault()?.Object is RDFResource allValuesFrom) + { + LoadObjectAllValuesFrom(ont, clsIRI, allValuesFrom, out OWLObjectAllValuesFrom objAVF); + if (objAVF != null) + { + clex = objAVF; + return; + } + LoadDataAllValuesFrom(ont, clsIRI, allValuesFrom, out OWLDataAllValuesFrom dtAVF); + if (dtAVF != null) + { + clex = dtAVF; + return; + } + } + #endregion + + #region SomeValuesFrom + if (clsGraph[null, RDFVocabulary.OWL.SOME_VALUES_FROM, null, null].FirstOrDefault()?.Object is RDFResource someValuesFrom) + { + LoadObjectSomeValuesFrom(ont, clsIRI, someValuesFrom, out OWLObjectSomeValuesFrom objSVF); + if (objSVF != null) + { + clex = objSVF; + return; + } + LoadDataSomeValuesFrom(ont, clsIRI, someValuesFrom, out OWLDataSomeValuesFrom dtSVF); + if (dtSVF != null) + { + clex = dtSVF; + return; + } + } + #endregion + + #region HasSelf + if (clsGraph[null, RDFVocabulary.OWL.HAS_SELF, null, RDFTypedLiteral.True].TriplesCount > 0) + { + LoadObjectHasSelf(ont, clsIRI, out OWLObjectHasSelf objHS); + if (objHS != null) + { + clex = objHS; + return; + } + } + #endregion + + #region HasValue + if (clsGraph[null, RDFVocabulary.OWL.HAS_VALUE, null, null].TriplesCount > 0) + { + LoadObjectHasValue(ont, clsIRI, out OWLObjectHasValue objHV); + if (objHV != null) + { + clex = objHV; + return; + } + LoadDataHasValue(ont, clsIRI, out OWLDataHasValue dtHV); + if (dtHV != null) + { + clex = dtHV; + return; + } + } + #endregion + + #region ExactCardinality + if (clsGraph[null, RDFVocabulary.OWL.CARDINALITY, null, null].TriplesCount > 0 + || clsGraph[null, RDFVocabulary.OWL.QUALIFIED_CARDINALITY, null, null].TriplesCount > 0) + { + LoadObjectExactCardinality(ont, clsIRI, out OWLObjectExactCardinality objEXCR); + if (objEXCR != null) + { + clex = objEXCR; + return; + } + LoadDataExactCardinality(ont, clsIRI, out OWLDataExactCardinality dtEXCR); + if (dtEXCR != null) + { + clex = dtEXCR; + return; + } + } + #endregion + + #region MinCardinality + if (clsGraph[null, RDFVocabulary.OWL.MIN_CARDINALITY, null, null].TriplesCount > 0 + || clsGraph[null, RDFVocabulary.OWL.MIN_QUALIFIED_CARDINALITY, null, null].TriplesCount > 0) + { + LoadObjectMinCardinality(ont, clsIRI, out OWLObjectMinCardinality objMINCR); + if (objMINCR != null) + { + clex = objMINCR; + return; + } + LoadDataMinCardinality(ont, clsIRI, out OWLDataMinCardinality dtMINCR); + if (dtMINCR != null) + { + clex = dtMINCR; + return; + } + } + #endregion + + #region MaxCardinality + if (clsGraph[null, RDFVocabulary.OWL.MAX_CARDINALITY, null, null].TriplesCount > 0 + || clsGraph[null, RDFVocabulary.OWL.MAX_QUALIFIED_CARDINALITY, null, null].TriplesCount > 0) + { + LoadObjectMaxCardinality(ont, clsIRI, out OWLObjectMaxCardinality objMAXCR); + if (objMAXCR != null) + { + clex = objMAXCR; + return; + } + LoadDataMaxCardinality(ont, clsIRI, out OWLDataMaxCardinality dtMAXCR); + if (dtMAXCR != null) + { + clex = dtMAXCR; + return; + } + } + #endregion + } + #endregion + + #region Composite + if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS, null].TriplesCount > 0 + && (clsGraph[null, RDFVocabulary.OWL.UNION_OF, null, null].TriplesCount > 0 + || clsGraph[null, RDFVocabulary.OWL.INTERSECTION_OF, null, null].TriplesCount > 0 + || clsGraph[null, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].TriplesCount > 0)) + { + #region UnionOf + LoadObjectUnionOf(ont, clsIRI, out OWLObjectUnionOf objUNOF); + if (objUNOF != null) + { + clex = objUNOF; + return; + } + #endregion + + #region IntersectionOf + LoadObjectIntersectionOf(ont, clsIRI, out OWLObjectIntersectionOf objINTOF); + if (objINTOF != null) + { + clex = objINTOF; + return; + } + #endregion + + #region ComplementOf + LoadObjectComplementOf(ont, clsIRI, out OWLObjectComplementOf objCMPOF); + if (objCMPOF != null) + { + clex = objCMPOF; + return; + } + #endregion + } + #endregion + + #region Enumerate + if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS, null].TriplesCount > 0 + && clsGraph[null, RDFVocabulary.OWL.ONE_OF, null, null].TriplesCount > 0) + { + #region OneOf + LoadObjectOneOf(ont, clsIRI, out OWLObjectOneOf objONEOF); + if (objONEOF != null) + { + clex = objONEOF; + return; + } + #endregion + } + #endregion + + #region Class + if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS, null].TriplesCount > 0) + clex = new OWLClass(clsIRI); + #endregion + } + void LoadObjectAllValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource allValuesFrom, out OWLObjectAllValuesFrom objAVF) + { + objAVF = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); + if (onPropertyOPEX == null) + return; + + LoadClassExpression(ont, allValuesFrom, out OWLClassExpression allValuesFromCLEX); + if (allValuesFromCLEX != null) + objAVF = new OWLObjectAllValuesFrom(onPropertyOPEX, allValuesFromCLEX); + } + } + void LoadObjectSomeValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource someValuesFrom, out OWLObjectSomeValuesFrom objSVF) + { + objSVF = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); + if (onPropertyOPEX == null) + return; + + LoadClassExpression(ont, someValuesFrom, out OWLClassExpression someValuesFromCLEX); + if (someValuesFromCLEX != null) + objSVF = new OWLObjectSomeValuesFrom(onPropertyOPEX, someValuesFromCLEX); + } + } + void LoadObjectHasSelf(OWLOntology ont, RDFResource clsIRI, out OWLObjectHasSelf objHS) + { + objHS = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); + if (onPropertyOPEX != null) + objHS = new OWLObjectHasSelf(onPropertyOPEX); + } + } + void LoadObjectHasValue(OWLOntology ont, RDFResource clsIRI, out OWLObjectHasValue objHV) + { + objHV = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); + if (onPropertyOPEX == null) + return; + + if (graph[clsIRI, RDFVocabulary.OWL.HAS_VALUE, null, null].FirstOrDefault()?.Object is RDFResource hasValue) + { + LoadIndividualExpression(ont, hasValue, out OWLIndividualExpression hasValueIDVEX); + if (hasValueIDVEX != null) + objHV = new OWLObjectHasValue(onPropertyOPEX, hasValueIDVEX); + } + } + } + void LoadObjectExactCardinality(OWLOntology ont, RDFResource clsIRI, out OWLObjectExactCardinality objEXCR) + { + objEXCR = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); + if (onPropertyOPEX == null) + return; + + //Cardinality + if (graph[clsIRI, RDFVocabulary.OWL.CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactCardLit + && exactCardLit.HasDecimalDatatype() + && uint.TryParse(exactCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactCardinality)) + { + objEXCR = new OWLObjectExactCardinality(onPropertyOPEX, exactCardinality); + return; + } + + //QualifiedCardinality + if (graph[clsIRI, RDFVocabulary.OWL.QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactQCardLit + && exactQCardLit.HasDecimalDatatype() + && uint.TryParse(exactQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactQCardinality) + && graph[clsIRI, RDFVocabulary.OWL.ON_CLASS, null, null].FirstOrDefault()?.Object is RDFResource onClass) + { + LoadClassExpression(ont, onClass, out OWLClassExpression onClassEX); + if (onClassEX != null) + objEXCR = new OWLObjectExactCardinality(onPropertyOPEX, exactQCardinality, onClassEX); + } + } + } + void LoadObjectMinCardinality(OWLOntology ont, RDFResource clsIRI, out OWLObjectMinCardinality objMINCR) + { + objMINCR = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); + if (onPropertyOPEX == null) + return; + + //Cardinality + if (graph[clsIRI, RDFVocabulary.OWL.MIN_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minCardLit + && minCardLit.HasDecimalDatatype() + && uint.TryParse(minCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minCardinality)) + { + objMINCR = new OWLObjectMinCardinality(onPropertyOPEX, minCardinality); + return; + } + + //QualifiedCardinality + if (graph[clsIRI, RDFVocabulary.OWL.MIN_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minQCardLit + && minQCardLit.HasDecimalDatatype() + && uint.TryParse(minQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minQCardinality) + && graph[clsIRI, RDFVocabulary.OWL.ON_CLASS, null, null].FirstOrDefault()?.Object is RDFResource onClass) + { + LoadClassExpression(ont, onClass, out OWLClassExpression onClassEX); + if (onClassEX != null) + objMINCR = new OWLObjectMinCardinality(onPropertyOPEX, minQCardinality, onClassEX); + } + } + } + void LoadObjectMaxCardinality(OWLOntology ont, RDFResource clsIRI, out OWLObjectMaxCardinality objMAXCR) + { + objMAXCR = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); + if (onPropertyOPEX == null) + return; + + //Cardinality + if (graph[clsIRI, RDFVocabulary.OWL.MAX_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxCardLit + && maxCardLit.HasDecimalDatatype() + && uint.TryParse(maxCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxCardinality)) + { + objMAXCR = new OWLObjectMaxCardinality(onPropertyOPEX, maxCardinality); + return; + } + + //QualifiedCardinality + if (graph[clsIRI, RDFVocabulary.OWL.MAX_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxQCardLit + && maxQCardLit.HasDecimalDatatype() + && uint.TryParse(maxQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxQCardinality) + && graph[clsIRI, RDFVocabulary.OWL.ON_CLASS, null, null].FirstOrDefault()?.Object is RDFResource onClass) + { + LoadClassExpression(ont, onClass, out OWLClassExpression onClassEX); + if (onClassEX != null) + objMAXCR = new OWLObjectMaxCardinality(onPropertyOPEX, maxQCardinality, onClassEX); + } + } + } + void LoadDataAllValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource allValuesFrom, out OWLDataAllValuesFrom dtAVF) + { + dtAVF = null; + + LoadDataRangeExpression(ont, allValuesFrom, out OWLDataRangeExpression allValuesFromDREX); + if (allValuesFromDREX == null) + return; + + foreach (RDFResource onProperty in graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null] + .Select(t => t.Object) + .OfType()) + { + LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); + if (onPropertyDPEX != null) + { + if (dtAVF == null) + dtAVF = new OWLDataAllValuesFrom() { DataProperties = new List() }; + dtAVF.DataProperties.Add((OWLDataProperty)onPropertyDPEX); + dtAVF.DataRangeExpression = allValuesFromDREX; + } + } + } + void LoadDataSomeValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource someValuesFrom, out OWLDataSomeValuesFrom dtSVF) + { + dtSVF = null; + + LoadDataRangeExpression(ont, someValuesFrom, out OWLDataRangeExpression someValuesFromDREX); + if (someValuesFromDREX == null) + return; + + foreach (RDFResource onProperty in graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null] + .Select(t => t.Object) + .OfType()) + { + LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); + if (onPropertyDPEX != null) + { + if (dtSVF == null) + dtSVF = new OWLDataSomeValuesFrom() { DataProperties = new List() }; + dtSVF.DataProperties.Add((OWLDataProperty)onPropertyDPEX); + dtSVF.DataRangeExpression = someValuesFromDREX; + } + } + } + void LoadDataHasValue(OWLOntology ont, RDFResource clsIRI, out OWLDataHasValue dtHV) { - clex = objONEOF; - return; + dtHV = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); + if (onPropertyDPEX == null) + return; + + if (graph[clsIRI, RDFVocabulary.OWL.HAS_VALUE, null, null].FirstOrDefault()?.Object is RDFLiteral hasValueLIT) + dtHV = new OWLDataHasValue((OWLDataProperty)onPropertyDPEX, new OWLLiteral(hasValueLIT)); + } + } + void LoadDataExactCardinality(OWLOntology ont, RDFResource clsIRI, out OWLDataExactCardinality dtEXCR) + { + dtEXCR = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); + if (onPropertyDPEX == null) + return; + + //Cardinality + if (graph[clsIRI, RDFVocabulary.OWL.CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactCardLit + && exactCardLit.HasDecimalDatatype() + && uint.TryParse(exactCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactCardinality)) + { + dtEXCR = new OWLDataExactCardinality((OWLDataProperty)onPropertyDPEX, exactCardinality); + return; + } + + //QualifiedCardinality + if (graph[clsIRI, RDFVocabulary.OWL.QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactQCardLit + && exactQCardLit.HasDecimalDatatype() + && uint.TryParse(exactQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactQCardinality) + && graph[clsIRI, RDFVocabulary.OWL.ON_DATARANGE, null, null].FirstOrDefault()?.Object is RDFResource onDataRange) + { + LoadDataRangeExpression(ont, onDataRange, out OWLDataRangeExpression onDataRangeEX); + if (onDataRangeEX != null) + dtEXCR = new OWLDataExactCardinality((OWLDataProperty)onPropertyDPEX, exactQCardinality, onDataRangeEX); + } + } + } + void LoadDataMinCardinality(OWLOntology ont, RDFResource clsIRI, out OWLDataMinCardinality dtMINCR) + { + dtMINCR = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); + if (onPropertyDPEX == null) + return; + + //Cardinality + if (graph[clsIRI, RDFVocabulary.OWL.MIN_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minCardLit + && minCardLit.HasDecimalDatatype() + && uint.TryParse(minCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minCardinality)) + { + dtMINCR = new OWLDataMinCardinality((OWLDataProperty)onPropertyDPEX, minCardinality); + return; + } + + //QualifiedCardinality + if (graph[clsIRI, RDFVocabulary.OWL.MIN_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minQCardLit + && minQCardLit.HasDecimalDatatype() + && uint.TryParse(minQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minQCardinality) + && graph[clsIRI, RDFVocabulary.OWL.ON_DATARANGE, null, null].FirstOrDefault()?.Object is RDFResource onDataRange) + { + LoadDataRangeExpression(ont, onDataRange, out OWLDataRangeExpression onDataRangeEX); + if (onDataRangeEX != null) + dtMINCR = new OWLDataMinCardinality((OWLDataProperty)onPropertyDPEX, minQCardinality, onDataRangeEX); + } + } + } + void LoadDataMaxCardinality(OWLOntology ont, RDFResource clsIRI, out OWLDataMaxCardinality dtMAXCR) + { + dtMAXCR = null; + + RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; + if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) + { + LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); + if (onPropertyDPEX == null) + return; + + //Cardinality + if (graph[clsIRI, RDFVocabulary.OWL.MAX_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxCardLit + && maxCardLit.HasDecimalDatatype() + && uint.TryParse(maxCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxCardinality)) + { + dtMAXCR = new OWLDataMaxCardinality((OWLDataProperty)onPropertyDPEX, maxCardinality); + return; + } + + //QualifiedCardinality + if (graph[clsIRI, RDFVocabulary.OWL.MAX_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxQCardLit + && maxQCardLit.HasDecimalDatatype() + && uint.TryParse(maxQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxQCardinality) + && graph[clsIRI, RDFVocabulary.OWL.ON_DATARANGE, null, null].FirstOrDefault()?.Object is RDFResource onDataRange) + { + LoadDataRangeExpression(ont, onDataRange, out OWLDataRangeExpression onDataRangeEX); + if (onDataRangeEX != null) + dtMAXCR = new OWLDataMaxCardinality((OWLDataProperty)onPropertyDPEX, maxQCardinality, onDataRangeEX); + } + } + } + void LoadObjectUnionOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectUnionOf objUNOF) + { + objUNOF = null; + + if (graph[clsIRI, RDFVocabulary.OWL.UNION_OF, null, null].FirstOrDefault()?.Object is RDFResource unionOf) + { + List objectUnionOfMembers = new List(); + RDFCollection unionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, unionOf, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource unionOfMember in unionOfMembers.Items.Cast()) + { + LoadClassExpression(ont, unionOfMember, out OWLClassExpression clsExp); + if (clsExp != null) + objectUnionOfMembers.Add(clsExp); + } + objUNOF = new OWLObjectUnionOf(objectUnionOfMembers); + } + } + void LoadObjectIntersectionOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectIntersectionOf objINTOF) + { + objINTOF = null; + + if (graph[clsIRI, RDFVocabulary.OWL.INTERSECTION_OF, null, null].FirstOrDefault()?.Object is RDFResource intersectionOf) + { + List objectIntersectionOfMembers = new List(); + RDFCollection intersectionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, intersectionOf, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource intersectionOfMember in intersectionOfMembers.Items.Cast()) + { + LoadClassExpression(ont, intersectionOfMember, out OWLClassExpression clsExp); + if (clsExp != null) + objectIntersectionOfMembers.Add(clsExp); + } + objINTOF = new OWLObjectIntersectionOf(objectIntersectionOfMembers); + } + } + void LoadObjectComplementOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectComplementOf objCMPOF) + { + objCMPOF = null; + + if (graph[clsIRI, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].FirstOrDefault()?.Object is RDFResource complementOf) + { + LoadClassExpression(ont, complementOf, out OWLClassExpression clsExp); + if (clsExp != null) + objCMPOF = new OWLObjectComplementOf(clsExp); + } + } + void LoadObjectOneOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectOneOf objONEOF) + { + objONEOF = null; + + if (graph[clsIRI, RDFVocabulary.OWL.ONE_OF, null, null].FirstOrDefault()?.Object is RDFResource oneOf) + { + List objectOneOfMembers = new List(); + RDFCollection oneOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, oneOf, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource oneOfMember in oneOfMembers.Items.Cast()) + { + LoadIndividualExpression(ont, oneOfMember, out OWLIndividualExpression idvExp); + if (idvExp != null) + objectOneOfMembers.Add(idvExp); + } + objONEOF = new OWLObjectOneOf(objectOneOfMembers); + } + } + void LoadDataRangeExpression(OWLOntology ont, RDFResource drIRI, out OWLDataRangeExpression drex) + { + drex = null; + RDFGraph drGraph = graph[drIRI, null, null, null]; + + #region Composite + if (drGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DATA_RANGE, null].TriplesCount > 0 + && (drGraph[null, RDFVocabulary.OWL.UNION_OF, null, null].TriplesCount > 0 + || drGraph[null, RDFVocabulary.OWL.INTERSECTION_OF, null, null].TriplesCount > 0 + || drGraph[null, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].TriplesCount > 0)) + { + #region UnionOf + LoadDataUnionOf(ont, drIRI, out OWLDataUnionOf dtUNOF); + if (dtUNOF != null) + { + drex = dtUNOF; + return; + } + #endregion + + #region IntersectionOf + LoadDataIntersectionOf(ont, drIRI, out OWLDataIntersectionOf dtINTOF); + if (dtINTOF != null) + { + drex = dtINTOF; + return; + } + #endregion + + #region ComplementOf + LoadDataComplementOf(ont, drIRI, out OWLDataComplementOf dtCMPOF); + if (dtCMPOF != null) + { + drex = dtCMPOF; + return; + } + #endregion + } + #endregion + + #region Enumerate + if (drGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DATA_RANGE, null].TriplesCount > 0 + && drGraph[null, RDFVocabulary.OWL.ONE_OF, null, null].TriplesCount > 0) + { + #region OneOf + LoadDataOneOf(ont, drIRI, out OWLDataOneOf dtONEOF); + if (dtONEOF != null) + { + drex = dtONEOF; + return; + } + #endregion + } + #endregion + + #region DatatypeRestriction/Datatype + if (drGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.DATATYPE, null].TriplesCount > 0) + { + #region DatatypeRestriction (Faceted Datatype) + if (drGraph[null, RDFVocabulary.OWL.WITH_RESTRICTIONS, null, null].FirstOrDefault()?.Object is RDFResource withRestrictions + && drGraph[null, RDFVocabulary.OWL.ON_DATATYPE, null, null].FirstOrDefault()?.Object is RDFResource onDatatype) + { + LoadDatatypeRestriction(ont, drIRI, onDatatype, withRestrictions, out OWLDatatypeRestriction dtRST); + if (dtRST != null) + { + drex = dtRST; + return; + } + } + #endregion + + #region DatatypeRestriction (Alias Datatype) + else if (drGraph[null, RDFVocabulary.OWL.EQUIVALENT_CLASS, null, null].FirstOrDefault()?.Object is RDFResource equivalentDatatype) + { + drex = new OWLDatatypeRestriction(new OWLDatatype(equivalentDatatype), null); + return; + } + #endregion + + #region Datatype + drex = new OWLDatatype(drIRI); + #endregion + } + #endregion + } + void LoadDataUnionOf(OWLOntology ont, RDFResource dtIRI, out OWLDataUnionOf dtUNOF) + { + dtUNOF = null; + + if (graph[dtIRI, RDFVocabulary.OWL.UNION_OF, null, null].FirstOrDefault()?.Object is RDFResource unionOf) + { + List dtUnionOfMembers = new List(); + RDFCollection unionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, unionOf, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource unionOfMember in unionOfMembers.Items.Cast()) + { + LoadDataRangeExpression(ont, unionOfMember, out OWLDataRangeExpression dtExp); + if (dtExp != null) + dtUnionOfMembers.Add(dtExp); + } + dtUNOF = new OWLDataUnionOf(dtUnionOfMembers); + } + } + void LoadDataIntersectionOf(OWLOntology ont, RDFResource dtIRI, out OWLDataIntersectionOf dtINTOF) + { + dtINTOF = null; + + if (graph[dtIRI, RDFVocabulary.OWL.INTERSECTION_OF, null, null].FirstOrDefault()?.Object is RDFResource intersectionOf) + { + List dtIntersectionOfMembers = new List(); + RDFCollection intersectionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, intersectionOf, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource intersectionOfMember in intersectionOfMembers.Items.Cast()) + { + LoadDataRangeExpression(ont, intersectionOfMember, out OWLDataRangeExpression dtExp); + if (dtExp != null) + dtIntersectionOfMembers.Add(dtExp); + } + dtINTOF = new OWLDataIntersectionOf(dtIntersectionOfMembers); + } + } + void LoadDataComplementOf(OWLOntology ont, RDFResource dtIRI, out OWLDataComplementOf dtCMPOF) + { + dtCMPOF = null; + + if (graph[dtIRI, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].FirstOrDefault()?.Object is RDFResource complementOf) + { + LoadDataRangeExpression(ont, complementOf, out OWLDataRangeExpression dtExp); + if (dtExp != null) + dtCMPOF = new OWLDataComplementOf(dtExp); + } + } + void LoadDataOneOf(OWLOntology ont, RDFResource drIRI, out OWLDataOneOf dtONEOF) + { + dtONEOF = null; + + if (graph[drIRI, RDFVocabulary.OWL.ONE_OF, null, null].FirstOrDefault()?.Object is RDFResource oneOf) + { + List dataOneOfMembers = new List(); + RDFCollection oneOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, oneOf, RDFModelEnums.RDFTripleFlavors.SPL); + foreach (RDFLiteral oneOfMember in oneOfMembers.Items.Cast()) + dataOneOfMembers.Add(new OWLLiteral(oneOfMember)); + dtONEOF = new OWLDataOneOf(dataOneOfMembers); + } + } + void LoadDatatypeRestriction(OWLOntology ont, RDFResource drIRI, RDFResource onDatatype, RDFResource withRestrictions, out OWLDatatypeRestriction dtRST) + { + dtRST = null; + + List facetRestrictions = new List(); + RDFCollection facetRestrictionMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, withRestrictions, RDFModelEnums.RDFTripleFlavors.SPO); + foreach (RDFResource facetRestrictionMember in facetRestrictionMembers.Items.Cast()) + { + if (graph[facetRestrictionMember, RDFVocabulary.XSD.LENGTH, null, null].FirstOrDefault()?.Object is RDFLiteral fctLengthDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctLengthDT), RDFVocabulary.XSD.LENGTH)); + else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MIN_LENGTH, null, null].FirstOrDefault()?.Object is RDFLiteral fctMinLengthDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMinLengthDT), RDFVocabulary.XSD.MIN_LENGTH)); + else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MAX_LENGTH, null, null].FirstOrDefault()?.Object is RDFLiteral fctMaxLengthDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMaxLengthDT), RDFVocabulary.XSD.MAX_LENGTH)); + else if (graph[facetRestrictionMember, RDFVocabulary.XSD.PATTERN, null, null].FirstOrDefault()?.Object is RDFLiteral fctPatternDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctPatternDT), RDFVocabulary.XSD.PATTERN)); + else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MAX_INCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMaxInclusiveDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMaxInclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); + else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MAX_EXCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMaxExclusiveDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMaxExclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); + else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MIN_INCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMinInclusiveDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMinInclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); + else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MIN_EXCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMinExclusiveDT) + facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMinExclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); + } + + dtRST = new OWLDatatypeRestriction(new OWLDatatype(onDatatype), facetRestrictions); } #endregion - } - #endregion - - #region Class - if (clsGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.CLASS, null].TriplesCount > 0) - clex = new OWLClass(clsIRI); - #endregion - } - void LoadObjectAllValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource allValuesFrom, out OWLObjectAllValuesFrom objAVF) - { - objAVF = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); - if (onPropertyOPEX == null) - return; - - LoadClassExpression(ont, allValuesFrom, out OWLClassExpression allValuesFromCLEX); - if (allValuesFromCLEX != null) - objAVF = new OWLObjectAllValuesFrom(onPropertyOPEX, allValuesFromCLEX); - } - } - void LoadObjectSomeValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource someValuesFrom, out OWLObjectSomeValuesFrom objSVF) - { - objSVF = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); - if (onPropertyOPEX == null) - return; - - LoadClassExpression(ont, someValuesFrom, out OWLClassExpression someValuesFromCLEX); - if (someValuesFromCLEX != null) - objSVF = new OWLObjectSomeValuesFrom(onPropertyOPEX, someValuesFromCLEX); - } - } - void LoadObjectHasSelf(OWLOntology ont, RDFResource clsIRI, out OWLObjectHasSelf objHS) - { - objHS = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); - if (onPropertyOPEX != null) - objHS = new OWLObjectHasSelf(onPropertyOPEX); - } - } - void LoadObjectHasValue(OWLOntology ont, RDFResource clsIRI, out OWLObjectHasValue objHV) - { - objHV = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); - if (onPropertyOPEX == null) - return; - - if (graph[clsIRI, RDFVocabulary.OWL.HAS_VALUE, null, null].FirstOrDefault()?.Object is RDFResource hasValue) - { - LoadIndividualExpression(ont, hasValue, out OWLIndividualExpression hasValueIDVEX); - if (hasValueIDVEX != null) - objHV = new OWLObjectHasValue(onPropertyOPEX, hasValueIDVEX); - } - } - } - void LoadObjectExactCardinality(OWLOntology ont, RDFResource clsIRI, out OWLObjectExactCardinality objEXCR) - { - objEXCR = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); - if (onPropertyOPEX == null) - return; - - //Cardinality - if (graph[clsIRI, RDFVocabulary.OWL.CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactCardLit - && exactCardLit.HasDecimalDatatype() - && uint.TryParse(exactCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactCardinality)) - { - objEXCR = new OWLObjectExactCardinality(onPropertyOPEX, exactCardinality); - return; - } - - //QualifiedCardinality - if (graph[clsIRI, RDFVocabulary.OWL.QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactQCardLit - && exactQCardLit.HasDecimalDatatype() - && uint.TryParse(exactQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactQCardinality) - && graph[clsIRI, RDFVocabulary.OWL.ON_CLASS, null, null].FirstOrDefault()?.Object is RDFResource onClass) - { - LoadClassExpression(ont, onClass, out OWLClassExpression onClassEX); - if (onClassEX != null) - objEXCR = new OWLObjectExactCardinality(onPropertyOPEX, exactQCardinality, onClassEX); - } - } - } - void LoadObjectMinCardinality(OWLOntology ont, RDFResource clsIRI, out OWLObjectMinCardinality objMINCR) - { - objMINCR = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); - if (onPropertyOPEX == null) - return; - - //Cardinality - if (graph[clsIRI, RDFVocabulary.OWL.MIN_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minCardLit - && minCardLit.HasDecimalDatatype() - && uint.TryParse(minCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minCardinality)) - { - objMINCR = new OWLObjectMinCardinality(onPropertyOPEX, minCardinality); - return; - } - - //QualifiedCardinality - if (graph[clsIRI, RDFVocabulary.OWL.MIN_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minQCardLit - && minQCardLit.HasDecimalDatatype() - && uint.TryParse(minQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minQCardinality) - && graph[clsIRI, RDFVocabulary.OWL.ON_CLASS, null, null].FirstOrDefault()?.Object is RDFResource onClass) - { - LoadClassExpression(ont, onClass, out OWLClassExpression onClassEX); - if (onClassEX != null) - objMINCR = new OWLObjectMinCardinality(onPropertyOPEX, minQCardinality, onClassEX); - } - } - } - void LoadObjectMaxCardinality(OWLOntology ont, RDFResource clsIRI, out OWLObjectMaxCardinality objMAXCR) - { - objMAXCR = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadObjectPropertyExpression(ont, onProperty, out OWLObjectPropertyExpression onPropertyOPEX); - if (onPropertyOPEX == null) - return; - - //Cardinality - if (graph[clsIRI, RDFVocabulary.OWL.MAX_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxCardLit - && maxCardLit.HasDecimalDatatype() - && uint.TryParse(maxCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxCardinality)) - { - objMAXCR = new OWLObjectMaxCardinality(onPropertyOPEX, maxCardinality); - return; - } - - //QualifiedCardinality - if (graph[clsIRI, RDFVocabulary.OWL.MAX_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxQCardLit - && maxQCardLit.HasDecimalDatatype() - && uint.TryParse(maxQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxQCardinality) - && graph[clsIRI, RDFVocabulary.OWL.ON_CLASS, null, null].FirstOrDefault()?.Object is RDFResource onClass) - { - LoadClassExpression(ont, onClass, out OWLClassExpression onClassEX); - if (onClassEX != null) - objMAXCR = new OWLObjectMaxCardinality(onPropertyOPEX, maxQCardinality, onClassEX); - } - } - } - void LoadDataAllValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource allValuesFrom, out OWLDataAllValuesFrom dtAVF) - { - dtAVF = null; - - LoadDataRangeExpression(ont, allValuesFrom, out OWLDataRangeExpression allValuesFromDREX); - if (allValuesFromDREX == null) - return; - - foreach (RDFResource onProperty in graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null] - .Select(t => t.Object) - .OfType()) - { - LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); - if (onPropertyDPEX != null) - { - if (dtAVF == null) - dtAVF = new OWLDataAllValuesFrom() { DataProperties = new List() }; - dtAVF.DataProperties.Add((OWLDataProperty)onPropertyDPEX); - dtAVF.DataRangeExpression = allValuesFromDREX; - } - } - } - void LoadDataSomeValuesFrom(OWLOntology ont, RDFResource clsIRI, RDFResource someValuesFrom, out OWLDataSomeValuesFrom dtSVF) - { - dtSVF = null; - - LoadDataRangeExpression(ont, someValuesFrom, out OWLDataRangeExpression someValuesFromDREX); - if (someValuesFromDREX == null) - return; - - foreach (RDFResource onProperty in graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null] - .Select(t => t.Object) - .OfType()) - { - LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); - if (onPropertyDPEX != null) - { - if (dtSVF == null) - dtSVF = new OWLDataSomeValuesFrom() { DataProperties = new List() }; - dtSVF.DataProperties.Add((OWLDataProperty)onPropertyDPEX); - dtSVF.DataRangeExpression = someValuesFromDREX; - } - } - } - void LoadDataHasValue(OWLOntology ont, RDFResource clsIRI, out OWLDataHasValue dtHV) - { - dtHV = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); - if (onPropertyDPEX == null) - return; - - if (graph[clsIRI, RDFVocabulary.OWL.HAS_VALUE, null, null].FirstOrDefault()?.Object is RDFLiteral hasValueLIT) - dtHV = new OWLDataHasValue((OWLDataProperty)onPropertyDPEX, new OWLLiteral(hasValueLIT)); - } - } - void LoadDataExactCardinality(OWLOntology ont, RDFResource clsIRI, out OWLDataExactCardinality dtEXCR) - { - dtEXCR = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); - if (onPropertyDPEX == null) - return; - - //Cardinality - if (graph[clsIRI, RDFVocabulary.OWL.CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactCardLit - && exactCardLit.HasDecimalDatatype() - && uint.TryParse(exactCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactCardinality)) - { - dtEXCR = new OWLDataExactCardinality((OWLDataProperty)onPropertyDPEX, exactCardinality); - return; - } - - //QualifiedCardinality - if (graph[clsIRI, RDFVocabulary.OWL.QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral exactQCardLit - && exactQCardLit.HasDecimalDatatype() - && uint.TryParse(exactQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint exactQCardinality) - && graph[clsIRI, RDFVocabulary.OWL.ON_DATARANGE, null, null].FirstOrDefault()?.Object is RDFResource onDataRange) - { - LoadDataRangeExpression(ont, onDataRange, out OWLDataRangeExpression onDataRangeEX); - if (onDataRangeEX != null) - dtEXCR = new OWLDataExactCardinality((OWLDataProperty)onPropertyDPEX, exactQCardinality, onDataRangeEX); - } - } - } - void LoadDataMinCardinality(OWLOntology ont, RDFResource clsIRI, out OWLDataMinCardinality dtMINCR) - { - dtMINCR = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); - if (onPropertyDPEX == null) - return; - - //Cardinality - if (graph[clsIRI, RDFVocabulary.OWL.MIN_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minCardLit - && minCardLit.HasDecimalDatatype() - && uint.TryParse(minCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minCardinality)) - { - dtMINCR = new OWLDataMinCardinality((OWLDataProperty)onPropertyDPEX, minCardinality); - return; - } - - //QualifiedCardinality - if (graph[clsIRI, RDFVocabulary.OWL.MIN_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral minQCardLit - && minQCardLit.HasDecimalDatatype() - && uint.TryParse(minQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint minQCardinality) - && graph[clsIRI, RDFVocabulary.OWL.ON_DATARANGE, null, null].FirstOrDefault()?.Object is RDFResource onDataRange) - { - LoadDataRangeExpression(ont, onDataRange, out OWLDataRangeExpression onDataRangeEX); - if (onDataRangeEX != null) - dtMINCR = new OWLDataMinCardinality((OWLDataProperty)onPropertyDPEX, minQCardinality, onDataRangeEX); - } - } - } - void LoadDataMaxCardinality(OWLOntology ont, RDFResource clsIRI, out OWLDataMaxCardinality dtMAXCR) - { - dtMAXCR = null; - - RDFGraph onPropertyGraph = graph[clsIRI, RDFVocabulary.OWL.ON_PROPERTY, null, null]; - if (onPropertyGraph.TriplesCount == 1 && onPropertyGraph.Single().Object is RDFResource onProperty) - { - LoadDataPropertyExpression(ont, onProperty, out OWLDataPropertyExpression onPropertyDPEX); - if (onPropertyDPEX == null) - return; - - //Cardinality - if (graph[clsIRI, RDFVocabulary.OWL.MAX_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxCardLit - && maxCardLit.HasDecimalDatatype() - && uint.TryParse(maxCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxCardinality)) - { - dtMAXCR = new OWLDataMaxCardinality((OWLDataProperty)onPropertyDPEX, maxCardinality); - return; - } - - //QualifiedCardinality - if (graph[clsIRI, RDFVocabulary.OWL.MAX_QUALIFIED_CARDINALITY, null, null].FirstOrDefault()?.Object is RDFTypedLiteral maxQCardLit - && maxQCardLit.HasDecimalDatatype() - && uint.TryParse(maxQCardLit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint maxQCardinality) - && graph[clsIRI, RDFVocabulary.OWL.ON_DATARANGE, null, null].FirstOrDefault()?.Object is RDFResource onDataRange) - { - LoadDataRangeExpression(ont, onDataRange, out OWLDataRangeExpression onDataRangeEX); - if (onDataRangeEX != null) - dtMAXCR = new OWLDataMaxCardinality((OWLDataProperty)onPropertyDPEX, maxQCardinality, onDataRangeEX); - } - } - } - void LoadObjectUnionOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectUnionOf objUNOF) - { - objUNOF = null; - - if (graph[clsIRI, RDFVocabulary.OWL.UNION_OF, null, null].FirstOrDefault()?.Object is RDFResource unionOf) - { - List objectUnionOfMembers = new List(); - RDFCollection unionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, unionOf, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource unionOfMember in unionOfMembers.Items.Cast()) - { - LoadClassExpression(ont, unionOfMember, out OWLClassExpression clsExp); - if (clsExp != null) - objectUnionOfMembers.Add(clsExp); - } - objUNOF = new OWLObjectUnionOf(objectUnionOfMembers); - } - } - void LoadObjectIntersectionOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectIntersectionOf objINTOF) - { - objINTOF = null; - - if (graph[clsIRI, RDFVocabulary.OWL.INTERSECTION_OF, null, null].FirstOrDefault()?.Object is RDFResource intersectionOf) - { - List objectIntersectionOfMembers = new List(); - RDFCollection intersectionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, intersectionOf, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource intersectionOfMember in intersectionOfMembers.Items.Cast()) - { - LoadClassExpression(ont, intersectionOfMember, out OWLClassExpression clsExp); - if (clsExp != null) - objectIntersectionOfMembers.Add(clsExp); - } - objINTOF = new OWLObjectIntersectionOf(objectIntersectionOfMembers); - } - } - void LoadObjectComplementOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectComplementOf objCMPOF) - { - objCMPOF = null; - - if (graph[clsIRI, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].FirstOrDefault()?.Object is RDFResource complementOf) - { - LoadClassExpression(ont, complementOf, out OWLClassExpression clsExp); - if (clsExp != null) - objCMPOF = new OWLObjectComplementOf(clsExp); - } - } - void LoadObjectOneOf(OWLOntology ont, RDFResource clsIRI, out OWLObjectOneOf objONEOF) - { - objONEOF = null; - - if (graph[clsIRI, RDFVocabulary.OWL.ONE_OF, null, null].FirstOrDefault()?.Object is RDFResource oneOf) - { - List objectOneOfMembers = new List(); - RDFCollection oneOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, oneOf, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource oneOfMember in oneOfMembers.Items.Cast()) - { - LoadIndividualExpression(ont, oneOfMember, out OWLIndividualExpression idvExp); - if (idvExp != null) - objectOneOfMembers.Add(idvExp); - } - objONEOF = new OWLObjectOneOf(objectOneOfMembers); - } - } - void LoadDataRangeExpression(OWLOntology ont, RDFResource drIRI, out OWLDataRangeExpression drex) - { - drex = null; - RDFGraph drGraph = graph[drIRI, null, null, null]; - - #region Composite - if (drGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DATA_RANGE, null].TriplesCount > 0 - && (drGraph[null, RDFVocabulary.OWL.UNION_OF, null, null].TriplesCount > 0 - || drGraph[null, RDFVocabulary.OWL.INTERSECTION_OF, null, null].TriplesCount > 0 - || drGraph[null, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].TriplesCount > 0)) - { - #region UnionOf - LoadDataUnionOf(ont, drIRI, out OWLDataUnionOf dtUNOF); - if (dtUNOF != null) - { - drex = dtUNOF; - return; - } - #endregion - - #region IntersectionOf - LoadDataIntersectionOf(ont, drIRI, out OWLDataIntersectionOf dtINTOF); - if (dtINTOF != null) - { - drex = dtINTOF; - return; - } - #endregion - - #region ComplementOf - LoadDataComplementOf(ont, drIRI, out OWLDataComplementOf dtCMPOF); - if (dtCMPOF != null) - { - drex = dtCMPOF; - return; - } - #endregion - } - #endregion - - #region Enumerate - if (drGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DATA_RANGE, null].TriplesCount > 0 - && drGraph[null, RDFVocabulary.OWL.ONE_OF, null, null].TriplesCount > 0) - { - #region OneOf - LoadDataOneOf(ont, drIRI, out OWLDataOneOf dtONEOF); - if (dtONEOF != null) - { - drex = dtONEOF; - return; - } - #endregion - } - #endregion - - #region DatatypeRestriction/Datatype - if (drGraph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.DATATYPE, null].TriplesCount > 0) - { - #region DatatypeRestriction (Faceted Datatype) - if (drGraph[null, RDFVocabulary.OWL.WITH_RESTRICTIONS, null, null].FirstOrDefault()?.Object is RDFResource withRestrictions - && drGraph[null, RDFVocabulary.OWL.ON_DATATYPE, null, null].FirstOrDefault()?.Object is RDFResource onDatatype) - { - LoadDatatypeRestriction(ont, drIRI, onDatatype, withRestrictions, out OWLDatatypeRestriction dtRST); - if (dtRST != null) - { - drex = dtRST; - return; - } - } - #endregion - - #region DatatypeRestriction (Alias Datatype) - else if (drGraph[null, RDFVocabulary.OWL.EQUIVALENT_CLASS, null, null].FirstOrDefault()?.Object is RDFResource equivalentDatatype) - { - drex = new OWLDatatypeRestriction(new OWLDatatype(equivalentDatatype), null); - return; - } - #endregion - - #region Datatype - drex = new OWLDatatype(drIRI); - #endregion - } - #endregion - } - void LoadDataUnionOf(OWLOntology ont, RDFResource dtIRI, out OWLDataUnionOf dtUNOF) - { - dtUNOF = null; - - if (graph[dtIRI, RDFVocabulary.OWL.UNION_OF, null, null].FirstOrDefault()?.Object is RDFResource unionOf) - { - List dtUnionOfMembers = new List(); - RDFCollection unionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, unionOf, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource unionOfMember in unionOfMembers.Items.Cast()) - { - LoadDataRangeExpression(ont, unionOfMember, out OWLDataRangeExpression dtExp); - if (dtExp != null) - dtUnionOfMembers.Add(dtExp); - } - dtUNOF = new OWLDataUnionOf(dtUnionOfMembers); - } - } - void LoadDataIntersectionOf(OWLOntology ont, RDFResource dtIRI, out OWLDataIntersectionOf dtINTOF) - { - dtINTOF = null; - - if (graph[dtIRI, RDFVocabulary.OWL.INTERSECTION_OF, null, null].FirstOrDefault()?.Object is RDFResource intersectionOf) - { - List dtIntersectionOfMembers = new List(); - RDFCollection intersectionOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, intersectionOf, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource intersectionOfMember in intersectionOfMembers.Items.Cast()) - { - LoadDataRangeExpression(ont, intersectionOfMember, out OWLDataRangeExpression dtExp); - if (dtExp != null) - dtIntersectionOfMembers.Add(dtExp); - } - dtINTOF = new OWLDataIntersectionOf(dtIntersectionOfMembers); - } - } - void LoadDataComplementOf(OWLOntology ont, RDFResource dtIRI, out OWLDataComplementOf dtCMPOF) - { - dtCMPOF = null; - - if (graph[dtIRI, RDFVocabulary.OWL.COMPLEMENT_OF, null, null].FirstOrDefault()?.Object is RDFResource complementOf) - { - LoadDataRangeExpression(ont, complementOf, out OWLDataRangeExpression dtExp); - if (dtExp != null) - dtCMPOF = new OWLDataComplementOf(dtExp); - } - } - void LoadDataOneOf(OWLOntology ont, RDFResource drIRI, out OWLDataOneOf dtONEOF) - { - dtONEOF = null; - - if (graph[drIRI, RDFVocabulary.OWL.ONE_OF, null, null].FirstOrDefault()?.Object is RDFResource oneOf) - { - List dataOneOfMembers = new List(); - RDFCollection oneOfMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, oneOf, RDFModelEnums.RDFTripleFlavors.SPL); - foreach (RDFLiteral oneOfMember in oneOfMembers.Items.Cast()) - dataOneOfMembers.Add(new OWLLiteral(oneOfMember)); - dtONEOF = new OWLDataOneOf(dataOneOfMembers); - } - } - void LoadDatatypeRestriction(OWLOntology ont, RDFResource drIRI, RDFResource onDatatype, RDFResource withRestrictions, out OWLDatatypeRestriction dtRST) - { - dtRST = null; - - List facetRestrictions = new List(); - RDFCollection facetRestrictionMembers = RDFModelUtilities.DeserializeCollectionFromGraph(graph, withRestrictions, RDFModelEnums.RDFTripleFlavors.SPO); - foreach (RDFResource facetRestrictionMember in facetRestrictionMembers.Items.Cast()) - { - if (graph[facetRestrictionMember, RDFVocabulary.XSD.LENGTH, null, null].FirstOrDefault()?.Object is RDFLiteral fctLengthDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctLengthDT), RDFVocabulary.XSD.LENGTH)); - else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MIN_LENGTH, null, null].FirstOrDefault()?.Object is RDFLiteral fctMinLengthDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMinLengthDT), RDFVocabulary.XSD.MIN_LENGTH)); - else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MAX_LENGTH, null, null].FirstOrDefault()?.Object is RDFLiteral fctMaxLengthDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMaxLengthDT), RDFVocabulary.XSD.MAX_LENGTH)); - else if (graph[facetRestrictionMember, RDFVocabulary.XSD.PATTERN, null, null].FirstOrDefault()?.Object is RDFLiteral fctPatternDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctPatternDT), RDFVocabulary.XSD.PATTERN)); - else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MAX_INCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMaxInclusiveDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMaxInclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); - else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MAX_EXCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMaxExclusiveDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMaxExclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); - else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MIN_INCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMinInclusiveDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMinInclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); - else if (graph[facetRestrictionMember, RDFVocabulary.XSD.MIN_EXCLUSIVE, null, null].FirstOrDefault()?.Object is RDFLiteral fctMinExclusiveDT) - facetRestrictions.Add(new OWLFacetRestriction(new OWLLiteral(fctMinExclusiveDT), RDFVocabulary.XSD.MAX_INCLUSIVE)); - } - - dtRST = new OWLDatatypeRestriction(new OWLDatatype(onDatatype), facetRestrictions); - } - #endregion - - //Ontology - LoadOntology(out OWLOntology ontology); - LoadImports(ontology); - LoadDeclarations(ontology); - PrefetchAnnotationAxioms(ontology, out RDFGraph annotationAxiomsGraph); - LoadOntologyAnnotations(ontology, annotationAxiomsGraph); - //Axioms - LoadFunctionalObjectProperties(ontology, annotationAxiomsGraph); - LoadInverseFunctionalObjectProperties(ontology, annotationAxiomsGraph); - LoadSymmetricObjectProperties(ontology, annotationAxiomsGraph); - LoadAsymmetricObjectProperties(ontology, annotationAxiomsGraph); - LoadReflexiveObjectProperties(ontology, annotationAxiomsGraph); - LoadIrreflexiveObjectProperties(ontology, annotationAxiomsGraph); - LoadTransitiveObjectProperties(ontology, annotationAxiomsGraph); - LoadInverseObjectProperties(ontology, annotationAxiomsGraph); - LoadEquivalentObjectProperties(ontology, annotationAxiomsGraph); - LoadDisjointObjectProperties(ontology, annotationAxiomsGraph); - LoadSubObjectProperties(ontology, annotationAxiomsGraph); - LoadObjectPropertyDomain(ontology, annotationAxiomsGraph); - LoadObjectPropertyRange(ontology, annotationAxiomsGraph); - LoadFunctionalDataProperties(ontology, annotationAxiomsGraph); - LoadEquivalentDataProperties(ontology, annotationAxiomsGraph); - LoadDisjointDataProperties(ontology, annotationAxiomsGraph); - LoadSubDataProperties(ontology, annotationAxiomsGraph); - LoadDataPropertyDomain(ontology, annotationAxiomsGraph); - LoadDataPropertyRange(ontology, annotationAxiomsGraph); - LoadSubClassOf(ontology, annotationAxiomsGraph); - LoadEquivalentClasses(ontology, annotationAxiomsGraph); - LoadDisjointClasses(ontology, annotationAxiomsGraph); - LoadDisjointUnion(ontology, annotationAxiomsGraph); - LoadHasKey(ontology, annotationAxiomsGraph); - LoadDatatypeDefinition(ontology, annotationAxiomsGraph); - LoadSameIndividual(ontology, annotationAxiomsGraph); - LoadDifferentIndividuals(ontology, annotationAxiomsGraph); - LoadObjectPropertyAssertions(ontology, annotationAxiomsGraph); - LoadNegativeObjectPropertyAssertions(ontology, annotationAxiomsGraph); - LoadDataPropertyAssertions(ontology, annotationAxiomsGraph); - LoadNegativeDataPropertyAssertions(ontology, annotationAxiomsGraph); - LoadClassAssertions(ontology, annotationAxiomsGraph); - LoadAnnotationAssertions(ontology, annotationAxiomsGraph); - LoadSubAnnotationProperties(ontology, annotationAxiomsGraph); - LoadAnnotationPropertyDomain(ontology, annotationAxiomsGraph); - LoadAnnotationPropertyRange(ontology, annotationAxiomsGraph); - - return ontology; - } - public static OWLOntology FromFile(OWLEnums.OWLFormats owlFormat, string inputFile) - { - #region Guards - if (string.IsNullOrWhiteSpace(inputFile)) - throw new OWLException("Cannot read ontology from file because given \"inputFile\" parameter is null or empty"); + //Ontology + LoadOntology(out OWLOntology ontology); + LoadImports(ontology); + LoadDeclarations(ontology); + PrefetchAnnotationAxioms(ontology, out RDFGraph annotationAxiomsGraph); + LoadOntologyAnnotations(ontology, annotationAxiomsGraph); + //Axioms + LoadFunctionalObjectProperties(ontology, annotationAxiomsGraph); + LoadInverseFunctionalObjectProperties(ontology, annotationAxiomsGraph); + LoadSymmetricObjectProperties(ontology, annotationAxiomsGraph); + LoadAsymmetricObjectProperties(ontology, annotationAxiomsGraph); + LoadReflexiveObjectProperties(ontology, annotationAxiomsGraph); + LoadIrreflexiveObjectProperties(ontology, annotationAxiomsGraph); + LoadTransitiveObjectProperties(ontology, annotationAxiomsGraph); + LoadInverseObjectProperties(ontology, annotationAxiomsGraph); + LoadEquivalentObjectProperties(ontology, annotationAxiomsGraph); + LoadDisjointObjectProperties(ontology, annotationAxiomsGraph); + LoadSubObjectProperties(ontology, annotationAxiomsGraph); + LoadObjectPropertyDomain(ontology, annotationAxiomsGraph); + LoadObjectPropertyRange(ontology, annotationAxiomsGraph); + LoadFunctionalDataProperties(ontology, annotationAxiomsGraph); + LoadEquivalentDataProperties(ontology, annotationAxiomsGraph); + LoadDisjointDataProperties(ontology, annotationAxiomsGraph); + LoadSubDataProperties(ontology, annotationAxiomsGraph); + LoadDataPropertyDomain(ontology, annotationAxiomsGraph); + LoadDataPropertyRange(ontology, annotationAxiomsGraph); + LoadSubClassOf(ontology, annotationAxiomsGraph); + LoadEquivalentClasses(ontology, annotationAxiomsGraph); + LoadDisjointClasses(ontology, annotationAxiomsGraph); + LoadDisjointUnion(ontology, annotationAxiomsGraph); + LoadHasKey(ontology, annotationAxiomsGraph); + LoadDatatypeDefinition(ontology, annotationAxiomsGraph); + LoadSameIndividual(ontology, annotationAxiomsGraph); + LoadDifferentIndividuals(ontology, annotationAxiomsGraph); + LoadObjectPropertyAssertions(ontology, annotationAxiomsGraph); + LoadNegativeObjectPropertyAssertions(ontology, annotationAxiomsGraph); + LoadDataPropertyAssertions(ontology, annotationAxiomsGraph); + LoadNegativeDataPropertyAssertions(ontology, annotationAxiomsGraph); + LoadClassAssertions(ontology, annotationAxiomsGraph); + LoadAnnotationAssertions(ontology, annotationAxiomsGraph); + LoadSubAnnotationProperties(ontology, annotationAxiomsGraph); + LoadAnnotationPropertyDomain(ontology, annotationAxiomsGraph); + LoadAnnotationPropertyRange(ontology, annotationAxiomsGraph); + + return ontology; + }); + + public static Task FromFileAsync(OWLEnums.OWLFormats owlFormat, string inputFile) + { + if (string.IsNullOrWhiteSpace(inputFile)) + throw new OWLException("Cannot read ontology from file because given \"inputFile\" parameter is null or empty"); if (!File.Exists(inputFile)) - throw new OWLException("Cannot read ontology from file because given \"inputFile\" parameter (" + inputFile + ") does not indicate an existing file"); - #endregion + throw new OWLException("Cannot read ontology from file because given \"inputFile\" parameter (" + inputFile + ") does not indicate an existing file"); - return FromStream(owlFormat, new FileStream(inputFile, FileMode.Open)); - } + return FromStreamAsync(owlFormat, new FileStream(inputFile, FileMode.Open)); + } - public static OWLOntology FromStream(OWLEnums.OWLFormats owlFormat, Stream inputStream) - { - #region Guards - if (inputStream == null) - throw new OWLException("Cannot read ontology from stream because given \"inputStream\" parameter is null"); - #endregion - - try - { - switch (owlFormat) + public static Task FromStreamAsync(OWLEnums.OWLFormats owlFormat, Stream inputStream) + => Task.Run(() => { - case OWLEnums.OWLFormats.OWL2XML: - default: - using (StreamReader streamReader = new StreamReader(inputStream, RDFModelUtilities.UTF8_NoBOM)) - return OWLSerializer.DeserializeOntology(streamReader.ReadToEnd()); - } - } - catch (Exception ex) - { - throw new OWLException($"Cannot read ontology from stream because: {ex.Message}", ex); - } - } + if (inputStream == null) + throw new OWLException("Cannot read ontology from stream because given \"inputStream\" parameter is null"); - public async Task ImportAsync(Uri importUri, int timeoutMilliseconds=20000) - { - #region Guards - if (importUri == null) - throw new OWLException("Cannot import ontology because given \"importUri\" parameter is null"); - #endregion + try + { + switch (owlFormat) + { + case OWLEnums.OWLFormats.OWL2XML: + default: + using (StreamReader streamReader = new StreamReader(inputStream, RDFModelUtilities.UTF8_NoBOM)) + return OWLSerializer.DeserializeOntology(streamReader.ReadToEnd()); + } + } + catch (Exception ex) + { + throw new OWLException($"Cannot read ontology from stream because: {ex.Message}", ex); + } + }); - try - { - RDFAsyncGraph importedGraph = await RDFAsyncGraph.FromUriAsync(importUri, timeoutMilliseconds); - OWLOntology importedOntology = await Task.Run(() => FromRDFGraph(importedGraph.WrappedGraph)); - - Annotations.Add(new OWLAnnotation(new OWLAnnotationProperty(RDFVocabulary.OWL.IMPORTS), new RDFResource(importedOntology.IRI))); - importedOntology.AnnotationAxioms.ForEach(ax => { ax.IsImport = true; AnnotationAxioms.Add(ax); }); - importedOntology.AssertionAxioms.ForEach(ax => { ax.IsImport = true; AssertionAxioms.Add(ax); }); - importedOntology.ClassAxioms.ForEach(ax => { ax.IsImport = true; ClassAxioms.Add(ax); }); - importedOntology.DataPropertyAxioms.ForEach(ax => { ax.IsImport = true; DataPropertyAxioms.Add(ax); }); - importedOntology.DatatypeDefinitionAxioms.ForEach(ax => { ax.IsImport = true; DatatypeDefinitionAxioms.Add(ax); }); - importedOntology.DeclarationAxioms.ForEach(ax => { ax.IsImport = true; DeclarationAxioms.Add(ax); }); - importedOntology.KeyAxioms.ForEach(ax => { ax.IsImport = true; KeyAxioms.Add(ax); }); - importedOntology.ObjectPropertyAxioms.ForEach(ax => { ax.IsImport = true; ObjectPropertyAxioms.Add(ax); }); - importedOntology.Prefixes.ForEach(pfx => { - if (!Prefixes.Any(PFX => string.Equals(PFX.Name, pfx.Name, StringComparison.OrdinalIgnoreCase))) - Prefixes.Add(pfx); }); - } - catch (Exception ex) - { - throw new OWLException($"Cannot import ontology because: {ex.Message}", ex); - } - } + public Task ImportAsync(Uri ontologyIRI, int timeoutMilliseconds=20000) + => Task.Run(async() => + { + if (ontologyIRI == null) + throw new OWLException("Cannot import ontology because given \"ontologyIRI\" parameter is null"); + + try + { + RDFAsyncGraph importedGraph = await RDFAsyncGraph.FromUriAsync(ontologyIRI, timeoutMilliseconds); + OWLOntology importedOntology = await FromRDFGraphAsync(importedGraph.WrappedGraph); + + Annotations.Add(new OWLAnnotation(new OWLAnnotationProperty(RDFVocabulary.OWL.IMPORTS), new RDFResource(importedOntology.IRI))); + importedOntology.AnnotationAxioms.ForEach(ax => { ax.IsImport = true; AnnotationAxioms.Add(ax); }); + importedOntology.AssertionAxioms.ForEach(ax => { ax.IsImport = true; AssertionAxioms.Add(ax); }); + importedOntology.ClassAxioms.ForEach(ax => { ax.IsImport = true; ClassAxioms.Add(ax); }); + importedOntology.DataPropertyAxioms.ForEach(ax => { ax.IsImport = true; DataPropertyAxioms.Add(ax); }); + importedOntology.DatatypeDefinitionAxioms.ForEach(ax => { ax.IsImport = true; DatatypeDefinitionAxioms.Add(ax); }); + importedOntology.DeclarationAxioms.ForEach(ax => { ax.IsImport = true; DeclarationAxioms.Add(ax); }); + importedOntology.KeyAxioms.ForEach(ax => { ax.IsImport = true; KeyAxioms.Add(ax); }); + importedOntology.ObjectPropertyAxioms.ForEach(ax => { ax.IsImport = true; ObjectPropertyAxioms.Add(ax); }); + importedOntology.Prefixes.ForEach(pfx => + { + if (!Prefixes.Any(PFX => string.Equals(PFX.Name, pfx.Name, StringComparison.OrdinalIgnoreCase))) + Prefixes.Add(pfx); + }); + } + catch (Exception ex) + { + throw new OWLException($"Cannot import ontology from IRI {ontologyIRI} because: {ex.Message}", ex); + } + }); #endregion } } \ No newline at end of file