コード例 #1
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);
        }
コード例 #2
0
        /// <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);
                    }
                }
            }
        }
コード例 #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();

            //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);
        }
コード例 #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 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);
        }
コード例 #5
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 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);
        }
コード例 #6
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);
        }
コード例 #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 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);
        }
コード例 #8
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);
            }
        }