| /** |
| * 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.generators; |
| |
| import java.util.ArrayList; |
| import java.util.HashSet; |
| import java.util.LinkedHashSet; |
| import java.util.List; |
| 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.sw.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() { |
| return "synthetic_gen" + "/" + getModuleNameStatic(); |
| } |
| |
| public static String getMakeFilePathStatic() { |
| return getModulePathStatic() + "/" + "CMakeLists.txt"; |
| } |
| |
| 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_"); |
| builder.append(this.runnable.getName()); |
| builder.append("()"); |
| return builder.toString(); |
| } |
| |
| @Override |
| public String getIncFile() { |
| return getModuleName() + ".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 void genFiles() { |
| if (isSrcFileEmpty()) { |
| srcAppend("#include \"" + getIncFile() + "\"\n"); |
| } |
| final LinkedHashSet<String> includes = new LinkedHashSet<>(); |
| final ArrayList<String> calls = new ArrayList<>(); |
| 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(); |
| if (!StringExtensions.isNullOrEmpty(call)) { |
| calls.add(call); |
| } |
| } |
| }; |
| _items.forEach(_function); |
| |
| incAppend("\n//Runnable " + runnable.getName() + "----\n"); |
| toH(includes); |
| |
| srcAppend("\n//Runnable " + runnable.getName() + "----\n"); |
| toCpp(includes, calls); |
| } |
| |
| public void toH(final HashSet<String> includes) { |
| 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 "); |
| builder.append(this.getCall()); |
| builder.append(";"); |
| builder.newLineIfNotEmpty(); |
| this.incAppend(builder.toString()); |
| } |
| |
| public void toCpp(final HashSet<String> includes, final List<String> calls) { |
| srcAppend("void " + getCall() + "{\n"); |
| |
| final Consumer<String> _function = new Consumer<String>() { |
| @Override |
| public void accept(final String call) { |
| RunnableTranslationUnit.this.srcAppend((("\t" + call) + ";\n")); |
| } |
| }; |
| calls.forEach(_function); |
| |
| srcAppend("}\n\n"); |
| } |
| |
| public String getBasePath() { |
| return "synthetic_gen"; |
| } |
| |
| public String getModulePath() { |
| return getBasePath() + "/" + getModuleName(); |
| } |
| |
| @Override |
| public String getSrcFile() { |
| return getModuleName() + ".cpp"; |
| } |
| |
| public String getIncPath() { |
| return getModulePath() + "/_inc/" + getIncFile(); |
| } |
| |
| public String getSrcPath() { |
| return getModulePath() + "/_src/" + getSrcFile(); |
| } |
| |
| public boolean isIncFileEmpty() { |
| return !outputBuffer.bufferExists("INC", getModulePath() + "/_inc/" + getModuleName()); |
| } |
| |
| public boolean isSrcFileEmpty() { |
| return !outputBuffer.bufferExists("SRC", getModulePath() + "/_src/" + getModuleName()); |
| } |
| |
| public void incAppend(final String str) { |
| outputBuffer.appendTo("INC", getModulePath() + "/_inc/" + getModuleName(), str); |
| } |
| |
| public void srcAppend(final String str) { |
| outputBuffer.appendTo("SRC", getModulePath() + "/_src/" + getModuleName(), str); |
| } |
| } |