Ejemplo n.º 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();

            #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);
        }
        /// <summary>
        /// Gets the value nodes of the given focus node
        /// </summary>
        internal static List <RDFPatternMember> GetValueNodesOf(this RDFGraph dataGraph, RDFShape shape, RDFPatternMember focusNode)
        {
            List <RDFPatternMember> result = new List <RDFPatternMember>();

            if (shape != null && dataGraph != null)
            {
                switch (shape)
                {
                //sh:NodeShape
                case RDFNodeShape nodeShape:
                    result.Add(focusNode);
                    break;

                //sh:PropertyShape
                case RDFPropertyShape propertyShape:
                    if (focusNode is RDFResource)
                    {
                        foreach (var triple in dataGraph.SelectTriplesBySubject((RDFResource)focusNode)
                                 .SelectTriplesByPredicate(((RDFPropertyShape)shape).Path))
                        {
                            result.Add(triple.Object);
                        }
                    }
                    break;
                }
            }
            return(RDFQueryUtilities.RemoveDuplicates(result));
        }
Ejemplo n.º 3
0
        private static RDFShape ParseShapeType(DataRow shapesRow)
        {
            RDFShape shape = null;

            //sh:NodeShape
            if (!shapesRow.IsNull("?NSHAPE"))
            {
                shape = new RDFNodeShape(new RDFResource(shapesRow.Field <string>("?NSHAPE")));
            }

            //sh:PropertyShape
            else if (!shapesRow.IsNull("?PSHAPE") && !shapesRow.IsNull("?PATH"))
            {
                shape = new RDFPropertyShape(new RDFResource(shapesRow.Field <string>("?PSHAPE")), new RDFResource(shapesRow.Field <string>("?PATH")));

                //sh:description
                if (!shapesRow.IsNull("?DESCRIPTION"))
                {
                    RDFPatternMember description = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?DESCRIPTION"));
                    if (description is RDFLiteral)
                    {
                        ((RDFPropertyShape)shape).AddDescription((RDFLiteral)description);
                    }
                }

                //sh:name
                if (!shapesRow.IsNull("?NAME"))
                {
                    RDFPatternMember name = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?NAME"));
                    if (name is RDFLiteral)
                    {
                        ((RDFPropertyShape)shape).AddName((RDFLiteral)name);
                    }
                }

                //sh:group
                if (!shapesRow.IsNull("?GROUP"))
                {
                    RDFPatternMember group = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?GROUP"));
                    if (group is RDFResource)
                    {
                        ((RDFPropertyShape)shape).SetGroup((RDFResource)group);
                    }
                }

                //sh:order
                if (!shapesRow.IsNull("?ORDER"))
                {
                    RDFPatternMember order = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?ORDER"));
                    if (order is RDFTypedLiteral && ((RDFTypedLiteral)order).Datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER))
                    {
                        ((RDFPropertyShape)shape).SetOrder(int.Parse(((RDFTypedLiteral)order).Value));
                    }
                }
            }

            return(shape);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Compacts the reified quadruples by removing their 4 standard statements
        /// </summary>
        public override RDFStore UnreifyQuadruples()
        {
            //Create SPARQL SELECT query for detecting reified quadruples
            var T = new RDFVariable("T", true);
            var C = new RDFVariable("C", true);
            var S = new RDFVariable("S", true);
            var P = new RDFVariable("P", true);
            var O = new RDFVariable("O", true);
            var Q = new RDFSelectQuery()
                    .AddPatternGroup(new RDFPatternGroup("UnreifyQuadruples")
                                     .AddPattern(new RDFPattern(C, T, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT))
                                     .AddPattern(new RDFPattern(C, T, RDFVocabulary.RDF.SUBJECT, S))
                                     .AddPattern(new RDFPattern(C, T, RDFVocabulary.RDF.PREDICATE, P))
                                     .AddPattern(new RDFPattern(C, T, RDFVocabulary.RDF.OBJECT, O))
                                     .AddFilter(new RDFIsUriFilter(C))
                                     .AddFilter(new RDFIsUriFilter(T))
                                     .AddFilter(new RDFIsUriFilter(S))
                                     .AddFilter(new RDFIsUriFilter(P))
                                     );

            //Apply it to the store
            var R = Q.ApplyToStore(this);

            //Iterate results
            var reifiedQuadruples = R.SelectResults.Rows.GetEnumerator();

            while (reifiedQuadruples.MoveNext())
            {
                //Get reification data (T, C, S, P, O)
                var tRepresent = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedQuadruples.Current)["?T"].ToString());
                var tContext   = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedQuadruples.Current)["?C"].ToString());
                var tSubject   = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedQuadruples.Current)["?S"].ToString());
                var tPredicate = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedQuadruples.Current)["?P"].ToString());
                var tObject    = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedQuadruples.Current)["?O"].ToString());

                //Cleanup store from detected reifications
                if (tObject is RDFResource)
                {
                    this.AddQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tSubject, (RDFResource)tPredicate, (RDFResource)tObject));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.SUBJECT, (RDFResource)tSubject));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.PREDICATE, (RDFResource)tPredicate));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.OBJECT, (RDFResource)tObject));
                }
                else
                {
                    this.AddQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tSubject, (RDFResource)tPredicate, (RDFLiteral)tObject));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.SUBJECT, (RDFResource)tSubject));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.PREDICATE, (RDFResource)tPredicate));
                    this.RemoveQuadruple(new RDFQuadruple(new RDFContext(((RDFResource)tContext).URI), (RDFResource)tRepresent, RDFVocabulary.RDF.OBJECT, (RDFLiteral)tObject));
                }
            }

            return(this);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Compacts the reified triples by removing their 4 standard statements
        /// </summary>
        public void UnreifyTriples()
        {
            //Create SPARQL SELECT query for detecting reified triples
            var T = new RDFVariable("T");
            var S = new RDFVariable("S");
            var P = new RDFVariable("P");
            var O = new RDFVariable("O");
            var Q = new RDFSelectQuery()
                    .AddPatternGroup(new RDFPatternGroup("UnreifyTriples")
                                     .AddPattern(new RDFPattern(T, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT))
                                     .AddPattern(new RDFPattern(T, RDFVocabulary.RDF.SUBJECT, S))
                                     .AddPattern(new RDFPattern(T, RDFVocabulary.RDF.PREDICATE, P))
                                     .AddPattern(new RDFPattern(T, RDFVocabulary.RDF.OBJECT, O))
                                     .AddFilter(new RDFIsUriFilter(T))
                                     .AddFilter(new RDFIsUriFilter(S))
                                     .AddFilter(new RDFIsUriFilter(P))
                                     );

            //Apply it to the graph
            var R = Q.ApplyToGraph(this);

            //Iterate results
            var reifiedTriples = R.SelectResults.Rows.GetEnumerator();

            while (reifiedTriples.MoveNext())
            {
                //Get reification data (T, S, P, O)
                var tRepresent = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedTriples.Current)["?T"].ToString());
                var tSubject   = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedTriples.Current)["?S"].ToString());
                var tPredicate = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedTriples.Current)["?P"].ToString());
                var tObject    = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedTriples.Current)["?O"].ToString());

                //Cleanup graph from detected reifications
                if (tObject is RDFResource)
                {
                    this.AddTriple(new RDFTriple((RDFResource)tSubject, (RDFResource)tPredicate, (RDFResource)tObject));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.SUBJECT, (RDFResource)tSubject));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.PREDICATE, (RDFResource)tPredicate));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.OBJECT, (RDFResource)tObject));
                }
                else
                {
                    this.AddTriple(new RDFTriple((RDFResource)tSubject, (RDFResource)tPredicate, (RDFLiteral)tObject));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.SUBJECT, (RDFResource)tSubject));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.PREDICATE, (RDFResource)tPredicate));
                    this.RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.OBJECT, (RDFLiteral)tObject));
                }
            }
        }
        /// <summary>
        /// Gets the focus nodes of the given shape
        /// </summary>
        internal static List <RDFPatternMember> GetFocusNodesOf(this RDFGraph dataGraph, RDFShape shape)
        {
            List <RDFPatternMember> result = new List <RDFPatternMember>();

            if (shape != null && dataGraph != null)
            {
                foreach (RDFTarget target in shape.Targets)
                {
                    switch (target)
                    {
                    //sh:targetClass
                    case RDFTargetClass targetClass:
                        result.AddRange(dataGraph.GetInstancesOfClass(target.TargetValue)
                                        .OfType <RDFResource>());
                        break;

                    //sh:targetNode
                    case RDFTargetNode targetNode:
                        result.Add(target.TargetValue);
                        break;

                    //sh:targetSubjectsOf
                    case RDFTargetSubjectsOf targetSubjectsOf:
                        result.AddRange(dataGraph.SelectTriplesByPredicate(target.TargetValue)
                                        .Select(x => x.Subject)
                                        .OfType <RDFResource>());
                        break;

                    //sh:targetObjectsOf
                    case RDFTargetObjectsOf targetObjectsOf:
                        result.AddRange(dataGraph.SelectTriplesByPredicate(target.TargetValue)
                                        .Select(x => x.Object)
                                        .OfType <RDFResource>());
                        break;
                    }
                }
            }
            return(RDFQueryUtilities.RemoveDuplicates(result));
        }
