diff --git a/RDFSharp.Test/Model/Validation/Abstractions/Constraints/RDFQualifiedValueShapeConstraintTest.cs b/RDFSharp.Test/Model/Validation/Abstractions/Constraints/RDFQualifiedValueShapeConstraintTest.cs index e66aec26..01150465 100644 --- a/RDFSharp.Test/Model/Validation/Abstractions/Constraints/RDFQualifiedValueShapeConstraintTest.cs +++ b/RDFSharp.Test/Model/Validation/Abstractions/Constraints/RDFQualifiedValueShapeConstraintTest.cs @@ -70,7 +70,7 @@ public void ShouldExportQualifiedValueShapeConstraint() && t.Value.ObjectID.Equals(new RDFResource("ex:QVShape").PatternMemberID))); Assert.IsTrue(graph.IndexedTriples.Any(t => t.Value.SubjectID.Equals(new RDFResource("ex:NodeShape").PatternMemberID) && t.Value.PredicateID.Equals(RDFVocabulary.SHACL.QUALIFIED_MIN_COUNT.PatternMemberID) - && t.Value.ObjectID.Equals(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER).PatternMemberID))); + && t.Value.ObjectID.Equals(RDFTypedLiteral.One.PatternMemberID))); Assert.IsTrue(graph.IndexedTriples.Any(t => t.Value.SubjectID.Equals(new RDFResource("ex:NodeShape").PatternMemberID) && t.Value.PredicateID.Equals(RDFVocabulary.SHACL.QUALIFIED_MAX_COUNT.PatternMemberID) && t.Value.ObjectID.Equals(new RDFTypedLiteral("2", RDFModelEnums.RDFDatatypes.XSD_INTEGER).PatternMemberID))); diff --git a/RDFSharp.Test/Model/Validation/RDFValidationHelperTest.cs b/RDFSharp.Test/Model/Validation/RDFValidationHelperTest.cs index 867f3318..1d05b398 100644 --- a/RDFSharp.Test/Model/Validation/RDFValidationHelperTest.cs +++ b/RDFSharp.Test/Model/Validation/RDFValidationHelperTest.cs @@ -926,7 +926,7 @@ public void ShouldParsePropertyShapeFromGraph(RDFValidationEnums.RDFShapeSeverit Assert.IsTrue(shape2.Names.Count == 1); Assert.IsTrue(shape2.Names.Single().Equals(new RDFPlainLiteral("name", "en-US"))); Assert.IsNotNull(shape2.Order); - Assert.IsTrue(shape2.Order.Equals(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER))); + Assert.IsTrue(shape2.Order.Equals(RDFTypedLiteral.One)); Assert.IsNotNull(shape2.Group); Assert.IsTrue(shape2.Group.Equals(new RDFResource("ex:shapeGroup"))); diff --git a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFCoalesceExpressionTest.cs b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFCoalesceExpressionTest.cs index 5fdab26d..77201a70 100644 --- a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFCoalesceExpressionTest.cs +++ b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFCoalesceExpressionTest.cs @@ -141,7 +141,7 @@ public void ShouldApplyExpressionWithEEAndCalculateResultCoalesceLeft() RDFCoalesceExpression expression = new RDFCoalesceExpression( new RDFVariableExpression(new RDFVariable("?A")), - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B"))); + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B"))); RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); Assert.IsNotNull(expressionResult); @@ -161,7 +161,7 @@ public void ShouldApplyExpressionWithEEAndCalculateResultCoalesceRight() table.AcceptChanges(); RDFCoalesceExpression expression = new RDFCoalesceExpression( - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B")), + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B")), new RDFVariableExpression(new RDFVariable("?A"))); RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); @@ -183,7 +183,7 @@ public void ShouldApplyExpressionWithEEAndCalculateResultCoalesceNull() RDFCoalesceExpression expression = new RDFCoalesceExpression( new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("5", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?A")), - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B"))); + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B"))); RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); Assert.IsNull(expressionResult); @@ -262,10 +262,10 @@ public void ShouldApplyExpressionWithNestedCoalesce() RDFCoalesceExpression expression = new RDFCoalesceExpression( new RDFCoalesceExpression( new RDFSubstringExpression(new RDFVariable("?C"), 1), - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B"))), + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B"))), new RDFCoalesceExpression( new RDFVariableExpression(new RDFVariable("?A")), - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B")))); + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B")))); RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); Assert.IsNotNull(expressionResult); @@ -289,7 +289,7 @@ public void ShouldApplyExpressionWithNestedCoalesceNull() RDFCoalesceExpression expression = new RDFCoalesceExpression( new RDFCoalesceExpression( new RDFSubstringExpression(new RDFVariable("?C"), 1), - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B"))), + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B"))), new RDFCoalesceExpression( new RDFVariableExpression(new RDFVariable("?Q")), new RDFVariable("?Y"))); @@ -316,7 +316,7 @@ public void ShouldApplyExpressionWithDeepNestedCoalesce() new RDFCoalesceExpression( new RDFCoalesceExpression( new RDFSubstringExpression(new RDFVariable("?C"), 1), - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B"))), + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B"))), new RDFCoalesceExpression( new RDFVariableExpression(new RDFVariable("?T")), new RDFAddExpression(new RDFVariable("?C"), new RDFVariable("?B")))), @@ -345,7 +345,7 @@ public void ShouldApplyExpressionWithDeepNestedCoalesceNull() new RDFCoalesceExpression( new RDFCoalesceExpression( new RDFSubstringExpression(new RDFVariable("?C"), 1), - new RDFAddExpression(new RDFConstantExpression(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)), new RDFVariable("?B"))), + new RDFAddExpression(new RDFConstantExpression(RDFTypedLiteral.One), new RDFVariable("?B"))), new RDFCoalesceExpression( new RDFVariableExpression(new RDFVariable("?T")), new RDFAddExpression(new RDFVariable("?C"), new RDFVariable("?B")))), diff --git a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFDatatypeExpressionTest.cs b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFDatatypeExpressionTest.cs new file mode 100644 index 00000000..e9fe4a77 --- /dev/null +++ b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFDatatypeExpressionTest.cs @@ -0,0 +1,257 @@ +/* + Copyright 2012-2024 Marco De Salvo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific Datatypeuage governing permissions and + limitations under the License. +*/ + +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Data; +using RDFSharp.Model; +using RDFSharp.Query; + +namespace RDFSharp.Test.Query +{ + [TestClass] + public class RDFDatatypeExpressionTest + { + #region Tests + [TestMethod] + public void ShouldCreateDatatypeExpressionWithExpression() + { + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFAddExpression(new RDFVariable("?V1"), new RDFVariable("?V2"))); + + Assert.IsNotNull(expression); + Assert.IsNotNull(expression.LeftArgument); + Assert.IsNull(expression.RightArgument); + Assert.IsTrue(expression.ToString().Equals("(DATATYPE((?V1 + ?V2)))")); + Assert.IsTrue(expression.ToString([]).Equals("(DATATYPE((?V1 + ?V2)))")); + } + + [TestMethod] + public void ShouldCreateDatatypeExpressionWithVariable() + { + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariable("?V1")); + + Assert.IsNotNull(expression); + Assert.IsNotNull(expression.LeftArgument); + Assert.IsNull(expression.RightArgument); + Assert.IsTrue(expression.ToString().Equals("(DATATYPE(?V1))")); + Assert.IsTrue(expression.ToString([]).Equals("(DATATYPE(?V1))")); + } + + [TestMethod] + public void ShouldThrowExceptionOnCreatingDatatypeExpressionWithExpressionBecauseNullLeftArgument() + => Assert.ThrowsException(() => new RDFDatatypeExpression(null as RDFExpression)); + + [TestMethod] + public void ShouldThrowExceptionOnCreatingDatatypeExpressionWithVariableBecauseNullLeftArgument() + => Assert.ThrowsException(() => new RDFDatatypeExpression(null as RDFVariable)); + + [TestMethod] + public void ShouldApplyExpressionWithExpressionAndCalculateResultOnNull() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = null; + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariableExpression(new RDFVariable("?A"))); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult.Equals(RDFVocabulary.XSD.STRING)); + } + + [TestMethod] + public void ShouldApplyExpressionWithExpressionAndCalculateResultOnResource() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFResource("ex:subj").ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariableExpression(new RDFVariable("?A"))); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNull(expressionResult); + } + + [TestMethod] + public void ShouldApplyExpressionWithExpressionAndCalculateResultOnPlainLiteral() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFPlainLiteral("hello").ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariableExpression(new RDFVariable("?A"))); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult.Equals(RDFVocabulary.XSD.STRING)); + } + + [TestMethod] + public void ShouldApplyExpressionWithExpressionAndCalculateResultOnPlainLiteralWithLanguage() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFPlainLiteral("hello","en-US").ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariableExpression(new RDFVariable("?A"))); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDF.LANG_STRING)); + } + + [TestMethod] + public void ShouldApplyExpressionWithExpressionAndCalculateResultOnTypedLiteral() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFTypedLiteral("hello", RDFModelEnums.RDFDatatypes.RDFS_LITERAL).ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariableExpression(new RDFVariable("?A"))); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDFS.LITERAL)); + } + + [TestMethod] + public void ShouldApplyExpressionWithExpressionAndNotCalculateResultBecauseNotBoundVariable() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFTypedLiteral("45", RDFModelEnums.RDFDatatypes.XSD_NORMALIZEDSTRING).ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariableExpression(new RDFVariable("?Q"))); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNull(expressionResult); + } + + [TestMethod] + public void ShouldApplyExpressionWithVariableAndCalculateResultOnResource() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFResource("ex:subj").ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariable("?A")); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNull(expressionResult); + } + + [TestMethod] + public void ShouldApplyExpressionWithVariableAndCalculateResultOnPlainLiteral() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFPlainLiteral("hello").ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariable("?A")); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult.Equals(RDFVocabulary.XSD.STRING)); + } + + [TestMethod] + public void ShouldApplyExpressionWithVariableAndCalculateResultOnPlainLiteralWithLanguage() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFPlainLiteral("hello", "en-US").ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariable("?A")); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDF.LANG_STRING)); + } + + [TestMethod] + public void ShouldApplyExpressionWithVariableAndCalculateResultOnTypedLiteral() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFTypedLiteral("hello", RDFModelEnums.RDFDatatypes.RDFS_LITERAL).ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariable("?A")); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDFS.LITERAL)); + } + + [TestMethod] + public void ShouldApplyExpressionWithVariableAndNotCalculateResultBecauseNotBoundVariable() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFTypedLiteral("45", RDFModelEnums.RDFDatatypes.XSD_NORMALIZEDSTRING).ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFDatatypeExpression expression = new RDFDatatypeExpression( + new RDFVariable("?Q")); + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + + Assert.IsNull(expressionResult); + } + #endregion + } +} \ No newline at end of file diff --git a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFGeoDimensionExpressionTest.cs b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFGeoDimensionExpressionTest.cs index 9e87798d..82608e35 100644 --- a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFGeoDimensionExpressionTest.cs +++ b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFGeoDimensionExpressionTest.cs @@ -93,7 +93,7 @@ public void ShouldApplyExpressionWithVARAndCalculateResult() RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); Assert.IsNotNull(expressionResult); - Assert.IsTrue(expressionResult.Equals(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER))); + Assert.IsTrue(expressionResult.Equals(RDFTypedLiteral.One)); } [TestMethod] diff --git a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFMonthExpressionTest.cs b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFMonthExpressionTest.cs index fe74e210..5bfacfb2 100644 --- a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFMonthExpressionTest.cs +++ b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFMonthExpressionTest.cs @@ -75,7 +75,7 @@ public void ShouldApplyExpressionWithExpressionAndCalculateResult() RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); Assert.IsNotNull(expressionResult); - Assert.IsTrue(expressionResult.Equals(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER))); + Assert.IsTrue(expressionResult.Equals(RDFTypedLiteral.One)); } [TestMethod] @@ -93,7 +93,7 @@ public void ShouldApplyExpressionWithVariableAndCalculateResult() RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); Assert.IsNotNull(expressionResult); - Assert.IsTrue(expressionResult.Equals(new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER))); + Assert.IsTrue(expressionResult.Equals(RDFTypedLiteral.One)); } [TestMethod] diff --git a/RDFSharp.Test/Query/Mirella/RDFQueryEngineTest.cs b/RDFSharp.Test/Query/Mirella/RDFQueryEngineTest.cs index 0b02a4f7..0e2f2820 100644 --- a/RDFSharp.Test/Query/Mirella/RDFQueryEngineTest.cs +++ b/RDFSharp.Test/Query/Mirella/RDFQueryEngineTest.cs @@ -7095,7 +7095,7 @@ public void ShouldProjectTableAlsoWithSubsequentExpressions() .AddProjectionVariable(new RDFVariable("?X")) .AddProjectionVariable(new RDFVariable("?AGEX2"), new RDFMultiplyExpression(new RDFVariable("?A"), new RDFTypedLiteral("2", RDFModelEnums.RDFDatatypes.XSD_INT))) .AddProjectionVariable(new RDFVariable("?AGEX4"), new RDFMultiplyExpression(new RDFVariable("?AGEX2"), new RDFTypedLiteral("2", RDFModelEnums.RDFDatatypes.XSD_INT))) - .AddProjectionVariable(new RDFVariable("?AGEX4PLUS1"), new RDFVariableExpression(new RDFAddExpression(new RDFVariable("?AGEX4"), new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER)))); + .AddProjectionVariable(new RDFVariable("?AGEX4PLUS1"), new RDFVariableExpression(new RDFAddExpression(new RDFVariable("?AGEX4"), RDFTypedLiteral.One))); RDFSelectQueryResult result = new RDFQueryEngine().EvaluateSelectQuery(query, graph); Assert.IsNotNull(result.SelectResults); diff --git a/RDFSharp/Model/RDFTypedLiteral.cs b/RDFSharp/Model/RDFTypedLiteral.cs index babee3ec..16628fe2 100644 --- a/RDFSharp/Model/RDFTypedLiteral.cs +++ b/RDFSharp/Model/RDFTypedLiteral.cs @@ -34,6 +34,10 @@ public class RDFTypedLiteral : RDFLiteral /// Represents an handy typed literal for integer Zero /// public static RDFTypedLiteral Zero = new RDFTypedLiteral("0", RDFModelEnums.RDFDatatypes.XSD_INTEGER); + /// + /// Represents an handy typed literal for integer One + /// + public static RDFTypedLiteral One = new RDFTypedLiteral("1", RDFModelEnums.RDFDatatypes.XSD_INTEGER); #endregion #region Properties diff --git a/RDFSharp/Query/Mirella/Algebra/Expressions/RDFDatatypeExpression.cs b/RDFSharp/Query/Mirella/Algebra/Expressions/RDFDatatypeExpression.cs new file mode 100644 index 00000000..d43b4346 --- /dev/null +++ b/RDFSharp/Query/Mirella/Algebra/Expressions/RDFDatatypeExpression.cs @@ -0,0 +1,100 @@ +/* + Copyright 2012-2024 Marco De Salvo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific Datatypeuage governing permissions and + limitations under the License. +*/ + +using RDFSharp.Model; +using System.Collections.Generic; +using System.Data; +using System.Text; + +namespace RDFSharp.Query +{ + /// + /// RDFDatatypeExpression represents a datatype-extractor function to be applied on a query results table. + /// + public class RDFDatatypeExpression : RDFExpression + { + #region Ctors + /// + /// Default-ctor to build a datatype function with given arguments + /// + public RDFDatatypeExpression(RDFExpression leftArgument) : base(leftArgument, null as RDFExpression) { } + + /// + /// Default-ctor to build a datatype function with given arguments + /// + public RDFDatatypeExpression(RDFVariable leftArgument) : base(leftArgument, null as RDFExpression) { } + #endregion + + #region Interfaces + /// + /// Gives the string representation of the datatype function + /// + public override string ToString() + => ToString(new List()); + internal override string ToString(List prefixes) + { + StringBuilder sb = new StringBuilder(); + + //(DATATYPE(L)) + sb.Append("(DATATYPE("); + if (LeftArgument is RDFExpression expLeftArgument) + sb.Append(expLeftArgument.ToString(prefixes)); + else + sb.Append(RDFQueryPrinter.PrintPatternMember((RDFPatternMember)LeftArgument, prefixes)); + sb.Append("))"); + + return sb.ToString(); + } + #endregion + + #region Methods + /// + /// Applies the datatype function on the given datarow + /// + internal override RDFPatternMember ApplyExpression(DataRow row) + { + RDFResource expressionResult = null; + + #region Guards + if (LeftArgument is RDFVariable && !row.Table.Columns.Contains(LeftArgument.ToString())) + return expressionResult; + #endregion + + try + { + #region Evaluate Arguments + //Evaluate left argument (Expression VS Variable) + RDFPatternMember leftArgumentPMember = null; + if (LeftArgument is RDFExpression leftArgumentExpression) + leftArgumentPMember = leftArgumentExpression.ApplyExpression(row); + else + leftArgumentPMember = RDFQueryUtilities.ParseRDFPatternMember(row[LeftArgument.ToString()].ToString()); + #endregion + + #region Calculate Result + if (leftArgumentPMember is RDFTypedLiteral leftArgumentPMemberTLiteral) + expressionResult = new RDFResource(leftArgumentPMemberTLiteral.Datatype.ToString()); + else if (leftArgumentPMember is RDFPlainLiteral leftArgumentPMemberPLiteral) + expressionResult = leftArgumentPMemberPLiteral.HasLanguage() ? RDFVocabulary.RDF.LANG_STRING : RDFVocabulary.XSD.STRING; + #endregion + } + catch { /* Just a no-op, since type errors are normal when trying to face variable's bindings */ } + + return expressionResult; + } + #endregion + } +} \ No newline at end of file