Exemplo n.º 1
0
        /// <summary>
        /// Validates the given data graph against the given SHACL shape
        /// </summary>
        internal static RDFValidationReport ValidateShape(RDFShapesGraph shapesGraph, RDFGraph dataGraph, RDFShape shape, List <RDFPatternMember> focusNodes = null)
        {
            RDFValidationReport report = new RDFValidationReport(new RDFResource());

            if (!shape.Deactivated)
            {
                //Resolve focus nodes
                if (focusNodes == null)
                {
                    focusNodes = dataGraph.GetFocusNodesOf(shape);
                }
                foreach (RDFPatternMember focusNode in focusNodes)
                {
                    //Resolve value nodes
                    List <RDFPatternMember> valueNodes = dataGraph.GetValueNodesOf(shape, focusNode);

                    //Evaluate constraints
                    foreach (RDFConstraint constraint in shape)
                    {
                        report.MergeResults(constraint.ValidateConstraint(shapesGraph, dataGraph, shape, focusNode, valueNodes));
                    }
                }
            }
            return(report);
        }
Exemplo n.º 2
0
        internal static RDFShapesGraph FromRDFGraph(RDFGraph graph)
        {
            if (graph != null)
            {
                RDFShapesGraph       result       = new RDFShapesGraph(new RDFResource(graph.Context.ToString()));
                RDFSelectQueryResult shapesResult = GetShapeQuery().ApplyToGraph(graph);
                foreach (DataRow shapesRow in shapesResult.SelectResults.AsEnumerable())
                {
                    RDFShape shape = ParseShapeType(shapesRow);
                    if (shape == null)
                    {
                        continue;
                    }

                    //Targets
                    ParseShapeTargets(shapesRow, shape);

                    //Attributes
                    ParseShapeAttributes(shapesRow, shape);

                    //Constraints
                    ParseShapeConstraints(shapesRow, graph, shape);

                    //Merge
                    MergeShape(result, shape);
                }
                return(result);
            }
            return(null);
        }
