Skip to content

Commit

Permalink
Add serialize function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Hale committed Oct 10, 2024
1 parent 3616c94 commit 110c102
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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("<<<http://whatever.com/subj> <http://whatever.com/pred> \"foobar\">>"), l);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 110c102

Please sign in to comment.