| /** |
| * Copyright (c) 2020 Robert Bosch GmbH. |
| * |
| * 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 |
| * |
| * Contributors: |
| * Robert Bosch GmbH - initial API and implementation |
| */ |
| package org.eclipse.app4mc.slg.commons.m2t.translationUnits; |
| |
| import java.util.ArrayList; |
| import java.util.LinkedHashSet; |
| import java.util.LinkedList; |
| import java.util.function.BiConsumer; |
| import java.util.function.Consumer; |
| |
| import org.eclipse.app4mc.amalthea.model.ActivityGraph; |
| import org.eclipse.app4mc.amalthea.model.ActivityGraphItem; |
| import org.eclipse.app4mc.amalthea.model.Component; |
| import org.eclipse.app4mc.amalthea.model.RunnableCall; |
| import org.eclipse.app4mc.amalthea.model.Tag; |
| import org.eclipse.app4mc.amalthea.model.Task; |
| import org.eclipse.app4mc.slg.commons.m2t.transformers.LabelTransformer; |
| import org.eclipse.app4mc.slg.commons.m2t.transformers.RunnableTransformer; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.xtend2.lib.StringConcatenation; |
| import org.eclipse.xtext.xbase.lib.Functions.Function1; |
| import org.eclipse.xtext.xbase.lib.IterableExtensions; |
| |
| @SuppressWarnings("unused") |
| public class TaskTranslationUnit extends TranslationUnit { |
| public RunnableTransformer runnableTransformer; |
| |
| public LabelTransformer labelTransformer; |
| |
| public String getBasePath() { |
| return ""; |
| } |
| |
| public String getModuleName() { |
| return this.task.getName(); |
| } |
| |
| public String getInitCall() { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("initialize_task_"); |
| String _moduleName = this.getModuleName(); |
| _builder.append(_moduleName); |
| _builder.append("()"); |
| return _builder.toString(); |
| } |
| |
| public String stepCall() { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("step_task_"); |
| String _moduleName = this.getModuleName(); |
| _builder.append(_moduleName); |
| _builder.append("()"); |
| return _builder.toString(); |
| } |
| |
| private final Task task; |
| |
| private final Component component; |
| |
| private OutputBuffer outputBuffer; |
| |
| public TaskTranslationUnit(final OutputBuffer outputBuffer, final RunnableTransformer runnableTransformer, final LabelTransformer labelTransformer, final Task task, final Component component) { |
| super(); |
| this.runnableTransformer = runnableTransformer; |
| this.labelTransformer = labelTransformer; |
| this.task = task; |
| this.component = component; |
| this.outputBuffer = outputBuffer; |
| this.genFiles(); |
| } |
| |
| public TaskTranslationUnit(final OutputBuffer outputBuffer, final RunnableTransformer runnableTransformer, final LabelTransformer labelTransformer, final Task task) { |
| super(); |
| this.task = task; |
| this.component = null; |
| this.runnableTransformer = runnableTransformer; |
| this.labelTransformer = labelTransformer; |
| this.outputBuffer = outputBuffer; |
| } |
| |
| public boolean genFiles() { |
| boolean _xblockexpression = false; |
| { |
| final LinkedHashSet<String> includes = new LinkedHashSet<String>(); |
| final LinkedList<String> initCalls = new LinkedList<String>(); |
| final LinkedList<String> stepCalls = new LinkedList<String>(); |
| ActivityGraph _activityGraph = null; |
| if (this.task!=null) { |
| _activityGraph=this.task.getActivityGraph(); |
| } |
| EList<ActivityGraphItem> _items = null; |
| if (_activityGraph!=null) { |
| _items=_activityGraph.getItems(); |
| } |
| final Consumer<ActivityGraphItem> _function = new Consumer<ActivityGraphItem>() { |
| @Override |
| public void accept(final ActivityGraphItem item) { |
| if ((item instanceof RunnableCall)) { |
| final org.eclipse.app4mc.amalthea.model.Runnable runnable = ((RunnableCall) item).getRunnable(); |
| final RunnableTranslationUnit tu = TaskTranslationUnit.this.runnableTransformer.transform(((org.eclipse.app4mc.amalthea.model.Runnable) runnable)); |
| includes.add(tu.getIncFile()); |
| final Function1<Tag, Boolean> _function = new Function1<Tag, Boolean>() { |
| @Override |
| public Boolean apply(final Tag tag) { |
| return Boolean.valueOf("initialize".equals(tag.getName())); |
| } |
| }; |
| boolean _isEmpty = IterableExtensions.isEmpty(IterableExtensions.<Tag>filter(((RunnableCall)item).getTags(), _function)); |
| boolean _not = (!_isEmpty); |
| if (_not) { |
| initCalls.add(tu.getCall()); |
| } else { |
| stepCalls.add(tu.getCall()); |
| } |
| } |
| } |
| }; |
| _items.forEach(_function); |
| final BiConsumer<ArrayList<?>, LabelTranslationUnit> _function_1 = new BiConsumer<ArrayList<?>, LabelTranslationUnit>() { |
| @Override |
| public void accept(final ArrayList<?> label, final LabelTranslationUnit tu) { |
| includes.add(tu.getIncFile()); |
| initCalls.add(tu.getInitCall()); |
| } |
| }; |
| this.labelTransformer.getCache().forEach(_function_1); |
| this.incAppend(this.toH()); |
| _xblockexpression = this.srcAppend(this.toCpp(includes, initCalls, stepCalls)); |
| } |
| return _xblockexpression; |
| } |
| |
| private String toH() { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.newLine(); |
| _builder.append("//Header of Task "); |
| String _moduleName = this.getModuleName(); |
| _builder.append(_moduleName); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("#include <stdint.h>"); |
| _builder.newLine(); |
| _builder.newLine(); |
| _builder.append("class "); |
| String _moduleName_1 = this.getModuleName(); |
| _builder.append(_moduleName_1); |
| _builder.append(" {"); |
| _builder.newLineIfNotEmpty(); |
| _builder.newLine(); |
| _builder.append("public:"); |
| _builder.newLine(); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("//constructor"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| String _moduleName_2 = this.getModuleName(); |
| _builder.append(_moduleName_2, "\t"); |
| _builder.append("(void);"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("// destructor"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("virtual ~"); |
| String _moduleName_3 = this.getModuleName(); |
| _builder.append(_moduleName_3, "\t"); |
| _builder.append("() =default;"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("void "); |
| String _initCall = this.getInitCall(); |
| _builder.append(_initCall, "\t"); |
| _builder.append(";"); |
| _builder.newLineIfNotEmpty(); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("void "); |
| String _stepCall = this.stepCall(); |
| _builder.append(_stepCall, "\t"); |
| _builder.append(";"); |
| _builder.newLineIfNotEmpty(); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("};"); |
| _builder.newLine(); |
| return _builder.toString(); |
| } |
| |
| private String toCpp(final LinkedHashSet<String> includes, final LinkedList<String> initCalls, final LinkedList<String> stepCalls) { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("#include \""); |
| String _incFile = this.getIncFile(); |
| _builder.append(_incFile); |
| _builder.append("\""); |
| _builder.newLineIfNotEmpty(); |
| { |
| for(final String include : includes) { |
| _builder.append("#include \""); |
| _builder.append(include); |
| _builder.append("\""); |
| _builder.newLineIfNotEmpty(); |
| } |
| } |
| _builder.newLine(); |
| String _moduleName = this.getModuleName(); |
| _builder.append(_moduleName); |
| _builder.append("::"); |
| String _moduleName_1 = this.getModuleName(); |
| _builder.append(_moduleName_1); |
| _builder.append("(){"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("//default constructor"); |
| _builder.newLine(); |
| _builder.append("}"); |
| _builder.newLine(); |
| _builder.newLine(); |
| _builder.newLine(); |
| _builder.append("void "); |
| String _moduleName_2 = this.getModuleName(); |
| _builder.append(_moduleName_2); |
| _builder.append("::"); |
| String _initCall = this.getInitCall(); |
| _builder.append(_initCall); |
| _builder.append(" {"); |
| _builder.newLineIfNotEmpty(); |
| { |
| for(final String call : initCalls) { |
| _builder.append(" "); |
| _builder.append(call, " "); |
| _builder.append(";"); |
| _builder.newLineIfNotEmpty(); |
| } |
| } |
| _builder.append("} "); |
| _builder.newLine(); |
| _builder.newLine(); |
| _builder.append("void "); |
| String _moduleName_3 = this.getModuleName(); |
| _builder.append(_moduleName_3); |
| _builder.append("::"); |
| String _stepCall = this.stepCall(); |
| _builder.append(_stepCall); |
| _builder.append(" {"); |
| _builder.newLineIfNotEmpty(); |
| { |
| for(final String call_1 : stepCalls) { |
| _builder.append(" "); |
| _builder.append(call_1, " "); |
| _builder.append(";"); |
| _builder.newLineIfNotEmpty(); |
| } |
| } |
| _builder.append("}"); |
| _builder.newLine(); |
| return _builder.toString(); |
| } |
| |
| public String getModulePath() { |
| String _basePath = this.getBasePath(); |
| String _plus = (_basePath + "/"); |
| String _moduleName = this.getModuleName(); |
| return (_plus + _moduleName); |
| } |
| |
| @Override |
| public String getIncFile() { |
| String _moduleName = this.getModuleName(); |
| return (_moduleName + ".hpp"); |
| } |
| |
| @Override |
| public String getSrcFile() { |
| String _moduleName = this.getModuleName(); |
| return (_moduleName + ".cpp"); |
| } |
| |
| public String getIncPath() { |
| String _modulePath = this.getModulePath(); |
| String _plus = (_modulePath + "/_inc/"); |
| String _incFile = this.getIncFile(); |
| return (_plus + _incFile); |
| } |
| |
| public String getSrcPath() { |
| String _modulePath = this.getModulePath(); |
| String _plus = (_modulePath + "/_src/"); |
| String _srcFile = this.getSrcFile(); |
| return (_plus + _srcFile); |
| } |
| |
| public boolean isIncFileEmpty() { |
| String _modulePath = this.getModulePath(); |
| String _plus = (_modulePath + "/_inc/"); |
| String _moduleName = this.getModuleName(); |
| String _plus_1 = (_plus + _moduleName); |
| boolean _bufferExists = this.outputBuffer.bufferExists("H", _plus_1); |
| return (!_bufferExists); |
| } |
| |
| public boolean isSrcFileEmpty() { |
| String _modulePath = this.getModulePath(); |
| String _plus = (_modulePath + "/_src/"); |
| String _moduleName = this.getModuleName(); |
| String _plus_1 = (_plus + _moduleName); |
| boolean _bufferExists = this.outputBuffer.bufferExists("C", _plus_1); |
| return (!_bufferExists); |
| } |
| |
| public boolean incAppend(final String str) { |
| String _modulePath = this.getModulePath(); |
| String _plus = (_modulePath + "/_inc/"); |
| String _moduleName = this.getModuleName(); |
| String _plus_1 = (_plus + _moduleName); |
| return this.outputBuffer.appendTo("H", _plus_1, str); |
| } |
| |
| public boolean srcAppend(final String str) { |
| String _modulePath = this.getModulePath(); |
| String _plus = (_modulePath + "/_src/"); |
| String _moduleName = this.getModuleName(); |
| String _plus_1 = (_plus + _moduleName); |
| return this.outputBuffer.appendTo("C", _plus_1, str); |
| } |
| } |