package test;
import static org.basex.query.value.type.SeqType.*;
import org.basex.query.*;
import org.basex.query.expr.*;
import org.basex.query.util.list.*;
import org.basex.query.value.*;
import org.basex.query.value.item.*;
import org.basex.query.value.type.*;
import org.basex.query.var.*;
import org.basex.util.hash.*;
public class Test extends QueryModule
{
public FuncItem makeFunction() throws QueryException {
final Var var = new VarScope().addNew(new QNm("input"), STRING_O, queryContext, null);
final Var[] params = { var };
final Expr[] exprs = new Expr[] { new VarRef(null, var) };
final FuncType funcType = FuncType.get(SeqType.STRING_O, STRING_O);
final TestFunction tf = new TestFunction(exprs);
return new FuncItem(null, tf, params, AnnList.EMPTY, funcType, params.length, null);
}
private static final class TestFunction extends Arr {
protected TestFunction(final Expr[] args) {
super(null, SeqType.STRING_O, args);
}
@Override
public Value value(final QueryContext qc) throws QueryException {
return arg(0).value(qc);
}
@Override
public Expr copy(final CompileContext cc, final IntObjMap<Var> vm) {
return copyType(new TestFunction(copyAll(cc, vm, args())));
}
@Override
public void toString(final QueryString qs) {
qs.token("test-function").params(exprs);
}
}
}
named-entity-recognition($grammar as item(), $options as map(*)?) as function(item()) as node()*This is a higher-order XQuery function that returns another (anonymous) XQuery function. The returned function is a parser (for named entities), similar to what the `invisible-xml` function returns.
@Requires(Permission.NONE)
@Deterministic
@ContextDependent
public FuncItem namedEntityRecognition(Object grammar, Map<String, String> options) throws QueryException {
// Types of the arguments of the generated function.
final Var[] generatedFunctionParameters = { new VarScope().addNew(new QNm("input"), SeqType.ITEM_O, queryContext, null) };
// Type of the generated function.
final FuncType generatedFunctionType = FuncType.get(SeqType.NODE_ZM, generatedFunctionParameters[0].declType);
// The generated function.
NamedEntityRecognitionFunction nerf = new NamedEntityRecognitionFunction(grammar, options, generatedFunctionType, queryContext);
// Return a function item.
return new FuncItem(null, nerf, generatedFunctionParameters, AnnList.EMPTY, generatedFunctionType, generatedFunctionParameters.length, null);
}
Value inputValue = arg(0).value(qc); // JavaThe namedEntityRecognition function works fine when I use it like this (XQuery):
let $ner-parse := ner:named-entity-recognition($grammar, map{})However, is does not work when the parsing function is put into a declared variable, like
return $input => $ner-parse()
declare variable $ner-parse as function(item()) as node()* := ner:named-entity-recognition($grammar,map{});In this case, the parameter value obtained by arg(0).value(qc) is null. When I debug this expression, it turns out that the parameter ($input) is not on the stack. The stack (which is qc.stack) is all nulls, and the begin and end indexes of the stack are both 0, meaning that the stack is empty..
$input => $ner-parse()