-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mdesalvo
committed
May 16, 2020
1 parent
98f20c0
commit d25f6f7
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
RDFSharp/Model/Validation/Abstractions/Constraints/RDFXoneConstraint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
Copyright 2012-2020 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.Query; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace RDFSharp.Model | ||
{ | ||
/// <summary> | ||
/// RDFXoneConstraint represents a SHACL constraint requiring exactly one of the given shapes for a given RDF term | ||
/// </summary> | ||
public class RDFXoneConstraint : RDFConstraint { | ||
|
||
#region Properties | ||
/// <summary> | ||
/// Shapes required for the given RDF term | ||
/// </summary> | ||
internal Dictionary<Int64, RDFResource> XoneShapes { get; set; } | ||
#endregion | ||
|
||
#region Ctors | ||
/// <summary> | ||
/// Default-ctor to build a xone constraint | ||
/// </summary> | ||
public RDFXoneConstraint() : base() { | ||
this.XoneShapes = new Dictionary<Int64, RDFResource>(); | ||
} | ||
#endregion | ||
|
||
#region Methods | ||
/// <summary> | ||
/// Adds the given shape to the required shapes of this constraint | ||
/// </summary> | ||
public RDFXoneConstraint AddShape(RDFResource shapeUri) { | ||
if (shapeUri != null && !this.XoneShapes.ContainsKey(shapeUri.PatternMemberID)) { | ||
this.XoneShapes.Add(shapeUri.PatternMemberID, shapeUri); | ||
} | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Evaluates this constraint against the given data graph | ||
/// </summary> | ||
internal override RDFValidationReport ValidateConstraint(RDFShapesGraph shapesGraph, RDFGraph dataGraph, RDFShape shape, RDFPatternMember focusNode, List<RDFPatternMember> valueNodes) { | ||
RDFValidationReport report = new RDFValidationReport(); | ||
|
||
//Search for given xone shapes | ||
List<RDFShape> xoneShapes = new List<RDFShape>(); | ||
foreach (RDFResource xoneShapeUri in this.XoneShapes.Values) { | ||
RDFShape xoneShape = shapesGraph.SelectShape(xoneShapeUri.ToString()); | ||
if (xoneShape != null) | ||
xoneShapes.Add(xoneShape); | ||
} | ||
|
||
#region Evaluation | ||
foreach (RDFPatternMember valueNode in valueNodes) { | ||
Int32 valueNodeConformsCounter = 0; | ||
foreach (RDFShape xoneShape in xoneShapes) { | ||
RDFValidationReport xoneShapeReport = RDFValidationEngine.ValidateShape(shapesGraph, dataGraph, xoneShape, new List<RDFPatternMember>() { valueNode }); | ||
if (xoneShapeReport.Conforms) | ||
valueNodeConformsCounter++; | ||
} | ||
|
||
if (valueNodeConformsCounter != 1) | ||
report.AddResult(new RDFValidationResult(shape, | ||
RDFVocabulary.SHACL.XONE_CONSTRAINT_COMPONENT, | ||
focusNode, | ||
shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null, | ||
valueNode, | ||
shape.Messages, | ||
shape.Severity)); | ||
} | ||
#endregion | ||
|
||
return report; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a graph representation of this constraint | ||
/// </summary> | ||
internal override RDFGraph ToRDFGraph(RDFShape shape) { | ||
RDFGraph result = new RDFGraph(); | ||
if (shape != null) { | ||
|
||
//Get collection from xoneShapes | ||
RDFCollection xoneShapes = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource) { InternalReificationSubject = this }; | ||
foreach (RDFResource xoneShape in this.XoneShapes.Values) | ||
xoneShapes.AddItem(xoneShape); | ||
result.AddCollection(xoneShapes); | ||
|
||
//sh:xone | ||
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.XONE, xoneShapes.ReificationSubject)); | ||
|
||
} | ||
return result; | ||
} | ||
#endregion | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters