Skip to content

Commit

Permalink
apacheGH-1374: add Context#withFunctionRegistries(Context) and QueryE…
Browse files Browse the repository at this point in the history
…xecutionFactory#create(Query, Graph, Context) helpers
  • Loading branch information
sszuev authored and Aklakan committed Jul 10, 2022
1 parent ba61155 commit 45399f3
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ public static void set(Context context, FunctionRegistry reg) {
context.set(ARQConstants.registryFunctions, reg);
}

/**
* Copies the origin registry into a new one, or makes a fresh instance if the specified registry is {@code null).
* @param from {@link FunctionRegistry } or {@code null}
* @return {@link FunctionRegistry} a new instance
*/
public static FunctionRegistry createFrom(FunctionRegistry from) {
FunctionRegistry res = new FunctionRegistry();
if (from != null) {
res.registry.putAll(from.registry);
}
return res;
}

public FunctionRegistry()
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ public synchronized static PropertyFunctionRegistry get() {
return reg;
}

/**
* Copies the origin registry into a new one, or makes a fresh instance if the specified registry is {@code null).
* @param from {@link PropertyFunctionRegistry} or {@code null}
* @return {@link PropertyFunctionRegistry} a new instance
*/
public static PropertyFunctionRegistry createFrom(PropertyFunctionRegistry from) {
PropertyFunctionRegistry res = new PropertyFunctionRegistry();
if (from != null) {
res.registry.putAll(from.registry);
}
return res;
}

/** Insert an PropertyFunction class.
* Re-inserting with the same URI overwrites the old entry.
* New instance created on retrieval (auto-factory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ public static void set(Context context, ServiceExecutorRegistry reg)
context.set(ARQConstants.registryServiceExecutors, reg) ;
}

/**
* Copies the origin registry into a new one, or makes a fresh instance if the specified registry is {@code null).
* @param from {@link ServiceExecutorRegistry} or {@code null}
* @return {@link ServiceExecutorRegistry} a new instance
*/
public static ServiceExecutorRegistry createFrom(ServiceExecutorRegistry from) {
ServiceExecutorRegistry res = new ServiceExecutorRegistry();
if (from != null) {
res.bulkChain.addAll(from.bulkChain);
res.singleChain.addAll(from.singleChain);
}
return res;
}

public ServiceExecutorRegistry()
{}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.apache.jena.sparql.util;

import org.apache.jena.sparql.function.FunctionRegistry;
import org.apache.jena.sparql.pfunction.PropertyFunctionRegistry;
import org.apache.jena.sparql.service.ServiceExecutorRegistry;

/**
* Utils to work with {@link Context}.
*/
public class ContextUtils {

/**
* Copies the given context also copying its registries
* ({@link FunctionRegistry}, {@link PropertyFunctionRegistry} and {@link ServiceExecutorRegistry}).
* If the input context is null, then method just creates a new empty instance.
*
* @param from {@link Context} or {@code null}
* @return a new {@link Context} instance
*/
public static Context copyWithRegistries(Context from) {
FunctionRegistry fr = FunctionRegistry.createFrom(FunctionRegistry.get(from));
PropertyFunctionRegistry pfr = PropertyFunctionRegistry.createFrom(PropertyFunctionRegistry.get(from));
ServiceExecutorRegistry ser = ServiceExecutorRegistry.createFrom(ServiceExecutorRegistry.get(from));
Context res = from == null ? new Context() : from.copy();
FunctionRegistry.set(res, fr);
PropertyFunctionRegistry.set(res, pfr);
ServiceExecutorRegistry.set(res, ser);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
TestDateTimeParsing.class ,
TestList.class ,
TestFmtUtils.class,
TestVersion.class
TestVersion.class,
TestContextUtils.class,
})
public class TS_Util
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.apache.jena.sparql.util;

