Skip to content

Commit

Permalink
Deep refactoring of OWLReasoner internals
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco De Salvo committed Dec 13, 2023
1 parent 3aae12f commit 8241ab3
Show file tree
Hide file tree
Showing 33 changed files with 313 additions and 214 deletions.
111 changes: 111 additions & 0 deletions OWLSharp.Test/Extensions/SWRL/Reasoner/SWRLReasonerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2014-2024 Marco De Salvo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OWLSharp.Extensions.TIME;
using RDFSharp.Model;
using RDFSharp.Query;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OWLSharp.Extensions.SWRL.Test
{
[TestClass]
public class SWRLReasonerTest
{
#region Tests
[TestMethod]
public void ShouldAddSWRLRule()
{
OWLReasoner reasoner = new OWLReasoner();
reasoner.AddSWRLRule(new SWRLRule("testRule", "description",
new SWRLAntecedent(), new SWRLConsequent()));

Assert.IsNotNull(reasoner);
Assert.IsNotNull(reasoner.Rules);
Assert.IsTrue(reasoner.Rules.Count == 3);
Assert.IsTrue(reasoner.Rules.ContainsKey("STD"));
Assert.IsTrue(reasoner.Rules["STD"] is List<OWLEnums.OWLReasonerRules> stdRules && stdRules.Count == 0);
Assert.IsTrue(reasoner.Rules.ContainsKey("SWRL"));
Assert.IsTrue(reasoner.Rules["SWRL"] is List<SWRLRule> swrlRules && swrlRules.Count == 1);
Assert.IsTrue(reasoner.Rules.ContainsKey("TIME"));
Assert.IsTrue(reasoner.Rules["TIME"] is List<TIMEEnums.TIMEReasonerRules> timeRules && timeRules.Count == 0);
}

[TestMethod]
public void ShouldReasonWithSWRLRule()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:class"));
ontology.Data.DeclareIndividual(new RDFResource("ex:indiv"));
ontology.Data.DeclareIndividualType(new RDFResource("ex:indiv"), new RDFResource("ex:class"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddSWRLRule(new SWRLRule("testRule", "this is test rule",
new SWRLAntecedent().AddAtom(new SWRLClassAtom(new RDFResource("ex:class"), new RDFVariable("?C"))),
new SWRLConsequent().AddAtom(new SWRLObjectPropertyAtom(RDFVocabulary.RDF.TYPE, new RDFVariable("?C"), RDFVocabulary.OWL.INDIVIDUAL))));

OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Assert.IsNotNull(reasonerReport);
Assert.IsTrue(reasonerReport.EvidencesCount == 1);
}

[TestMethod]
public void ShouldReasonWithSWRLRuleAndSubscribedEvents()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:class"));
ontology.Data.DeclareIndividual(new RDFResource("ex:indiv"));
ontology.Data.DeclareIndividualType(new RDFResource("ex:indiv"), new RDFResource("ex:class"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddSWRLRule(new SWRLRule("testRule", "this is test rule",
new SWRLAntecedent().AddAtom(new SWRLClassAtom(new RDFResource("ex:class"), new RDFVariable("?C"))),
new SWRLConsequent().AddAtom(new SWRLObjectPropertyAtom(RDFVocabulary.RDF.TYPE, new RDFVariable("?C"), RDFVocabulary.OWL.INDIVIDUAL))));

string warningMsg = null;
OWLEvents.OnInfo += (string msg) => { warningMsg += msg; };

OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Assert.IsNotNull(reasonerReport);
Assert.IsTrue(reasonerReport.EvidencesCount == 1);
Assert.IsNotNull(warningMsg);
Assert.IsTrue(warningMsg.IndexOf("found 1 evidences") > -1);
}

[TestMethod]
public async Task ShouldReasonWithSWRLRuleAsync()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:class"));
ontology.Data.DeclareIndividual(new RDFResource("ex:indiv"));
ontology.Data.DeclareIndividualType(new RDFResource("ex:indiv"), new RDFResource("ex:class"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddSWRLRule(new SWRLRule("testRule", "this is test rule",
new SWRLAntecedent().AddAtom(new SWRLClassAtom(new RDFResource("ex:class"), new RDFVariable("?C"))),
new SWRLConsequent().AddAtom(new SWRLObjectPropertyAtom(RDFVocabulary.RDF.TYPE, new RDFVariable("?C"), RDFVocabulary.OWL.INDIVIDUAL))));

OWLReasonerReport reasonerReport = await reasoner.ApplyToOntologyAsync(ontology);

Assert.IsNotNull(reasonerReport);
Assert.IsTrue(reasonerReport.EvidencesCount == 1);
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void ShouldExecuteEqualsEntailmentViaReasoner()
ontology.DeclareTimeIntervalRelation(new TIMEInterval(new RDFResource("ex:Interval1Name")), new TIMEInterval(new RDFResource("ex:Interval2Name")), TIMEEnums.TIMEIntervalRelation.Starts);
ontology.DeclareTimeIntervalRelation(new TIMEInterval(new RDFResource("ex:Interval1Name")), new TIMEInterval(new RDFResource("ex:Interval2Name")), TIMEEnums.TIMEIntervalRelation.Finishes);

OWLReasoner reasoner = new OWLReasoner().AddTIMEExtensionRule(TIMEEnums.TIMEReasonerExtensionRules.TIME_EqualsEntailment);
OWLReasoner reasoner = new OWLReasoner().AddTIMERule(TIMEEnums.TIMEReasonerRules.TIME_EqualsEntailment);
OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Assert.IsNotNull(reasonerReport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void ShouldExecuteMeetsEntailmentViaReasoner()
ontology.DeclareTimeIntervalRelation(new TIMEInterval(new RDFResource("ex:Interval1Name")), new TIMEInterval(new RDFResource("ex:Interval2Name")), TIMEEnums.TIMEIntervalRelation.Meets);
ontology.DeclareTimeIntervalRelation(new TIMEInterval(new RDFResource("ex:Interval2Name")), new TIMEInterval(new RDFResource("ex:Interval3Name")), TIMEEnums.TIMEIntervalRelation.Starts);

OWLReasoner reasoner = new OWLReasoner().AddTIMEExtensionRule(TIMEEnums.TIMEReasonerExtensionRules.TIME_MeetsEntailment);
OWLReasoner reasoner = new OWLReasoner().AddTIMERule(TIMEEnums.TIMEReasonerRules.TIME_MeetsEntailment);
OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Assert.IsNotNull(reasonerReport);
Expand Down
44 changes: 44 additions & 0 deletions OWLSharp.Test/Extensions/TIME/Reasoner/TIMEReasonerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2014-2024 Marco De Salvo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OWLSharp.Extensions.SWRL;
using System.Collections.Generic;

namespace OWLSharp.Extensions.TIME.Test
{
[TestClass]
public class TIMEReasonerTest
{
[TestMethod]
public void ShouldAddTIMERule()
{
OWLReasoner reasoner = new OWLReasoner();
reasoner.AddTIMERule(TIMEEnums.TIMEReasonerRules.TIME_EqualsEntailment);
reasoner.AddTIMERule(TIMEEnums.TIMEReasonerRules.TIME_EqualsEntailment); //Will be discarded, since duplicate standard rules are not allowed

Assert.IsNotNull(reasoner);
Assert.IsNotNull(reasoner.Rules);
Assert.IsTrue(reasoner.Rules.Count == 3);
Assert.IsTrue(reasoner.Rules.ContainsKey("STD"));
Assert.IsTrue(reasoner.Rules["STD"] is List<OWLEnums.OWLReasonerRules> stdRules && stdRules.Count == 0);
Assert.IsTrue(reasoner.Rules.ContainsKey("SWRL"));
Assert.IsTrue(reasoner.Rules["SWRL"] is List<SWRLRule> swrlRules && swrlRules.Count == 0);
Assert.IsTrue(reasoner.Rules.ContainsKey("TIME"));
Assert.IsTrue(reasoner.Rules["TIME"] is List<TIMEEnums.TIMEReasonerRules> timeRules && timeRules.Count == 1);
}
}
}
109 changes: 31 additions & 78 deletions OWLSharp.Test/Reasoner/OWLReasonerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ limitations under the License.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OWLSharp.Extensions.SWRL;
using OWLSharp.Extensions.TIME;
using RDFSharp.Model;
using RDFSharp.Query;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OWLSharp.Reasoner.Test
Expand All @@ -32,42 +34,36 @@ public void ShouldCreateReasoner()
OWLReasoner reasoner = new OWLReasoner();

Assert.IsNotNull(reasoner);
Assert.IsNotNull(reasoner.StandardRules);
Assert.IsTrue(reasoner.StandardRules.Count == 0);
Assert.IsNotNull(reasoner.SWRLExtensionRules);
Assert.IsTrue(reasoner.SWRLExtensionRules.Count == 0);
Assert.IsNotNull(reasoner.Rules);
Assert.IsTrue(reasoner.Rules.Count == 3);
Assert.IsTrue(reasoner.Rules.ContainsKey("STD"));
Assert.IsTrue(reasoner.Rules["STD"] is List<OWLEnums.OWLReasonerRules> stdRules && stdRules.Count == 0);
Assert.IsTrue(reasoner.Rules.ContainsKey("SWRL"));
Assert.IsTrue(reasoner.Rules["SWRL"] is List<SWRLRule> swrlRules && swrlRules.Count == 0);
Assert.IsTrue(reasoner.Rules.ContainsKey("TIME"));
Assert.IsTrue(reasoner.Rules["TIME"] is List<TIMEEnums.TIMEReasonerRules> timeRules && timeRules.Count == 0);
}

[TestMethod]
public void ShouldAddStandarReasonerRule()
public void ShouldAddRule()
{
OWLReasoner reasoner = new OWLReasoner();
reasoner.AddStandardRule(OWLEnums.OWLReasonerStandardRules.SubClassTransitivity);
reasoner.AddStandardRule(OWLEnums.OWLReasonerStandardRules.SubClassTransitivity); //Will be discarded, since duplicate standard rules are not allowed
reasoner.AddRule(OWLEnums.OWLReasonerRules.SubClassTransitivity)
.AddRule(OWLEnums.OWLReasonerRules.SubClassTransitivity); //Will be discarded, since duplicate standard rules are not allowed

Assert.IsNotNull(reasoner);
Assert.IsNotNull(reasoner.StandardRules);
Assert.IsTrue(reasoner.StandardRules.Count == 1);
Assert.IsNotNull(reasoner.SWRLExtensionRules);
Assert.IsTrue(reasoner.SWRLExtensionRules.Count == 0);
Assert.IsNotNull(reasoner.Rules);
Assert.IsTrue(reasoner.Rules.Count == 3);
Assert.IsTrue(reasoner.Rules.ContainsKey("STD"));
Assert.IsTrue(reasoner.Rules["STD"] is List<OWLEnums.OWLReasonerRules> stdRules && stdRules.Count == 1);
Assert.IsTrue(reasoner.Rules.ContainsKey("SWRL"));
Assert.IsTrue(reasoner.Rules["SWRL"] is List<SWRLRule> swrlRules && swrlRules.Count == 0);
Assert.IsTrue(reasoner.Rules.ContainsKey("TIME"));
Assert.IsTrue(reasoner.Rules["TIME"] is List<TIMEEnums.TIMEReasonerRules> timeRules && timeRules.Count == 0);
}

[TestMethod]
public void ShouldAddCustomReasonerRule()
{
OWLReasoner reasoner = new OWLReasoner();
reasoner.AddSWRLExtensionRule(new SWRLRule("testRule", "description",
new SWRLAntecedent(), new SWRLConsequent()));

Assert.IsNotNull(reasoner);
Assert.IsNotNull(reasoner.StandardRules);
Assert.IsTrue(reasoner.StandardRules.Count == 0);
Assert.IsNotNull(reasoner.SWRLExtensionRules);
Assert.IsTrue(reasoner.SWRLExtensionRules.Count == 1);
}

[TestMethod]
public void ShouldReasonWithStandardRule()
public void ShouldReasonWithRule()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:classA"));
Expand All @@ -77,7 +73,7 @@ public void ShouldReasonWithStandardRule()
ontology.Model.ClassModel.DeclareSubClasses(new RDFResource("ex:classB"), new RDFResource("ex:classC"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddStandardRule(OWLEnums.OWLReasonerStandardRules.SubClassTransitivity);
reasoner.AddRule(OWLEnums.OWLReasonerRules.SubClassTransitivity);

OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Expand All @@ -86,7 +82,7 @@ public void ShouldReasonWithStandardRule()
}

[TestMethod]
public void ShouldReasonWithStandardRuleAndSubscribedEvents()
public void ShouldReasonWithRuleAndSubscribedEvents()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:classA"));
Expand All @@ -96,50 +92,7 @@ public void ShouldReasonWithStandardRuleAndSubscribedEvents()
ontology.Model.ClassModel.DeclareSubClasses(new RDFResource("ex:classB"), new RDFResource("ex:classC"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddStandardRule(OWLEnums.OWLReasonerStandardRules.SubClassTransitivity);

string warningMsg = null;
OWLEvents.OnInfo += (string msg) => { warningMsg += msg; };

OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Assert.IsNotNull(reasonerReport);
Assert.IsTrue(reasonerReport.EvidencesCount == 1);
Assert.IsNotNull(warningMsg);
Assert.IsTrue(warningMsg.IndexOf("found 1 evidences") > -1);
}

[TestMethod]
public void ShouldReasonWithCustomRule()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:class"));
ontology.Data.DeclareIndividual(new RDFResource("ex:indiv"));
ontology.Data.DeclareIndividualType(new RDFResource("ex:indiv"), new RDFResource("ex:class"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddSWRLExtensionRule(new SWRLRule("testRule", "this is test rule",
new SWRLAntecedent().AddAtom(new SWRLClassAtom(new RDFResource("ex:class"), new RDFVariable("?C"))),
new SWRLConsequent().AddAtom(new SWRLObjectPropertyAtom(RDFVocabulary.RDF.TYPE, new RDFVariable("?C"), RDFVocabulary.OWL.INDIVIDUAL))));

OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Assert.IsNotNull(reasonerReport);
Assert.IsTrue(reasonerReport.EvidencesCount == 1);
}

[TestMethod]
public void ShouldReasonWithCustomRuleAndSubscribedEvents()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:class"));
ontology.Data.DeclareIndividual(new RDFResource("ex:indiv"));
ontology.Data.DeclareIndividualType(new RDFResource("ex:indiv"), new RDFResource("ex:class"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddSWRLExtensionRule(new SWRLRule("testRule", "this is test rule",
new SWRLAntecedent().AddAtom(new SWRLClassAtom(new RDFResource("ex:class"), new RDFVariable("?C"))),
new SWRLConsequent().AddAtom(new SWRLObjectPropertyAtom(RDFVocabulary.RDF.TYPE, new RDFVariable("?C"), RDFVocabulary.OWL.INDIVIDUAL))));
reasoner.AddRule(OWLEnums.OWLReasonerRules.SubClassTransitivity);

string warningMsg = null;
OWLEvents.OnInfo += (string msg) => { warningMsg += msg; };
Expand All @@ -153,7 +106,7 @@ public void ShouldReasonWithCustomRuleAndSubscribedEvents()
}

[TestMethod]
public void ShouldReasonWithStandardAndCustomRules()
public void ShouldReasonWithMixedRules()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:classA"));
Expand All @@ -165,8 +118,8 @@ public void ShouldReasonWithStandardAndCustomRules()
ontology.Data.DeclareIndividualType(new RDFResource("ex:indiv"), new RDFResource("ex:classA"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddStandardRule(OWLEnums.OWLReasonerStandardRules.SubClassTransitivity);
reasoner.AddSWRLExtensionRule(new SWRLRule("testRule", "this is test rule",
reasoner.AddRule(OWLEnums.OWLReasonerRules.SubClassTransitivity);
reasoner.AddSWRLRule(new SWRLRule("testRule", "this is test rule",
new SWRLAntecedent().AddAtom(new SWRLClassAtom(new RDFResource("ex:classA"), new RDFVariable("?C"))),
new SWRLConsequent().AddAtom(new SWRLObjectPropertyAtom(RDFVocabulary.RDF.TYPE, new RDFVariable("?C"), RDFVocabulary.OWL.INDIVIDUAL))));

Expand All @@ -177,7 +130,7 @@ public void ShouldReasonWithStandardAndCustomRules()
}

[TestMethod]
public async Task ShouldReasonWithStandardAndCustomRulesAsync()
public async Task ShouldReasonWithMixedRulesAsync()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.DeclareClass(new RDFResource("ex:classA"));
Expand All @@ -189,8 +142,8 @@ public async Task ShouldReasonWithStandardAndCustomRulesAsync()
ontology.Data.DeclareIndividualType(new RDFResource("ex:indiv"), new RDFResource("ex:classA"));

OWLReasoner reasoner = new OWLReasoner();
reasoner.AddStandardRule(OWLEnums.OWLReasonerStandardRules.SubClassTransitivity);
reasoner.AddSWRLExtensionRule(new SWRLRule("testRule", "this is test rule",
reasoner.AddRule(OWLEnums.OWLReasonerRules.SubClassTransitivity);
reasoner.AddSWRLRule(new SWRLRule("testRule", "this is test rule",
new SWRLAntecedent().AddAtom(new SWRLClassAtom(new RDFResource("ex:classA"), new RDFVariable("?C"))),
new SWRLConsequent().AddAtom(new SWRLObjectPropertyAtom(RDFVocabulary.RDF.TYPE, new RDFVariable("?C"), RDFVocabulary.OWL.INDIVIDUAL))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void ShouldExecuteDifferentFromEntailmentViaReasoner()
ontology.Data.DeclareSameIndividuals(new RDFResource("ex:indivB"), new RDFResource("ex:indivC"));
ontology.Data.DeclareSameIndividuals(new RDFResource("ex:indivC"), new RDFResource("ex:indivD"));

OWLReasoner reasoner = new OWLReasoner().AddStandardRule(OWLEnums.OWLReasonerStandardRules.DifferentFromEntailment);
OWLReasoner reasoner = new OWLReasoner().AddRule(OWLEnums.OWLReasonerRules.DifferentFromEntailment);
OWLReasonerReport reasonerReport = reasoner.ApplyToOntology(ontology);

Assert.IsNotNull(reasonerReport);
Expand Down
Loading

0 comments on commit 8241ab3

Please sign in to comment.