| /** |
| ******************************************************************************** |
| * Copyright (c) 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.commons.m2t.transformers.sw; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.LinkedHashSet; |
| import java.util.function.Consumer; |
| |
| 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.slg.commons.m2t.generators.TranslationUnit; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.xtext.xbase.lib.CollectionLiterals; |
| import org.eclipse.xtext.xbase.lib.StringExtensions; |
| |
| import com.google.inject.Inject; |
| import com.google.inject.Singleton; |
| |
| @Singleton |
| public class GroupHeaderTransformer extends ActivityGraphItemTransformer { |
| |
| @Inject private OutputBuffer outputBuffer; |
| @Inject private ActivityGraphItemTransformer psCallGraphItemTransformer; |
| |
| public static String getModuleName(final Group group) { |
| return "groups"; |
| } |
| |
| public static String getFunctionDef(final Group group) { |
| return "group_" + group.getName() + "()"; |
| } |
| |
| public TranslationUnit transform(final Group group, final Component component) { |
| final ArrayList<?> key = CollectionLiterals.newArrayList(group, component); |
| final TranslationUnit _result; |
| synchronized (cache) { |
| if (cache.containsKey(key)) { |
| return cache.get(key); |
| } |
| TranslationUnit _translationUnit = new TranslationUnit(); |
| _result = _translationUnit; |
| cache.put(key, _result); |
| } |
| _init_transform(_result, group, component); |
| return _result; |
| } |
| |
| private final HashMap<ArrayList<?>, TranslationUnit> cache = CollectionLiterals.newHashMap(); |
| |
| private void _init_transform(final TranslationUnit it, final Group group, final Component component) { |
| it.setPath(GroupHeaderTransformer.getModuleName(group)); |
| it.setCall(GroupHeaderTransformer.getFunctionDef(group)); |
| |
| final LinkedHashSet<String> groupItemIncludes = new LinkedHashSet<>(); |
| final LinkedHashSet<String> groupItemCalls = new LinkedHashSet<>(); |
| |
| EList<ActivityGraphItem> _items = null; |
| if (group != null) { |
| _items = group.getItems(); |
| } |
| final Consumer<ActivityGraphItem> _function = (ActivityGraphItem item) -> { |
| final TranslationUnit tmp = this.psCallGraphItemTransformer.transform(item); |
| if (((!StringExtensions.isNullOrEmpty(tmp.getExtendedIncPath())) |
| && (!it.getExtendedIncPath().equals(tmp.getExtendedIncPath())))) { |
| groupItemIncludes.add(tmp.getExtendedIncPath()); |
| } |
| if (!StringExtensions.isNullOrEmpty(tmp.getCall())) { |
| groupItemCalls.add(tmp.getCall()); |
| } |
| }; |
| _items.forEach(_function); |
| |
| final int index = it.getExtendedIncPath().lastIndexOf("."); |
| final String trimmedIncPath = it.getExtendedIncPath().substring(0, index); |
| |
| outputBuffer.appendTo("INC", trimmedIncPath, "\n//Group " + group.getName() + "----\n"); |
| |
| groupItemIncludes.forEach(include -> |
| outputBuffer.appendTo("INC", trimmedIncPath, "#include \"" + include + ".h\"\n")); |
| |
| outputBuffer.appendTo("INC", trimmedIncPath, "void " + it.getCall() + ";\n"); |
| } |
| |
| } |