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 10, 2024
1 parent 70044c4 commit a4c42ee
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 3 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
63 changes: 63 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,63 @@
package com.msd.gin.halyard.spin;

import static org.junit.Assert.assertTrue;

import org.eclipse.rdf4j.query.BooleanQuery;
import org.eclipse.rdf4j.query.QueryLanguage;
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.Before;
import org.junit.Test;

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 {
BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix apf: <http://jena.apache.org/ARQ/property#> " + "ask where { ?y apf:concat ('Foo' ' ' 'Bar') filter(?y = 'Foo Bar')}");
assertTrue(bq.evaluate());
}

@Test
public void testStrSplit() throws Exception {
BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix apf: <http://jena.apache.org/ARQ/property#> " + "ask where { ?y apf:strSplit ('Foo Bar' '\\\\s+') filter(?y in ('Foo', 'Bar'))}");
assertTrue(bq.evaluate());
}

@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());
}
}

0 comments on commit a4c42ee

Please sign in to comment.