use of eu.esdihumboldt.cst.functions.groovy.helper.HelperContext in project hale by halestudio.
the class HelperFunctionsExtension method loadFunction.
/**
* Load helper function via reflection from a method.
*
* @param callMethod the method (probably) defining a helper function
* @param helperClass the class defining the method
* @return the loaded helper function or <code>null</code>
*/
@Nullable
protected HelperFunctionWrapper loadFunction(final Method callMethod, Class<?> helperClass) {
int modifiers = callMethod.getModifiers();
// a candidate -> check parameters
Class<?>[] params = callMethod.getParameterTypes();
if (params != null && params.length <= 2) {
// has maximum two parameters
// last parameter may be context parameter
final boolean hasContextParam = params.length >= 1 && params[params.length - 1].equals(HelperContext.class);
// check if there is an actual main parameter
final boolean hasMainParam = (hasContextParam && params.length == 2) || (!hasContextParam && params.length == 1);
final boolean isStatic = Modifier.isStatic(modifiers);
// Get the specification from field
String specFieldOrMethodName = callMethod.getName() + SPEC_END;
Object fieldV = null;
try {
Field field = helperClass.getField(specFieldOrMethodName);
int fieldModifiers = field.getModifiers();
if (Modifier.isStatic(fieldModifiers) && Modifier.isFinal(fieldModifiers)) {
fieldV = field.get(null);
}
} catch (Exception e) {
// do nothing
}
final Object fieldValue = fieldV;
// Get spec from method
Method meth = null;
boolean isSpecStatic = false;
try {
meth = helperClass.getMethod(specFieldOrMethodName, new Class[] { String.class });
int specModifier = meth.getModifiers();
isSpecStatic = Modifier.isStatic(specModifier);
} catch (Exception e) {
// do nothing
}
final Method specMethod = meth;
final boolean isSpecMethodStatic = isSpecStatic;
HelperFunction<Object> function = new ContextAwareHelperFunction<Object>() {
@Override
public Object call(Object arg, HelperContext context) throws Exception {
Object helper = null;
if (!isStatic) {
helper = helperClass.newInstance();
}
if (hasMainParam) {
if (hasContextParam) {
return callMethod.invoke(helper, arg, context);
} else {
return callMethod.invoke(helper, arg);
}
} else {
if (hasContextParam) {
return callMethod.invoke(helper, context);
} else {
return callMethod.invoke(helper);
}
}
}
@Override
public Specification getSpec(String name) throws Exception {
if (fieldValue != null && fieldValue instanceof Specification) {
return ((Specification) fieldValue);
} else if (specMethod != null) {
if (isSpecMethodStatic) {
return (Specification) specMethod.invoke(null, name);
} else {
Object helper = helperClass.newInstance();
return (Specification) specMethod.invoke(helper, name);
}
}
return null;
}
};
// method name
String name = callMethod.getName().substring(1);
return new HelperFunctionWrapper(function, name);
}
return null;
}
use of eu.esdihumboldt.cst.functions.groovy.helper.HelperContext in project hale by halestudio.
the class HelperFunctionsExtension method getChildren.
@Override
public Iterable<HelperFunctionOrCategory> getChildren(Category cat, HelperContext context) {
init();
final HelperContext theContext = extendContext(context);
synchronized (children) {
final Map<String, HelperFunctionOrCategory> catMap = children.get(cat);
if (catMap == null) {
return Collections.emptyList();
} else {
return () -> catMap.values().stream().map(fc -> injectContext(fc, theContext)).iterator();
}
}
}
use of eu.esdihumboldt.cst.functions.groovy.helper.HelperContext in project hale by halestudio.
the class GroovyUtil method createBinding.
/**
* Creates a basic binding used by all Groovy functions.
*
* @param builder the instance builder, may be <code>null</code>
* @param cell the cell of the function
* @param typeCell the type cell the function works on, may be
* <code>null</code>
* @param log the transformation log
* @param executionContext the execution context
* @param targetInstanceType the type of the target instance to create
* @return a basic binding
*/
public static Binding createBinding(InstanceBuilder builder, Cell cell, Cell typeCell, TransformationLog log, ExecutionContext executionContext, TypeDefinition targetInstanceType) {
Binding binding = new Binding();
HelperContext helperContext = new DefaultHelperContext(executionContext, executionContext, cell, typeCell);
binding.setVariable(BINDING_HELPER_FUNCTIONS, HelperFunctions.createDefault(helperContext));
binding.setVariable(BINDING_BUILDER, builder);
binding.setVariable(BINDING_CELL, cell);
TransformationLogWrapper cellLog = new TransformationLogWrapper(log);
binding.setVariable(BINDING_LOG, cellLog);
binding.setVariable(BINDING_CELL_CONTEXT, SynchronizedContextProvider.getContextClosure(executionContext.getCellContext()));
binding.setVariable(BINDING_FUNCTION_CONTEXT, SynchronizedContextProvider.getContextClosure(executionContext.getFunctionContext()));
binding.setVariable(BINDING_TRANSFORMATION_CONTEXT, SynchronizedContextProvider.getContextClosure(executionContext.getTransformationContext()));
// init type cell types
ArrayList<TypeEntityDefinition> sourceTypes = null;
TypeEntityDefinition targetType = null;
if (typeCell != null) {
targetType = ((Type) CellUtil.getFirstEntity(typeCell.getTarget())).getDefinition();
if (typeCell.getSource() != null) {
Collection<? extends Entity> sources = typeCell.getSource().values();
sourceTypes = new ArrayList<>(sources.size());
for (Object entity : sources) {
sourceTypes.add(((Type) entity).getDefinition());
}
}
}
binding.setVariable(BINDING_SOURCE_TYPES, sourceTypes);
binding.setVariable(BINDING_TARGET_TYPE, targetType);
binding.setVariable(BINDING_TARGET, new TargetCollector(builder, targetInstanceType));
binding.setVariable(BINDING_PROJECT, new ProjectAccessor(executionContext.getService(ProjectInfoService.class), cellLog, executionContext));
binding.setVariable(BINDING_SPATIAL_INDEX, executionContext.getService(SpatialIndexService.class));
binding.setVariable(BINDING_INSTANCE_INDEX, executionContext.getService(InstanceIndexService.class));
return binding;
}
Aggregations