| /** |
| * 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.Arrays; |
| |
| import org.eclipse.app4mc.amalthea.model.LabelAccess; |
| import org.eclipse.app4mc.amalthea.model.MinAvgMaxStatistic; |
| import org.eclipse.app4mc.amalthea.model.NumericStatistic; |
| import org.eclipse.app4mc.amalthea.model.SingleValueStatistic; |
| import org.eclipse.app4mc.slg.commons.m2t.generators.TranslationUnit; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| |
| public class RosLabelAccessTranslationUnit extends TranslationUnit { |
| private RosLabelTranslationUnit tuLabel; |
| |
| private LabelAccess labelAccess; |
| |
| private OutputBuffer outputBuffer; |
| |
| public RosLabelAccessTranslationUnit(final OutputBuffer outputBuffer, final RosLabelTranslationUnit tuLabel, |
| final LabelAccess labelAccess) { |
| super(); |
| this.tuLabel = tuLabel; |
| this.labelAccess = labelAccess; |
| this.outputBuffer = outputBuffer; |
| } |
| |
| public String getBasePath() { |
| return this.tuLabel.getBasePath(); |
| } |
| |
| public String getModuleName() { |
| return this.tuLabel.getModuleName(); |
| } |
| |
| @Override |
| public String getIncFile() { |
| return this.tuLabel.getIncFile(); |
| } |
| |
| // ---------- names of generated 'C' functions ---------- |
| |
| @Override |
| public String getCall() { |
| NumericStatistic stat = null; |
| if (labelAccess != null && labelAccess.getStatistic() != null) { |
| stat = labelAccess.getStatistic().getValue(); |
| } |
| |
| String statString = (stat == null) ? "1" : getNumericStatistic(stat); |
| |
| if (labelAccess.getAccess() != null) { |
| switch (labelAccess.getAccess()) { |
| case READ: |
| return tuLabel.readCall(statString); |
| case WRITE: |
| return tuLabel.writeCall(statString); |
| default: |
| // underspecified |
| } |
| } |
| |
| return "/*underspecified access to label" + labelAccess.getData().getName() + "*/"; |
| |
| } |
| |
| public String getModulePath() { |
| return getBasePath() + "/" + getModuleName(); |
| } |
| |
| @Override |
| public String getSrcFile() { |
| return getModuleName() + ".cpp"; |
| } |
| |
| public String getIncPath() { |
| return getModulePath() + "/_inc/" + getIncFile(); |
| } |
| |
| public String getSrcPath() { |
| return getModulePath() + "/_src/" + getSrcFile(); |
| } |
| |
| public boolean isIncFileEmpty() { |
| return !outputBuffer.bufferExists("INC", getModulePath() + "/_inc/" + getModuleName()); |
| } |
| |
| public boolean isSrcFileEmpty() { |
| return !outputBuffer.bufferExists("SRC", getModulePath() + "/_src/" + getModuleName()); |
| } |
| |
| public boolean incAppend(final String str) { |
| return outputBuffer.appendTo("INC", getModulePath() + "/_inc/" + getModuleName(), str); |
| } |
| |
| public boolean srcAppend(final String str) { |
| return outputBuffer.appendTo("SRC", getModulePath() + "/_src/" + getModuleName(), str); |
| } |
| |
| protected static String _getNumericStatistic(final MinAvgMaxStatistic stat) { |
| String _string = Integer.valueOf(Double.valueOf(Math.ceil(stat.getAvg())).intValue()).toString(); |
| return (_string + "/*MinAvgMaxStatistic not supported yet*/"); |
| } |
| |
| protected static String _getNumericStatistic(final SingleValueStatistic stat) { |
| return Integer.valueOf(Double.valueOf(Math.ceil(stat.getValue())).intValue()).toString(); |
| } |
| |
| public static String getNumericStatistic(final NumericStatistic stat) { |
| if (stat instanceof MinAvgMaxStatistic) { |
| return _getNumericStatistic((MinAvgMaxStatistic) stat); |
| } else if (stat instanceof SingleValueStatistic) { |
| return _getNumericStatistic((SingleValueStatistic) stat); |
| } else { |
| throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(stat).toString()); |
| } |
| } |
| } |