| /** |
| * Copyright (c) 2019-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.amlt2systemc.m2t.transformers.sw; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.HashMap; |
| import java.util.LinkedList; |
| |
| import org.eclipse.app4mc.amalthea.model.Stimulus; |
| import org.eclipse.app4mc.amalthea.model.Task; |
| import org.eclipse.app4mc.amlt2systemc.m2t.transformers.TranslationUnit; |
| import org.eclipse.app4mc.amlt2systemc.m2t.transformers.stimuli.StimulusTransformer; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| |
| import com.google.inject.Inject; |
| import com.google.inject.Singleton; |
| |
| @Singleton |
| public class TaskTransformer extends IActivityGraphItemContainerTransformer { |
| |
| @Inject private OutputBuffer outputBuffer; |
| @Inject private StimulusTransformer stimulusTransformer; |
| |
| // ---------- generic part "def create new transform(...)" ---------- |
| |
| private final HashMap<ArrayList<?>, TranslationUnit> transformCache = new HashMap<>(); |
| |
| public HashMap<ArrayList<?>, TranslationUnit> getCache() { |
| return this.transformCache; |
| } |
| |
| public TranslationUnit transform(final Task task) { |
| final ArrayList<?> key = new ArrayList<>(Arrays.asList(task)); |
| final TranslationUnit tu; |
| |
| synchronized (transformCache) { |
| if (transformCache.containsKey(key)) { |
| return transformCache.get(key); |
| } |
| tu = createTranslationUnit(task); |
| transformCache.put(key, tu); |
| } |
| |
| // if translation unit is newly created and valid -> create files |
| if (tu.isValid()) { |
| doTransform(tu, task); |
| } |
| |
| return tu; |
| } |
| |
| // --------------------------------------------------- |
| |
| private TranslationUnit createTranslationUnit(final Task task) { |
| if ((task == null)) { |
| return new TranslationUnit("UNSPECIFIED_TASK"); |
| } else { |
| String module = TaskGenerator.getModulePath(task); |
| String call = TaskGenerator.getFunctionDef(task); |
| return new TranslationUnit(module, call); |
| } |
| } |
| |
| private void doTransform(final TranslationUnit taskTU, final Task task) { |
| |
| final LinkedList<String> stimuliCalls = new LinkedList<>(); |
| |
| super.transformItems(task.getActivityGraph(), taskTU); |
| |
| for (Stimulus stimulus : task.getStimuli()) { |
| final TranslationUnit stimulusTU = stimulusTransformer.transform(stimulus); |
| |
| String stimulusModule = stimulusTU.getModule(); |
| if (stimulusModule != null && !stimulusModule.isEmpty() && !taskTU.getModule().equals(stimulusModule)) { |
| super.activityItemIncludes.add(stimulusModule); |
| } |
| String stimulusCall = stimulusTU.getCall(); |
| if (stimulusCall != null && !stimulusCall.isEmpty()) { |
| stimuliCalls.add(stimulusCall); |
| } |
| } |
| |
| this.outputBuffer.appendTo("INC", taskTU.getModule(), TaskGenerator.toH(task)); |
| this.outputBuffer.appendTo("SRC", taskTU.getModule(), TaskGenerator.toCpp(task, stimuliCalls, activityItemIncludes, activityItemCalls)); |
| } |
| |
| } |