| /** |
| * Copyright (c) 2020-2021 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.customization; |
| |
| import java.util.ArrayList; |
| import java.util.HashSet; |
| import java.util.LinkedHashSet; |
| import java.util.List; |
| |
| import org.eclipse.app4mc.amalthea.model.ActivityGraphItem; |
| import org.eclipse.app4mc.amalthea.model.InterProcessTrigger; |
| import org.eclipse.app4mc.amalthea.model.Process; |
| import org.eclipse.app4mc.amalthea.model.Runnable; |
| import org.eclipse.app4mc.amalthea.model.Ticks; |
| import org.eclipse.app4mc.slg.commons.m2t.generators.RunnableTranslationUnit; |
| import org.eclipse.app4mc.slg.commons.m2t.generators.TicksTranslationUnit; |
| import org.eclipse.app4mc.slg.commons.m2t.generators.TranslationUnit; |
| import org.eclipse.app4mc.slg.commons.m2t.transformers.sw.ActivityGraphItemTransformer; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| |
| public class CustomRunnableTranslationUnit extends RunnableTranslationUnit { |
| private final List<Process> processedTasks = new ArrayList<>(); |
| |
| public CustomRunnableTranslationUnit(final OutputBuffer outputBuffer, |
| final ActivityGraphItemTransformer callGraphItemTransformer, |
| final Runnable runnable) { |
| super(outputBuffer, callGraphItemTransformer, runnable); |
| } |
| |
| @Override |
| public String getCall() { |
| return "run_" + runnable.getName(); |
| } |
| |
| @Override |
| public void genFiles() { |
| if (isSrcFileEmpty()) { |
| srcAppend("#include \"" + this.getIncFile() + "\"\n"); |
| } |
| final HashSet<String> includes = new LinkedHashSet<>(); |
| final List<String> calls = new ArrayList<>(); |
| |
| if (runnable != null && runnable.getActivityGraph() != null) { |
| for (final ActivityGraphItem item : runnable.getActivityGraph().getItems()) { |
| if ((item instanceof Ticks)) { |
| Ticks ticks = (Ticks) item; |
| final List<TicksTranslationUnit> translationUnits = callGraphItemTransformer.transformAllItems(ticks); |
| |
| TicksTranslationUnit defaultPUTicks = null; |
| boolean ticksAssociatedToPUs = false; |
| |
| for (final TicksTranslationUnit tmp : translationUnits) { |
| if (tmp.getPuName().equals("default")) { |
| defaultPUTicks = tmp; |
| } else { |
| if (tmp.getIncFile() != null && !tmp.getIncFile().isEmpty() && !getIncFile().equals(tmp.getIncFile())) { |
| includes.add(tmp.getIncFile()); |
| } |
| final String call = tmp.getCall(); |
| if (call != null && !call.isEmpty()) { |
| calls.add((ticksAssociatedToPUs ? "else" : "") + " if(strcmp(coreName,\"" + tmp.getPuName() + "\")==0){"); |
| calls.add(call); |
| calls.add("}"); |
| ticksAssociatedToPUs = true; |
| } |
| } |
| } |
| |
| if ((defaultPUTicks != null)) { |
| if (ticksAssociatedToPUs) { |
| calls.add("else "); |
| } |
| |
| if (defaultPUTicks.getCall() != null && !defaultPUTicks.getCall().isEmpty()) { |
| calls.add("{"); |
| calls.add(defaultPUTicks.getCall()); |
| calls.add("}"); |
| } |
| } |
| } else { |
| if ((item instanceof InterProcessTrigger)) { |
| InterProcessTrigger trigger = (InterProcessTrigger) item; |
| final String value = CustomRunnableGenerator.handleInterProcessTrigger(trigger.getStimulus(), processedTasks); |
| if (value != null && !value.trim().isEmpty()) { |
| calls.add(value); |
| } |
| } else { |
| final TranslationUnit tmp = callGraphItemTransformer.transform(item); |
| |
| if (tmp.getIncFile() != null && !tmp.getIncFile().isEmpty() && !getIncFile().equals(tmp.getIncFile())) { |
| includes.add(tmp.getIncFile()); |
| } |
| final String call = tmp.getCall(); |
| if (call != null && !call.isEmpty()) { |
| calls.add(call); |
| } |
| } |
| } |
| } |
| |
| } |
| // write header |
| this.toH(includes); |
| |
| // write body |
| this.srcAppend("\n//Runnable " + runnable.getName() + "----\n"); |
| this.toCpp(includes, calls); |
| } |
| |
| @Override |
| public void toH(final HashSet<String> includes) { |
| if (isIncFileEmpty()) { |
| incAppend("#include <string.h>\n"); |
| } |
| |
| for(String include : includes) { |
| incAppend("#include \"" + include + "\"\n"); |
| } |
| |
| incAppend("\n//Runnable " + this.runnable.getName() + "----\n"); |
| |
| incAppend("void " + getCall() + "(char* coreName);\n"); |
| } |
| |
| @Override |
| public void toCpp(final HashSet<String> includes, final List<String> calls) { |
| srcAppend((("void " + this.getCall()) + "(char* coreName){\n")); |
| |
| for (String call : calls) { |
| srcAppend("\t" + call + (call.endsWith(")") ? ";" : "") + "\n"); |
| } |
| |
| srcAppend("}\n\n"); |
| } |
| |
| } |