Skip to content

Commit

Permalink
#347 Implement LANG expression for extracting language of plain liter…
Browse files Browse the repository at this point in the history
…als (#348)
  • Loading branch information
mdesalvo authored Nov 11, 2024
1 parent fe00a56 commit 3376ff3
Show file tree
Hide file tree
Showing 24 changed files with 407 additions and 51 deletions.
2 changes: 1 addition & 1 deletion RDFSharp.Test/Model/RDFTripleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void ShouldCreateSPLTripleFromNullInputs(string p)
Assert.IsTrue(triple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL);
Assert.IsTrue(((RDFResource)triple.Subject).IsBlank);
Assert.IsTrue(triple.Predicate.Equals(pred));
Assert.IsTrue(((RDFPlainLiteral)triple.Object).Equals(new RDFPlainLiteral(string.Empty)));
Assert.IsTrue(((RDFPlainLiteral)triple.Object).Equals(RDFPlainLiteral.Empty));
Assert.IsTrue(triple.ReificationSubject.Equals(new RDFResource(string.Concat("bnode:", triple.TripleID.ToString()))));
}

Expand Down
2 changes: 1 addition & 1 deletion RDFSharp.Test/Model/Serializers/RDFNTriplesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void ShouldDeserializeGraphWithSPLTripleEvenIfEmptyLiteral()

Assert.IsNotNull(graph);
Assert.IsTrue(graph.TriplesCount == 1);
Assert.IsTrue(graph.ContainsTriple(new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFPlainLiteral(string.Empty))));
Assert.IsTrue(graph.ContainsTriple(new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), RDFPlainLiteral.Empty)));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion RDFSharp.Test/Model/Serializers/RDFTriXTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public void ShouldDeserializeGraphWithSPLTripleEvenIfEmptyLiteral()

Assert.IsNotNull(graph);
Assert.IsTrue(graph.TriplesCount == 1);
Assert.IsTrue(graph.ContainsTriple(new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFPlainLiteral(string.Empty))));
Assert.IsTrue(graph.ContainsTriple(new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), RDFPlainLiteral.Empty)));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion RDFSharp.Test/Model/Serializers/RDFXmlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public void ShouldDeserializeGraphWithSPLTripleHavingXXEEntityAsLiteralNotExploi