Exemplo n.º 3
0
        /// <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();

            #region Evaluation
            List <RDFPatternMember> predicateNodes = dataGraph.Where(t => t.Subject.Equals(focusNode) &&
                                                                     t.Predicate.Equals(this.LessThanPredicate))
                                                     .Select(x => x.Object)
                                                     .ToList();

            foreach (RDFPatternMember valueNode in valueNodes)
            {
                foreach (RDFPatternMember predicateNode in predicateNodes)
                {
                    int comparison = RDFQueryUtilities.CompareRDFPatternMembers(valueNode, predicateNode);
                    if (comparison == -99 || comparison >= 0)
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.LESS_THAN_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 4
0
        /// <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 node shape
            RDFNodeShape nodeShape = shapesGraph.SelectShape(this.NodeShapeUri.ToString()) as RDFNodeShape;

            if (nodeShape == null)
            {
                return(report);
            }

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                RDFValidationReport nodeShapeReport = RDFValidationEngine.ValidateShape(shapesGraph, dataGraph, nodeShape, new List <RDFPatternMember>()
                {
                    valueNode
                });
                if (!nodeShapeReport.Conforms)
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.NODE_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                }
            }
            #endregion

            return(report);
        }
        /// <summary>
        /// Detects the typed instances of shacl:PropertyShape and populates the shapes graph with their definition
        /// </summary>
        private static void DetectTypedPropertyShapes(RDFGraph graph, RDFShapesGraph shapesGraph)
        {
            RDFGraph declaredPropertyShapes = graph.SelectTriplesByPredicate(RDFVocabulary.RDF.TYPE)
                                              .SelectTriplesByObject(RDFVocabulary.SHACL.PROPERTY_SHAPE);

            foreach (RDFTriple declaredPropertyShape in declaredPropertyShapes)
            {
                RDFTriple declaredPropertyShapePath = graph.SelectTriplesBySubject((RDFResource)declaredPropertyShape.Subject)
                                                      .SelectTriplesByPredicate(RDFVocabulary.SHACL.PATH)
                                                      .FirstOrDefault();

                if (declaredPropertyShapePath != null &&
                    declaredPropertyShapePath.Object is RDFResource)
                {
                    RDFPropertyShape propertyShape = new RDFPropertyShape((RDFResource)declaredPropertyShape.Subject, (RDFResource)declaredPropertyShapePath.Object);

                    DetectShapeTargets(graph, propertyShape);
                    DetectShapeAttributes(graph, propertyShape);
                    DetectShapeNonValidatingAttributes(graph, propertyShape);
                    DetectShapeConstraints(graph, propertyShape);

                    shapesGraph.AddShape(propertyShape);
                }
            }
        }
        /// <summary>
        /// Detects the inline instances of shacl:PropertyShape and populates the shapes graph with their definition
        /// </summary>
        private static void DetectInlinePropertyShapes(RDFGraph graph, RDFShapesGraph shapesGraph)
        {
            RDFGraph inlinePropertyShapes = graph.SelectTriplesByPredicate(RDFVocabulary.SHACL.PROPERTY);

            foreach (RDFTriple inlinePropertyShape in inlinePropertyShapes)
            {
                //Inline property shapes are blank objects of "sh:property" constraints:
                //we wont find their explicit shape definition within the shapes graph.
                if (inlinePropertyShape.Object is RDFResource inlinePropertyShapeResource &&
                    inlinePropertyShapeResource.IsBlank &&
                    shapesGraph.SelectShape(inlinePropertyShapeResource.ToString()) == null)
                {
                    RDFTriple inlinePropertyShapePath = graph.SelectTriplesBySubject(inlinePropertyShapeResource)
                                                        .SelectTriplesByPredicate(RDFVocabulary.SHACL.PATH)
                                                        .FirstOrDefault();

                    if (inlinePropertyShapePath != null &&
                        inlinePropertyShapePath.Object is RDFResource)
                    {
                        RDFPropertyShape propertyShape = new RDFPropertyShape(inlinePropertyShapeResource, (RDFResource)inlinePropertyShapePath.Object);

                        DetectShapeTargets(graph, propertyShape);
                        DetectShapeAttributes(graph, propertyShape);
                        DetectShapeNonValidatingAttributes(graph, propertyShape);
                        DetectShapeConstraints(graph, propertyShape);

                        shapesGraph.AddShape(propertyShape);
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <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 qualified value shape
            RDFShape qualifiedValueShape = shapesGraph.SelectShape(this.QualifiedValueShapeUri.ToString());

            if (qualifiedValueShape == null)
            {
                return(report);
            }

            #region Evaluation
            if (this.QualifiedValueMinCount.HasValue || this.QualifiedValueMaxCount.HasValue)
            {
                int conformingValues = 0;
                foreach (RDFPatternMember valueNode in valueNodes)
                {
                    RDFValidationReport qualifiedValueShapeReport = RDFValidationEngine.ValidateShape(shapesGraph, dataGraph, qualifiedValueShape, new List <RDFPatternMember>()
                    {
                        valueNode
                    });
                    if (qualifiedValueShapeReport.Conforms)
                    {
                        conformingValues++;
                    }
                }

                if (this.QualifiedValueMinCount.HasValue && conformingValues < this.QualifiedValueMinCount)
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.QUALIFIED_MIN_COUNT_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             null,
                                                             shape.Messages,
                                                             shape.Severity));
                }

                if (this.QualifiedValueMaxCount.HasValue && conformingValues > this.QualifiedValueMaxCount)
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.QUALIFIED_MAX_COUNT_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             null,
                                                             shape.Messages,
                                                             shape.Severity));
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 8
0
        /// <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();

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                switch (valueNode)
                {
                //Resource
                case RDFResource valueNodeResource:
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.DATATYPE_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                    break;

                //PlainLiteral
                case RDFPlainLiteral valueNodePlainLiteral:
                    if (this.Datatype != RDFModelEnums.RDFDatatypes.XSD_STRING ||
                        !string.IsNullOrEmpty(valueNodePlainLiteral.Language))
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.DATATYPE_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                    break;

                //TypedLiteral
                case RDFTypedLiteral valueNodeTypedLiteral:
                    if (this.Datatype != valueNodeTypedLiteral.Datatype)
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.DATATYPE_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                    break;
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Validates the given data graph against the given SHACL shapes graph
        /// </summary>
        public static RDFValidationReport Validate(this RDFShapesGraph shapesGraph, RDFGraph dataGraph)
        {
            RDFValidationReport report = new RDFValidationReport(new RDFResource());

            if (dataGraph != null)
            {
                foreach (RDFShape shape in shapesGraph)
                {
                    report.MergeResults(ValidateShape(shapesGraph, dataGraph, shape));
                }
            }
            return(report);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Merges the shapes of the given shapes graph to this shapes graph
 /// </summary>
 public RDFShapesGraph MergeShapes(RDFShapesGraph shapesGraph)
 {
     if (shapesGraph != null)
     {
         foreach (var shape in shapesGraph)
         {
             if (!this.Shapes.ContainsKey(shape.PatternMemberID))
             {
                 this.Shapes.Add(shape.PatternMemberID, shape);
             }
         }
     }
     return(this);
 }
        internal static RDFShapesGraph FromRDFGraph(RDFGraph graph)
        {
            if (graph != null)
            {
                RDFShapesGraph result = new RDFShapesGraph(new RDFResource(graph.Context.ToString()));

                DetectTypedNodeShapes(graph, result);
                DetectTypedPropertyShapes(graph, result);
                DetectInlinePropertyShapes(graph, result);

                return(result);
            }
            return(null);
        }
Exemplo n.º 12
0
        /// <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 and shapes
            List <RDFShape> andShapes = new List <RDFShape>();

            foreach (RDFResource andShapeUri in this.AndShapes.Values)
            {
                RDFShape andShape = shapesGraph.SelectShape(andShapeUri.ToString());
                if (andShape != null)
                {
                    andShapes.Add(andShape);
                }
            }

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                bool valueNodeConforms = true;
                foreach (RDFShape andShape in andShapes)
                {
                    RDFValidationReport andShapeReport = RDFValidationEngine.ValidateShape(shapesGraph, dataGraph, andShape, new List <RDFPatternMember>()
                    {
                        valueNode
                    });
                    if (!andShapeReport.Conforms)
                    {
                        valueNodeConforms = false;
                        break;
                    }
                }

                if (!valueNodeConforms)
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.AND_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                }
            }
            #endregion

            return(report);
        }
        /// <summary>
        /// Detects the typed instances of shacl:NodeShape and populates the shapes graph with their definition
        /// </summary>
        private static void DetectTypedNodeShapes(RDFGraph graph, RDFShapesGraph shapesGraph)
        {
            RDFGraph declaredNodeShapes = graph.SelectTriplesByPredicate(RDFVocabulary.RDF.TYPE)
                                          .SelectTriplesByObject(RDFVocabulary.SHACL.NODE_SHAPE);

            foreach (RDFTriple declaredNodeShape in declaredNodeShapes)
            {
                RDFNodeShape nodeShape = new RDFNodeShape((RDFResource)declaredNodeShape.Subject);

                DetectShapeTargets(graph, nodeShape);
                DetectShapeAttributes(graph, nodeShape);
                DetectShapeConstraints(graph, nodeShape);

                shapesGraph.AddShape(nodeShape);
            }
        }
        /// <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)
            {
                int 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);
        }
