| /********************************************************************* |
| * Copyright (c) 2019 The University of York. |
| * |
| * This program and the accompanying materials are made |
| * available under the terms of the Eclipse Public License 2.0 |
| * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| **********************************************************************/ |
| package org.eclipse.epsilon.executors.eol; |
| |
| import java.io.File; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Optional; |
| |
| import org.eclipse.epsilon.common.parse.problem.ParseProblem; |
| import org.eclipse.epsilon.eol.IEolModule; |
| import org.eclipse.epsilon.eol.concurrent.EolModuleParallel; |
| import org.eclipse.epsilon.eol.dom.Operation; |
| import org.eclipse.epsilon.eol.exceptions.EolRuntimeException; |
| import org.eclipse.epsilon.eol.models.IModel; |
| import org.eclipse.epsilon.eol.types.IToolNativeTypeDelegate; |
| import org.eclipse.epsilon.erl.execute.RuleProfiler; |
| import org.eclipse.epsilon.executors.ModuleWrap; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * The EOL executor |
| * |
| * By default the complete EOL script is executed. Alternatively, a specific operation can be |
| * invoked by setting the desired operationName name (see {@link #setOperationName(String)}). |
| * |
| * Additionally, the {@link #setOperationArguments(List)} method can be used to provide a list of |
| * arguments to use for invocation of the operation. The arguments are considered to be in the same |
| * order and of the correct type of the operation signature. |
| * |
| * @author Horacio Hoyos Rodriguez |
| * @since 1.6 |
| */ |
| public class SimpleEolExecutor implements EolExecutor { |
| |
| private static final Logger logger = LoggerFactory.getLogger(SimpleEolExecutor.class); |
| |
| private final Optional<String> operationName; |
| private final List<Object> arguments; |
| private final EolMode mode; |
| private IEolModule module; |
| private ModuleWrap helper; |
| |
| public SimpleEolExecutor() { |
| this(null, Collections.emptyList()); |
| } |
| |
| public SimpleEolExecutor(IEolModule mdl) { |
| this(null, Collections.emptyList(), mdl); |
| } |
| |
| public SimpleEolExecutor(String oprtnNm, List<Object> arguments) { |
| this(oprtnNm, arguments, 1); |
| } |
| |
| public SimpleEolExecutor(String oprtnNm, List<Object> argmnts, int nmbrThrds) { |
| this(oprtnNm, argmnts, new EolModuleParallel(nmbrThrds)); |
| } |
| |
| public SimpleEolExecutor(String oprtnNm, List<Object> argmnts, IEolModule mdl) { |
| operationName = Optional.ofNullable(oprtnNm); |
| mode = operationName.isPresent() ? EolMode.OPERATION : EolMode.SCRIPT; |
| arguments = argmnts; |
| module = mdl; |
| helper = new ModuleWrap(module); |
| } |
| |
| @Override |
| public Object execute() throws EolRuntimeException { |
| switch(mode) { |
| case SCRIPT: |
| logger.info("Executing complete EOL script."); |
| return module.execute(); |
| case OPERATION: |
| Operation operation = module |
| .getDeclaredOperations() |
| .getOperation(operationName.orElseThrow(() -> new EolRuntimeException("Can not invoke executor in OPERATION mode without an operation name assigned."))); |
| logger.info("Executing EOL operation {} with arguments: {}.", operationName, arguments); |
| return operation.execute(null, arguments, module.getContext()); |
| } |
| return null; |
| } |
| |
| @Override |
| public SimpleEolExecutor changeOperation(String operationName, List<Object> arguments) { |
| return new SimpleEolExecutor(operationName, arguments); |
| } |
| |
| @Override |
| public SimpleEolExecutor changeOperation(String operationName) { |
| return new SimpleEolExecutor(operationName, Collections.emptyList()); |
| } |
| |
| public boolean parse(File file) throws Exception { |
| return helper.parse(file); |
| } |
| |
| public boolean parse(String code) throws Exception { |
| return helper.parse(code); |
| } |
| |
| public List<ParseProblem> getParseProblems() { |
| return helper.getParseProblems(); |
| } |
| |
| public void addModels(Collection<IModel> models) { |
| helper.addModels(models); |
| } |
| |
| public void addParamters(Map<String, ?> parameters) { |
| helper.addParamters(parameters); |
| } |
| |
| public void addNativeTypeDelegates(Collection<IToolNativeTypeDelegate> nativeDelegates) { |
| helper.addNativeTypeDelegates(nativeDelegates); |
| } |
| |
| public Optional<RuleProfiler> getRuleProfiler() { |
| return helper.getRuleProfiler(); |
| } |
| |
| public void disposeModelRepository() { |
| helper.disposeModelRepository(); |
| } |
| |
| public void clearModelRepository() { |
| helper.clearModelRepository(); |
| } |
| |
| public void dispose() { |
| helper.dispose(); |
| } |
| |
| public void preProcess() { |
| |
| } |
| |
| public void postProcess() { |
| |
| } |
| |
| } |