From 6d7a2b2c9dfadfe48126b1703cb36976d0483701 Mon Sep 17 00:00:00 2001 From: Annika Greif Date: Sun, 24 Nov 2024 11:58:25 +0100 Subject: [PATCH] Add Test IdToLiteralOrIri and some formatting --- src/engine/ExportQueryExecutionTrees.cpp | 70 +++++++++---------- src/engine/ExportQueryExecutionTrees.h | 5 ++ .../sparqlExpressions/StringExpressions.cpp | 4 +- test/ExportQueryExecutionTreesTest.cpp | 32 ++++++--- 4 files changed, 60 insertions(+), 51 deletions(-) diff --git a/src/engine/ExportQueryExecutionTrees.cpp b/src/engine/ExportQueryExecutionTrees.cpp index 2c8651378e..33a8d2c747 100644 --- a/src/engine/ExportQueryExecutionTrees.cpp +++ b/src/engine/ExportQueryExecutionTrees.cpp @@ -361,6 +361,35 @@ ExportQueryExecutionTrees::idToLiteralOrIriForEncodedValue( return LiteralOrIri::literalWithoutQuotes(optionalStringAndType->first); } +// _____________________________________________________________________________ +std::optional ExportQueryExecutionTrees::handleIriOrLiteral( + const LiteralOrIri& word, bool onlyReturnLiterals, + bool onlyReturnLiteralsWithXsdString) { + auto datatypeIsXSDString = [](const LiteralOrIri& word) -> bool { + return word.hasDatatype() && + std::string_view( + reinterpret_cast(word.getDatatype().data()), + word.getDatatype().size()) == XSD_STRING; + }; + + if (onlyReturnLiterals && !word.isLiteral()) { + return std::nullopt; + } + if (onlyReturnLiteralsWithXsdString) { + if (word.isLiteral() && + (!word.hasDatatype() || datatypeIsXSDString(word))) { + return word; + } + return std::nullopt; + } + if (word.isLiteral() && word.hasDatatype() && !datatypeIsXSDString(word)) { + return LiteralOrIri{ + ad_utility::triple_component::Literal::literalWithNormalizedContent( + word.getContent())}; + } + return word; +} + // _____________________________________________________________________________ ad_utility::triple_component::LiteralOrIri ExportQueryExecutionTrees::getLiteralOrIriFromVocabIndex( @@ -433,47 +462,13 @@ std::optional ExportQueryExecutionTrees::idToLiteralOrIri( bool onlyReturnLiteralsWithXsdString) { using enum Datatype; auto datatype = id.getDatatype(); + if constexpr (onlyReturnLiterals) { if (!(datatype == VocabIndex || datatype == LocalVocabIndex)) { return std::nullopt; } } - auto handleIriOrLiteral = - [onlyReturnLiteralsWithXsdString]( - const LiteralOrIri& word) -> std::optional { - if constexpr (onlyReturnLiterals) { - if (!word.isLiteral()) { - return std::nullopt; - } - } - // Return only literals without datatype or literals with xsd:string - // datatype - if (onlyReturnLiteralsWithXsdString) { - if (word.isLiteral()) { - if (!word.hasDatatype() || - (word.hasDatatype() && - std::string_view( - reinterpret_cast(word.getDatatype().data()), - word.getDatatype().size()) == XSD_STRING)) { - return word; - } - } - return std::nullopt; - } - // If the literal has a datatype that is not xsd:string, remove the datatype - if (word.isLiteral()) { - if (word.hasDatatype() && - std::string_view( - reinterpret_cast(word.getDatatype().data()), - word.getDatatype().size()) != XSD_STRING) { - return LiteralOrIri{ - ad_utility::triple_component::Literal::literalWithNormalizedContent( - word.getContent())}; - } - } - return word; - }; switch (datatype) { case WordVocabIndex: return LiteralOrIri::literalWithoutQuotes( @@ -481,9 +476,10 @@ std::optional ExportQueryExecutionTrees::idToLiteralOrIri( case VocabIndex: case LocalVocabIndex: return handleIriOrLiteral( - getLiteralOrIriFromVocabIndex(index, id, localVocab)); + getLiteralOrIriFromVocabIndex(index, id, localVocab), + onlyReturnLiterals, onlyReturnLiteralsWithXsdString); case TextRecordIndex: - // TODO + // TODO: Handle TextRecordIndex if needed return std::nullopt; default: return idToLiteralOrIriForEncodedValue(id, diff --git a/src/engine/ExportQueryExecutionTrees.h b/src/engine/ExportQueryExecutionTrees.h index 20af6830b5..d9f7c67f9f 100644 --- a/src/engine/ExportQueryExecutionTrees.h +++ b/src/engine/ExportQueryExecutionTrees.h @@ -92,6 +92,11 @@ class ExportQueryExecutionTrees { static std::optional idToLiteralOrIriForEncodedValue( Id id, bool onlyReturnLiteralsWithXsdString = false); + // Checks and processes a LiteralOrIri based on the given flags. + static std::optional handleIriOrLiteral( + const LiteralOrIri& word, bool onlyReturnLiterals, + bool onlyReturnLiteralsWithXsdString); + // Acts as a helper to retrieve an LiteralOrIri object // from an Id, where the Id is of type `VocabIndex` or `LocalVocabIndex`. // This function should only be called with suitable `Datatype` Id's, diff --git a/src/engine/sparqlExpressions/StringExpressions.cpp b/src/engine/sparqlExpressions/StringExpressions.cpp index 29553996d0..2d52ba17f4 100644 --- a/src/engine/sparqlExpressions/StringExpressions.cpp +++ b/src/engine/sparqlExpressions/StringExpressions.cpp @@ -23,7 +23,7 @@ constexpr auto toLiteral = [](std::string_view normalizedContent) { constexpr auto toLiteralWithDescriptor = [](std::string_view normalizedContent, - std::optional> descriptor) { + const std::optional> descriptor) { return LiteralOrIri{ ad_utility::triple_component::Literal::literalWithNormalizedContent( asNormalizedStringViewUnsafe(normalizedContent), descriptor)}; @@ -247,8 +247,6 @@ class SubstrImpl { using SubstrExpression = NARY<3, FV>; -// using SubstrExpression = -// StringExpressionImpl<3, SubstrImpl, NumericValueGetter, NumericValueGetter>; // STRSTARTS [[maybe_unused]] auto strStartsImpl = [](std::string_view text, diff --git a/test/ExportQueryExecutionTreesTest.cpp b/test/ExportQueryExecutionTreesTest.cpp index 647cb3560e..0e9e319868 100644 --- a/test/ExportQueryExecutionTreesTest.cpp +++ b/test/ExportQueryExecutionTreesTest.cpp @@ -1640,8 +1640,9 @@ TEST(ExportQueryExecutionTrees, convertGeneratorForChunkedTransfer) { TEST(ExportQueryExecutionTrees, idToLiteralOrIriFunctionality) { std::string kg = - "