Assert.IsNotNull(graph);
Assert.IsTrue(graph.Context.Equals(RDFNamespaceRegister.DefaultNamespace.NamespaceUri));
Assert.IsTrue(graph.ContainsTriple(new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/pred"), new RDFPlainLiteral(string.Empty)))); //XXE entity will NOT be accessed, leading to an empty parsed value
Assert.IsTrue(graph.ContainsTriple(new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/pred"), RDFPlainLiteral.Empty))); //XXE entity will NOT be accessed, leading to an empty parsed value
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public void ShouldApplyExpressionAndCalculateResultAllEmpties()
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(new RDFPlainLiteral(string.Empty)));
Assert.IsTrue(expressionResult.Equals(RDFPlainLiteral.Empty));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void ShouldApplyExpressionAndCalculateResultEvenOnNullLeftAndRight2()
table.Columns.Add("?B", typeof(string));
DataRow row = table.NewRow();
row["?A"] = null;
row["?B"] = new RDFPlainLiteral(string.Empty);
row["?B"] = RDFPlainLiteral.Empty;
table.Rows.Add(row);
table.AcceptChanges();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ShouldApplyExpressionWithExpressionAndCalculateResultOnNull()
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(new RDFPlainLiteral(string.Empty)));
Assert.IsTrue(expressionResult.Equals(RDFPlainLiteral.Empty));
}

[TestMethod]
Expand Down Expand Up @@ -199,7 +199,7 @@ public void ShouldApplyExpressionWithVariableAndCalculateResultOnNull()
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(new RDFPlainLiteral(string.Empty)));
Assert.IsTrue(expressionResult.Equals(RDFPlainLiteral.Empty));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void ShouldApplyExpressionAndCalculateResultEvenOnNullLeftAndRight2()
table.Columns.Add("?B", typeof(string));
DataRow row = table.NewRow();
row["?A"] = null;
row["?B"] = new RDFPlainLiteral(string.Empty);
row["?B"] = RDFPlainLiteral.Empty;
table.Rows.Add(row);
table.AcceptChanges();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/*
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 language 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 RDFLangExpressionTest
{
#region Tests
[TestMethod]
public void ShouldCreateLangExpressionWithExpression()
{
RDFLangExpression expression = new RDFLangExpression(
new RDFAddExpression(new RDFVariable("?V1"), new RDFVariable("?V2")));

Assert.IsNotNull(expression);
Assert.IsNotNull(expression.LeftArgument);
Assert.IsNull(expression.RightArgument);
Assert.IsTrue(expression.ToString().Equals("(LANG((?V1 + ?V2)))"));
Assert.IsTrue(expression.ToString([]).Equals("(LANG((?V1 + ?V2)))"));
}

[TestMethod]
public void ShouldCreateLangExpressionWithVariable()
{
RDFLangExpression expression = new RDFLangExpression(
new RDFVariable("?V1"));

Assert.IsNotNull(expression);
Assert.IsNotNull(expression.LeftArgument);
Assert.IsNull(expression.RightArgument);
Assert.IsTrue(expression.ToString().Equals("(LANG(?V1))"));
Assert.IsTrue(expression.ToString([]).Equals("(LANG(?V1))"));
}

[TestMethod]
public void ShouldThrowExceptionOnCreatingLangExpressionWithExpressionBecauseNullLeftArgument()
=> Assert.ThrowsException<RDFQueryException>(() => new RDFLangExpression(null as RDFExpression));

[TestMethod]
public void ShouldThrowExceptionOnCreatingLangExpressionWithVariableBecauseNullLeftArgument()
=> Assert.ThrowsException<RDFQueryException>(() => new RDFLangExpression(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();

RDFLangExpression expression = new RDFLangExpression(
new RDFVariableExpression(new RDFVariable("?A")));
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(RDFPlainLiteral.Empty));
}

[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();

RDFLangExpression expression = new RDFLangExpression(
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();

RDFLangExpression expression = new RDFLangExpression(
new RDFVariableExpression(new RDFVariable("?A")));
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(RDFPlainLiteral.Empty));
}

[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();

RDFLangExpression expression = new RDFLangExpression(
new RDFVariableExpression(new RDFVariable("?A")));
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(new RDFPlainLiteral("EN-US")));
}

[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();

RDFLangExpression expression = new RDFLangExpression(
new RDFVariableExpression(new RDFVariable("?A")));
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNull(expressionResult);
}

[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();

RDFLangExpression expression = new RDFLangExpression(
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();

RDFLangExpression expression = new RDFLangExpression(
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();

RDFLangExpression expression = new RDFLangExpression(
new RDFVariable("?A"));
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(RDFPlainLiteral.Empty));
}

[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();

RDFLangExpression expression = new RDFLangExpression(
new RDFVariable("?A"));
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(new RDFPlainLiteral("EN-US")));
}

[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();

RDFLangExpression expression = new RDFLangExpression(
new RDFVariable("?A"));
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNull(expressionResult);
}

[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();

RDFLangExpression expression = new RDFLangExpression(
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 @@ -15,7 +15,6 @@ limitations under the License.
*/

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Data;
using RDFSharp.Model;
using RDFSharp.Query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ShouldApplyExpressionWithExpressionAndCalculateResultOnNull()
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]);

Assert.IsNotNull(expressionResult);
Assert.IsTrue(expressionResult.Equals(new RDFPlainLiteral(string.Empty)));
Assert.IsTrue(expressionResult.Equals(RDFPlainLiteral.Empty));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void ShouldApplyExpressionAndCalculateResultEvenOnNullLeftAndRight2()
table.Columns.Add("?B", typeof(string));
DataRow row = table.NewRow();
row["?A"] = null;
row["?B"] = new RDFPlainLiteral(string.Empty);
row["?B"] = RDFPlainLiteral.Empty;
table.Rows.Add(row);
table.AcceptChanges();

Expand Down
Loading

0 comments on commit 3376ff3

Please sign in to comment.