From 110c10223a6a8d3cd4bc6b7cd03203d9bd12493a Mon Sep 17 00:00:00 2001 From: Mark Hale Date: Thu, 10 Oct 2024 10:18:15 +0100 Subject: [PATCH] Add serialize function. --- .../gin/halyard/model/vocabulary/HALYARD.java | 1 + .../algebra/evaluation/function}/Like.java | 7 +++-- .../evaluation/function/Serialize.java | 28 +++++++++++++++++++ .../evaluation/function/SerializeTest.java | 22 +++++++++++++++ ...query.algebra.evaluation.function.Function | 2 -- 5 files changed, 55 insertions(+), 5 deletions(-) rename {spin/src/main/java/com/msd/gin/halyard/spin/function/halyard => queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function}/Like.java (95%) create mode 100644 queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function/Serialize.java create mode 100644 queryalgebra/src/test/java/com/msd/gin/halyard/query/algebra/evaluation/function/SerializeTest.java diff --git a/model/src/main/java/com/msd/gin/halyard/model/vocabulary/HALYARD.java b/model/src/main/java/com/msd/gin/halyard/model/vocabulary/HALYARD.java index 9e5877fa0..448195b56 100644 --- a/model/src/main/java/com/msd/gin/halyard/model/vocabulary/HALYARD.java +++ b/model/src/main/java/com/msd/gin/halyard/model/vocabulary/HALYARD.java @@ -109,6 +109,7 @@ public final class HALYARD implements Vocabulary { public final static IRI DATASET_IRI_FUNCTION = SVF.createIRI(NAMESPACE, "datasetIRI"); public final static IRI PARALLEL_SPLIT_FUNCTION = SVF.createIRI(NAMESPACE, "forkAndFilterBy"); + public final static IRI SERIALIZE_FUNCTION = SVF.createIRI(NAMESPACE, "serialize"); public final static IRI VECTOR_EMBEDDING_FUNCTION = SVF.createIRI(NAMESPACE, "vectorEmbedding"); public final static IRI DATA_URL_FUNCTION = SVF.createIRI(NAMESPACE, "dataURL"); diff --git a/spin/src/main/java/com/msd/gin/halyard/spin/function/halyard/Like.java b/queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function/Like.java similarity index 95% rename from spin/src/main/java/com/msd/gin/halyard/spin/function/halyard/Like.java rename to queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function/Like.java index 4d4ae5ab4..318ec124b 100644 --- a/spin/src/main/java/com/msd/gin/halyard/spin/function/halyard/Like.java +++ b/queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function/Like.java @@ -1,17 +1,18 @@ -package com.msd.gin.halyard.spin.function.halyard; +package com.msd.gin.halyard.query.algebra.evaluation.function; import java.util.Locale; -import org.eclipse.rdf4j.model.IRI; import org.eclipse.rdf4j.model.Literal; import org.eclipse.rdf4j.model.Value; import org.eclipse.rdf4j.model.ValueFactory; import org.eclipse.rdf4j.model.impl.BooleanLiteral; import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException; import org.eclipse.rdf4j.query.algebra.evaluation.function.Function; +import org.kohsuke.MetaInfServices; import com.msd.gin.halyard.model.vocabulary.HALYARD; +@MetaInfServices(Function.class) public class Like implements Function { @Override public String getURI() { @@ -27,7 +28,7 @@ public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExpr Value val = args[0]; String strVal; if (val.isIRI()) { - strVal = ((IRI) val).stringValue(); + strVal = val.stringValue(); } else if (val.isLiteral()) { strVal = ((Literal) val).getLabel(); } else { diff --git a/queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function/Serialize.java b/queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function/Serialize.java new file mode 100644 index 000000000..f4e70e55d --- /dev/null +++ b/queryalgebra/src/main/java/com/msd/gin/halyard/query/algebra/evaluation/function/Serialize.java @@ -0,0 +1,28 @@ +package com.msd.gin.halyard.query.algebra.evaluation.function; + +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.ValueFactory; +import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException; +import org.eclipse.rdf4j.query.algebra.evaluation.function.Function; +import org.eclipse.rdf4j.rio.helpers.NTriplesUtil; +import org.kohsuke.MetaInfServices; + +import com.msd.gin.halyard.model.vocabulary.HALYARD; + +@MetaInfServices(Function.class) +public final class Serialize implements Function { + + @Override + public String getURI() { + return HALYARD.SERIALIZE_FUNCTION.stringValue(); + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + if (args.length != 1) { + throw new ValueExprEvaluationException(String.format("%s requires 1 argument", getURI())); + } + return valueFactory.createLiteral(NTriplesUtil.toNTriplesString(args[0], true)); + } + +} diff --git a/queryalgebra/src/test/java/com/msd/gin/halyard/query/algebra/evaluation/function/SerializeTest.java b/queryalgebra/src/test/java/com/msd/gin/halyard/query/algebra/evaluation/function/SerializeTest.java new file mode 100644 index 000000000..5ba1575cf --- /dev/null +++ b/queryalgebra/src/test/java/com/msd/gin/halyard/query/algebra/evaluation/function/SerializeTest.java @@ -0,0 +1,22 @@ +package com.msd.gin.halyard.query.algebra.evaluation.function; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.eclipse.rdf4j.model.Literal; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.ValueFactory; +import org.eclipse.rdf4j.query.algebra.evaluation.TripleSource; +import org.junit.jupiter.api.Test; + +import com.msd.gin.halyard.query.algebra.evaluation.EmptyTripleSource; + +public class SerializeTest { + @Test + public void test() { + TripleSource ts = new EmptyTripleSource(); + ValueFactory vf = ts.getValueFactory(); + Value v = vf.createTriple(vf.createIRI("http://whatever.com/subj"), vf.createIRI("http://whatever.com/pred"), vf.createLiteral("foobar")); + Literal l = (Literal) new Serialize().evaluate(ts, v); + assertEquals(vf.createLiteral("<< \"foobar\">>"), l); + } +} diff --git a/spin/src/main/resources/META-INF/services/org.eclipse.rdf4j.query.algebra.evaluation.function.Function b/spin/src/main/resources/META-INF/services/org.eclipse.rdf4j.query.algebra.evaluation.function.Function index 30a42cf08..a67ff7e24 100644 --- a/spin/src/main/resources/META-INF/services/org.eclipse.rdf4j.query.algebra.evaluation.function.Function +++ b/spin/src/main/resources/META-INF/services/org.eclipse.rdf4j.query.algebra.evaluation.function.Function @@ -42,5 +42,3 @@ com.msd.gin.halyard.spin.function.spif.UpperCase com.msd.gin.halyard.spin.function.spif.LowerCase com.msd.gin.halyard.spin.function.spif.TitleCase com.msd.gin.halyard.spin.function.spif.LowerTitleCase -# halyard -com.msd.gin.halyard.spin.function.halyard.Like