Skip to content

Commit

Permalink
Add more apf functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Hale committed Oct 11, 2024
1 parent 70044c4 commit 24ee750
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 27 deletions.
27 changes: 27 additions & 0 deletions model/src/main/java/com/msd/gin/halyard/model/vocabulary/APF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.msd.gin.halyard.model.vocabulary;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.impl.SimpleNamespace;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.kohsuke.MetaInfServices;

/**
* http://jena.apache.org/ARQ/property#.
*/
@MetaInfServices(Vocabulary.class)
public final class APF implements Vocabulary {
private static final SimpleValueFactory SVF = SimpleValueFactory.getInstance();

/**
* http://jena.apache.org/ARQ/property#
*/
public static final String NAMESPACE = "http://jena.apache.org/ARQ/property#";

public static final String PREFIX = "apf";

public static final Namespace NS = new SimpleNamespace(PREFIX, NAMESPACE);
public static final IRI STR_SPLIT = SVF.createIRI(NAMESPACE, "strSplit");
public static final IRI CONCAT = SVF.createIRI(NAMESPACE, "concat");
public static final IRI SPLIT_IRI = SVF.createIRI(NAMESPACE, "splitIRI");
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.vocabulary.APF;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.algebra.evaluation.function.string.Concat;

import com.msd.gin.halyard.model.vocabulary.APF;
import com.msd.gin.halyard.spin.function.InverseMagicProperty;

public class ConcatTupleFunction implements InverseMagicProperty {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.msd.gin.halyard.spin.function.apf;

import java.util.Arrays;
import java.util.List;

import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.eclipse.rdf4j.common.iteration.SingletonIteration;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException;
import org.eclipse.rdf4j.query.algebra.evaluation.function.TupleFunction;

import com.msd.gin.halyard.model.vocabulary.APF;

public class SplitIRI implements TupleFunction {

@Override
public String getURI() {
return APF.SPLIT_IRI.toString();
}

@Override
public CloseableIteration<? extends List<? extends Value>> evaluate(ValueFactory valueFactory, Value... args) throws QueryEvaluationException {
if (args.length != 1 || !args[0].isIRI()) {
throw new ValueExprEvaluationException(String.format("%s requires an IRI", getURI()));
}
IRI iri = (IRI) args[0];
return new SingletonIteration<List<? extends Value>>(
Arrays.asList(valueFactory.createLiteral(iri.getNamespace()), valueFactory.createLiteral(iri.getLocalName())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
*******************************************************************************/
package com.msd.gin.halyard.spin.function.apf;

import com.msd.gin.halyard.model.vocabulary.APF;
import com.msd.gin.halyard.spin.function.spif.Split;

import org.eclipse.rdf4j.model.vocabulary.APF;

public class StrSplit extends Split {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ com.msd.gin.halyard.spin.function.spif.ForEach
com.msd.gin.halyard.spin.function.spif.For
com.msd.gin.halyard.spin.function.spif.Split
# apf
com.msd.gin.halyard.spin.function.apf.SplitIRI
com.msd.gin.halyard.spin.function.apf.StrSplit
com.msd.gin.halyard.spin.function.apf.ConcatTupleFunction
# list
Expand Down
79 changes: 79 additions & 0 deletions spin/src/test/java/com/msd/gin/halyard/spin/SailApfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.msd.gin.halyard.spin;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.eclipse.rdf4j.common.iteration.Iterations;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.BooleanQuery;
import org.eclipse.rdf4j.query.QueryLanguage;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.sail.Sail;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;

public class SailApfTest {

private Repository repo;

private RepositoryConnection conn;

@Before
public final void setup() throws Exception {
repo = new SailRepository(createSail());
repo.init();
conn = repo.getConnection();
}

protected Sail createSail() throws Exception {
return new SpinMemoryStore();
}

@After
public final void tearDown() throws Exception {
if (conn != null) {
conn.close();
}
if (repo != null) {
repo.shutDown();
}
postCleanup();
}

protected void postCleanup() throws Exception {
}

@Test
public void testConcat() throws Exception {
TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, "prefix apf: <http://jena.apache.org/ARQ/property#>\n" + "\n" + "select ?text where {\n" + " ?text apf:concat (\"very\" \"sour\" \"berry\") . }");
try (TupleQueryResult tqresult = tq.evaluate()) {
Assert.assertEquals("verysourberry", tqresult.next().getValue("text").stringValue());
}
}

@Test
public void testStrSplit() throws Exception {
TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, "prefix apf: <http://jena.apache.org/ARQ/property#>\n" + "\n" + "select ?text where {\n" + " ?text apf:strSplit (\"very:sour:berry\" \":\") . }");
try (TupleQueryResult tqr = tq.evaluate()) {
List<BindingSet> resultList = Iterations.asList(tqr);
List<String> resultStringList = Lists.transform(resultList, (BindingSet input) -> input.getValue("text").stringValue());

Assert.assertArrayEquals(new String[] { "very", "sour", "berry" }, resultStringList.toArray(new String[resultStringList.size()]));
}
}

@Test
public void testSplitIRI() throws Exception {
BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix apf: <http://jena.apache.org/ARQ/property#> " + "ask where { apf:foobar apf:splitIRI (?ns ?ln) filter(?ns = str(apf:) && ?ln = 'foobar')}");
assertTrue(bq.evaluate());
}
}
25 changes: 1 addition & 24 deletions spin/src/test/java/com/msd/gin/halyard/spin/SailSpifTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Locale;

import org.eclipse.rdf4j.common.iteration.Iterations;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
Expand All @@ -24,11 +22,9 @@
import org.eclipse.rdf4j.rio.helpers.NTriplesUtil;
import org.eclipse.rdf4j.sail.Sail;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.msd.gin.halyard.model.ArrayLiteral;
import com.msd.gin.halyard.model.TupleLiteral;

Expand Down Expand Up @@ -210,7 +206,7 @@ public void testSplit() throws Exception {
try (TupleQueryResult tqr = tq.evaluate()) {
for (int i = 1; i <= 3; i++) {
BindingSet bs = tqr.next();
assertThat(((Literal) bs.getValue("x")).stringValue()).isEqualTo(Integer.toString(i));
assertThat(bs.getValue("x").stringValue()).isEqualTo(Integer.toString(i));
}
assertFalse(tqr.hasNext());
}
Expand All @@ -229,23 +225,4 @@ public void testCantInvoke() throws Exception {
BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix spif: <http://spinrdf.org/spif#> " + "ask where {filter(spif:canInvoke(spif:indexOf, 'foobar', 2))}");
assertFalse(bq.evaluate());
}

@Test
public void testConcat() throws Exception {
TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, "prefix apf: <http://jena.hpl.hp.com/ARQ/property#>\n" + "\n" + "select ?text where {\n" + " ?text apf:concat (\"very\" \"sour\" \"berry\") . }");
try (TupleQueryResult tqresult = tq.evaluate()) {
Assert.assertEquals("verysourberry", tqresult.next().getValue("text").stringValue());
}
}

@Test
public void testStrSplit() throws Exception {
TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, "prefix apf: <http://jena.hpl.hp.com/ARQ/property#>\n" + "\n" + "select ?text where {\n" + " ?text apf:strSplit (\"very:sour:berry\" \":\") . }");
try (TupleQueryResult tqr = tq.evaluate()) {
List<BindingSet> resultList = Iterations.asList(tqr);
List<String> resultStringList = Lists.transform(resultList, (BindingSet input) -> input.getValue("text").stringValue());

Assert.assertArrayEquals(new String[] { "very", "sour", "berry" }, resultStringList.toArray(new String[resultStringList.size()]));
}
}
}

0 comments on commit 24ee750

Please sign in to comment.