\"something\" .

1.

" - "\"some^^\" ."; + "

\"something\" .

1 .

" + "\"some\"^^ .

" + "\"dadudeldu\"^^ ."; auto qec = ad_utility::testing::getQec(kg); auto getId = ad_utility::testing::makeGetId(qec->getIndex()); using enum Datatype; @@ -1664,28 +1665,37 @@ TEST(ExportQueryExecutionTrees, idToLiteralOrIriFunctionality) { // Case Literal With Datatype String { - Id id = getId("\"some^^\""); + Id id = getId("\"some\"^^"); auto resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri( qec->getIndex(), id, LocalVocab{}); EXPECT_EQ(resultLiteral.value().toStringRepresentation(), - "\"some^^\""); - // TODO: Problem: The Literal has no Datatype - EXPECT_EQ(resultLiteral.value().hasDatatype(), false); + "\"some\"^^"); // Case onlyReturnLiterals resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri( qec->getIndex(), id, LocalVocab{}); EXPECT_EQ(resultLiteral.value().toStringRepresentation(), - "\"some^^\""); + "\"some\"^^"); // Case onlyReturnLiteralsWithXsdString resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri( qec->getIndex(), id, LocalVocab{}, true); EXPECT_EQ(resultLiteral.value().toStringRepresentation(), - "\"some^^\""); + "\"some\"^^"); } - // TODO: Case Literal With Datatype not equal String + // Case Literal With Datatype not equal String { - + Id id = getId("\"dadudeldu\"^^"); + auto resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri( + qec->getIndex(), id, LocalVocab{}); + EXPECT_EQ(resultLiteral.value().toStringRepresentation(), "\"dadudeldu\""); + // Case onlyReturnLiterals + resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri( + qec->getIndex(), id, LocalVocab{}); + EXPECT_EQ(resultLiteral.value().toStringRepresentation(), "\"dadudeldu\""); + // Case onlyReturnLiteralsWithXsdString + resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri( + qec->getIndex(), id, LocalVocab{}, true); + EXPECT_EQ(resultLiteral, std::nullopt); } // Case Iri @@ -1727,4 +1737,4 @@ TEST(ExportQueryExecutionTrees, idToLiteralOrIriFunctionality) { qec->getIndex(), id, LocalVocab{}); EXPECT_EQ(resultLiteral, std::nullopt); } -} \ No newline at end of file +}