Exemplo n.º 15
0
        /// <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();

            #region Evaluation
            if (!valueNodes.Any(v => v.Equals(this.Value))) {
                report.AddResult(new RDFValidationResult(shape,
                                                         RDFVocabulary.SHACL.HAS_VALUE_CONSTRAINT_COMPONENT,
                                                         focusNode,
                                                         shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                         null,
                                                         shape.Messages,
                                                         shape.Severity));
            }
            #endregion

            return report;
        }
Exemplo n.º 16
0
        /// <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();

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                switch (valueNode)
                {
                //Resource
                case RDFResource valueNodeResource:
                    if (valueNodeResource.IsBlank ||
                        valueNodeResource.ToString().Length > this.MaxLength)
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.MAX_LENGTH_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                    break;

                //Literal
                case RDFLiteral valueNodeLiteral:
                    if (valueNodeLiteral.Value.Length > this.MaxLength)
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.MAX_LENGTH_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                    break;
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 17
0
        /// <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();

            #region Evaluation
            if (this.Closed)
            {
                //Extend ignored properties with paths of property constraints
                List <RDFResource> allowedProperties = new List <RDFResource>(this.IgnoredProperties.Values);
                IEnumerable <RDFPropertyConstraint> propertyConstraints = shape.Constraints.OfType <RDFPropertyConstraint>();
                foreach (RDFPropertyConstraint propertyConstraint in propertyConstraints)
                {
                    RDFPropertyShape propertyShape = shapesGraph.SelectShape(propertyConstraint.PropertyShapeUri.ToString()) as RDFPropertyShape;
                    if (propertyShape != null)
                    {
                        allowedProperties.Add(propertyShape.Path);
                    }
                }

                //Detect unallowed predicates
                foreach (RDFPatternMember valueNode in valueNodes)
                {
                    if (valueNode is RDFResource valueNodeResource)
                    {
                        RDFGraph valuenodeResourceGraph          = dataGraph.SelectTriplesBySubject(valueNodeResource);
                        IEnumerable <RDFTriple> unallowedTriples = valuenodeResourceGraph.Where(t => !allowedProperties.Any(p => p.Equals(t.Predicate)));
                        foreach (RDFTriple unallowedTriple in unallowedTriples)
                        {
                            report.AddResult(new RDFValidationResult(shape,
                                                                     RDFVocabulary.SHACL.CLOSED_CONSTRAINT_COMPONENT,
                                                                     valueNodeResource,
                                                                     unallowedTriple.Predicate as RDFResource,
                                                                     unallowedTriple.Object,
                                                                     shape.Messages,
                                                                     shape.Severity));
                        }
                    }
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 18
0
        /// <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();

            #region Evaluation
            if (valueNodes.Count > this.MaxCount)
            {
                report.AddResult(new RDFValidationResult(shape,
                                                         RDFVocabulary.SHACL.MAX_COUNT_CONSTRAINT_COMPONENT,
                                                         focusNode,
                                                         shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                         null,
                                                         shape.Messages,
                                                         shape.Severity));
            }
            #endregion

            return(report);
        }
Exemplo n.º 19
0
        /// <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();

            #region Evaluation
            List <RDFPatternMember> predicateNodes = dataGraph.Where(t => t.Subject.Equals(focusNode) &&
                                                                     t.Predicate.Equals(this.EqualsPredicate))
                                                     .Select(x => x.Object)
                                                     .ToList();

            foreach (RDFPatternMember predicateNode in predicateNodes)
            {
                if (!valueNodes.Any(v => v.Equals(predicateNode)))
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.EQUALS_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             predicateNode,
                                                             shape.Messages,
                                                             shape.Severity));
                }
            }

            foreach (RDFPatternMember valueNode in valueNodes)
            {
                if (!predicateNodes.Any(p => p.Equals(valueNode)))
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.EQUALS_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 20
0
        /// <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();
            List <RDFPatternMember> classInstances = dataGraph.GetInstancesOfClass(this.ClassType);

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                switch (valueNode)
                {
                //Resource
                case RDFResource valueNodeResource:
                    if (!classInstances.Any(x => x.Equals(valueNodeResource)))
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.CLASS_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                    break;

                //Literal
                case RDFLiteral valueNodeLiteral:
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.CLASS_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                    break;
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 21
0
        /// <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 property shape
            RDFPropertyShape propertyShape = shapesGraph.SelectShape(this.PropertyShapeUri.ToString()) as RDFPropertyShape;

            if (propertyShape == null)
            {
                return(report);
            }

            #region Evaluation
            RDFValidationReport propertyShapeReport = RDFValidationEngine.ValidateShape(shapesGraph, dataGraph, propertyShape, valueNodes);
            if (!propertyShapeReport.Conforms)
            {
                report.MergeResults(propertyShapeReport);
            }
            #endregion

            return(report);
        }
Exemplo n.º 22
0
        /// <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();

            #region Evaluation
            if (this.UniqueLang)
            {
                HashSet <string>       reportedLangs     = new HashSet <string>();
                List <RDFPlainLiteral> langlitValueNodes = valueNodes.OfType <RDFPlainLiteral>()
                                                           .Where(vn => !string.IsNullOrEmpty(vn.Language))
                                                           .ToList();

                foreach (RDFPlainLiteral innerlanglitValueNode in langlitValueNodes)
                {
                    foreach (RDFPlainLiteral outerlanglitValueNode in langlitValueNodes)
                    {
                        if (!innerlanglitValueNode.Equals(outerlanglitValueNode) &&
                            innerlanglitValueNode.Language.Equals(outerlanglitValueNode.Language))
                        {
                            //Ensure to not report twice the same language tag
                            if (!reportedLangs.Contains(innerlanglitValueNode.Language))
                            {
                                reportedLangs.Add(innerlanglitValueNode.Language);
                                report.AddResult(new RDFValidationResult(shape,
                                                                         RDFVocabulary.SHACL.UNIQUE_LANG_CONSTRAINT_COMPONENT,
                                                                         focusNode,
                                                                         shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                         null,
                                                                         shape.Messages,
                                                                         shape.Severity));
                            }
                        }
                    }
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 23
0
        private static void MergeShape(RDFShapesGraph result, RDFShape shape)
        {
            RDFShape existingShape = result.SelectShape(shape.ToString());

            if (existingShape == null)
            {
                result.AddShape(shape);
            }
            else
            {
                existingShape.Targets.AddRange(shape.Targets);
                existingShape.Constraints.AddRange(shape.Constraints);
                existingShape.Messages.AddRange(shape.Messages);
                if (existingShape is RDFPropertyShape && shape is RDFPropertyShape)
                {
                    ((RDFPropertyShape)existingShape).Descriptions.AddRange(((RDFPropertyShape)shape).Descriptions);
                    ((RDFPropertyShape)existingShape).Names.AddRange(((RDFPropertyShape)shape).Names);
                }
                result.RemoveShape(existingShape);
                result.AddShape(existingShape);
            }
        }
Exemplo n.º 24
0
        /// <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();

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                Int32 comparison = RDFQueryUtilities.CompareRDFPatternMembers(this.Value, valueNode);
                if (comparison == -99 || comparison <= 0)
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.MAX_EXCLUSIVE_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 25
0
        /// <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();

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                if (dataGraph.Any(t => t.Subject.Equals(focusNode) &&
                                  t.Predicate.Equals(this.DisjointPredicate) &&
                                  t.Object.Equals(valueNode)))
                {
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.DISJOINT_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 26
0
 /// <summary>
 /// Asynchronously validates the given data graph against the given SHACL shapes graph
 /// </summary>
 public static Task <RDFValidationReport> ValidateAsync(this RDFShapesGraph shapesGraph, RDFGraph dataGraph)
 => Task.Run(() => Validate(shapesGraph, dataGraph));
        /// <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(new RDFResource());

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                switch (valueNode)
                {
                //PlainLiteral
                case RDFPlainLiteral valueNodePlainLiteral:
                    bool langMatches        = false;
                    var  langTagsEnumerator = this.LanguageTags.GetEnumerator();
                    while (langTagsEnumerator.MoveNext() && !langMatches)
                    {
                        //NO language is found in the variable
                        if (langTagsEnumerator.Current == string.Empty)
                        {
                            langMatches = !Regex.IsMatch(valueNodePlainLiteral.ToString(), "@[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        }

                        //ANY language is found in the variable
                        else if (langTagsEnumerator.Current == "*")
                        {
                            langMatches = Regex.IsMatch(valueNodePlainLiteral.ToString(), "@[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        }

                        //GIVEN language is found in the variable
                        else
                        {
                            langMatches = Regex.IsMatch(valueNodePlainLiteral.ToString(), string.Concat("@", langTagsEnumerator.Current, "(-[a-zA-Z0-9]{1,8})*$"), RegexOptions.IgnoreCase);
                        }
                    }
                    if (!langMatches)
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.LANGUAGE_IN_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                    break;

                //Resource/TypedLiteral
                default:
                    report.AddResult(new RDFValidationResult(shape,
                                                             RDFVocabulary.SHACL.LANGUAGE_IN_CONSTRAINT_COMPONENT,
                                                             focusNode,
                                                             shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                             valueNode,
                                                             shape.Messages,
                                                             shape.Severity));
                    break;
                }
            }
            #endregion

            return(report);
        }
Exemplo n.º 28
0
 /// <summary>
 /// Evaluates this constraint against the given data graph
 /// </summary>
 internal abstract RDFValidationReport ValidateConstraint(RDFShapesGraph shapesGraph,
                                                          RDFGraph dataGraph,
                                                          RDFShape shape,
                                                          RDFPatternMember focusNode,
                                                          List <RDFPatternMember> valueNodes);
Exemplo n.º 29
0
        /// <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();

            #region Evaluation
            foreach (RDFPatternMember valueNode in valueNodes)
            {
                switch (valueNode)
                {
                //Resource
                case RDFResource valueNodeResource:
                    if (valueNodeResource.IsBlank)
                    {
                        if (this.NodeKind == RDFValidationEnums.RDFNodeKinds.IRI ||
                            this.NodeKind == RDFValidationEnums.RDFNodeKinds.IRIOrLiteral ||
                            this.NodeKind == RDFValidationEnums.RDFNodeKinds.Literal)
                        {
                            report.AddResult(new RDFValidationResult(shape,
                                                                     RDFVocabulary.SHACL.NODE_KIND_CONSTRAINT_COMPONENT,
                                                                     focusNode,
                                                                     shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                     valueNode,
                                                                     shape.Messages,
                                                                     shape.Severity));
                        }
                    }
                    else
                    {
                        if (this.NodeKind == RDFValidationEnums.RDFNodeKinds.BlankNode ||
                            this.NodeKind == RDFValidationEnums.RDFNodeKinds.BlankNodeOrLiteral ||
                            this.NodeKind == RDFValidationEnums.RDFNodeKinds.Literal)
                        {
                            report.AddResult(new RDFValidationResult(shape,
                                                                     RDFVocabulary.SHACL.NODE_KIND_CONSTRAINT_COMPONENT,
                                                                     focusNode,
                                                                     shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                     valueNode,
                                                                     shape.Messages,
                                                                     shape.Severity));
                        }
                    }
                    break;

                //Literal
                case RDFLiteral valueNodeLiteral:
                    if (this.NodeKind == RDFValidationEnums.RDFNodeKinds.BlankNode ||
                        this.NodeKind == RDFValidationEnums.RDFNodeKinds.BlankNodeOrIRI ||
                        this.NodeKind == RDFValidationEnums.RDFNodeKinds.IRI)
                    {
                        report.AddResult(new RDFValidationResult(shape,
                                                                 RDFVocabulary.SHACL.NODE_KIND_CONSTRAINT_COMPONENT,
                                                                 focusNode,
                                                                 shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
                                                                 valueNode,
                                                                 shape.Messages,
                                                                 shape.Severity));
                    }
                    break;
                }
            }
            #endregion

            return(report);
        }