Skip to content

Commit

Permalink
v3.8.1 - Added OWLThingNothing validator rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Oct 17, 2023
1 parent d2a0775 commit ede2789
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
2 changes: 1 addition & 1 deletion OWLSharp.Test/OWLSharp.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AssemblyTitle>OWLSharp.Test</AssemblyTitle>
<AssemblyName>OWLSharp.Test</AssemblyName>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Version>3.8.0</Version>
<Version>3.8.1</Version>
<Authors>Marco De Salvo</Authors>
<Copyright>Marco De Salvo</Copyright>
<TargetFramework>net6.0</TargetFramework>
Expand Down
116 changes: 116 additions & 0 deletions OWLSharp.Test/Validator/Rules/OWLThingNothingRuleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2012-2023 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 RDFSharp.Model;
namespace OWLSharp.Validator.Test
{
[TestClass]
public class OWLThingNothingRuleTest
{
#region Tests

[TestMethod]
public void ShouldValidateThing()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.TBoxGraph.AddTriple(new RDFTriple(RDFVocabulary.OWL.THING, RDFVocabulary.RDFS.SUB_CLASS_OF, new RDFResource("ex:SuperThing")));

OWLValidatorReport validatorReport = OWLThingNothingRule.ExecuteRule(ontology);

Assert.IsNotNull(validatorReport);
Assert.IsTrue(validatorReport.EvidencesCount == 1);
Assert.IsTrue(validatorReport.SelectErrors().Count == 1);
Assert.IsTrue(validatorReport.SelectWarnings().Count == 0);
}

[TestMethod]
public void ShouldValidateThing_ViaValidator()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.TBoxGraph.AddTriple(new RDFTriple(RDFVocabulary.OWL.THING, RDFVocabulary.RDFS.SUB_CLASS_OF, new RDFResource("ex:SuperThing")));

OWLValidator validator = new OWLValidator().AddStandardRule(OWLEnums.OWLValidatorStandardRules.ThingNothing);
OWLValidatorReport validatorReport = validator.ApplyToOntology(ontology);

Assert.IsNotNull(validatorReport);
Assert.IsTrue(validatorReport.EvidencesCount == 1);
Assert.IsTrue(validatorReport.SelectErrors().Count == 1);
Assert.IsTrue(validatorReport.SelectWarnings().Count == 0);
}

[TestMethod]
public void ShouldValidateNothing()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.TBoxGraph.AddTriple(new RDFTriple(new RDFResource("ex:SubNothing"), RDFVocabulary.RDFS.SUB_CLASS_OF, RDFVocabulary.OWL.NOTHING));

OWLValidatorReport validatorReport = OWLThingNothingRule.ExecuteRule(ontology);

Assert.IsNotNull(validatorReport);
Assert.IsTrue(validatorReport.EvidencesCount == 1);
Assert.IsTrue(validatorReport.SelectErrors().Count == 1);
Assert.IsTrue(validatorReport.SelectWarnings().Count == 0);
}

[TestMethod]
public void ShouldValidateNothing_ViaValidator()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.TBoxGraph.AddTriple(new RDFTriple(new RDFResource("ex:SubNothing"), RDFVocabulary.RDFS.SUB_CLASS_OF, RDFVocabulary.OWL.NOTHING));

OWLValidator validator = new OWLValidator().AddStandardRule(OWLEnums.OWLValidatorStandardRules.ThingNothing);
OWLValidatorReport validatorReport = validator.ApplyToOntology(ontology);

Assert.IsNotNull(validatorReport);
Assert.IsTrue(validatorReport.EvidencesCount == 1);
Assert.IsTrue(validatorReport.SelectErrors().Count == 1);
Assert.IsTrue(validatorReport.SelectWarnings().Count == 0);
}

[TestMethod]
public void ShouldValidateNothingIndividual()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.Classes.Add(RDFVocabulary.OWL.NOTHING.PatternMemberID, RDFVocabulary.OWL.NOTHING);
ontology.Data.ABoxGraph.AddTriple(new RDFTriple(new RDFResource("ex:NothingIDV"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NOTHING));

OWLValidatorReport validatorReport = OWLThingNothingRule.ExecuteRule(ontology);

Assert.IsNotNull(validatorReport);
Assert.IsTrue(validatorReport.EvidencesCount == 1);
Assert.IsTrue(validatorReport.SelectErrors().Count == 1);
Assert.IsTrue(validatorReport.SelectWarnings().Count == 0);
}

