/// <summary>
        /// Compares the given pattern members, throwing a "Type Error" whenever the comparison operator detects sematically incompatible members;
        /// </summary>
        internal static Int32 CompareRDFPatternMembers(RDFPatternMember left, RDFPatternMember right)
        {
            #region NULLS
            if (left      == null) {
                if (right == null) {
                    return  0;
                }
                return -1;
            }
            if (right     == null) {
                return 1;
            }
            #endregion

            #region RESOURCE/CONTEXT
            if (left      is RDFResource || left is RDFContext) {

                //RESOURCE/CONTEXT VS RESOURCE/CONTEXT/PLAINLITERAL
                if (right is RDFResource || right is RDFContext || right is RDFPlainLiteral) {
                    return String.Compare(left.ToString(), right.ToString(), StringComparison.Ordinal);
                }

                //RESOURCE/CONTEXT VS TYPEDLITERAL
                else {
                    if (((RDFTypedLiteral)right).HasStringDatatype()) {
                        return String.Compare(left.ToString(), ((RDFTypedLiteral)right).Value, StringComparison.Ordinal);
                    }
                    return -99; //Type Error
                }

            }
            #endregion

            #region PLAINLITERAL
            else if (left is RDFPlainLiteral) {

                //PLAINLITERAL VS RESOURCE/CONTEXT/PLAINLITERAL
                if (right is RDFResource || right is RDFContext || right is RDFPlainLiteral) {
                    return String.Compare(left.ToString(), right.ToString(), StringComparison.Ordinal);
                }

                //PLAINLITERAL VS TYPEDLITERAL
                else {
                    if (((RDFTypedLiteral)right).HasStringDatatype()) {
                        return String.Compare(left.ToString(), ((RDFTypedLiteral)right).Value, StringComparison.Ordinal);
                    }
                    return -99; //Type Error
                }

            }
            #endregion

            #region TYPEDLITERAL
               else {

                //TYPEDLITERAL VS RESOURCE/CONTEXT/PLAINLITERAL
                if (right is RDFResource || right is RDFContext || right is RDFPlainLiteral) {
                    if (((RDFTypedLiteral)left).HasStringDatatype()) {
                        return String.Compare(((RDFTypedLiteral)left).Value, right.ToString(), StringComparison.Ordinal);
                    }
                    return -99; //Type Error
                }

                //TYPEDLITERAL VS TYPEDLITERAL
                else {
                    if (((RDFTypedLiteral)left).HasBooleanDatatype()       && ((RDFTypedLiteral)right).HasBooleanDatatype()) {
                        Boolean leftValueBoolean    = Boolean.Parse(((RDFTypedLiteral)left).Value);
                        Boolean rightValueBoolean   = Boolean.Parse(((RDFTypedLiteral)right).Value);
                        return leftValueBoolean.CompareTo(rightValueBoolean);
                    }
                    else if (((RDFTypedLiteral)left).HasDatetimeDatatype() && ((RDFTypedLiteral)right).HasDatetimeDatatype()) {
                        DateTime leftValueDateTime  = DateTime.Parse(((RDFTypedLiteral)left).Value,  CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
                        DateTime rightValueDateTime = DateTime.Parse(((RDFTypedLiteral)right).Value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
                        return leftValueDateTime.CompareTo(rightValueDateTime);
                    }
                    else if (((RDFTypedLiteral)left).HasDecimalDatatype()  && ((RDFTypedLiteral)right).HasDecimalDatatype()) {
                        Decimal leftValueDecimal    = Decimal.Parse(((RDFTypedLiteral)left).Value,  CultureInfo.InvariantCulture);
                        Decimal rightValueDecimal   = Decimal.Parse(((RDFTypedLiteral)right).Value, CultureInfo.InvariantCulture);
                        return leftValueDecimal.CompareTo(rightValueDecimal);
                    }
                    else if (((RDFTypedLiteral)left).HasStringDatatype()   && ((RDFTypedLiteral)right).HasStringDatatype()) {
                        String leftValueString      = ((RDFTypedLiteral)left).Value;
                        String rightValueString     = ((RDFTypedLiteral)right).Value;
                        return leftValueString.CompareTo(rightValueString);
                    }
                    else if (((RDFTypedLiteral)left).HasTimespanDatatype() && ((RDFTypedLiteral)right).HasTimespanDatatype()) {
                        TimeSpan leftValueDuration  = XmlConvert.ToTimeSpan(((RDFTypedLiteral)left).Value);
                        TimeSpan rightValueDuration = XmlConvert.ToTimeSpan(((RDFTypedLiteral)right).Value);
                        return leftValueDuration.CompareTo(rightValueDuration);
                    }
                    else {
                        return -99; //Type Error
                    }
                }

            }
            #endregion
        }
        /// <summary>
        /// Gives a formatted string representation of the given pattern member
        /// </summary>
        internal static String PrintRDFPatternMember(RDFPatternMember patternMember)
        {
            if (patternMember != null) {

                #region Variable
                if (patternMember is RDFVariable) {
                    return patternMember.ToString();
                }
                #endregion

                #region Non-Variable

                #region Resource/Context
                if (patternMember is RDFResource || patternMember is RDFContext) {
                    if (patternMember is RDFResource && ((RDFResource)patternMember).IsBlank) {
                        return patternMember.ToString();
                    }
                    return "<" + patternMember + ">";
                }
                #endregion

                #region Literal
                if (patternMember is RDFPlainLiteral) {
                    if (((RDFPlainLiteral)patternMember).Language != String.Empty) {
                        return "\"" + ((RDFPlainLiteral)patternMember).Value + "\"@" + ((RDFPlainLiteral)patternMember).Language;
                    }
                    return "\"" + ((RDFPlainLiteral)patternMember).Value + "\"";
                }
                return "\"" + ((RDFTypedLiteral)patternMember).Value + "\"^^<" + RDFModelUtilities.GetDatatypeFromEnum(((RDFTypedLiteral)patternMember).Datatype) + ">";
                #endregion

                #endregion

            }
            throw new RDFQueryException("Cannot print pattern member because given \"patternMember\" parameter is null.");
        }
Exemple #3
0
        /// <summary>
        /// Creates a store from a "Context-Subject-Predicate-Object" datatable
        /// </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") && tableRow["CONTEXT"].ToString() != String.Empty)
                        {
                            RDFPatternMember rowCont = RDFQueryUtilities.ParseRDFPatternMember(tableRow["CONTEXT"].ToString());
                            if (rowCont is RDFResource)
                            {
                                #region SUBJECT
                                //Parse the quadruple subject
                                if (!tableRow.IsNull("SUBJECT") && tableRow["SUBJECT"].ToString() != String.Empty)
                                {
                                    RDFPatternMember rowSubj = RDFQueryUtilities.ParseRDFPatternMember(tableRow["SUBJECT"].ToString());
                                    if (rowSubj is RDFResource)
                                    {
                                        #region PREDICATE
                                        //Parse the quadruple predicate
                                        if (!tableRow.IsNull("PREDICATE") && tableRow["PREDICATE"].ToString() != String.Empty)
                                        {
                                            RDFPatternMember rowPred = RDFQueryUtilities.ParseRDFPatternMember(tableRow["PREDICATE"].ToString());
                                            if (rowPred is RDFResource && !((RDFResource)rowPred).IsBlank)
                                            {
                                                #region OBJECT
                                                //Parse the quadruple object
                                                if (!tableRow.IsNull("OBJECT"))
                                                {
                                                    RDFPatternMember 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 RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter contains a row having NULL value in the \"OBJECT\" column.");
                                                }
                                                #endregion
                                            }
                                            else
                                            {
                                                throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter contains a row not having a resource, or having a blank resource, in the \"PREDICATE\" column.");
                                            }
                                        }
                                        else
                                        {
                                            throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter contains a row having null or empty value in the \"PREDICATE\" column.");
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter contains a row not having a resource in the \"SUBJECT\" column.");
                                    }
                                }
                                else
                                {
                                    throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter contains a row having null or empty value in the \"SUBJECT\" column.");
                                }
                                #endregion
                            }
                            else
                            {
                                throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter contains a row not having a context in the \"CONTEXT\" column.");
                            }
                        }
                        else
                        {
                            throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter contains a row having null or empty value in the \"CONTEXT\" column.");
                        }
                        #endregion
                    }
                }
                else
                {
                    throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter does not have the required 4 columns: \"CONTEXT\", \"SUBJECT\", \"PREDICATE\", \"OBJECT\".");
                }
            }
            else
            {
                throw new RDFModelException("Cannot create RDFMemoryStore because given \"table\" parameter is null, or it does not have exactly the required 4 columns.");
            }

            return(result);
        }
        /// <summary>
        /// Compares the given pattern members, throwing a "Type Error" whenever the comparison operator detects sematically incompatible members;
        /// </summary>
        public static Int32 CompareRDFPatternMembers(RDFPatternMember left, RDFPatternMember right) {

            #region CornerCase Comparisons
            if (left      == null) {
                if (right == null) {
                    return  0;
                }
                return -1;
            }
            if (right     == null) {
                return 1;
            }
            #endregion

            #region Effective  Comparisons

            #region RESOURCE/CONTEXT
            if (left is RDFResource      || left is RDFContext) {

                //RESOURCE/CONTEXT VS RESOURCE/CONTEXT
                if (right is RDFResource || right is RDFContext) {
                    return String.Compare(left.ToString(), right.ToString(), StringComparison.Ordinal);
                }

                //RESOURCE/CONTEXT VS "XSD:ANYURI" TYPED LITERAL
                if (right is RDFTypedLiteral && ((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "anyURI"))) {
                    return String.Compare(left.ToString(), ((RDFTypedLiteral)right).Value, StringComparison.Ordinal);
                }

                //RESOURCE/CONTEXT VS LITERAL
                return -1;

            }
            #endregion

            #region PLAINLITERAL
            if (left is RDFPlainLiteral) {

                //PLAIN LITERAL VS RESOURCE/CONTEXT
                if (right is RDFResource || right is RDFContext) {
                    return 1;
                }

                //PLAIN LITERAL VS PLAIN LITERAL
                if (right is RDFPlainLiteral) {
                    return String.Compare(left.ToString(), right.ToString(), StringComparison.Ordinal);
                }

                //PLAIN LITERAL VS "XSD:STRING" TYPED LITERAL
                if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "string"))) {
                    return String.Compare(left.ToString(), ((RDFTypedLiteral)right).Value, StringComparison.Ordinal);
                }

                //PLAIN LITERAL VS TYPED LITERAL
                return -1;

            }
            #endregion

            #region TYPEDLITERAL
            //TYPED LITERAL VS RESOURCE/CONTEXT
            if (right is RDFResource || right is RDFContext) {

                //"XSD:ANYURI" TYPED LITERAL VS RESOURCE/CONTEXT
                if (left is RDFTypedLiteral && ((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "anyURI"))) {
                    return String.Compare(((RDFTypedLiteral)left).Value, right.ToString(), StringComparison.Ordinal);
                }

                //TYPED LITERAL VS RESOURCE/CONTEXT
                return 1;

            }

            //TYPED LITERAL VS PLAIN LITERAL
            if (right is RDFPlainLiteral) {

                //"XSD:STRING" TYPED LITERAL VS PLAIN LITERAL
                if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "string"))) {
                    return String.Compare(((RDFTypedLiteral)left).Value, right.ToString(), StringComparison.Ordinal);
                }

                //TYPED LITERAL VS PLAIN LITERAL
                return 1;

            }

            //TYPED LITERAL VS TYPED LITERAL
            //SEMANTICALLY COMPATIBLE CATEGORY
            if (((RDFTypedLiteral)left).Datatype.Category.Equals(((RDFTypedLiteral)right).Datatype.Category)) {
                Int32 comparison = 0;
                switch (((RDFTypedLiteral)left).Datatype.Category) {

                    case RDFModelEnums.RDFDatatypeCategory.Numeric:
                        Decimal leftValueDecimal    = Decimal.Parse(((RDFTypedLiteral)left).Value,  NumberStyles.Number, CultureInfo.InvariantCulture);
                        Decimal rightValueDecimal   = Decimal.Parse(((RDFTypedLiteral)right).Value, NumberStyles.Number, CultureInfo.InvariantCulture);
                        comparison                  = leftValueDecimal.CompareTo(rightValueDecimal);
                        break;

                    case RDFModelEnums.RDFDatatypeCategory.Boolean:
                        Boolean leftValueBoolean    = Boolean.Parse(((RDFTypedLiteral)left).Value);
                        Boolean rightValueBoolean   = Boolean.Parse(((RDFTypedLiteral)right).Value);
                        comparison                  = leftValueBoolean.CompareTo(rightValueBoolean);
                        break;

                    case RDFModelEnums.RDFDatatypeCategory.DateTime:
                        DateTime leftValueDateTime;
                        DateTime rightValueDateTime;

                        //Detect exact type of left typed literal (dateTime, date, time, gYearMonth, gMonthDay, gYear, gMonth, gDay)
                        if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "dateTime"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyy-MM-ddTHH:mm:ssK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "date"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyy-MM-ddK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "time"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "HH:mm:ssK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "HH:mm:ss", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gYearMonth"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyy-MMK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyy-MM", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gMonthDay"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "--MM-ddK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "--MM-dd", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gYear"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyyK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "yyyy", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gMonth"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "MMK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "MM", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)left).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gDay"))) {
                            try {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "ddK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                leftValueDateTime  = DateTime.ParseExact(((RDFTypedLiteral)left).Value, "dd", CultureInfo.InvariantCulture);
                            }
                        }
                        else {
                            throw new RDFQueryException("Cannot parse typed literal of DateTime category because unknown format detected. Please, switch to one of XSD types: 'dateTime', 'date', 'time', 'gYearMonth', 'gMonthDay', 'gYear', 'gMonth', 'gDay'.");
                        }

                        //Detect exact type of right typed literal (dateTime, date, time, gYearMonth, gMonthDay, gYear, gMonth, gDay)
                        if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "dateTime"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyy-MM-ddTHH:mm:ssK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "date"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyy-MM-ddK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "time"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "HH:mm:ssK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "HH:mm:ss", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gYearMonth"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyy-MMK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyy-MM", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gMonthDay"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "--MM-ddK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "--MM-dd", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gYear"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyyK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "yyyy", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gMonth"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "MMK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "MM", CultureInfo.InvariantCulture);
                            }
                        }
                        else if (((RDFTypedLiteral)right).Datatype.Equals(RDFDatatypeRegister.GetByPrefixAndDatatype(RDFVocabulary.XSD.PREFIX, "gDay"))) {
                            try {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "ddK", CultureInfo.InvariantCulture);
                            }
                            catch {
                                rightValueDateTime = DateTime.ParseExact(((RDFTypedLiteral)right).Value, "dd", CultureInfo.InvariantCulture);
                            }
                        }
                        else {
                            throw new RDFQueryException("Cannot parse typed literal of DateTime category because unknown format detected. Please, switch to one of XSD types: 'dateTime', 'date', 'time', 'gYearMonth', 'gMonthDay', 'gYear', 'gMonth', 'gDay'.");
                        }

                        comparison                  = leftValueDateTime.CompareTo(rightValueDateTime);
                        break;

                    case RDFModelEnums.RDFDatatypeCategory.TimeSpan:
                        TimeSpan leftValueDuration  = XmlConvert.ToTimeSpan(((RDFTypedLiteral)left).Value);
                        TimeSpan rightValueDuration = XmlConvert.ToTimeSpan(((RDFTypedLiteral)right).Value);
                        comparison                  = leftValueDuration.CompareTo(rightValueDuration);
                        break;

                    case RDFModelEnums.RDFDatatypeCategory.String:
                        String leftValueString      = ((RDFTypedLiteral)left).Value;
                        String rightValueString     = ((RDFTypedLiteral)right).Value;
                        comparison                  = String.Compare(leftValueString, rightValueString, StringComparison.Ordinal);
                        break;

                }
                return comparison;
            }

            //SEMANTICALLY NOT COMPATIBLE CATEGORY
            throw new RDFQueryException("Type Error: Typed Literal cannot be compared to Typed Literal with different category.");

            #endregion

            #endregion

        }
Exemple #5
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);
                }
            }
        }