Search in sources :

Example 1 with HelperContext

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;
}
Also used : HelperContext(eu.esdihumboldt.cst.functions.groovy.helper.HelperContext) Specification(eu.esdihumboldt.cst.functions.groovy.helper.spec.Specification) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) ContextAwareHelperFunction(eu.esdihumboldt.cst.functions.groovy.helper.ContextAwareHelperFunction) Nullable(javax.annotation.Nullable)

Example 2 with HelperContext

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();
        }
    }
}
Also used : HelperFunctionOrCategory(eu.esdihumboldt.cst.functions.groovy.helper.HelperFunctionOrCategory) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ALogger(de.fhg.igd.slf4jplus.ALogger) HelperContext(eu.esdihumboldt.cst.functions.groovy.helper.HelperContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Category(eu.esdihumboldt.cst.functions.groovy.helper.Category) ExtensionUtil(de.fhg.igd.eclipse.util.extension.ExtensionUtil) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Map(java.util.Map) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) HelperFunction(eu.esdihumboldt.cst.functions.groovy.helper.HelperFunction) Method(java.lang.reflect.Method) Splitter(com.google.common.base.Splitter) Nullable(javax.annotation.Nullable) ExecutionContext(eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext) HelperFunctionOrCategory(eu.esdihumboldt.cst.functions.groovy.helper.HelperFunctionOrCategory) ALoggerFactory(de.fhg.igd.slf4jplus.ALoggerFactory) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) ContextAwareHelperFunction(eu.esdihumboldt.cst.functions.groovy.helper.ContextAwareHelperFunction) Field(java.lang.reflect.Field) List(java.util.List) Modifier(java.lang.reflect.Modifier) HelperFunctionsService(eu.esdihumboldt.cst.functions.groovy.helper.HelperFunctionsService) Specification(eu.esdihumboldt.cst.functions.groovy.helper.spec.Specification) Platform(org.eclipse.core.runtime.Platform) Collections(java.util.Collections) HelperContext(eu.esdihumboldt.cst.functions.groovy.helper.HelperContext)

Example 3 with HelperContext

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;
}
Also used : Binding(groovy.lang.Binding) HelperContext(eu.esdihumboldt.cst.functions.groovy.helper.HelperContext) DefaultHelperContext(eu.esdihumboldt.cst.functions.groovy.helper.DefaultHelperContext) DefaultHelperContext(eu.esdihumboldt.cst.functions.groovy.helper.DefaultHelperContext) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) SpatialIndexService(eu.esdihumboldt.hale.common.instance.index.spatial.SpatialIndexService) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)

Aggregations

HelperContext (eu.esdihumboldt.cst.functions.groovy.helper.HelperContext)3 ContextAwareHelperFunction (eu.esdihumboldt.cst.functions.groovy.helper.ContextAwareHelperFunction)2 Specification (eu.esdihumboldt.cst.functions.groovy.helper.spec.Specification)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 Nullable (javax.annotation.Nullable)2 Splitter (com.google.common.base.Splitter)1 ExtensionUtil (de.fhg.igd.eclipse.util.extension.ExtensionUtil)1 ALogger (de.fhg.igd.slf4jplus.ALogger)1 ALoggerFactory (de.fhg.igd.slf4jplus.ALoggerFactory)1 Category (eu.esdihumboldt.cst.functions.groovy.helper.Category)1 DefaultHelperContext (eu.esdihumboldt.cst.functions.groovy.helper.DefaultHelperContext)1 HelperFunction (eu.esdihumboldt.cst.functions.groovy.helper.HelperFunction)1 HelperFunctionOrCategory (eu.esdihumboldt.cst.functions.groovy.helper.HelperFunctionOrCategory)1 HelperFunctionsService (eu.esdihumboldt.cst.functions.groovy.helper.HelperFunctionsService)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 ExecutionContext (eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext)1 ServiceProvider (eu.esdihumboldt.hale.common.core.service.ServiceProvider)1 InstanceIndexService (eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)1