diff --git a/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFBNodeExpressionTest.cs b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFBNodeExpressionTest.cs new file mode 100644 index 00000000..a18b526a --- /dev/null +++ b/RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFBNodeExpressionTest.cs @@ -0,0 +1,65 @@ +/* + Copyright 2012-2023 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.Collections.Generic; +using System.Data; +using RDFSharp.Model; +using RDFSharp.Query; + +namespace RDFSharp.Test.Query +{ + [TestClass] + public class RDFBNodeExpressionTest + { + #region Tests + [TestMethod] + public void ShouldCreateBNodeExpression() + { + RDFBNodeExpression expression = new RDFBNodeExpression(); + + Assert.IsNotNull(expression); + Assert.IsNull(expression.LeftArgument); + Assert.IsNull(expression.RightArgument); + Assert.IsTrue(expression.ToString().Equals("(BNODE())")); + Assert.IsTrue(expression.ToString(new List()).Equals("(BNODE())")); + } + + [TestMethod] + public void ShouldApplyExpressionAndCalculateResult() + { + DataTable table = new DataTable(); + table.Columns.Add("?A", typeof(string)); + table.Columns.Add("?B", typeof(string)); + DataRow row = table.NewRow(); + row["?A"] = new RDFTypedLiteral("5.1", RDFModelEnums.RDFDatatypes.XSD_DOUBLE).ToString(); + row["?B"] = new RDFTypedLiteral("25", RDFModelEnums.RDFDatatypes.XSD_INT).ToString(); + table.Rows.Add(row); + table.AcceptChanges(); + + RDFBNodeExpression expression = new RDFBNodeExpression(); + + RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); + Assert.IsNotNull(expressionResult); + Assert.IsTrue(expressionResult is RDFResource exprRes && exprRes.IsBlank); + + RDFPatternMember expressionResult2 = expression.ApplyExpression(table.Rows[0]); + Assert.IsTrue(expressionResult2 is RDFResource exprRes2 && exprRes2.IsBlank); + Assert.IsFalse(expressionResult.Equals(expressionResult2)); + } + #endregion + } +} \ No newline at end of file diff --git a/RDFSharp/Query/Mirella/Algebra/Expressions/RDFBNodeExpression.cs b/RDFSharp/Query/Mirella/Algebra/Expressions/RDFBNodeExpression.cs new file mode 100644 index 00000000..d650bbe3 --- /dev/null +++ b/RDFSharp/Query/Mirella/Algebra/Expressions/RDFBNodeExpression.cs @@ -0,0 +1,53 @@ +/* + Copyright 2012-2023 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 RDFSharp.Model; +using System.Collections.Generic; +using System.Data; + +namespace RDFSharp.Query +{ + /// + /// RDFBNodeExpression represents a blank node generator function to be applied on a query results table. + /// + public class RDFBNodeExpression : RDFExpression + { + #region Ctors + /// + /// Default-ctor to build a blank node generator function + /// + public RDFBNodeExpression() { } + #endregion + + #region Interfaces + /// + /// Gives the string representation of the blank node generator function + /// + public override string ToString() + => ToString(new List()); + internal override string ToString(List prefixes) + => "(BNODE())"; + #endregion + + #region Methods + /// + /// Applies the blank node generator expression on the given datarow + /// + internal override RDFPatternMember ApplyExpression(DataRow row) + => new RDFResource(); + #endregion + } +} \ No newline at end of file diff --git a/RDFSharp/Query/Mirella/Algebra/RDFExpression.cs b/RDFSharp/Query/Mirella/Algebra/RDFExpression.cs index 83017773..f80529a8 100644 --- a/RDFSharp/Query/Mirella/Algebra/RDFExpression.cs +++ b/RDFSharp/Query/Mirella/Algebra/RDFExpression.cs @@ -67,8 +67,12 @@ public RDFExpression(RDFPatternMember leftArgument, RDFPatternMember rightArgume /// internal RDFExpression(RDFExpressionArgument leftArgument, RDFExpressionArgument rightArgument) { - if ((!(this is RDFRandExpression)) && leftArgument == null) + #region Guards + bool isRandExpression = this is RDFRandExpression; + bool isBNodeExpression = this is RDFBNodeExpression; + if (!isRandExpression && !isBNodeExpression && leftArgument == null) throw new RDFQueryException("Cannot create expression because given \"leftArgument\" parameter is null"); + #endregion LeftArgument = leftArgument; RightArgument = rightArgument;