Skip to content

Commit

Permalink
#349 Added implementation of SPARQL DATATYPE expression
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Nov 12, 2024
1 parent 3376ff3 commit d48739b
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
2 changes: 1 addition & 1 deletion RDFSharp.Test/Model/Validation/RDFValidationHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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")));
Expand All @@ -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")))),
Expand Down Expand Up @@ -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")))),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RDFQueryException>(() => new RDFDatatypeExpression(null as RDFExpression));

[TestMethod]
public void ShouldThrowExceptionOnCreatingDatatypeExpressionWithVariableBecauseNullLeftArgument()
=> Assert.ThrowsException<RDFQueryException>(() => 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion RDFSharp.Test/Query/Mirella/RDFQueryEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit d48739b

Please sign in to comment.