Skip to content

Commit

Permalink
Add Test IdToLiteralOrIri and some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Annika Greif committed Nov 24, 2024
1 parent 2581f4c commit 6d7a2b2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 51 deletions.
70 changes: 33 additions & 37 deletions src/engine/ExportQueryExecutionTrees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,35 @@ ExportQueryExecutionTrees::idToLiteralOrIriForEncodedValue(
return LiteralOrIri::literalWithoutQuotes(optionalStringAndType->first);
}

// _____________________________________________________________________________
std::optional<LiteralOrIri> ExportQueryExecutionTrees::handleIriOrLiteral(
const LiteralOrIri& word, bool onlyReturnLiterals,
bool onlyReturnLiteralsWithXsdString) {
auto datatypeIsXSDString = [](const LiteralOrIri& word) -> bool {
return word.hasDatatype() &&
std::string_view(
reinterpret_cast<const char*>(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(
Expand Down Expand Up @@ -433,57 +462,24 @@ std::optional<LiteralOrIri> 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<LiteralOrIri> {
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<const char*>(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<const char*>(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(
index.indexToString(id.getWordVocabIndex()));
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,
Expand Down
5 changes: 5 additions & 0 deletions src/engine/ExportQueryExecutionTrees.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ class ExportQueryExecutionTrees {
static std::optional<LiteralOrIri> idToLiteralOrIriForEncodedValue(
Id id, bool onlyReturnLiteralsWithXsdString = false);

// Checks and processes a LiteralOrIri based on the given flags.
static std::optional<LiteralOrIri> 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,
Expand Down
4 changes: 1 addition & 3 deletions src/engine/sparqlExpressions/StringExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constexpr auto toLiteral = [](std::string_view normalizedContent) {

constexpr auto toLiteralWithDescriptor =
[](std::string_view normalizedContent,
std::optional<std::variant<Iri, std::string>> descriptor) {
const std::optional<std::variant<Iri, std::string>> descriptor) {
return LiteralOrIri{
ad_utility::triple_component::Literal::literalWithNormalizedContent(
asNormalizedStringViewUnsafe(normalizedContent), descriptor)};
Expand Down Expand Up @@ -247,8 +247,6 @@ class SubstrImpl {

using SubstrExpression = NARY<3, FV<SubstrImpl, LiteralOrIriValueGetter,
NumericValueGetter, NumericValueGetter>>;
// using SubstrExpression =
// StringExpressionImpl<3, SubstrImpl, NumericValueGetter, NumericValueGetter>;

// STRSTARTS
[[maybe_unused]] auto strStartsImpl = [](std::string_view text,
Expand Down
32 changes: 21 additions & 11 deletions test/ExportQueryExecutionTreesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,8 +1640,9 @@ TEST(ExportQueryExecutionTrees, convertGeneratorForChunkedTransfer) {

TEST(ExportQueryExecutionTrees, idToLiteralOrIriFunctionality) {
std::string kg =
"<s> <p> \"something\" . <s> <p> 1. <s> <p> "
"\"some^^<http://www.w3.org/2001/XMLSchema#string>\" .";
"<s> <p> \"something\" . <s> <p> 1 . <s> <p> "
"\"some\"^^<http://www.w3.org/2001/XMLSchema#string> . <s> <p> "
"\"dadudeldu\"^^<http://www.dadudeldu.com/NoSuchDatatype> .";
auto qec = ad_utility::testing::getQec(kg);
auto getId = ad_utility::testing::makeGetId(qec->getIndex());
using enum Datatype;
Expand All @@ -1664,28 +1665,37 @@ TEST(ExportQueryExecutionTrees, idToLiteralOrIriFunctionality) {

// Case Literal With Datatype String
{
Id id = getId("\"some^^<http://www.w3.org/2001/XMLSchema#string>\"");
Id id = getId("\"some\"^^<http://www.w3.org/2001/XMLSchema#string>");
auto resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri(
qec->getIndex(), id, LocalVocab{});
EXPECT_EQ(resultLiteral.value().toStringRepresentation(),
"\"some^^<http://www.w3.org/2001/XMLSchema#string>\"");
// TODO: Problem: The Literal has no Datatype
EXPECT_EQ(resultLiteral.value().hasDatatype(), false);
"\"some\"^^<http://www.w3.org/2001/XMLSchema#string>");
// Case onlyReturnLiterals
resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri<true>(
qec->getIndex(), id, LocalVocab{});
EXPECT_EQ(resultLiteral.value().toStringRepresentation(),
"\"some^^<http://www.w3.org/2001/XMLSchema#string>\"");
"\"some\"^^<http://www.w3.org/2001/XMLSchema#string>");
// Case onlyReturnLiteralsWithXsdString
resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri(
qec->getIndex(), id, LocalVocab{}, true);
EXPECT_EQ(resultLiteral.value().toStringRepresentation(),
"\"some^^<http://www.w3.org/2001/XMLSchema#string>\"");
"\"some\"^^<http://www.w3.org/2001/XMLSchema#string>");
}

// TODO: Case Literal With Datatype not equal String
// Case Literal With Datatype not equal String
{

Id id = getId("\"dadudeldu\"^^<http://www.dadudeldu.com/NoSuchDatatype>");
auto resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri(
qec->getIndex(), id, LocalVocab{});
EXPECT_EQ(resultLiteral.value().toStringRepresentation(), "\"dadudeldu\"");
// Case onlyReturnLiterals
resultLiteral = ExportQueryExecutionTrees::idToLiteralOrIri<true>(
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
Expand Down Expand Up @@ -1727,4 +1737,4 @@ TEST(ExportQueryExecutionTrees, idToLiteralOrIriFunctionality) {
qec->getIndex(), id, LocalVocab{});
EXPECT_EQ(resultLiteral, std::nullopt);
}
}
}

0 comments on commit 6d7a2b2

Please sign in to comment.