RDFCollection represents a generic collection in the RDF model. It is made up of items, which must be all resources or all literals.
Inheritance: IEnumerable
        /// <summary>
        /// Gets a graph representation of this constraint
        /// </summary>
        internal override RDFGraph ToRDFGraph(RDFShape shape)
        {
            RDFGraph result = new RDFGraph();

            if (shape != null)
            {
                //sh:closed
                result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.CLOSED, new RDFTypedLiteral(this.Closed.ToString(), RDFModelEnums.RDFDatatypes.XSD_BOOLEAN)));

                //Get collection from ignored properties
                RDFCollection ignoredProperties = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource)
                {
                    InternalReificationSubject = this
                };
                foreach (RDFResource ignoredProperty in this.IgnoredProperties.Values)
                {
                    ignoredProperties.AddItem(ignoredProperty);
                }
                result.AddCollection(ignoredProperties);

                //sh:ignoredProperties
                result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.IGNORED_PROPERTIES, ignoredProperties.ReificationSubject));
            }
            return(result);
        }
Exemple #2
0
        /// <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 inValues
                RDFCollection inValues = new RDFCollection(this.ItemType)
                {
                    InternalReificationSubject = this
                };
                foreach (RDFPatternMember inValue in this.InValues.Values)
                {
                    if (this.ItemType == RDFModelEnums.RDFItemTypes.Literal)
                    {
                        inValues.AddItem((RDFLiteral)inValue);
                    }
                    else
                    {
                        inValues.AddItem((RDFResource)inValue);
                    }
                }
                result.AddCollection(inValues);

                //sh:in
                result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.IN, inValues.ReificationSubject));
            }
            return(result);
        }
 /// <summary>
 /// Default-ctor to build an ontology enumerate class of the given category
 /// </summary>
 public RDFOntologyEnumerateClass(RDFSemanticsEnums.RDFOntologyEnumerateClassCategory enumerateClassCategory): base(new RDFResource()) {
     this.Category                 = enumerateClassCategory;
     this.EnumerateMembers         = new Dictionary<Int64, RDFOntologyFact>();
     this.RepresentativeCollection = (enumerateClassCategory == RDFSemanticsEnums.RDFOntologyEnumerateClassCategory.ResourceEnumeration ?
                                         new RDFCollection(RDFModelEnums.RDFItemTypes.Resource) : 
                                         new RDFCollection(RDFModelEnums.RDFItemTypes.Literal));
 }
 /// <summary>
 /// Adds the given collection to the graph
 /// </summary>
 public RDFGraph AddCollection(RDFCollection collection)
 {
     if (collection != null)
     {
         //Reify the collection to get its graph representation
         var reifColl = collection.ReifyCollection();
         //Iterate on the constructed triples
         foreach (var t in reifColl)
         {
             this.AddTriple(t);
         }
     }
     return(this);
 }
        /// <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 language tags
                RDFCollection languageTags = new RDFCollection(RDFModelEnums.RDFItemTypes.Literal)
                {
                    InternalReificationSubject = this
                };
                foreach (string languageTag in this.LanguageTags)
                {
                    languageTags.AddItem(new RDFPlainLiteral(languageTag));
                }
                result.AddCollection(languageTags);

                //sh:languageIn
                result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.LANGUAGE_IN, languageTags.ReificationSubject));
            }
            return(result);
        }
        /// <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);
        }
 /// <summary>
 /// Default-ctor to build a union class
 /// </summary>
 public RDFOntologyUnionClass(): base(new RDFResource()) {
     this.CompositingClasses       = new Dictionary<Int64, RDFOntologyClass>();
     this.RepresentativeCollection = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource);
 }
        /// <summary>
        /// Detects the constraints of the given shape
        /// </summary>
        private static void DetectShapeConstraints(RDFGraph graph, RDFShape shape)
        {
            RDFGraph shapeDefinition = graph.SelectTriplesBySubject(shape);

            //sh:and (accepted occurrences: N)
            RDFGraph shapeAndConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.AND);

            foreach (RDFTriple shapeAndConstraint in shapeAndConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                RDFAndConstraint andConstraint           = new RDFAndConstraint();
                RDFCollection    andConstraintCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)shapeAndConstraint.Object, RDFModelEnums.RDFTripleFlavors.SPO);
                andConstraintCollection.Items.ForEach(item => andConstraint.AddShape((RDFResource)item));
                shape.AddConstraint(andConstraint);
            }

            //sh:class (accepted occurrences: N)
            RDFGraph shapeClassConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.CLASS);

            foreach (RDFTriple shapeClassConstraint in shapeClassConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFClassConstraint((RDFResource)shapeClassConstraint.Object));
            }

            //sh:closed (accepted occurrences: 1)
            RDFTriple shapeClosedConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.CLOSED).FirstOrDefault();

            if (shapeClosedConstraint != null)
            {
                if (shapeClosedConstraint.Object is RDFTypedLiteral shapeClosedConstraintLiteral &&
                    shapeClosedConstraintLiteral.HasBooleanDatatype())
                {
                    RDFClosedConstraint closedConstraint = new RDFClosedConstraint(bool.Parse(shapeClosedConstraintLiteral.Value));

                    //sh:ignoredProperties (accepted occurrences: 1)
                    RDFTriple shapeIgnoredPropertiesConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.IGNORED_PROPERTIES).FirstOrDefault();
                    if (shapeIgnoredPropertiesConstraint != null)
                    {
                        if (shapeIgnoredPropertiesConstraint.Object is RDFResource shapeIgnoredPropertiesConstraintResource)
                        {
                            RDFCollection shapeIgnoredPropertiesConstraintCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, shapeIgnoredPropertiesConstraintResource, RDFModelEnums.RDFTripleFlavors.SPO);
                            shapeIgnoredPropertiesConstraintCollection.Items.ForEach(item => closedConstraint.AddIgnoredProperty((RDFResource)item));
                        }
                    }

                    shape.AddConstraint(closedConstraint);
                }
            }

            //sh:datatype (accepted occurrences: N)
            RDFGraph shapeDatatypeConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.DATATYPE);

            foreach (RDFTriple shapeDatatypeConstraint in shapeDatatypeConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFDatatypeConstraint(RDFModelUtilities.GetDatatypeFromString(shapeDatatypeConstraint.Object.ToString())));
            }

            //sh:disjoint (accepted occurrences: N)
            RDFGraph shapeDisjointConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.DISJOINT);

            foreach (RDFTriple shapeDisjointConstraint in shapeDisjointConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFDisjointConstraint((RDFResource)shapeDisjointConstraint.Object));
            }

            //sh:equals (accepted occurrences: N)
            RDFGraph shapeEqualsConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.EQUALS);

            foreach (RDFTriple shapeEqualsConstraint in shapeEqualsConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFEqualsConstraint((RDFResource)shapeEqualsConstraint.Object));
            }

            //sh:hasValue (accepted occurrences: N)
            RDFGraph shapeHasValueConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.HAS_VALUE);

            foreach (RDFTriple shapeHasValueConstraint in shapeHasValueConstraints)
            {
                if (shapeHasValueConstraint.Object is RDFResource)
                {
                    shape.AddConstraint(new RDFHasValueConstraint((RDFResource)shapeHasValueConstraint.Object));
                }
                else if (shapeHasValueConstraint.Object is RDFLiteral)
                {
                    shape.AddConstraint(new RDFHasValueConstraint((RDFLiteral)shapeHasValueConstraint.Object));
                }
            }

            //sh:in (accepted occurrences: N)
            RDFGraph shapeInConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.IN);

            foreach (RDFTriple shapeInConstraint in shapeInConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                RDFModelEnums.RDFTripleFlavors shapeInConstraintCollectionFlavor = RDFModelUtilities.DetectCollectionFlavorFromGraph(graph, (RDFResource)shapeInConstraint.Object);
                RDFCollection   shapeInConstraintCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)shapeInConstraint.Object, shapeInConstraintCollectionFlavor);
                RDFInConstraint inConstraint = new RDFInConstraint(shapeInConstraintCollection.ItemType);
                shapeInConstraintCollection.Items.ForEach(item =>
                {
                    if (item is RDFResource)
                    {
                        inConstraint.AddValue((RDFResource)item);
                    }
                    else if (item is RDFLiteral)
                    {
                        inConstraint.AddValue((RDFLiteral)item);
                    }
                });
                shape.AddConstraint(inConstraint);
            }

            //sh:languageIn (accepted occurrences: N)
            RDFGraph shapeLanguageInConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.LANGUAGE_IN);

            foreach (RDFTriple shapeLanguageInConstraint in shapeLanguageInConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                RDFCollection shapeLanguageInConstraintCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)shapeLanguageInConstraint.Object, RDFModelEnums.RDFTripleFlavors.SPL);
                shape.AddConstraint(new RDFLanguageInConstraint(shapeLanguageInConstraintCollection.Select(x => x.ToString()).ToList()));
            }

            //sh:lessThan (accepted occurrences: N)
            RDFGraph shapeLessThanConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.LESS_THAN);

            foreach (RDFTriple shapeLessThanConstraint in shapeLessThanConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFLessThanConstraint((RDFResource)shapeLessThanConstraint.Object));
            }

            //sh:lessThanOrEquals (accepted occurrences: N)
            RDFGraph shapeLessThanOrEqualsConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.LESS_THAN_OR_EQUALS);

            foreach (RDFTriple shapeLessThanOrEqualsConstraint in shapeLessThanOrEqualsConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFLessThanOrEqualsConstraint((RDFResource)shapeLessThanOrEqualsConstraint.Object));
            }

            //sh:maxCount (accepted occurrences: 1)
            RDFTriple shapeMaxCountConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MAX_COUNT).FirstOrDefault();

            if (shapeMaxCountConstraint != null)
            {
                if (shapeMaxCountConstraint.Object is RDFTypedLiteral shaclMaxCountConstraintLiteral &&
                    shaclMaxCountConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMaxCountConstraint(int.Parse(shaclMaxCountConstraintLiteral.Value)));
                }
            }

            //sh:maxExclusive (accepted occurrences: 1)
            RDFTriple shapeMaxExclusiveConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MAX_EXCLUSIVE).FirstOrDefault();

            if (shapeMaxExclusiveConstraint != null)
            {
                if (shapeMaxExclusiveConstraint.Object is RDFResource)
                {
                    shape.AddConstraint(new RDFMaxExclusiveConstraint((RDFResource)shapeMaxExclusiveConstraint.Object));
                }
                else if (shapeMaxExclusiveConstraint.Object is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMaxExclusiveConstraint((RDFLiteral)shapeMaxExclusiveConstraint.Object));
                }
            }

            //sh:maxInclusive (accepted occurrences: 1)
            RDFTriple shapeMaxInclusiveConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MAX_INCLUSIVE).FirstOrDefault();

            if (shapeMaxInclusiveConstraint != null)
            {
                if (shapeMaxInclusiveConstraint.Object is RDFResource)
                {
                    shape.AddConstraint(new RDFMaxInclusiveConstraint((RDFResource)shapeMaxInclusiveConstraint.Object));
                }
                else if (shapeMaxInclusiveConstraint.Object is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMaxInclusiveConstraint((RDFLiteral)shapeMaxInclusiveConstraint.Object));
                }
            }

            //sh:maxLength (accepted occurrences: 1)
            RDFTriple shapeMaxLengthConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MAX_LENGTH).FirstOrDefault();

            if (shapeMaxLengthConstraint != null)
            {
                if (shapeMaxLengthConstraint.Object is RDFTypedLiteral shaclMaxLengthConstraintLiteral &&
                    shaclMaxLengthConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMaxLengthConstraint(int.Parse(shaclMaxLengthConstraintLiteral.Value)));
                }
            }

            //sh:minCount (accepted occurrences: 1)
            RDFTriple shapeMinCountConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MIN_COUNT).FirstOrDefault();

            if (shapeMinCountConstraint != null)
            {
                if (shapeMinCountConstraint.Object is RDFTypedLiteral shaclMinCountConstraintLiteral &&
                    shaclMinCountConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMinCountConstraint(int.Parse(shaclMinCountConstraintLiteral.Value)));
                }
            }

            //sh:minExclusive (accepted occurrences: 1)
            RDFTriple shapeMinExclusiveConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MIN_EXCLUSIVE).FirstOrDefault();

            if (shapeMinExclusiveConstraint != null)
            {
                if (shapeMinExclusiveConstraint.Object is RDFResource)
                {
                    shape.AddConstraint(new RDFMinExclusiveConstraint((RDFResource)shapeMinExclusiveConstraint.Object));
                }
                else if (shapeMinExclusiveConstraint.Object is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMinExclusiveConstraint((RDFLiteral)shapeMinExclusiveConstraint.Object));
                }
            }

            //sh:minInclusive (accepted occurrences: 1)
            RDFTriple shapeMinInclusiveConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MIN_INCLUSIVE).FirstOrDefault();

            if (shapeMinInclusiveConstraint != null)
            {
                if (shapeMinInclusiveConstraint.Object is RDFResource)
                {
                    shape.AddConstraint(new RDFMinInclusiveConstraint((RDFResource)shapeMinInclusiveConstraint.Object));
                }
                else if (shapeMinInclusiveConstraint.Object is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMinInclusiveConstraint((RDFLiteral)shapeMinInclusiveConstraint.Object));
                }
            }

            //sh:minLength (accepted occurrences: 1)
            RDFTriple shapeMinLengthConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.MIN_LENGTH).FirstOrDefault();

            if (shapeMinLengthConstraint != null)
            {
                if (shapeMinLengthConstraint.Object is RDFTypedLiteral shaclMinLengthConstraintLiteral &&
                    shaclMinLengthConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMinLengthConstraint(int.Parse(shaclMinLengthConstraintLiteral.Value)));
                }
            }

            //sh:node (accepted occurrences: N)
            RDFGraph shapeNodeConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.NODE);

            foreach (RDFTriple shapeNodeConstraint in shapeNodeConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFNodeConstraint((RDFResource)shapeNodeConstraint.Object));
            }

            //sh:nodeKind (accepted occurrences: 1)
            RDFTriple shapeNodeKindConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.NODE_KIND).FirstOrDefault();

            if (shapeNodeKindConstraint != null)
            {
                if (shapeNodeKindConstraint.Object.Equals(RDFVocabulary.SHACL.BLANK_NODE))
                {
                    shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.BlankNode));
                }
                else if (shapeNodeKindConstraint.Object.Equals(RDFVocabulary.SHACL.BLANK_NODE_OR_IRI))
                {
                    shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.BlankNodeOrIRI));
                }
                else if (shapeNodeKindConstraint.Object.Equals(RDFVocabulary.SHACL.BLANK_NODE_OR_LITERAL))
                {
                    shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.BlankNodeOrLiteral));
                }
                else if (shapeNodeKindConstraint.Object.Equals(RDFVocabulary.SHACL.IRI))
                {
                    shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.IRI));
                }
                else if (shapeNodeKindConstraint.Object.Equals(RDFVocabulary.SHACL.IRI_OR_LITERAL))
                {
                    shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.IRIOrLiteral));
                }
                else if (shapeNodeKindConstraint.Object.Equals(RDFVocabulary.SHACL.LITERAL))
                {
                    shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.Literal));
                }
            }

            //sh:not (accepted occurrences: N)
            RDFGraph shapeNotConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.NOT);

            foreach (RDFTriple shapeNotConstraint in shapeNotConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFNotConstraint((RDFResource)shapeNotConstraint.Object));
            }

            //sh:or (accepted occurrences: N)
            RDFGraph shapeOrConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.OR);

            foreach (RDFTriple shapeOrConstraint in shapeOrConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                RDFOrConstraint orConstraint           = new RDFOrConstraint();
                RDFCollection   orConstraintCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)shapeOrConstraint.Object, RDFModelEnums.RDFTripleFlavors.SPO);
                orConstraintCollection.Items.ForEach(item => orConstraint.AddShape((RDFResource)item));
                shape.AddConstraint(orConstraint);
            }

            //sh:pattern (accepted occurrences: 1)
            RDFTriple shapePatternConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.PATTERN).FirstOrDefault();

            if (shapePatternConstraint != null)
            {
                if (shapePatternConstraint.Object is RDFTypedLiteral shapePatternConstraintLiteral &&
                    shapePatternConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_STRING))
                {
                    //sh:flags (accepted occurrences: 1)
                    RegexOptions regexOptions         = RegexOptions.None;
                    RDFTriple    shapeFlagsConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.FLAGS).FirstOrDefault();
                    if (shapeFlagsConstraint != null)
                    {
                        if (shapeFlagsConstraint.Object is RDFTypedLiteral shapeFlagsConstraintLiteral &&
                            shapeFlagsConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_STRING))
                        {
                            if (shapeFlagsConstraintLiteral.Value.Contains("i"))
                            {
                                regexOptions |= RegexOptions.IgnoreCase;
                            }
                            if (shapeFlagsConstraintLiteral.Value.Contains("s"))
                            {
                                regexOptions |= RegexOptions.Singleline;
                            }
                            if (shapeFlagsConstraintLiteral.Value.Contains("m"))
                            {
                                regexOptions |= RegexOptions.Multiline;
                            }
                            if (shapeFlagsConstraintLiteral.Value.Contains("x"))
                            {
                                regexOptions |= RegexOptions.IgnorePatternWhitespace;
                            }
                        }
                    }
                    shape.AddConstraint(new RDFPatternConstraint(new Regex(shapePatternConstraintLiteral.Value, regexOptions)));
                }
            }

            //sh:property (accepted occurrences: N)
            RDFGraph shapePropertyConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.PROPERTY);

            foreach (RDFTriple shapePropertyConstraint in shapePropertyConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                shape.AddConstraint(new RDFPropertyConstraint((RDFResource)shapePropertyConstraint.Object));
            }

            //sh:qualifiedValueShape (accepted occurrences: 1)
            RDFTriple shapeQualifiedValueConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.QUALIFIED_VALUE_SHAPE).FirstOrDefault();

            if (shapeQualifiedValueConstraint != null)
            {
                if (shapeQualifiedValueConstraint.Object is RDFResource)
                {
                    //sh:qualifiedMinCount (accepted occurrences: 1)
                    int?      qualifiedMinCountValue           = null;
                    RDFTriple shapeQualifiedMinCountConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.QUALIFIED_MIN_COUNT).FirstOrDefault();
                    if (shapeQualifiedMinCountConstraint != null)
                    {
                        if (shapeQualifiedMinCountConstraint.Object is RDFTypedLiteral shapeQualifiedMinCountConstraintLiteral &&
                            shapeQualifiedMinCountConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                        {
                            qualifiedMinCountValue = int.Parse(shapeQualifiedMinCountConstraintLiteral.Value);
                        }
                    }

                    //sh:qualifiedMaxCount (accepted occurrences: 1)
                    int?      qualifiedMaxCountValue           = null;
                    RDFTriple shapeQualifiedMaxCountConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.QUALIFIED_MAX_COUNT).FirstOrDefault();
                    if (shapeQualifiedMaxCountConstraint != null)
                    {
                        if (shapeQualifiedMaxCountConstraint.Object is RDFTypedLiteral shapeQualifiedMaxCountConstraintLiteral &&
                            shapeQualifiedMaxCountConstraintLiteral.Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                        {
                            qualifiedMaxCountValue = int.Parse(shapeQualifiedMaxCountConstraintLiteral.Value);
                        }
                    }

                    shape.AddConstraint(new RDFQualifiedValueShapeConstraint((RDFResource)shapeQualifiedValueConstraint.Object, qualifiedMinCountValue, qualifiedMaxCountValue));
                }
            }

            //sh:uniqueLang (accepted occurrences: 1)
            RDFTriple shapeUniqueLangConstraint = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.UNIQUE_LANG).FirstOrDefault();

            if (shapeUniqueLangConstraint != null)
            {
                if (shapeUniqueLangConstraint.Object is RDFTypedLiteral shapeUniqueLangConstraintLiteral &&
                    shapeUniqueLangConstraintLiteral.HasBooleanDatatype())
                {
                    shape.AddConstraint(new RDFUniqueLangConstraint(bool.Parse(shapeUniqueLangConstraintLiteral.Value)));
                }
            }

            //sh:xone (accepted occurrences: N)
            RDFGraph shapeXoneConstraints = shapeDefinition.SelectTriplesByPredicate(RDFVocabulary.SHACL.XONE);

            foreach (RDFTriple shapeXoneConstraint in shapeXoneConstraints.Where(t => t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO))
            {
                RDFXoneConstraint xoneConstraint           = new RDFXoneConstraint();
                RDFCollection     xoneConstraintCollection = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)shapeXoneConstraint.Object, RDFModelEnums.RDFTripleFlavors.SPO);
                xoneConstraintCollection.Items.ForEach(item => xoneConstraint.AddShape((RDFResource)item));
                shape.AddConstraint(xoneConstraint);
            }
        }
Exemple #9
0
        private static void ParseShapeConstraints(DataRow shapesRow, RDFGraph graph, RDFShape shape)
        {
            //sh:and
            if (!shapesRow.IsNull("?AND"))
            {
                RDFPatternMember reifSubj = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?AND"));
                if (reifSubj is RDFResource)
                {
                    RDFAndConstraint andConstraint = new RDFAndConstraint();
                    RDFCollection    andColl       = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)reifSubj, RDFModelEnums.RDFTripleFlavors.SPO);
                    andColl.Items.ForEach(item => {
                        andConstraint.AddShape((RDFResource)item);
                    });
                    shape.AddConstraint(andConstraint);
                }
            }

            //sh:class
            if (!shapesRow.IsNull("?CLASS"))
            {
                RDFPatternMember cls = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?CLASS"));
                if (cls is RDFResource)
                {
                    shape.AddConstraint(new RDFClassConstraint((RDFResource)cls));
                }
            }

            //sh:closed
            if (!shapesRow.IsNull("?CLOSED"))
            {
                RDFPatternMember closed = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?CLOSED"));
                if (closed is RDFTypedLiteral &&
                    ((RDFTypedLiteral)closed).HasBooleanDatatype())
                {
                    RDFClosedConstraint closedConstraint = new RDFClosedConstraint(Boolean.Parse(((RDFTypedLiteral)closed).Value));

                    //sh:ignoredProperties
                    RDFPatternMember reifSubj = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?IGNOREDPROPERTIES"));
                    if (reifSubj is RDFResource)
                    {
                        RDFCollection ignoredPropsColl = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)reifSubj, RDFModelEnums.RDFTripleFlavors.SPO);
                        ignoredPropsColl.Items.ForEach(item => {
                            closedConstraint.AddIgnoredProperty((RDFResource)item);
                        });
                    }

                    shape.AddConstraint(closedConstraint);
                }
            }

            //sh:datatype
            if (!shapesRow.IsNull("?DATATYPE"))
            {
                RDFPatternMember datatype = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?DATATYPE"));
                if (datatype is RDFResource)
                {
                    shape.AddConstraint(new RDFDatatypeConstraint(RDFModelUtilities.GetDatatypeFromString(datatype.ToString())));
                }
            }

            //sh:disjoint
            if (!shapesRow.IsNull("?DISJOINT"))
            {
                RDFPatternMember disjpred = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?DISJOINT"));
                if (disjpred is RDFResource)
                {
                    shape.AddConstraint(new RDFDisjointConstraint((RDFResource)disjpred));
                }
            }

            //sh:equals
            if (!shapesRow.IsNull("?EQUALS"))
            {
                RDFPatternMember eqpred = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?EQUALS"));
                if (eqpred is RDFResource)
                {
                    shape.AddConstraint(new RDFEqualsConstraint((RDFResource)eqpred));
                }
            }

            //sh:hasValue
            if (!shapesRow.IsNull("?HASVALUE"))
            {
                RDFPatternMember value = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?HASVALUE"));
                if (value is RDFResource)
                {
                    shape.AddConstraint(new RDFHasValueConstraint((RDFResource)value));
                }
                else if (value is RDFLiteral)
                {
                    shape.AddConstraint(new RDFHasValueConstraint((RDFLiteral)value));
                }
            }

            //sh:in
            if (!shapesRow.IsNull("?IN"))
            {
                RDFPatternMember reifSubj = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?IN"));
                if (reifSubj is RDFResource)
                {
                    RDFModelEnums.RDFTripleFlavors inCollFlavor = RDFModelUtilities.DetectCollectionFlavorFromGraph(graph, (RDFResource)reifSubj);
                    RDFCollection   inColl       = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)reifSubj, inCollFlavor);
                    RDFInConstraint inConstraint = new RDFInConstraint(inColl.ItemType);
                    inColl.Items.ForEach(item => {
                        if (inColl.ItemType == RDFModelEnums.RDFItemTypes.Literal)
                        {
                            inConstraint.AddValue((RDFLiteral)item);
                        }
                        else
                        {
                            inConstraint.AddValue((RDFResource)item);
                        }
                    });
                    shape.AddConstraint(inConstraint);
                }
            }

            //sh:languageIn
            if (!shapesRow.IsNull("?LANGUAGEIN"))
            {
                RDFPatternMember reifSubj = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?LANGUAGEIN"));
                if (reifSubj is RDFResource)
                {
                    RDFCollection langTagsColl = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)reifSubj, RDFModelEnums.RDFTripleFlavors.SPL);
                    shape.AddConstraint(new RDFLanguageInConstraint(langTagsColl.Select(x => x.ToString()).ToList()));
                }
            }

            //sh:lessThan
            if (!shapesRow.IsNull("?LESSTHAN"))
            {
                RDFPatternMember ltpred = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?LESSTHAN"));
                if (ltpred is RDFResource)
                {
                    shape.AddConstraint(new RDFLessThanConstraint((RDFResource)ltpred));
                }
            }

            //sh:lessThanOrEquals
            if (!shapesRow.IsNull("?LESSTHANOREQUALS"))
            {
                RDFPatternMember lteqpred = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?LESSTHANOREQUALS"));
                if (lteqpred is RDFResource)
                {
                    shape.AddConstraint(new RDFLessThanOrEqualsConstraint((RDFResource)lteqpred));
                }
            }

            //sh:maxCount
            if (!shapesRow.IsNull("?MAXCOUNT"))
            {
                RDFPatternMember maxCount = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MAXCOUNT"));
                if (maxCount is RDFTypedLiteral && ((RDFTypedLiteral)maxCount).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMaxCountConstraint(int.Parse(((RDFTypedLiteral)maxCount).Value)));
                }
            }

            //sh:maxExclusive
            if (!shapesRow.IsNull("?MAXEXCLUSIVE"))
            {
                RDFPatternMember value = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MAXEXCLUSIVE"));
                if (value is RDFResource)
                {
                    shape.AddConstraint(new RDFMaxExclusiveConstraint((RDFResource)value));
                }
                else if (value is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMaxExclusiveConstraint((RDFLiteral)value));
                }
            }

            //sh:maxInclusive
            if (!shapesRow.IsNull("?MAXINCLUSIVE"))
            {
                RDFPatternMember value = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MAXINCLUSIVE"));
                if (value is RDFResource)
                {
                    shape.AddConstraint(new RDFMaxInclusiveConstraint((RDFResource)value));
                }
                else if (value is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMaxInclusiveConstraint((RDFLiteral)value));
                }
            }

            //sh:maxLength
            if (!shapesRow.IsNull("?MAXLENGTH"))
            {
                RDFPatternMember maxLength = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MAXLENGTH"));
                if (maxLength is RDFTypedLiteral && ((RDFTypedLiteral)maxLength).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMaxLengthConstraint(int.Parse(((RDFTypedLiteral)maxLength).Value)));
                }
            }

            //sh:minCount
            if (!shapesRow.IsNull("?MINCOUNT"))
            {
                RDFPatternMember minCount = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MINCOUNT"));
                if (minCount is RDFTypedLiteral && ((RDFTypedLiteral)minCount).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMinCountConstraint(int.Parse(((RDFTypedLiteral)minCount).Value)));
                }
            }

            //sh:minExclusive
            if (!shapesRow.IsNull("?MINEXCLUSIVE"))
            {
                RDFPatternMember value = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MINEXCLUSIVE"));
                if (value is RDFResource)
                {
                    shape.AddConstraint(new RDFMinExclusiveConstraint((RDFResource)value));
                }
                else if (value is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMinExclusiveConstraint((RDFLiteral)value));
                }
            }

            //sh:minInclusive
            if (!shapesRow.IsNull("?MININCLUSIVE"))
            {
                RDFPatternMember value = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MININCLUSIVE"));
                if (value is RDFResource)
                {
                    shape.AddConstraint(new RDFMinInclusiveConstraint((RDFResource)value));
                }
                else if (value is RDFLiteral)
                {
                    shape.AddConstraint(new RDFMinInclusiveConstraint((RDFLiteral)value));
                }
            }

            //sh:minLength
            if (!shapesRow.IsNull("?MINLENGTH"))
            {
                RDFPatternMember minLength = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MINLENGTH"));
                if (minLength is RDFTypedLiteral && ((RDFTypedLiteral)minLength).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                {
                    shape.AddConstraint(new RDFMinLengthConstraint(int.Parse(((RDFTypedLiteral)minLength).Value)));
                }
            }

            //sh:node
            if (!shapesRow.IsNull("?NODE"))
            {
                RDFPatternMember nodeshapeUri = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?NODE"));
                if (nodeshapeUri is RDFResource)
                {
                    shape.AddConstraint(new RDFNodeConstraint((RDFResource)nodeshapeUri));
                }
            }

            //sh:nodeKind
            if (!shapesRow.IsNull("?NODEKIND"))
            {
                RDFPatternMember nodeKind = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?NODEKIND"));
                if (nodeKind is RDFResource)
                {
                    if (nodeKind.Equals(RDFVocabulary.SHACL.BLANK_NODE))
                    {
                        shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.BlankNode));
                    }
                    else if (nodeKind.Equals(RDFVocabulary.SHACL.BLANK_NODE_OR_IRI))
                    {
                        shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.BlankNodeOrIRI));
                    }
                    else if (nodeKind.Equals(RDFVocabulary.SHACL.BLANK_NODE_OR_LITERAL))
                    {
                        shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.BlankNodeOrLiteral));
                    }
                    else if (nodeKind.Equals(RDFVocabulary.SHACL.IRI))
                    {
                        shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.IRI));
                    }
                    else if (nodeKind.Equals(RDFVocabulary.SHACL.IRI_OR_LITERAL))
                    {
                        shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.IRIOrLiteral));
                    }
                    else if (nodeKind.Equals(RDFVocabulary.SHACL.LITERAL))
                    {
                        shape.AddConstraint(new RDFNodeKindConstraint(RDFValidationEnums.RDFNodeKinds.Literal));
                    }
                }
            }

            //sh:not
            if (!shapesRow.IsNull("?NOT"))
            {
                RDFPatternMember notshapeUri = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?NOT"));
                if (notshapeUri is RDFResource)
                {
                    shape.AddConstraint(new RDFNotConstraint((RDFResource)notshapeUri));
                }
            }

            //sh:or
            if (!shapesRow.IsNull("?OR"))
            {
                RDFPatternMember reifSubj = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?OR"));
                if (reifSubj is RDFResource)
                {
                    RDFOrConstraint orConstraint = new RDFOrConstraint();
                    RDFCollection   orColl       = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)reifSubj, RDFModelEnums.RDFTripleFlavors.SPO);
                    orColl.Items.ForEach(item => {
                        orConstraint.AddShape((RDFResource)item);
                    });
                    shape.AddConstraint(orConstraint);
                }
            }

            //sh:pattern
            if (!shapesRow.IsNull("?PATTERN"))
            {
                RDFPatternMember regexPattern = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?PATTERN"));
                if (regexPattern is RDFTypedLiteral && ((RDFTypedLiteral)regexPattern).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_STRING))
                {
                    //sh:flags
                    RegexOptions regexOptions = RegexOptions.None;
                    if (!shapesRow.IsNull("?FLAGS"))
                    {
                        RDFPatternMember regexFlags = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?FLAGS"));
                        if (regexFlags is RDFTypedLiteral && ((RDFTypedLiteral)regexFlags).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_STRING))
                        {
                            if (((RDFTypedLiteral)regexFlags).Value.Contains("i"))
                            {
                                regexOptions |= RegexOptions.IgnoreCase;
                            }
                            if (((RDFTypedLiteral)regexFlags).Value.Contains("s"))
                            {
                                regexOptions |= RegexOptions.Singleline;
                            }
                            if (((RDFTypedLiteral)regexFlags).Value.Contains("m"))
                            {
                                regexOptions |= RegexOptions.Multiline;
                            }
                            if (((RDFTypedLiteral)regexFlags).Value.Contains("x"))
                            {
                                regexOptions |= RegexOptions.IgnorePatternWhitespace;
                            }
                        }
                    }
                    shape.AddConstraint(new RDFPatternConstraint(new Regex(((RDFTypedLiteral)regexPattern).Value, regexOptions)));
                }
            }

            //sh:property
            if (!shapesRow.IsNull("?PROPERTY"))
            {
                RDFPatternMember propertyshapeUri = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?PROPERTY"));
                if (propertyshapeUri is RDFResource)
                {
                    shape.AddConstraint(new RDFPropertyConstraint((RDFResource)propertyshapeUri));
                }
            }

            //sh:qualifiedValueShape
            if (!shapesRow.IsNull("?QUALIFIEDVALUESHAPE"))
            {
                RDFPatternMember qualifiedValueShapeUri = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?QUALIFIEDVALUESHAPE"));
                if (qualifiedValueShapeUri is RDFResource)
                {
                    //sh:qualifiedMinCount
                    int?qualifiedMinCountValue = null;
                    if (!shapesRow.IsNull("?QUALIFIEDMINCOUNT"))
                    {
                        RDFPatternMember qualifiedMinCount = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?QUALIFIEDMINCOUNT"));
                        if (qualifiedMinCount is RDFTypedLiteral && ((RDFTypedLiteral)qualifiedMinCount).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                        {
                            qualifiedMinCountValue = int.Parse(((RDFTypedLiteral)qualifiedMinCount).Value);
                        }
                    }
                    //sh:qualifiedMaxCount
                    int?qualifiedMaxCountValue = null;
                    if (!shapesRow.IsNull("?QUALIFIEDMAXCOUNT"))
                    {
                        RDFPatternMember qualifiedMaxCount = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?QUALIFIEDMAXCOUNT"));
                        if (qualifiedMaxCount is RDFTypedLiteral && ((RDFTypedLiteral)qualifiedMaxCount).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                        {
                            qualifiedMaxCountValue = int.Parse(((RDFTypedLiteral)qualifiedMaxCount).Value);
                        }
                    }
                    shape.AddConstraint(new RDFQualifiedValueShapeConstraint((RDFResource)qualifiedValueShapeUri, qualifiedMinCountValue, qualifiedMaxCountValue));
                }
            }

            //sh:uniqueLang
            if (!shapesRow.IsNull("?UNIQUELANG"))
            {
                RDFPatternMember uniqueLang = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?UNIQUELANG"));
                if (uniqueLang is RDFTypedLiteral &&
                    ((RDFTypedLiteral)uniqueLang).HasBooleanDatatype())
                {
                    shape.AddConstraint(new RDFUniqueLangConstraint(Boolean.Parse(((RDFTypedLiteral)uniqueLang).Value)));
                }
            }

            //sh:xone
            if (!shapesRow.IsNull("?XONE"))
            {
                RDFPatternMember reifSubj = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?XONE"));
                if (reifSubj is RDFResource)
                {
                    RDFXoneConstraint xoneConstraint = new RDFXoneConstraint();
                    RDFCollection     xoneColl       = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)reifSubj, RDFModelEnums.RDFTripleFlavors.SPO);
                    xoneColl.Items.ForEach(item => {
                        xoneConstraint.AddShape((RDFResource)item);
                    });
                    shape.AddConstraint(xoneConstraint);
                }
            }
        }