Commit 52bc05f2 authored by Sebastian Nickels's avatar Sebastian Nickels
Browse files

Temporarily removed unroll code for merge to master

parent 3bdb3bbc
Pipeline #170848 canceled with stages
in 5 minutes and 13 seconds
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchArgument;
import de.monticore.lang.monticar.cnnarch._ast.ASTUnroll;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.se_rwth.commons.Joiners;
import de.se_rwth.commons.logging.Log;
import java.util.HashSet;
import java.util.Set;
public class CheckUnroll implements CNNArchASTUnrollCoCo{
@Override
public void check(ASTUnroll node) {
Set<String> nameSet = new HashSet<>();
for (ASTArchArgument argument : node.getArgumentsList()){
String name = argument.getName();
if (nameSet.contains(name)){
Log.error("0" + ErrorCodes.DUPLICATED_ARG + " Duplicated name: " + name +
". Multiple values assigned to the same argument."
, argument.get_SourcePositionStart());
}
else {
nameSet.add(name);
}
}
LayerDeclarationSymbol layerDeclaration = ((LayerSymbol) node.getSymbolOpt().get()).getDeclaration();
if (layerDeclaration == null){
ArchitectureSymbol architecture = node.getSymbolOpt().get().getEnclosingScope().<ArchitectureSymbol>resolve("", ArchitectureSymbol.KIND).get();
Log.error("0" + ErrorCodes.UNKNOWN_LAYER + " Unknown layer. " +
"Layer with name '" + node.getName() + "' does not exist. " +
"Existing layers: " + Joiners.COMMA.join(architecture.getLayerDeclarations()) + "."
, node.get_SourcePositionStart());
}
else {
Set<String> requiredArguments = new HashSet<>();
for (ParameterSymbol param : layerDeclaration.getParameters()){
if (!param.getDefaultExpression().isPresent()){
requiredArguments.add(param.getName());
}
}
for (ASTArchArgument argument : node.getArgumentsList()){
requiredArguments.remove(argument.getName());
}
for (String missingArgumentName : requiredArguments){
Log.error("0"+ErrorCodes.MISSING_ARGUMENT + " Missing argument. " +
"The argument '" + missingArgumentName + "' is required."
, node.get_SourcePositionStart());
}
}
}
}
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
/* generated by template symboltable.ScopeSpanningSymbol*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.monticar.cnnarch.predefined.AllPredefinedVariables;
import de.monticore.symboltable.CommonScopeSpanningSymbol;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class UnrollDeclarationSymbol extends CommonScopeSpanningSymbol {
public static final UnrollDeclarationKind KIND = new UnrollDeclarationKind();
private List<ParameterSymbol> parameters;
private SerialCompositeElementSymbol body;
protected UnrollDeclarationSymbol(String name) {
super(name, KIND);
}
@Override
protected UnrollDeclarationScope createSpannedScope() {
return new UnrollDeclarationScope();
}
@Override
public UnrollDeclarationScope getSpannedScope() {
return (UnrollDeclarationScope) super.getSpannedScope();
}
public List<ParameterSymbol> getParameters() {
return parameters;
}
protected void setParameters(List<ParameterSymbol> parameters) {
this.parameters = parameters;
if (!getParameter(AllPredefinedVariables.CONDITIONAL_ARG_NAME).isPresent()){
ParameterSymbol ifParam = AllPredefinedVariables.createConditionalParameter();
this.parameters.add(ifParam);
ifParam.putInScope(getSpannedScope());
}
if (!getParameter(AllPredefinedVariables.SERIAL_ARG_NAME).isPresent()){
ParameterSymbol forParam = AllPredefinedVariables.createSerialParameter();
this.parameters.add(forParam);
forParam.putInScope(getSpannedScope());
}
if (!getParameter(AllPredefinedVariables.PARALLEL_ARG_NAME).isPresent()){
ParameterSymbol forParam = AllPredefinedVariables.createParallelParameter();
this.parameters.add(forParam);
forParam.putInScope(getSpannedScope());
}
}
public SerialCompositeElementSymbol getBody() {
return body;
}
protected void setBody(SerialCompositeElementSymbol body) {
this.body = body;
}
public boolean isPredefined() {
//Override by PredefinedUnrollDeclaration
return false;
}
public Optional<ParameterSymbol> getParameter(String name) {
Optional<ParameterSymbol> res = Optional.empty();
for (ParameterSymbol parameter : getParameters()){
if (parameter.getName().equals(name)){
res = Optional.of(parameter);
}
}
return res;
}
public ArchitectureElementSymbol call(UnrollSymbol layer) throws ArchResolveException{
checkForSequence(layer.getArguments());
if (isPredefined()){
return layer;
}
else {
reset();
set(layer.getArguments());
SerialCompositeElementSymbol copy = getBody().preResolveDeepCopy();
copy.putInScope(getSpannedScope());
copy.resolveOrError();
getSpannedScope().remove(copy);
getSpannedScope().removeSubScope(copy.getSpannedScope());
reset();
return copy;
}
}
private void reset(){
for (ParameterSymbol param : getParameters()){
param.reset();
}
}
private void set(List<ArgumentSymbol> arguments){
for (ArgumentSymbol arg : arguments){
arg.set();
}
}
private void checkForSequence(List<ArgumentSymbol> arguments){
boolean valid = true;
for (ArgumentSymbol arg : arguments){
if (arg.getRhs() instanceof ArchAbstractSequenceExpression){
valid = false;
}
}
if (!valid){
throw new IllegalArgumentException("Arguments with sequence expressions have to be resolved first before calling the layer method.");
}
}
public UnrollDeclarationSymbol deepCopy() {
UnrollDeclarationSymbol copy = new UnrollDeclarationSymbol(getName());
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
}
List<ParameterSymbol> parameterCopies = new ArrayList<>(getParameters().size());
for (ParameterSymbol parameter : getParameters()){
ParameterSymbol parameterCopy = parameter.deepCopy();
parameterCopies.add(parameterCopy);
parameterCopy.putInScope(copy.getSpannedScope());
}
copy.setParameters(parameterCopies);
copy.setBody(getBody().preResolveDeepCopy());
copy.getBody().putInScope(copy.getSpannedScope());
return copy;
}
/*public static class Builder{
private List<ParameterSymbol> parameters = new ArrayList<>();
private CompositeElementSymbol body;
private String name = "";
public Builder parameters(List<ParameterSymbol> parameters) {
this.parameters = parameters;
return this;
}
public Builder parameters(ParameterSymbol... parameters) {
this.parameters = new ArrayList<>(Arrays.asList(parameters));
return this;
}
public Builder body(CompositeElementSymbol body) {
this.body = body;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public UnrollDeclarationSymbol build(){
if (name == null || name.equals("")){
throw new IllegalStateException("Missing or empty name for UnrollDeclarationSymbol");
}
UnrollDeclarationSymbol sym = new UnrollDeclarationSymbol(name);
sym.setBody(body);
if (body != null){
body.putInScope(sym.getSpannedScope());
}
for (ParameterSymbol param : parameters){
param.putInScope(sym.getSpannedScope());
}
sym.setParameters(parameters);
return sym;
}
}*/
}
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import java.util.*;
import java.util.function.Function;
public class UnrollSymbol extends ArchitectureElementSymbol {
protected List<ArchitectureElementSymbol> elements = new ArrayList<>();
private UnrollDeclarationSymbol declaration = null;
private List<ArgumentSymbol> arguments;
public UnrollSymbol(String name) {
super(name);
setResolvedThis(this);
}
public List<ArchitectureElementSymbol> getElements() {
return elements;
}
public boolean isTrainable() {
boolean isTrainable = false;
for (ArchitectureElementSymbol element : elements) {
if (element instanceof CompositeElementSymbol) {
isTrainable |= ((CompositeElementSymbol) element).isTrainable();
}
else if (element instanceof LayerSymbol) {
isTrainable |= ((LayerSymbol) element).getDeclaration().isTrainable();
}
}
return isTrainable;
}
@Override
public boolean isResolvable() {
return super.isResolvable() && getDeclaration() != null;
}
public UnrollDeclarationSymbol getDeclaration() {
if (declaration == null){
Collection<UnrollDeclarationSymbol> declarationCollection = getEnclosingScope().resolveMany(getName(), UnrollDeclarationSymbol.KIND);
if (!declarationCollection.isEmpty()){
setDeclaration(declarationCollection.iterator().next());
}
}
return declaration;
}
private void setDeclaration(UnrollDeclarationSymbol declaration) {
this.declaration = declaration;
}
public List<ArgumentSymbol> getArguments() {
return arguments;
}
protected void setArguments(List<ArgumentSymbol> arguments) {
this.arguments = arguments;
}
@Override
public boolean isAtomic() {
return getElements().isEmpty();
}
@Override
public Set<ParameterSymbol> resolve() throws ArchResolveException {
if (!isResolved()) {
if (isResolvable()) {
List<ArchitectureElementSymbol> resolvedElements = new ArrayList<>();
for (ArchitectureElementSymbol element : getElements()) {
element.resolve();
}
}
}
return getUnresolvableParameters();
}
@Override
protected void resolveExpressions() throws ArchResolveException {
for (ArchitectureElementSymbol element : getElements()){
element.resolveExpressions();
}
}
@Override
public boolean isResolved() {
boolean isResolved = true;
for (ArchitectureElementSymbol element : getElements()){
if (!element.isResolved()){
isResolved = false;
}
}
return isResolved;
}
@Override
protected void computeUnresolvableParameters(Set<ParameterSymbol> unresolvableParameters, Set<ParameterSymbol> allParameters) {
for (ArchitectureElementSymbol element : getElements()){
element.checkIfResolvable(allParameters);
unresolvableParameters.addAll(element.getUnresolvableParameters());
}
}
@Override
protected void putInScope(Scope scope) {
Collection<Symbol> symbolsInScope = scope.getLocalSymbols().get(getName());
if (symbolsInScope == null || !symbolsInScope.contains(this)) {
scope.getAsMutableScope().add(this);
for (ArchitectureElementSymbol element : getElements()) {
element.putInScope(getSpannedScope());
}
}
}
protected void setElements(List<ArchitectureElementSymbol> elements) {
ArchitectureElementSymbol previous = null;
for (ArchitectureElementSymbol current : elements){
if (previous != null){
current.setInputElement(previous);
previous.setOutputElement(current);
}
else {
if (getInputElement().isPresent()){
current.setInputElement(getInputElement().get());
}
if (getOutputElement().isPresent()){
current.setOutputElement(getOutputElement().get());
}
}
previous = current;
}
this.elements = elements;
}
@Override
public void setInputElement(ArchitectureElementSymbol inputElement) {
super.setInputElement(inputElement);
if (!getElements().isEmpty()){
getElements().get(0).setInputElement(inputElement);
}
}
@Override
public void setOutputElement(ArchitectureElementSymbol outputElement) {
super.setOutputElement(outputElement);
if (!getElements().isEmpty()){
getElements().get(getElements().size()-1).setOutputElement(outputElement);
}
}
@Override
public List<ArchitectureElementSymbol> getFirstAtomicElements() {
if (getElements().isEmpty()){
return Collections.singletonList(this);
}
else {
return getElements().get(0).getFirstAtomicElements();
}
}
@Override
public List<ArchitectureElementSymbol> getLastAtomicElements() {
if (getElements().isEmpty()){
return Collections.singletonList(this);
}
else {
return getElements().get(getElements().size()-1).getLastAtomicElements();
}
}
@Override
public List<ArchTypeSymbol> computeOutputTypes() {
if (getElements().isEmpty()){
if (getInputElement().isPresent()){
return getInputElement().get().getOutputTypes();
}
else {
return Collections.emptyList();
}
}
else {
for (ArchitectureElementSymbol element : getElements()){
element.getOutputTypes();
}
return getElements().get(getElements().size() - 1).getOutputTypes();
}
}
public Optional<ArgumentSymbol> getArgument(String name){
for (ArgumentSymbol argument : getArguments()){
if (argument.getName().equals(name)) {
return Optional.of(argument);
}
}
return Optional.empty();
}
public Optional<Integer> getIntValue(String parameterName){
return getTValue(parameterName, ArchExpressionSymbol::getIntValue);
}
public Optional<List<Integer>> getIntTupleValue(String parameterName){
return getTValue(parameterName, ArchExpressionSymbol::getIntTupleValues);
}
public Optional<Boolean> getBooleanValue(String parameterName){
return getTValue(parameterName, ArchExpressionSymbol::getBooleanValue);
}
public Optional<String> getStringValue(String parameterName){
return getTValue(parameterName, ArchExpressionSymbol::getStringValue);
}
public Optional<Double> getDoubleValue(String parameterName){
return getTValue(parameterName, ArchExpressionSymbol::getDoubleValue);
}
public Optional<Object> getValue(String parameterName){
return getTValue(parameterName, ArchExpressionSymbol::getValue);
}
public <T> Optional<T> getTValue(String parameterName, Function<ArchExpressionSymbol, Optional<T>> getValue){
Optional<ArgumentSymbol> arg = getArgument(parameterName);
Optional<ParameterSymbol> param = getDeclaration().getParameter(parameterName);
if (arg.isPresent()){
return getValue.apply(arg.get().getRhs());
}
else if (param.isPresent() && param.get().getDefaultExpression().isPresent()){
return getValue.apply(param.get().getDefaultExpression().get());
}
else {
return Optional.empty();
}
}
@Override
public void checkInput() {
if (getResolvedThis().isPresent()){
if (getResolvedThis().get() == this){
//((PredefinedUnrollDeclaration) getDeclaration()).checkInput(getInputTypes(), this);
}
else {
getResolvedThis().get().checkInput();
}
}
}
@Override
public Optional<Integer> getParallelLength() {
return Optional.of(1);
}
@Override