Skip to content

Commit

Permalink
Update comments. Reformat. Simplify.
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jul 12, 2016
1 parent 4507109 commit 3bbe2d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 82 deletions.
58 changes: 20 additions & 38 deletions jena-arq/src/main/java/org/apache/jena/sparql/expr/E_Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,99 +39,81 @@ public class E_Function extends ExprFunctionN
public static boolean WarnOnUnknownFunction = true ;
private String functionIRI ;

// Only set after a copySubstitute has been done by PlanFilter.
// at which point this instance if not part of the query abstract syntax.
private Function function = null ;
private boolean functionBound = false ;

public E_Function(String functionIRI, ExprList args)
{
public E_Function(String functionIRI, ExprList args) {
super(name, args) ;
this.functionIRI = functionIRI ;
this.functionIRI = functionIRI ;
}

@Override
public String getFunctionIRI() { return functionIRI ; }

// The Function subsystem takes over evaluation via SpecialForms.
// This is merely to allow "function" to behave as special forms
// (this is discouraged).
// Doing the function call in evalSpecial maintains the old
// interface to functions.
// This allows a "function" to behave as special forms (this is discouraged).

@Override
public NodeValue evalSpecial(Binding binding, FunctionEnv env)
{
public NodeValue evalSpecial(Binding binding, FunctionEnv env) {
// Only needed because some tests call straight in.
// Otherwise, the buildFunction() calls should have done everything
if ( ! functionBound )
if ( !functionBound )
buildFunction(env.getContext()) ;
if ( function == null )
throw new ExprEvalException("URI <"+getFunctionIRI()+"> not bound") ;
throw new ExprEvalException("URI <" + getFunctionIRI() + "> not bound") ;
NodeValue r = function.exec(binding, args, getFunctionIRI(), env) ;
return r ;
}

@Override
public NodeValue eval(List<NodeValue> args)
{
// For functions, we delay argument evaluation to the "Function" heierarchy
// so applications can add their own functional forms.
public NodeValue eval(List<NodeValue> args) {
// evalSpecial hands over function evaluation to the "Function" hierarchy
throw new ARQInternalErrorException() ;
}

public void buildFunction(Context cxt)
{
public void buildFunction(Context cxt) {
try { bindFunction(cxt) ; }
catch (ExprException ex)
{
catch (ExprException ex) {
if ( WarnOnUnknownFunction )
ARQ.getExecLogger().warn("URI <"+functionIRI+"> has no registered function factory") ;
}
}

private void bindFunction(Context cxt)
{
private void bindFunction(Context cxt) {
if ( functionBound )
return ;

FunctionRegistry registry = chooseRegistry(cxt) ;
FunctionFactory ff = registry.get(functionIRI) ;

if ( ff == null )
{

if ( ff == null ) {
functionBound = true ;
throw new ExprEvalException("URI <"+functionIRI+"> not found as a function") ;
throw new ExprEvalException("URI <" + functionIRI + "> not found as a function") ;
}
function = ff.create(functionIRI) ;
function.build(functionIRI, args) ;
functionBound = true ;
}

private FunctionRegistry chooseRegistry(Context context)
{
private FunctionRegistry chooseRegistry(Context context) {
FunctionRegistry registry = FunctionRegistry.get(context) ;
if ( registry == null )
registry = FunctionRegistry.get() ;
return registry ;
}

@Override
public String getFunctionPrintName(SerializationContext cxt)
{
public String getFunctionPrintName(SerializationContext cxt) {
return FmtUtils.stringForURI(functionIRI, cxt) ;
}

@Override
public String getFunctionName(SerializationContext cxt)
{
public String getFunctionName(SerializationContext cxt) {
return FmtUtils.stringForURI(functionIRI, cxt) ;
}


@Override
public Expr copy(ExprList newArgs)
{
public Expr copy(ExprList newArgs) {
return new E_Function(getFunctionIRI(), newArgs) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,18 @@
import org.apache.jena.sparql.expr.Expr ;
import org.apache.jena.sparql.expr.ExprList ;
import org.apache.jena.sparql.expr.NodeValue ;
import org.apache.jena.sparql.util.Context ;

/** Interface to value-testing extensions to the expression evaluator. */
/** Impleemntation root for custom function evaluation. */
public abstract class FunctionBase implements Function {

public abstract class FunctionBase implements Function
{
String uri = null ;
protected ExprList arguments = null ;
private FunctionEnv env ;

@Override
public final void build(String uri, ExprList args)
{
this.uri = uri ;
arguments = args ;
public final void build(String uri, ExprList args) {
// Rename for legacy reasons.
checkBuild(uri, args) ;
}

@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env)
{
// This is merely to allow functions to be
// It duplicates code in E_Function/ExprFunctionN.

this.env = env ;

public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
if ( args == null )
// The contract on the function interface is that this should not happen.
throw new ARQInternalErrorException("FunctionBase: Null args list") ;
Expand All @@ -64,24 +50,11 @@ public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv en
}

NodeValue nv = exec(evalArgs) ;
arguments = null ;
return nv ;
}

/** Return the Context object for this execution */
public Context getContext() { return env.getContext() ; }

/** Function call to a list of evaluated argument values */
public abstract NodeValue exec(List<NodeValue> args) ;

public abstract void checkBuild(String uri, ExprList args) ;

// /** Get argument, indexing from 1 **/
// public NodeValue getArg(int i)
// {
// i = i-1 ;
// if ( i < 0 || i >= arguments.size() )
// return null ;
// return (NodeValue)arguments.get(i) ;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,35 @@

package org.apache.jena.sparql.function.library;

//import org.apache.commons.logging.*;

import org.apache.jena.atlas.lib.Lib ;
import org.apache.jena.graph.Node ;
import org.apache.jena.sparql.engine.binding.Binding ;
import org.apache.jena.sparql.expr.ExprEvalException ;
import org.apache.jena.sparql.expr.ExprException ;
import org.apache.jena.sparql.expr.ExprList ;
import org.apache.jena.sparql.expr.NodeValue ;
import org.apache.jena.sparql.function.FunctionBase0 ;
import org.apache.jena.sparql.function.Function ;
import org.apache.jena.sparql.function.FunctionEnv ;
import org.apache.jena.sparql.util.Symbol ;

/** Function that returns the value of a system variable. */

public class SystemVar extends FunctionBase0
/**
* Function that returns the value of a system variable.
*/
public class SystemVar implements Function
{
Symbol systemSymbol ;
private Symbol systemSymbol ;
protected SystemVar(Symbol systemSymbol)
{
if ( systemSymbol == null )
throw new ExprException("System symbol is null ptr") ;
this.systemSymbol = systemSymbol ;
}

/** Processes evaluated args */
// Need to intercept exec so we can get to the FunctionEnv
@Override
public NodeValue exec()
{
Object obj = getContext().get(systemSymbol) ;

public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
// Ignore arguments.
Object obj = env.getContext().get(systemSymbol) ;
if ( obj == null )
throw new ExprEvalException("null for system symbol: "+systemSymbol) ;
if ( ! ( obj instanceof Node ) )
Expand All @@ -58,4 +59,7 @@ public NodeValue exec()
NodeValue nv = NodeValue.makeNode(n) ;
return nv ;
}

@Override
public void build(String uri, ExprList args) {}
}

0 comments on commit 3bbe2d8

Please sign in to comment.