Ejemplo 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();

            #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);
        }
Ejemplo n.º 8
0
        private static void ParseShapeAttributes(DataRow shapesRow, RDFShape shape)
        {
            //sh:severity
            if (!shapesRow.IsNull("?SEVERITY"))
            {
                if (shapesRow.Field <string>("?SEVERITY").Equals(RDFVocabulary.SHACL.INFO.ToString()))
                {
                    shape.SetSeverity(RDFValidationEnums.RDFShapeSeverity.Info);
                }
                else if (shapesRow.Field <string>("?SEVERITY").Equals(RDFVocabulary.SHACL.WARNING.ToString()))
                {
                    shape.SetSeverity(RDFValidationEnums.RDFShapeSeverity.Warning);
                }
            }

            //sh:deactivated
            if (!shapesRow.IsNull("?DEACTIVATED"))
            {
                RDFPatternMember deactivated = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?DEACTIVATED"));
                if (deactivated is RDFTypedLiteral &&
                    ((RDFTypedLiteral)deactivated).HasBooleanDatatype() &&
                    Boolean.Parse(((RDFTypedLiteral)deactivated).Value))
                {
                    shape.Deactivate();
                }
            }

            //sh:message
            if (!shapesRow.IsNull("?MESSAGE"))
            {
                RDFPatternMember message = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field <string>("?MESSAGE"));
                if (message is RDFLiteral)
                {
                    shape.AddMessage((RDFLiteral)message);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Reads a memory store from a datatable with "Context-Subject-Predicate-Object" columns.
        /// </summary>
        public static RDFMemoryStore FromDataTable(DataTable table)
        {
            RDFMemoryStore result = new RDFMemoryStore();

            //Check the structure of the datatable for consistency against the "C-S-P-O" RDF model
            if (table != null && table.Columns.Count == 4)
            {
                if (table.Columns.Contains("?CONTEXT") &&
                    table.Columns.Contains("?SUBJECT") &&
                    table.Columns.Contains("?PREDICATE") &&
                    table.Columns.Contains("?OBJECT"))
                {
                    //Iterate the rows of the datatable
                    foreach (DataRow tableRow in table.Rows)
                    {
                        #region CONTEXT
                        //Parse the quadruple context
                        if (!tableRow.IsNull("?CONTEXT") && !string.IsNullOrEmpty(tableRow["?CONTEXT"].ToString()))
                        {
                            var rowCont = RDFQueryUtilities.ParseRDFPatternMember(tableRow["?CONTEXT"].ToString());
                            if (rowCont is RDFResource && !((RDFResource)rowCont).IsBlank)
                            {
                                #region SUBJECT
                                //Parse the quadruple subject
                                if (!tableRow.IsNull("?SUBJECT") && !string.IsNullOrEmpty(tableRow["?SUBJECT"].ToString()))
                                {
                                    var rowSubj = RDFQueryUtilities.ParseRDFPatternMember(tableRow["?SUBJECT"].ToString());
                                    if (rowSubj is RDFResource)
                                    {
                                        #region PREDICATE
                                        //Parse the quadruple predicate
                                        if (!tableRow.IsNull("?PREDICATE") && !string.IsNullOrEmpty(tableRow["?PREDICATE"].ToString()))
                                        {
                                            var rowPred = RDFQueryUtilities.ParseRDFPatternMember(tableRow["?PREDICATE"].ToString());
                                            if (rowPred is RDFResource && !((RDFResource)rowPred).IsBlank)
                                            {
                                                #region OBJECT
                                                //Parse the quadruple object
                                                if (!tableRow.IsNull("?OBJECT"))
                                                {
                                                    var rowObj = RDFQueryUtilities.ParseRDFPatternMember(tableRow["?OBJECT"].ToString());
                                                    if (rowObj is RDFResource)
                                                    {
                                                        result.AddQuadruple(new RDFQuadruple(new RDFContext(rowCont.ToString()), (RDFResource)rowSubj, (RDFResource)rowPred, (RDFResource)rowObj));
                                                    }
                                                    else
                                                    {
                                                        result.AddQuadruple(new RDFQuadruple(new RDFContext(rowCont.ToString()), (RDFResource)rowSubj, (RDFResource)rowPred, (RDFLiteral)rowObj));
                                                    }
                                                }
                                                else
                                                {
                                                    throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter contains a row having NULL value in the \"?OBJECT\" column.");
                                                }
                                                #endregion
                                            }
                                            else
                                            {
                                                throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter contains a row having a blank resource or a literal in the \"?PREDICATE\" column.");
                                            }
                                        }
                                        else
                                        {
                                            throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter contains a row having NULL or empty value in the \"?PREDICATE\" column.");
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter contains a row not having a resource in the \"?SUBJECT\" column.");
                                    }
                                }
                                else
                                {
                                    throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter contains a row having NULL or empty value in the \"?SUBJECT\" column.");
                                }
                                #endregion
                            }
                            else
                            {
                                throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter contains a row having a blank resource or a literal in the \"?CONTEXT\" column.");
                            }
                        }
                        else
                        {
                            throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter contains a row having NULL or empty value in the \"?CONTEXT\" column.");
                        }
                        #endregion
                    }
                }
                else
                {
                    throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter does not have the required columns \"?CONTEXT\", \"?SUBJECT\", \"?PREDICATE\", \"?OBJECT\".");
                }
            }
            else
            {
                throw new RDFStoreException("Cannot read RDF memory store from datatable because given \"table\" parameter is null, or it does not have exactly 4 columns.");
            }

            return(result);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Creates a graph from a datatable with "Subject-Predicate-Object" columns.
        /// </summary>
        public static RDFGraph FromDataTable(DataTable table)
        {
            var result = new RDFGraph();

            //Check the structure of the datatable for consistency against the "S-P-O" RDF model
            if (table != null && table.Columns.Count == 3)
            {
                if (table.Columns.Contains("SUBJECT") && table.Columns.Contains("PREDICATE") && table.Columns.Contains("OBJECT"))
                {
                    #region CONTEXT
                    //Parse the name of the datatable for Uri, in order to assign the graph name
                    Uri graphUri;
                    if (Uri.TryCreate(table.TableName, UriKind.Absolute, out graphUri))
                    {
                        result.SetContext(graphUri);
                    }
                    #endregion

                    //Iterate the rows of the datatable
                    foreach (DataRow tableRow in table.Rows)
                    {
                        #region SUBJECT
                        //Parse the triple subject
                        if (!tableRow.IsNull("SUBJECT") && tableRow["SUBJECT"].ToString() != String.Empty)
                        {
                            var rowSubj = RDFQueryUtilities.ParseRDFPatternMember(tableRow["SUBJECT"].ToString());
                            if (rowSubj is RDFResource)
                            {
                                #region PREDICATE
                                //Parse the triple predicate
                                if (!tableRow.IsNull("PREDICATE") && tableRow["PREDICATE"].ToString() != String.Empty)
                                {
                                    var rowPred = RDFQueryUtilities.ParseRDFPatternMember(tableRow["PREDICATE"].ToString());
                                    if (rowPred is RDFResource && !((RDFResource)rowPred).IsBlank)
                                    {
                                        #region OBJECT
                                        //Parse the triple object
                                        if (!tableRow.IsNull("OBJECT"))
                                        {
                                            var rowObj = RDFQueryUtilities.ParseRDFPatternMember(tableRow["OBJECT"].ToString());
                                            if (rowObj is RDFResource)
                                            {
                                                result.AddTriple(new RDFTriple((RDFResource)rowSubj, (RDFResource)rowPred, (RDFResource)rowObj));
                                            }
                                            else
                                            {
                                                result.AddTriple(new RDFTriple((RDFResource)rowSubj, (RDFResource)rowPred, (RDFLiteral)rowObj));
                                            }
                                        }
                                        else
                                        {
                                            throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter contains a row having NULL value in the \"OBJECT\" column.");
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter contains a row having a blank resource or a literal in the \"PREDICATE\" column.");
                                    }
                                }
                                else
                                {
                                    throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter contains a row having null or empty value in the \"PREDICATE\" column.");
                                }
                                #endregion
                            }
                            else
                            {
                                throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter contains a row not having a resource in the \"SUBJECT\" column.");
                            }
                        }
                        else
                        {
                            throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter contains a row having null or empty value in the \"SUBJECT\" column.");
                        }
                        #endregion
                    }
                }
                else
                {
                    throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter does not have the required columns \"SUBJECT\", \"PREDICATE\", \"OBJECT\".");
                }
            }
            else
            {
                throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter is null, or it does not have exactly 3 columns.");
            }

            return(result);
        }
Ejemplo n.º 11
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);
                }
            }
        }