import org.apache.jena.graph.Node;
import org.apache.jena.sparql.ARQConstants;
import org.apache.jena.sparql.engine.ExecutionContext;
import org.apache.jena.sparql.engine.QueryIterator;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.function.Function;
import org.apache.jena.sparql.function.FunctionBase1;
import org.apache.jena.sparql.function.FunctionFactory;
import org.apache.jena.sparql.function.FunctionRegistry;
import org.apache.jena.sparql.pfunction.PFuncSimple;
import org.apache.jena.sparql.pfunction.PropertyFunction;
import org.apache.jena.sparql.pfunction.PropertyFunctionFactory;
import org.apache.jena.sparql.pfunction.PropertyFunctionRegistry;
import org.apache.jena.sparql.service.ServiceExecutorRegistry;
import org.apache.jena.sparql.service.bulk.ChainingServiceExecutorBulk;
import org.apache.jena.sparql.service.single.ChainingServiceExecutor;
import org.apache.jena.sys.JenaSystem;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* To test utility {@link ContextUtils} methods.
*/
public class TestContextUtils {
@BeforeClass
public static void beforeClass() {
JenaSystem.init();
}

private static Function mockFunction() {
return new FunctionBase1() {
@Override
public NodeValue exec(NodeValue v) {
return null;
}
};
}

private static PropertyFunction mockPropertyFunction() {
return new PFuncSimple() {
@Override
public QueryIterator execEvaluated(Binding b, Node s, Node p, Node o, ExecutionContext ec) {
return null;
}
};
}

private static ChainingServiceExecutorBulk mockChainingServiceExecutorBulk() {
return (opService, input, execCxt, chain) -> null;
}

private static ChainingServiceExecutor mockChainingServiceExecutor() {
return (opExecute, opOriginal, binding, execCxt, chain) -> null;
}

@Test
public void testCopyWithRegistries() {
Context givenContext = new Context();
FunctionFactory givenFunctionFactory = uri -> mockFunction();
PropertyFunctionFactory givenPFunctionFactory = uri -> mockPropertyFunction();
ChainingServiceExecutorBulk givenServiceExecutionFactory = mockChainingServiceExecutorBulk();
ChainingServiceExecutor givenChainingServiceExecutor = mockChainingServiceExecutor();

PropertyFunctionRegistry givenPFunctionRegistry = new PropertyFunctionRegistry();
FunctionRegistry givenFunctionRegistry = new FunctionRegistry();
ServiceExecutorRegistry givenServiceExecutorRegistry = new ServiceExecutorRegistry();
givenFunctionRegistry.put("x", givenFunctionFactory);
givenPFunctionRegistry.put("y", givenPFunctionFactory);
givenServiceExecutorRegistry.addBulkLink(givenServiceExecutionFactory);
givenServiceExecutorRegistry.addSingleLink(givenChainingServiceExecutor);

givenContext.put(ARQConstants.registryFunctions, givenFunctionRegistry);
givenContext.put(ARQConstants.registryPropertyFunctions, givenPFunctionRegistry);
givenContext.put(ARQConstants.registryServiceExecutors, givenServiceExecutorRegistry);

Context actualContext = ContextUtils.copyWithRegistries(givenContext);
Assert.assertNotNull(actualContext);
Assert.assertNotSame(givenContext, actualContext);

PropertyFunctionRegistry actualPFunctionRegistry = PropertyFunctionRegistry.get(actualContext);
FunctionRegistry actualFunctionRegistry = FunctionRegistry.get(actualContext);
ServiceExecutorRegistry actualServiceExecutorRegistry = ServiceExecutorRegistry.get(actualContext);

Assert.assertNotNull(actualPFunctionRegistry);
Assert.assertNotNull(actualFunctionRegistry);
Assert.assertNotNull(actualServiceExecutorRegistry);

Assert.assertNotSame(givenFunctionRegistry, actualFunctionRegistry);
Assert.assertNotSame(givenPFunctionRegistry, actualPFunctionRegistry);
Assert.assertNotSame(givenServiceExecutorRegistry, actualServiceExecutorRegistry);

List<FunctionFactory> actualFunctionFactories = new ArrayList<>();
actualFunctionRegistry.keys().forEachRemaining(k -> actualFunctionFactories.add(actualFunctionRegistry.get(k)));
List<PropertyFunctionFactory> actualPFunctionFactories = new ArrayList<>();
actualPFunctionRegistry.keys().forEachRemaining(k -> actualPFunctionFactories.add(actualPFunctionRegistry.get(k)));

Assert.assertSame(givenPFunctionFactory, actualPFunctionRegistry.get("y"));
Assert.assertSame(givenFunctionFactory, actualFunctionRegistry.get("x"));
Assert.assertEquals(List.of(givenPFunctionFactory), actualPFunctionFactories);
Assert.assertEquals(List.of(givenFunctionFactory), actualFunctionFactories);
Assert.assertEquals(List.of(givenServiceExecutionFactory), actualServiceExecutorRegistry.getBulkChain());
Assert.assertEquals(List.of(givenChainingServiceExecutor), actualServiceExecutorRegistry.getSingleChain());
}
}

0 comments on commit 45399f3

Please sign in to comment.