forked from apache/jena
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-1374: add Context#withFunctionRegistries(Context) and QueryE…
…xecutionFactory#create(Query, Graph, Context) helpers
- Loading branch information
Showing
6 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
jena-arq/src/main/java/org/apache/jena/sparql/util/ContextUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
jena-arq/src/test/java/org/apache/jena/sparql/util/TestContextUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |