| /** |
| * 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.function.Consumer; |
| |
| import org.eclipse.app4mc.amalthea.model.ActivityGraph; |
| import org.eclipse.app4mc.amalthea.model.ActivityGraphItem; |
| import org.eclipse.app4mc.slg.commons.m2t.transformers.ActivityGraphItemTransformer; |
| 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.StringExtensions; |
| |
| public class RunnableTranslationUnit extends TranslationUnit { |
| public static String getModulePathStatic() { |
| String _basePathStatic = "synthetic_gen"; |
| String _plus = (_basePathStatic + "/"); |
| String _moduleNameStatic = RunnableTranslationUnit.getModuleNameStatic(); |
| return (_plus + _moduleNameStatic); |
| } |
| |
| public static String getMakeFilePathStatic() { |
| String _modulePathStatic = RunnableTranslationUnit.getModulePathStatic(); |
| String _plus = (_modulePathStatic + "/"); |
| String _makeFileName = "CMakeLists.txt"; |
| return (_plus + _makeFileName); |
| } |
| |
| public static String getModuleNameStatic() { |
| return "runnables"; |
| } |
| |
| public static String getLibName() { |
| return "RUNNABLES_LIB"; |
| } |
| |
| public String getModuleName() { |
| return RunnableTranslationUnit.getModuleNameStatic(); |
| } |
| |
| @Override |
| public String getCall() { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("run_"); |
| String _name = this.runnable.getName(); |
| _builder.append(_name); |
| _builder.append("()"); |
| return _builder.toString(); |
| } |
| |
| public String getIncFile() { |
| String _moduleName = this.getModuleName(); |
| return (_moduleName + ".h"); |
| } |
| |
| public ActivityGraphItemTransformer callGraphItemTransformer; |
| |
| public org.eclipse.app4mc.amalthea.model.Runnable runnable; |
| |
| private OutputBuffer outputBuffer; |
| |
| public RunnableTranslationUnit(final OutputBuffer outputBuffer, final ActivityGraphItemTransformer callGraphItemTransformer, final org.eclipse.app4mc.amalthea.model.Runnable runnable) { |
| super(); |
| this.callGraphItemTransformer = callGraphItemTransformer; |
| this.runnable = runnable; |
| this.outputBuffer = outputBuffer; |
| this.genFiles(); |
| } |
| |
| public boolean genFiles() { |
| boolean _xblockexpression = false; |
| { |
| boolean _isSrcFileEmpty = this.isSrcFileEmpty(); |
| if (_isSrcFileEmpty) { |
| String _incFile = this.getIncFile(); |
| String _plus = ("#include \"" + _incFile); |
| String _plus_1 = (_plus + "\"\n"); |
| this.srcAppend(_plus_1); |
| } |
| final LinkedHashSet<String> includes = new LinkedHashSet<String>(); |
| final ArrayList<String> calls = new ArrayList<String>(); |
| ActivityGraph _activityGraph = null; |
| if (this.runnable!=null) { |
| _activityGraph=this.runnable.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) { |
| final TranslationUnit tmp = RunnableTranslationUnit.this.callGraphItemTransformer.transform(item); |
| if (((!StringExtensions.isNullOrEmpty(tmp.getIncFile())) && (!RunnableTranslationUnit.this.getIncFile().equals(tmp.getIncFile())))) { |
| includes.add(tmp.getIncFile()); |
| } |
| final String call = tmp.getCall(); |
| boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(call); |
| boolean _not = (!_isNullOrEmpty); |
| if (_not) { |
| calls.add(call); |
| } |
| } |
| }; |
| _items.forEach(_function); |
| String _name = this.runnable.getName(); |
| String _plus_2 = ("\n//Runnable " + _name); |
| String _plus_3 = (_plus_2 + "----\n"); |
| this.incAppend(_plus_3); |
| this.toH(includes); |
| String _name_1 = this.runnable.getName(); |
| String _plus_4 = ("\n//Runnable " + _name_1); |
| String _plus_5 = (_plus_4 + "----\n"); |
| this.srcAppend(_plus_5); |
| _xblockexpression = this.toCpp(includes, calls); |
| } |
| return _xblockexpression; |
| } |
| |
| public boolean toH(final LinkedHashSet<String> includes) { |
| boolean _xblockexpression = false; |
| { |
| final Consumer<String> _function = new Consumer<String>() { |
| @Override |
| public void accept(final String include) { |
| RunnableTranslationUnit.this.incAppend((("#include \"" + include) + "\"\n")); |
| } |
| }; |
| includes.forEach(_function); |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("void "); |
| String _call = this.getCall(); |
| _builder.append(_call); |
| _builder.append(";"); |
| _builder.newLineIfNotEmpty(); |
| _xblockexpression = this.incAppend(_builder.toString()); |
| } |
| return _xblockexpression; |
| } |
| |
| public boolean toCpp(final LinkedHashSet<String> includes, final ArrayList<String> calls) { |
| boolean _xblockexpression = false; |
| { |
| String _call = this.getCall(); |
| String _plus = ("void " + _call); |
| String _plus_1 = (_plus + "{\n"); |
| this.srcAppend(_plus_1); |
| final Consumer<String> _function = new Consumer<String>() { |
| @Override |
| public void accept(final String call) { |
| RunnableTranslationUnit.this.srcAppend((("\t" + call) + ";\n")); |
| } |
| }; |
| calls.forEach(_function); |
| _xblockexpression = this.srcAppend("}\n\n"); |
| } |
| return _xblockexpression; |
| } |
| |
| public String getBasePath() { |
| return "synthetic_gen"; |
| } |
| |
| public String getModulePath() { |
| String _basePath = this.getBasePath(); |
| String _plus = (_basePath + "/"); |
| String _moduleName = this.getModuleName(); |
| return (_plus + _moduleName); |
| } |
| |
| @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); |
| } |
| } |