| /** |
| * 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.translationUnits; |
| |
| import java.util.ArrayList; |
| import java.util.LinkedHashSet; |
| import java.util.function.Consumer; |
| import org.eclipse.app4mc.amalthea.model.ActivityGraph; |
| import org.eclipse.app4mc.amalthea.model.ActivityGraphItem; |
| import org.eclipse.app4mc.amalthea.model.BooleanObject; |
| import org.eclipse.app4mc.amalthea.model.ChannelSend; |
| import org.eclipse.app4mc.amalthea.model.InterProcessTrigger; |
| import org.eclipse.app4mc.amalthea.model.Value; |
| import org.eclipse.app4mc.slg.commons.m2t.transformers.ActivityGraphItemTransformer; |
| import org.eclipse.app4mc.slg.commons.m2t.translationUnits.RunnableTranslationUnit; |
| import org.eclipse.app4mc.slg.commons.m2t.translationUnits.TranslationUnit; |
| import org.eclipse.app4mc.slg.ros2.transformers.utils.Utils; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.emf.common.util.EMap; |
| import org.eclipse.xtend2.lib.StringConcatenation; |
| import org.eclipse.xtext.xbase.lib.StringExtensions; |
| |
| public class RosRunnableTranslationUnit extends RunnableTranslationUnit { |
| private String param; |
| |
| private String nodeParam; |
| |
| private boolean measure_performance; |
| |
| private ArrayList<String> publishers; |
| |
| private ArrayList<String> client_declarations; |
| |
| private ArrayList<String> client_inits; |
| |
| public RosRunnableTranslationUnit(final OutputBuffer outputBuffer, final ActivityGraphItemTransformer transformer, final org.eclipse.app4mc.amalthea.model.Runnable runnable) { |
| super(outputBuffer, transformer, runnable); |
| } |
| |
| @Override |
| public String getCall() { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("run_"); |
| String _name = this.runnable.getName(); |
| _builder.append(_name); |
| _builder.append("("); |
| _builder.append(this.param); |
| _builder.append(")"); |
| return _builder.toString(); |
| } |
| |
| public String getNodeCall() { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("run_"); |
| String _name = this.runnable.getName(); |
| _builder.append(_name); |
| _builder.append("("); |
| _builder.append(this.nodeParam); |
| _builder.append(")"); |
| return _builder.toString(); |
| } |
| |
| public ArrayList<String> getPublishers() { |
| return this.publishers; |
| } |
| |
| public ArrayList<String> getClientDeclarations() { |
| return this.client_declarations; |
| } |
| |
| public ArrayList<String> getClientInits() { |
| return this.client_inits; |
| } |
| |
| @Override |
| public boolean genFiles() { |
| boolean _xblockexpression = false; |
| { |
| this.param = ""; |
| this.nodeParam = ""; |
| ArrayList<String> _arrayList = new ArrayList<String>(); |
| this.publishers = _arrayList; |
| ArrayList<String> _arrayList_1 = new ArrayList<String>(); |
| this.client_declarations = _arrayList_1; |
| ArrayList<String> _arrayList_2 = new ArrayList<String>(); |
| this.client_inits = _arrayList_2; |
| boolean _isSrcFileEmpty = this.isSrcFileEmpty(); |
| if (_isSrcFileEmpty) { |
| String _incFile = this.getIncFile(); |
| String _plus = ("#include \"" + _incFile); |
| String _plus_1 = (_plus + "\"\n"); |
| this.srcAppend(_plus_1); |
| } |
| final LinkedHashSet<String> includes = new LinkedHashSet<String>(); |
| final ArrayList<String> calls = new ArrayList<String>(); |
| EMap<String, Value> _customProperties = null; |
| if (this.runnable!=null) { |
| _customProperties=this.runnable.getCustomProperties(); |
| } |
| Value _get = null; |
| if (_customProperties!=null) { |
| _get=_customProperties.get("measure_performance"); |
| } |
| boolean _isValue = false; |
| if (((BooleanObject) _get)!=null) { |
| _isValue=((BooleanObject) _get).isValue(); |
| } |
| if (_isValue) { |
| includes.add("aml.h"); |
| this.measure_performance = true; |
| } |
| ActivityGraph _activityGraph = null; |
| if (this.runnable!=null) { |
| _activityGraph=this.runnable.getActivityGraph(); |
| } |
| EList<ActivityGraphItem> _items = null; |
| if (_activityGraph!=null) { |
| _items=_activityGraph.getItems(); |
| } |
| final Consumer<ActivityGraphItem> _function = (ActivityGraphItem item) -> { |
| final TranslationUnit tmp = this.callGraphItemTransformer.transform(((ActivityGraphItem) item)); |
| if (((!StringExtensions.isNullOrEmpty(tmp.getIncFile())) && (!this.getIncFile().equals(tmp.getIncFile())))) { |
| includes.add(tmp.getIncFile()); |
| } |
| if ((item instanceof ChannelSend)) { |
| String _name = ((ChannelSend) item).getData().getName(); |
| String _plus_2 = (_name + "_publisher"); |
| this.publishers.add(_plus_2); |
| String _nodeParam = this.nodeParam; |
| String _xifexpression = null; |
| boolean _equals = this.nodeParam.equals(""); |
| if (_equals) { |
| _xifexpression = ""; |
| } else { |
| _xifexpression = ";"; |
| } |
| String _name_1 = ((ChannelSend) item).getData().getName(); |
| String _plus_3 = (_xifexpression + _name_1); |
| String _plus_4 = (_plus_3 + "_publisher"); |
| this.nodeParam = (_nodeParam + _plus_4); |
| String _xifexpression_1 = null; |
| boolean _equals_1 = this.param.equals(""); |
| if (_equals_1) { |
| _xifexpression_1 = ""; |
| } else { |
| _xifexpression_1 = ";"; |
| } |
| String _plus_5 = (_xifexpression_1 + "rclcpp::Publisher<std_msgs::msg::String>::SharedPtr& "); |
| String _name_2 = ((ChannelSend) item).getData().getName(); |
| String _plus_6 = (_plus_5 + _name_2); |
| String _plus_7 = (_plus_6 + "_publisher"); |
| this.param = _plus_7; |
| } |
| if ((item instanceof InterProcessTrigger)) { |
| String _name_3 = ((InterProcessTrigger)item).getStimulus().getName(); |
| String _plus_8 = (_name_3 + "_service/srv/"); |
| String _name_4 = ((InterProcessTrigger)item).getStimulus().getName(); |
| String _plus_9 = (_plus_8 + _name_4); |
| String _plus_10 = (_plus_9 + "_service"); |
| String _plus_11 = (_plus_10 + ".hpp"); |
| includes.add(_plus_11); |
| String _name_5 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_12 = ("rclcpp::Client<" + _name_5); |
| String _plus_13 = (_plus_12 + "_service::srv::"); |
| String _name_6 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_14 = (_name_6 + "_service"); |
| String _idlCompliantName = Utils.toIdlCompliantName(_plus_14); |
| String _plus_15 = (_plus_13 + _idlCompliantName); |
| String _plus_16 = (_plus_15 + |
| ">::SharedPtr "); |
| String _name_7 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_17 = (_plus_16 + _name_7); |
| String _plus_18 = (_plus_17 + "_client"); |
| this.client_declarations.add(_plus_18); |
| String _name_8 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_19 = (_name_8 + "_client = this->create_client<"); |
| String _name_9 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_20 = (_plus_19 + _name_9); |
| String _plus_21 = (_plus_20 + "_service::srv::"); |
| String _name_10 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_22 = (_name_10 + "_service"); |
| String _idlCompliantName_1 = Utils.toIdlCompliantName(_plus_22); |
| String _plus_23 = (_plus_21 + _idlCompliantName_1); |
| String _plus_24 = (_plus_23 + ">"); |
| String _plus_25 = (_plus_24 + |
| "(\""); |
| String _name_11 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_26 = (_plus_25 + _name_11); |
| String _plus_27 = (_plus_26 + "_service\")"); |
| this.client_inits.add(_plus_27); |
| String _nodeParam_1 = this.nodeParam; |
| String _xifexpression_2 = null; |
| boolean _equals_2 = this.nodeParam.equals(""); |
| if (_equals_2) { |
| _xifexpression_2 = ""; |
| } else { |
| _xifexpression_2 = ";"; |
| } |
| String _name_12 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_28 = (_xifexpression_2 + _name_12); |
| String _plus_29 = (_plus_28 + "_client"); |
| this.nodeParam = (_nodeParam_1 + _plus_29); |
| String _xifexpression_3 = null; |
| boolean _equals_3 = this.param.equals(""); |
| if (_equals_3) { |
| _xifexpression_3 = ""; |
| } else { |
| _xifexpression_3 = ";"; |
| } |
| String _plus_30 = (_xifexpression_3 + "rclcpp::Client<"); |
| String _name_13 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_31 = (_plus_30 + _name_13); |
| String _plus_32 = (_plus_31 + "_service::srv::"); |
| String _name_14 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_33 = (_name_14 + "_service"); |
| String _idlCompliantName_2 = Utils.toIdlCompliantName(_plus_33); |
| String _plus_34 = (_plus_32 + _idlCompliantName_2); |
| String _plus_35 = (_plus_34 + |
| ">::SharedPtr& "); |
| String _name_15 = ((InterProcessTrigger) item).getStimulus().getName(); |
| String _plus_36 = (_plus_35 + _name_15); |
| String _plus_37 = (_plus_36 + "_client"); |
| this.param = _plus_37; |
| } |
| final String call = tmp.getCall(); |
| boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(call); |
| boolean _not = (!_isNullOrEmpty); |
| if (_not) { |
| calls.add(call); |
| } |
| }; |
| _items.forEach(_function); |
| String _name = this.runnable.getName(); |
| String _plus_2 = ("\n//Runnable " + _name); |
| String _plus_3 = (_plus_2 + "----\n"); |
| this.incAppend(_plus_3); |
| this.toH(includes); |
| String _name_1 = this.runnable.getName(); |
| String _plus_4 = ("\n//Runnable " + _name_1); |
| String _plus_5 = (_plus_4 + "----\n"); |
| this.srcAppend(_plus_5); |
| _xblockexpression = this.toCpp(includes, calls); |
| } |
| return _xblockexpression; |
| } |
| |
| @Override |
| public boolean toH(final LinkedHashSet<String> includes) { |
| boolean _xblockexpression = false; |
| { |
| final Consumer<String> _function = (String include) -> { |
| this.incAppend((("#include \"" + include) + "\"\n")); |
| }; |
| includes.forEach(_function); |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("void "); |
| String _call = this.getCall(); |
| _builder.append(_call); |
| _builder.append(";"); |
| _builder.newLineIfNotEmpty(); |
| _xblockexpression = this.incAppend(_builder.toString()); |
| } |
| return _xblockexpression; |
| } |
| |
| @Override |
| public boolean toCpp(final LinkedHashSet<String> includes, final ArrayList<String> calls) { |
| boolean _xblockexpression = false; |
| { |
| String _call = this.getCall(); |
| String _plus = ("void " + _call); |
| String _plus_1 = (_plus + "{\n"); |
| this.srcAppend(_plus_1); |
| if (this.measure_performance) { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("uint64_t event_list[] = {0x11, 0x13, 0x17}; //CPU CYCLES, MEM ACCESS, L2 Cache Refill"); |
| _builder.newLine(); |
| _builder.append("int total_events = sizeof(event_list)/sizeof(event_list[0]);"); |
| _builder.newLine(); |
| _builder.append("int fd = instrument_start(0,event_list, total_events);"); |
| _builder.newLine(); |
| this.srcAppend(_builder.toString()); |
| } |
| final Consumer<String> _function = (String call) -> { |
| this.srcAppend((("\t" + call) + ";\n")); |
| }; |
| calls.forEach(_function); |
| if (this.measure_performance) { |
| StringConcatenation _builder_1 = new StringConcatenation(); |
| _builder_1.append("instrument_stop(fd, \""); |
| String _name = this.runnable.getName(); |
| _builder_1.append(_name); |
| _builder_1.append(".log\");"); |
| _builder_1.newLineIfNotEmpty(); |
| this.srcAppend(_builder_1.toString()); |
| } |
| _xblockexpression = this.srcAppend("}\n\n"); |
| } |
| return _xblockexpression; |
| } |
| } |