[TestMethod]
public void ShouldValidateNothingIndividual_ViaValidator()
{
OWLOntology ontology = new OWLOntology("ex:ont");
ontology.Model.ClassModel.Classes.Add(RDFVocabulary.OWL.NOTHING.PatternMemberID, RDFVocabulary.OWL.NOTHING);
ontology.Data.ABoxGraph.AddTriple(new RDFTriple(new RDFResource("ex:NothingIDV"), RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NOTHING));

OWLValidator validator = new OWLValidator().AddStandardRule(OWLEnums.OWLValidatorStandardRules.ThingNothing);
OWLValidatorReport validatorReport = validator.ApplyToOntology(ontology);

Assert.IsNotNull(validatorReport);
Assert.IsTrue(validatorReport.EvidencesCount == 1);
Assert.IsTrue(validatorReport.SelectErrors().Count == 1);
Assert.IsTrue(validatorReport.SelectWarnings().Count == 0);
}
#endregion
}
}
6 changes: 5 additions & 1 deletion OWLSharp/OWLEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public enum OWLValidatorStandardRules
/// <summary>
/// This OWL2 rule checks for consistency of owl:disjointUnionOf knowledge
/// </summary>
DisjointUnion = 17
DisjointUnion = 17,
/// <summary>
/// This OWL-DL rule check for consistency of owl:Thing (as root) and owl:Nothing (as bottom)
/// </summary>
ThingNothing = 18
};

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion OWLSharp/OWLSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AssemblyTitle>OWLSharp</AssemblyTitle>
<AssemblyName>OWLSharp</AssemblyName>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Version>3.8.0</Version>
<Version>3.8.1</Version>
<Authors>Marco De Salvo</Authors>
<Copyright>Marco De Salvo</Copyright>
<Description>Lightweight and friendly .NET library for realizing intelligent Semantic Web applications</Description>
Expand Down
5 changes: 5 additions & 0 deletions OWLSharp/Validator/OWLValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public OWLValidator AddStandardRule(OWLEnums.OWLValidatorStandardRules standardR
/// </summary>
public OWLValidator AddCustomRule(OWLValidatorRule customRule)
{
#region Guards
if (customRule == null)
throw new OWLException("Cannot add custom rule to validator because given \"customRule\" parameter is null");
#endregion

CustomRules.Add(customRule);
return this;
Expand Down Expand Up @@ -144,6 +146,9 @@ public OWLValidatorReport ApplyToOntology(OWLOntology ontology)
case OWLEnums.OWLValidatorStandardRules.DisjointUnion:
validatorRegistry[OWLEnums.OWLValidatorStandardRules.DisjointUnion.ToString()] = OWLDisjointUnionRule.ExecuteRule(ontology);
break;
case OWLEnums.OWLValidatorStandardRules.ThingNothing:
validatorRegistry[OWLEnums.OWLValidatorStandardRules.ThingNothing.ToString()] = OWLThingNothingRule.ExecuteRule(ontology);
break;
}
OWLEvents.RaiseInfo($"Completed standard validator rule '{standardRule}': found {validatorRegistry[standardRule.ToString()].EvidencesCount} evidences");
Expand Down
52 changes: 52 additions & 0 deletions OWLSharp/Validator/Rules/OWLThingNothingRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2012-2023 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 RDFSharp.Model;

namespace OWLSharp
{
/// <summary>
/// OWL-DL rule checking for consistency of owl:Thing (as root) and owl:Nothing (as bottom)
/// </summary>
internal static class OWLThingNothingRule
{
internal static OWLValidatorReport ExecuteRule(OWLOntology ontology)
{
OWLValidatorReport validatorRuleReport = new OWLValidatorReport();

//owl:Thing
foreach (RDFResource thingSuperClass in ontology.Model.ClassModel.GetSuperClassesOf(RDFVocabulary.OWL.THING))
validatorRuleReport.AddEvidence(new OWLValidatorEvidence(
OWLEnums.OWLValidatorEvidenceCategory.Error,
nameof(OWLThingNothingRule),
$"Violation of OWL-DL integrity caused by '{thingSuperClass}' being superClass of 'owl:Thing'",
"Revise your class model: it is not allowed the use of superClasses of reserved top class 'owl:Thing'"));

//owl:Nothing
foreach (RDFResource nothingSubClass in ontology.Model.ClassModel.GetSubClassesOf(RDFVocabulary.OWL.NOTHING))
validatorRuleReport.AddEvidence(new OWLValidatorEvidence(
OWLEnums.OWLValidatorEvidenceCategory.Error,
nameof(OWLThingNothingRule),
$"Violation of OWL-DL integrity caused by '{nothingSubClass}' being subClass of 'owl:Nothing'",
"Revise your class model: it is not allowed the use of subClasses of reserved bottom class 'owl:Nothing'"));
foreach (RDFResource nothingIndividual in ontology.Data.GetIndividualsOf(ontology.Model, RDFVocabulary.OWL.NOTHING))
validatorRuleReport.AddEvidence(new OWLValidatorEvidence(
OWLEnums.OWLValidatorEvidenceCategory.Error,
nameof(OWLThingNothingRule),
$"Violation of OWL-DL integrity caused by '{nothingIndividual}' being individual of 'owl:Nothing'",
"Revise your data: it is not allowed the use of individuals of reserved bottom class 'owl:Nothing'"));

return validatorRuleReport;
}
}
}

0 comments on commit ede2789

Please sign in to comment.