Commit 13ca88a6 authored by Sebastian Nickels's avatar Sebastian Nickels
Browse files

Splitted the resolve functionality of ArchitectureElementSymbol into ResolvableSymbol

parent a890a6a6
Pipeline #179998 passed with stages
in 18 minutes and 25 seconds
......@@ -20,22 +20,15 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonScopeSpanningSymbol;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.se_rwth.commons.Joiners;
import java.util.*;
public abstract class ArchitectureElementSymbol extends CommonScopeSpanningSymbol {
public abstract class ArchitectureElementSymbol extends ResolvableSymbol {
public static final ArchitectureElementKind KIND = new ArchitectureElementKind();
private ArchitectureElementSymbol inputElement = null;
private ArchitectureElementSymbol outputElement = null;
private List<ArchTypeSymbol> outputTypes = null;
private Set<ParameterSymbol> unresolvableParameters = null;
private ArchitectureElementSymbol resolvedThis = null;
protected ArchitectureElementSymbol(String name) {
super(name, KIND);
......@@ -98,19 +91,6 @@ public abstract class ArchitectureElementSymbol extends CommonScopeSpanningSymbo
return false;
}
public ArchitectureSymbol getArchitecture(){
Symbol sym = getEnclosingScope().getSpanningSymbol().get();
if (sym instanceof ArchitectureSymbol){
return (ArchitectureSymbol) sym;
}
else if (sym instanceof UnrollSymbol) {
sym = sym.getEnclosingScope().getSpanningSymbol().get();
return (ArchitectureSymbol)sym;
}else {
return ((ArchitectureElementSymbol) sym).getArchitecture();
}
}
/**
* only call after resolve():
* @return returns the non-empty atomic element which have the output of this element as input.
......@@ -155,79 +135,22 @@ public abstract class ArchitectureElementSymbol extends CommonScopeSpanningSymbo
}
}
public Set<ParameterSymbol> getUnresolvableParameters() {
if (unresolvableParameters == null){
checkIfResolvable();
}
return unresolvableParameters;
}
protected void setUnresolvableParameters(Set<ParameterSymbol> unresolvableParameters) {
this.unresolvableParameters = unresolvableParameters;
}
public boolean isResolvable(){
return getUnresolvableParameters().isEmpty();
}
public void checkIfResolvable(){
checkIfResolvable(new HashSet<>());
}
protected void checkIfResolvable(Set<ParameterSymbol> occurringParameters){
Set<ParameterSymbol> unresolvableParameters = new HashSet<>();
computeUnresolvableParameters(unresolvableParameters, occurringParameters);
setUnresolvableParameters(unresolvableParameters);
}
public Optional<ArchitectureElementSymbol> getResolvedThis() {
return Optional.ofNullable(resolvedThis);
}
protected void setResolvedThis(ArchitectureElementSymbol resolvedThis) {
if (resolvedThis != null && resolvedThis != this){
resolvedThis.putInScope(getSpannedScope());
if (getInputElement().isPresent()){
resolvedThis.setInputElement(getInputElement().get());
}
if (getOutputElement().isPresent()){
resolvedThis.setOutputElement(getOutputElement().get());
}
}
this.resolvedThis = resolvedThis;
}
super.setResolvedThis(resolvedThis);
public void resolveOrError() throws ArchResolveException{
Set<ParameterSymbol> names = resolve();
if (!isResolved()){
throw new IllegalStateException("The following names could not be resolved: " + Joiners.COMMA.join(getUnresolvableParameters()));
}
}
ArchitectureElementSymbol resolvedElement = (ArchitectureElementSymbol) getResolvedThis().get();
public boolean isResolved(){
if (getResolvedThis().isPresent() && getResolvedThis().get() != this){
return getResolvedThis().get().isResolved();
if (getInputElement().isPresent()){
resolvedElement.setInputElement(getInputElement().get());
}
else {
return getResolvedThis().isPresent();
if (getOutputElement().isPresent()){
resolvedElement.setOutputElement(getOutputElement().get());
}
}
/**
* resolves all expressions and underlying architecture elements and handles layer method calls and sequences.
* Architecture parameters have to be set before calling resolve.
* Resolves prepares the architecture elements such that the output type and shape of each element can be calculated and checked.
* @return returns the set of all parameters which could not be resolved. Should be ignored.
* @throws ArchResolveException thrown to interrupt the recursive resolve process to avoid follow-up Runtime Exceptions in tests after an error was logged.
* Can be caught and ignored.
*/
abstract public Set<ParameterSymbol> resolve() throws ArchResolveException;
//only call after resolve
protected abstract List<ArchTypeSymbol> computeOutputTypes();
abstract protected void computeUnresolvableParameters(Set<ParameterSymbol> unresolvableParameters, Set<ParameterSymbol> allParameters);
abstract public Optional<Integer> getParallelLength();
abstract public Optional<List<Integer>> getSerialLengths();
......@@ -248,10 +171,6 @@ public abstract class ArchitectureElementSymbol extends CommonScopeSpanningSymbo
}
}
abstract protected void putInScope(Scope scope);
abstract protected void resolveExpressions() throws ArchResolveException;
/**
* only call after resolve.
* @return returns the first atomic elements which are contained in this element or itself if it is atomic.
......@@ -273,10 +192,4 @@ public abstract class ArchitectureElementSymbol extends CommonScopeSpanningSymbo
//only call after resolve; used in coco CheckElementInputs to check the input type and shape of each element.
abstract public void checkInput();
/**
* Creates a deep copy in the state before the architecture resolution.
* @return returns a deep copy of this object in the pre-resolve version.
*/
protected abstract ArchitectureElementSymbol preResolveDeepCopy();
}
......@@ -58,7 +58,7 @@ public class ConstantSymbol extends ArchitectureElementSymbol {
@Override
public List<ArchitectureElementSymbol> getFirstAtomicElements() {
if (getResolvedThis().isPresent() && getResolvedThis().get() != this) {
return getResolvedThis().get().getFirstAtomicElements();
return ((ArchitectureElementSymbol) getResolvedThis().get()).getFirstAtomicElements();
}
else {
return Collections.singletonList(this);
......@@ -68,7 +68,7 @@ public class ConstantSymbol extends ArchitectureElementSymbol {
@Override
public List<ArchitectureElementSymbol> getLastAtomicElements() {
if (getResolvedThis().isPresent() && getResolvedThis().get() != this) {
return getResolvedThis().get().getLastAtomicElements();
return ((ArchitectureElementSymbol) getResolvedThis().get()).getLastAtomicElements();
}
else {
return Collections.singletonList(this);
......@@ -114,7 +114,7 @@ public class ConstantSymbol extends ArchitectureElementSymbol {
if (!getResolvedThis().isPresent()){
throw new IllegalStateException("The architecture resolve() method was never called");
}
outputShapes = getResolvedThis().get().computeOutputTypes();
outputShapes = ((ArchitectureElementSymbol) getResolvedThis().get()).computeOutputTypes();
}
return outputShapes;
......@@ -132,7 +132,7 @@ public class ConstantSymbol extends ArchitectureElementSymbol {
if (!getResolvedThis().isPresent()) {
throw new IllegalStateException("The architecture resolve() method was never called");
}
getResolvedThis().get().checkInput();
((ArchitectureElementSymbol) getResolvedThis().get()).checkInput();
}
}
......
......@@ -81,7 +81,7 @@ public class LayerSymbol extends ArchitectureElementSymbol {
public void setInputElement(ArchitectureElementSymbol inputElement) {
super.setInputElement(inputElement);
if (getResolvedThis().isPresent() && getResolvedThis().get() != this){
getResolvedThis().get().setInputElement(inputElement);
((ArchitectureElementSymbol) getResolvedThis().get()).setInputElement(inputElement);
}
}
......@@ -89,7 +89,7 @@ public class LayerSymbol extends ArchitectureElementSymbol {
public void setOutputElement(ArchitectureElementSymbol outputElement) {
super.setOutputElement(outputElement);
if (getResolvedThis().isPresent() && getResolvedThis().get() != this){
getResolvedThis().get().setOutputElement(outputElement);
((ArchitectureElementSymbol) getResolvedThis().get()).setOutputElement(outputElement);
}
}
......@@ -118,7 +118,7 @@ public class LayerSymbol extends ArchitectureElementSymbol {
return Collections.singletonList(this);
}
else {
return getResolvedThis().get().getFirstAtomicElements();
return ((ArchitectureElementSymbol) getResolvedThis().get()).getFirstAtomicElements();
}
}
......@@ -128,7 +128,7 @@ public class LayerSymbol extends ArchitectureElementSymbol {
return Collections.singletonList(this);
}
else {
return getResolvedThis().get().getLastAtomicElements();
return ((ArchitectureElementSymbol) getResolvedThis().get()).getLastAtomicElements();
}
}
......@@ -260,7 +260,7 @@ public class LayerSymbol extends ArchitectureElementSymbol {
return ((PredefinedLayerDeclaration) getDeclaration()).computeOutputTypes(getInputTypes(), this, VariableSymbol.Member.NONE);
}
else {
return getResolvedThis().get().getOutputTypes();
return ((ArchitectureElementSymbol) getResolvedThis().get()).getOutputTypes();
}
}
......@@ -276,7 +276,7 @@ public class LayerSymbol extends ArchitectureElementSymbol {
((PredefinedLayerDeclaration) getDeclaration()).checkInput(getInputTypes(), this, VariableSymbol.Member.NONE);
}
else {
getResolvedThis().get().checkInput();
((ArchitectureElementSymbol) getResolvedThis().get()).checkInput();
}
}
}
......
/**
*
* ******************************************************************************
* 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.CommonScopeSpanningSymbol;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.monticore.symboltable.SymbolKind;
import de.se_rwth.commons.Joiners;
import java.util.*;
public abstract class ResolvableSymbol extends CommonScopeSpanningSymbol {
private Set<ParameterSymbol> unresolvableParameters = null;
private ResolvableSymbol resolvedThis = null;
protected ResolvableSymbol(String name, SymbolKind kind) {
super(name, kind);
}
public ArchitectureSymbol getArchitecture(){
Symbol sym = getEnclosingScope().getSpanningSymbol().get();
if (sym instanceof ArchitectureSymbol){
return (ArchitectureSymbol) sym;
}
else if (sym instanceof UnrollSymbol) {
sym = sym.getEnclosingScope().getSpanningSymbol().get();
return (ArchitectureSymbol) sym;
}
else {
return ((ArchitectureElementSymbol) sym).getArchitecture();
}
}
public Set<ParameterSymbol> getUnresolvableParameters() {
if (unresolvableParameters == null){
checkIfResolvable();
}
return unresolvableParameters;
}
protected void setUnresolvableParameters(Set<ParameterSymbol> unresolvableParameters) {
this.unresolvableParameters = unresolvableParameters;
}
public boolean isResolvable(){
return getUnresolvableParameters().isEmpty();
}
public void checkIfResolvable(){
checkIfResolvable(new HashSet<>());
}
protected void checkIfResolvable(Set<ParameterSymbol> occurringParameters){
Set<ParameterSymbol> unresolvableParameters = new HashSet<>();
computeUnresolvableParameters(unresolvableParameters, occurringParameters);
setUnresolvableParameters(unresolvableParameters);
}
public Optional<ResolvableSymbol> getResolvedThis() {
return Optional.ofNullable(resolvedThis);
}
protected void setResolvedThis(ResolvableSymbol resolvedThis) {
if (resolvedThis != null && resolvedThis != this){
resolvedThis.putInScope(getSpannedScope());
}
this.resolvedThis = resolvedThis;
}
public void resolveOrError() throws ArchResolveException{
Set<ParameterSymbol> names = resolve();
if (!isResolved()){
throw new IllegalStateException("The following names could not be resolved: " + Joiners.COMMA.join(getUnresolvableParameters()));
}
}
public boolean isResolved(){
if (getResolvedThis().isPresent() && getResolvedThis().get() != this){
return getResolvedThis().get().isResolved();
}
else {
return getResolvedThis().isPresent();
}
}
/**
* resolves all expressions and underlying architecture elements and handles layer method calls and sequences.
* Architecture parameters have to be set before calling resolve.
* Resolves prepares the architecture elements such that the output type and shape of each element can be calculated and checked.
* @return returns the set of all parameters which could not be resolved. Should be ignored.
* @throws ArchResolveException thrown to interrupt the recursive resolve process to avoid follow-up Runtime Exceptions in tests after an error was logged.
* Can be caught and ignored.
*/
abstract public Set<ParameterSymbol> resolve() throws ArchResolveException;
abstract protected void computeUnresolvableParameters(Set<ParameterSymbol> unresolvableParameters, Set<ParameterSymbol> allParameters);
abstract protected void putInScope(Scope scope);
abstract protected void resolveExpressions() throws ArchResolveException;
/**
* Creates a deep copy in the state before the architecture resolution.
* @return returns a deep copy of this object in the pre-resolve version.
*/
protected abstract ArchitectureElementSymbol preResolveDeepCopy();
}
......@@ -181,7 +181,7 @@ public class VariableSymbol extends ArchitectureElementSymbol {
@Override
public List<ArchitectureElementSymbol> getFirstAtomicElements() {
if (getResolvedThis().isPresent() && getResolvedThis().get() != this) {
return getResolvedThis().get().getFirstAtomicElements();
return ((ArchitectureElementSymbol) getResolvedThis().get()).getFirstAtomicElements();
}
else {
return Collections.singletonList(this);
......@@ -191,7 +191,7 @@ public class VariableSymbol extends ArchitectureElementSymbol {
@Override
public List<ArchitectureElementSymbol> getLastAtomicElements() {
if (getResolvedThis().isPresent() && getResolvedThis().get() != this) {
return getResolvedThis().get().getLastAtomicElements();
return ((ArchitectureElementSymbol) getResolvedThis().get()).getLastAtomicElements();
}
else {
return Collections.singletonList(this);
......@@ -371,7 +371,7 @@ public class VariableSymbol extends ArchitectureElementSymbol {
throw new IllegalStateException("The architecture resolve() method was never called");
}
return getResolvedThis().get().computeOutputTypes();
return ((ArchitectureElementSymbol) getResolvedThis().get()).computeOutputTypes();
}
}
......@@ -428,7 +428,7 @@ public class VariableSymbol extends ArchitectureElementSymbol {
throw new IllegalStateException("The architecture resolve() method was never called");
}
getResolvedThis().get().checkInput();
((ArchitectureElementSymbol) getResolvedThis().get()).checkInput();
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment