| /** |
| * 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.ros2.generators; |
| |
| import java.util.ArrayList; |
| import java.util.LinkedHashSet; |
| import java.util.LinkedList; |
| import java.util.List; |
| import java.util.Set; |
| import java.util.function.BiConsumer; |
| |
| import org.eclipse.app4mc.amalthea.model.ActivityGraphItem; |
| import org.eclipse.app4mc.amalthea.model.Component; |
| import org.eclipse.app4mc.amalthea.model.Group; |
| import org.eclipse.app4mc.amalthea.model.ITaggable; |
| import org.eclipse.app4mc.amalthea.model.InterProcessStimulus; |
| import org.eclipse.app4mc.amalthea.model.Runnable; |
| import org.eclipse.app4mc.amalthea.model.RunnableCall; |
| import org.eclipse.app4mc.amalthea.model.Stimulus; |
| import org.eclipse.app4mc.amalthea.model.Tag; |
| import org.eclipse.app4mc.amalthea.model.Task; |
| import org.eclipse.app4mc.slg.commons.m2t.generators.LabelTranslationUnit; |
| import org.eclipse.app4mc.slg.commons.m2t.generators.TaskTranslationUnit; |
| import org.eclipse.app4mc.slg.ros2.transformers.stimuli.RosInterProcessStimulusTransformer; |
| import org.eclipse.app4mc.slg.ros2.transformers.sw.RosLabelTransformer; |
| import org.eclipse.app4mc.slg.ros2.transformers.sw.RosRunnableTransformer; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| |
| public class RosTaskTranslationUnit extends TaskTranslationUnit { |
| private RosInterProcessStimulusTransformer ipStimulusTransformer; |
| private Task task; |
| |
| private final Set<String> includes = new LinkedHashSet<>(); |
| private final List<String> initCalls = new LinkedList<>(); |
| private final List<String> stepCalls = new LinkedList<>(); |
| private final List<Stimulus> stimuli = new ArrayList<>(); |
| private final List<String> publishers = new ArrayList<>(); |
| private final List<String> clientDeclarations = new ArrayList<>(); |
| private final List<String> clientInits = new ArrayList<>(); |
| |
| public RosTaskTranslationUnit(final OutputBuffer outputBuffer, final RosRunnableTransformer transformerRunnable, |
| final RosLabelTransformer transformerLabel, final RosInterProcessStimulusTransformer ipStimulusTransformer, |
| final Task task, final Component c) { |
| super(outputBuffer, transformerRunnable, transformerLabel, task); |
| this.ipStimulusTransformer = ipStimulusTransformer; |
| this.task = task; |
| |
| this.genFiles(); |
| } |
| |
| @Override |
| public String getBasePath() { |
| return ""; |
| } |
| |
| @Override |
| public String getModuleName() { |
| return this.task.getName(); |
| } |
| |
| @Override |
| public void genFiles() { |
| |
| if (task != null && task.getActivityGraph() != null) { |
| for (ActivityGraphItem item : task.getActivityGraph().getItems()) { |
| |
| if ((item instanceof RunnableCall)) { |
| final RunnableCall runnableCall = (RunnableCall) item; |
| final Runnable runnable = runnableCall.getRunnable(); |
| |
| RosRunnableTranslationUnit tu = (RosRunnableTranslationUnit) runnableTransformer.transform(runnable); |
| includes.add(tu.getIncFile()); |
| |
| if (hasTagNamed(runnableCall, "initialize")) { |
| initCalls.add(tu.getNodeCall()); |
| } else { |
| stepCalls.add(tu.getNodeCall()); |
| } |
| |
| // TODO: Make set |
| publishers.addAll(tu.getPublishers()); |
| clientDeclarations.addAll(tu.getClientDeclarations()); |
| clientInits.addAll(tu.getClientInits()); |
| // TODO: add terminate function, if requested |
| |
| } else if (item instanceof Group) { |
| final Group group = ((Group) item); |
| for (ActivityGraphItem groupitem : group.getItems()) { |
| if ((groupitem instanceof RunnableCall)) { |
| final RunnableCall runnableCall = (RunnableCall) groupitem; |
| final Runnable runnable = runnableCall.getRunnable(); |
| |
| RosRunnableTranslationUnit tu = (RosRunnableTranslationUnit) runnableTransformer.transform(runnable); |
| includes.add(tu.getIncFile()); |
| |
| if (hasTagNamed(runnableCall, "initialize")) { |
| initCalls.add((tu).getNodeCall()); |
| } else { |
| stepCalls.add((tu).getNodeCall()); |
| } |
| //TODO: add terminate function, if requested |
| } |
| } |
| } |
| } |
| } |
| |
| // labels must be initialized before usage, generated labels provide this method |
| |
| for (LabelTranslationUnit tu : labelTransformer.getCache().values()) { |
| includes.add(tu.getIncFile()); |
| initCalls.add(tu.getInitCall()); |
| } |
| |
| labelTransformer.getCache().forEach( |
| (BiConsumer<ArrayList<?>, LabelTranslationUnit>) (ArrayList<?> label, LabelTranslationUnit tu) -> { |
| |
| }); |
| |
| // add header for srv file in case of an interprocessstimulus |
| // create .srv file for the messages to be translated |
| |
| if (task != null) { |
| for (Stimulus stimulus : task.getStimuli()) { |
| if (stimulus instanceof InterProcessStimulus) { |
| String name = stimulus.getName(); |
| includes.add(name + "_service/srv/" + name + "_service.hpp"); |
| |
| ipStimulusTransformer.transform(((InterProcessStimulus) stimulus)); |
| } |
| } |
| |
| stimuli.addAll(task.getStimuli()); |
| } |
| |
| } |
| |
| private boolean hasTagNamed(ITaggable element, String name) { |
| for (Tag tag : element.getTags()) { |
| if (tag.getName().equals(name)) |
| return true; |
| } |
| return false; |
| } |
| |
| public String getDeclaration() { |
| return RosTaskGenerator.getDeclaration(stimuli, publishers, clientDeclarations); |
| } |
| |
| public String getInitialisation(String nodeName) { |
| return RosTaskGenerator.getInitialisation(nodeName, stimuli, publishers, clientInits); |
| } |
| |
| public String getServiceCallback() { |
| return RosTaskGenerator.getServiceCallback(stimuli, stepCalls); |
| } |
| |
| public String getCallback() { |
| return RosTaskGenerator.getCallback(stimuli, stepCalls); |
| } |
| |
| public String getHeaders() { |
| return RosTaskGenerator.getHeaders(includes); |
| } |
| } |