blob: c0c750bc25a9f10ca2f0359fca3247c0a9b9bf99 [file] [log] [blame]
/**
* Copyright (c) 2020 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.generators;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.function.BiConsumer;
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.Component;
import org.eclipse.app4mc.amalthea.model.RunnableCall;
import org.eclipse.app4mc.amalthea.model.Tag;
import org.eclipse.app4mc.amalthea.model.Task;
import org.eclipse.app4mc.slg.commons.m2t.transformers.sw.LabelTransformer;
import org.eclipse.app4mc.slg.commons.m2t.transformers.sw.RunnableTransformer;
import org.eclipse.app4mc.transformation.util.OutputBuffer;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
public class TaskTranslationUnit extends TranslationUnit {
public RunnableTransformer runnableTransformer;
public LabelTransformer labelTransformer;
public String getBasePath() {
return "";
}
public String getModuleName() {
return this.task.getName();
}
private final Task task;
private OutputBuffer outputBuffer;
public TaskTranslationUnit(final OutputBuffer outputBuffer, final RunnableTransformer runnableTransformer,
final LabelTransformer labelTransformer, final Task task, final Component component) {
super();
this.runnableTransformer = runnableTransformer;
this.labelTransformer = labelTransformer;
this.task = task;
this.outputBuffer = outputBuffer;
this.genFiles();
}
public TaskTranslationUnit(final OutputBuffer outputBuffer, final RunnableTransformer runnableTransformer,
final LabelTransformer labelTransformer, final Task task) {
super();
this.task = task;
this.runnableTransformer = runnableTransformer;
this.labelTransformer = labelTransformer;
this.outputBuffer = outputBuffer;
}
public void genFiles() {
final LinkedHashSet<String> includes = new LinkedHashSet<>();
final LinkedList<String> initCalls = new LinkedList<>();
final LinkedList<String> stepCalls = new LinkedList<>();
ActivityGraph _activityGraph = null;
if (this.task != null) {
_activityGraph = this.task.getActivityGraph();
}
EList<ActivityGraphItem> _items = null;
if (_activityGraph != null) {
_items = _activityGraph.getItems();
}
final Consumer<ActivityGraphItem> _function = item -> {
if ((item instanceof RunnableCall)) {
final org.eclipse.app4mc.amalthea.model.Runnable runnable = ((RunnableCall) item).getRunnable();
final RunnableTranslationUnit tu = TaskTranslationUnit.this.runnableTransformer
.transform(((org.eclipse.app4mc.amalthea.model.Runnable) runnable));
includes.add(tu.getIncFile());
final Function1<Tag, Boolean> _function1 = tag -> Boolean.valueOf("initialize".equals(tag.getName()));
boolean _isEmpty = IterableExtensions
.isEmpty(IterableExtensions.<Tag>filter(((RunnableCall) item).getTags(), _function1));
boolean _not = (!_isEmpty);
if (_not) {
initCalls.add(tu.getCall());
} else {
stepCalls.add(tu.getCall());
}
}
};
_items.forEach(_function);
labelTransformer.getCache().forEach((BiConsumer<ArrayList<?>, LabelTranslationUnit>) (label, tu) -> {
includes.add(tu.getIncFile());
initCalls.add(tu.getInitCall());
});
this.incAppend(TaskGenerator.toH(getModuleName()));
this.srcAppend(TaskGenerator.toCpp(getModuleName(), getIncFile(), includes, initCalls, stepCalls));
}
public String getModulePath() { return getBasePath() + "/" + getModuleName(); }
@Override
public String getIncFile() { return getModuleName() + ".hpp"; }
@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);
}
}