blob: b2b82fe65f48189696bce146d0323c1f0aa5b5f3 [file] [log] [blame]
/**
********************************************************************************
* 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.transformers.common;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.eclipse.app4mc.amalthea.model.ActivityGraphItem;
import org.eclipse.app4mc.amalthea.model.Amalthea;
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.CustomObjectsStore;
import org.eclipse.app4mc.slg.commons.m2t.transformers.SLGTranslationUnit;
import org.eclipse.app4mc.slg.config.CodehookType;
import org.eclipse.app4mc.slg.config.ConfigModel;
import org.eclipse.app4mc.slg.config.util.ConfigModelUtils;
import org.eclipse.app4mc.slg.microros.generators.MicroRosTagGenerator;
import org.eclipse.app4mc.slg.microros.transformers.MicroRosTaskCache;
import org.eclipse.app4mc.slg.microros.transformers.MicroRosTaskCache.MicroRosTaskStore;
import org.eclipse.app4mc.slg.microros.transformers.MicroRosTaskTransformer;
import org.eclipse.app4mc.slg.ros2.generators.RosTagGenerator;
import org.eclipse.app4mc.slg.ros2.transformers.RosBaseTransformer;
import org.eclipse.app4mc.slg.ros2.transformers.sw.RosTaskCache;
import org.eclipse.app4mc.slg.ros2.transformers.sw.RosTaskCache.TaskStore;
import org.eclipse.app4mc.transformation.TransformationConstants;
import org.eclipse.app4mc.util.sessionlog.SessionLogger;
import org.eclipse.app4mc.slg.ros2.transformers.sw.RosTaskTransformer;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class RosTagTransformer extends RosBaseTransformer {
@Inject private RosTaskTransformer rosTaskTransformer;
@Inject private MicroRosTaskTransformer microRosTaskTransformer;
@Inject private RosTaskCache rosTaskCache;
@Inject private MicroRosTaskCache microRosTaskCache;
@Inject private Properties properties;
@Inject private CustomObjectsStore customObjsStore;
@Inject private SessionLogger logger;
// ---------- generic part "def create new transform(...)" ----------
private final Map<List<Object>, SLGTranslationUnit> transformCache = new HashMap<>();
@Override
public Map<List<Object>, SLGTranslationUnit> getCache() {
return this.transformCache;
}
public SLGTranslationUnit transform(final Tag tag, final Amalthea model) {
final List<Object> key = new ArrayList<>(Arrays.asList(tag, model));
final SLGTranslationUnit tu;
synchronized (transformCache) {
if (transformCache.containsKey(key)) {
return transformCache.get(key);
}
tu = createTranslationUnit(tag);
transformCache.put(key, tu);
}
// if translation unit is newly created and valid -> create files
if (tu.isValid()) {
doTransform(tu, tag, model);
}
return tu;
}
// ---------------------------------------------------
private SLGTranslationUnit createTranslationUnit(final Tag tag) {
if ((tag == null)) {
return new SLGTranslationUnit("UNSPECIFIED TAG");
} else {
String tagType = tag.getTagType();
String basePath = "";
if(tagType!=null && tagType.equals("MicroROS")) {
basePath="MICROROS_SLG";
}else if(tagType!=null && tagType.equals("ROS")){
basePath="ROS2_SLG";
}
String moduleName = tag.getName();
String call = "";
return new SLGTranslationUnit(basePath, moduleName, call);
}
}
private void doTransform(final SLGTranslationUnit tu, final Tag tag, final Amalthea model) {
genFiles(tu, tag, model);
}
private void genFiles(final SLGTranslationUnit tu, final Tag tag, final Amalthea model) {
boolean isMicroRosTransformation=false;
String tagType = tag.getTagType();
if(tagType!=null && tagType.equals("MicroROS")) {
isMicroRosTransformation=true;
}
HashSet<String> codehookHeaders = new HashSet<>();
HashSet<String> header = new HashSet<>();
List<String> declaration = new LinkedList<>();
List<String> initialization = new LinkedList<>();
List<String> calls = new LinkedList<>();
List<String> serviceCallbacks = new LinkedList<>();
HashSet<Stimulus> ListStimulus = new HashSet<>();
HashSet<String> headerMicroRos = new HashSet<>();
LinkedHashSet<String> globalDeclarationMicroRos = new LinkedHashSet<>();
List<String> declarationMicroRos = new LinkedList<>();
List<String> initializationMicroRos = new LinkedList<>();
List<String> callsMicroRos = new LinkedList<>();
List<String> serviceCallbacksMicroRos = new LinkedList<>();
if (model != null && model.getSwModel() != null) {
for (Task task : model.getSwModel().getTasks()) {
/* for (Tag tag1 : task.getTags())
{
globalDeclarationMicroRos.add("firas" + tag1.getName());
} */
if (task.getTags().contains(tag)) {
if(isMicroRosTransformation) {
//TODO: handle microRos specific stuff
final SLGTranslationUnit taskTU = microRosTaskTransformer.transform(tag,task);
final MicroRosTaskStore storeMicroRos = microRosTaskCache.getStore(taskTU);
headerMicroRos.add(storeMicroRos.getHeaders());
//if (!(globalDeclarationMicroRos.contains(storeMicroRos.getGlobalDeclaration(model))))
globalDeclarationMicroRos.add(storeMicroRos.getGlobalDeclaration(model, tu.getModuleName()));
declarationMicroRos.add(storeMicroRos.getDeclaration());
initializationMicroRos.add(storeMicroRos.getInitialisation(tu.getModuleName()));
callsMicroRos.add(storeMicroRos.getCallback(model,tag));
serviceCallbacksMicroRos.add(storeMicroRos.getServiceCallback());
ListStimulus.addAll(storeMicroRos.getStimuli());
/*final TaskStore store = rosTaskCache.getStore(taskTU);
headerMicroRos.add(store.getHeaders());
declarationMicroRos.add(store.getDeclaration());
initializationMicroRos.add(store.getInitialisation(tu.getModuleName()));
callsMicroRos.add(store.getCallback());
serviceCallbacksMicroRos.add(store.getServiceCallback());*/
}
else {
//----------------------------------/* fetch headers names*/--------------------------------------
// to fetch the headers names we use the getHeaderFilesDirectories function and we go through every directory with the for loop
final ConfigModel configModel = customObjsStore.<ConfigModel>getInstance(ConfigModel.class);
boolean enableExtCode = Boolean.parseBoolean(properties.getProperty("enableExternalCode"));
if(enableExtCode) {
// input property to test if we need to include the external code headerfiles
String workingDirectory=customObjsStore.getData(TransformationConstants.WORKING_DIRECTORY);
for (String hDir : ConfigModelUtils.getHeaderFilesDirectories(configModel,CodehookType.TASK,workingDirectory,logger))
{
final File folder = new File(hDir.trim()); // fetching all the names in the headerfile directory.
String names = ConfigModelUtils.getHeaderFilesIncludeMultiString(folder,logger);
codehookHeaders.add(names);
}
}
//------------------------------------------------------------------------
final SLGTranslationUnit taskTU = rosTaskTransformer.transform(task);
final TaskStore store = rosTaskCache.getStore(taskTU);
header.add(store.getHeaders());
declaration.add(store.getDeclaration());
initialization.add(store.getInitialisation(tu.getModuleName()));
calls.add(store.getCallback());
System.out.println("Those are calls" + calls);
serviceCallbacks.add(store.getServiceCallback());
}
}
}
}
if(isMicroRosTransformation) {
//TODO: have a special handling if the TagType is MicroROS
srcAppend(tu, MicroRosTagGenerator.toCpp(tag, tu.getModuleName(), headerMicroRos, declarationMicroRos ,initializationMicroRos, callsMicroRos, serviceCallbacksMicroRos, globalDeclarationMicroRos, model, ListStimulus));
}
else {
srcAppend(tu, RosTagGenerator.toCpp(tag, tu.getModuleName(), header, declaration, initialization, calls, serviceCallbacks,codehookHeaders));
}
}
}