Commit c7034e15 authored by Thomas Michael Timmermanns's avatar Thomas Michael Timmermanns Committed by Thomas Michael Timmermanns

Improved instance creation.

Modified cocos such that they work with instances.
parent 461ee8ec
......@@ -28,46 +28,55 @@ import de.se_rwth.commons.logging.Log;
//check all cocos
public class CNNArchCocos {
public static ArchitectureSymbol checkAll(ArchitectureSymbol architecture){
ArchitectureSymbol resolvedArchitecture = null;
public static void checkAll(ArchitectureSymbol architecture){
ASTCNNArchNode node = (ASTCNNArchNode) architecture.getAstNode().get();
int findings = Log.getFindings().size();
createPreResolveChecker().checkAll(node);
if (findings == Log.getFindings().size()){
resolvedArchitecture = architecture.resolve();
if (findings == Log.getFindings().size()){
createPostResolveChecker().checkAll(resolvedArchitecture);
createASTChecker().checkAll(node);
if (findings == Log.getFindings().size()) {
createCNNArchPreResolveSymbolChecker().checkAll(architecture);
if (findings == Log.getFindings().size()) {
architecture.resolve();
if (findings == Log.getFindings().size()) {
createCNNArchPostResolveSymbolChecker().checkAll(architecture);
}
}
}
return resolvedArchitecture;
}
public static ArchitectureSymbol checkAll(CNNArchCompilationUnitSymbol compilationUnit){
ArchitectureSymbol resolvedArchitecture = null;
public static void checkAll(CNNArchCompilationUnitSymbol compilationUnit){
ASTCNNArchNode node = (ASTCNNArchNode) compilationUnit.getAstNode().get();
int findings = Log.getFindings().size();
createPreResolveChecker().checkAll(node);
if (findings == Log.getFindings().size()){
resolvedArchitecture = compilationUnit.getArchitecture().resolve();
if (findings == Log.getFindings().size()){
createPostResolveChecker().checkAll(resolvedArchitecture);
createASTChecker().checkAll(node);
if (findings == Log.getFindings().size()) {
createCNNArchPreResolveSymbolChecker().checkAll(compilationUnit);
if (findings == Log.getFindings().size()) {
compilationUnit.getArchitecture().resolve();
if (findings == Log.getFindings().size()) {
createCNNArchPostResolveSymbolChecker().checkAll(compilationUnit);
}
}
}
return resolvedArchitecture;
}
public static CNNArchExtendedCoCoChecker createPostResolveChecker() {
return new CNNArchExtendedCoCoChecker()
//checks cocos based on symbols after the resolve method of the ArchitectureSymbol is called
public static CNNArchSymbolCoCoChecker createCNNArchPostResolveSymbolChecker() {
return new CNNArchSymbolCoCoChecker()
.addCoCo(new CheckIOType())
.addCoCo(new CheckLayerInputs())
.addCoCo(new CheckIOAccessAndIOMissing())
.addCoCo(new CheckArchitectureFinished());
}
public static CNNArchCoCoChecker createPreResolveChecker() {
return new CNNArchCoCoChecker()
//checks cocos based on symbols before the resolve method of the ArchitectureSymbol is called
public static CNNArchSymbolCoCoChecker createCNNArchPreResolveSymbolChecker() {
return new CNNArchSymbolCoCoChecker()
.addCoCo(new CheckIOName())
.addCoCo(new CheckNameExpression())
.addCoCo(new CheckNameExpression());
}
//checks all normal cocos
public static CNNArchCoCoChecker createASTChecker() {
return new CNNArchCoCoChecker()
.addCoCo(new CheckMethodLayer())
.addCoCo(new CheckRangeOperators())
.addCoCo(new CheckVariableName())
......
......@@ -20,9 +20,87 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import de.monticore.symboltable.Symbol;
public interface CNNArchSymbolCoCo {
public class CNNArchSymbolCoCo {
void check(ArchitectureSymbol architecture);
public void check(Symbol sym){
if (sym instanceof ArchitectureSymbol){
check((ArchitectureSymbol) sym);
}
else if (sym instanceof MethodDeclarationSymbol){
check((MethodDeclarationSymbol) sym);
}
else if (sym instanceof LayerSymbol){
check((LayerSymbol) sym);
}
else if (sym instanceof ArchExpressionSymbol){
check((ArchExpressionSymbol) sym);
}
else if (sym instanceof ArchTypeSymbol){
check((ArchTypeSymbol) sym);
}
else if (sym instanceof VariableSymbol){
check((VariableSymbol) sym);
}
else if (sym instanceof ArgumentSymbol){
check((ArgumentSymbol) sym);
}
else if (sym instanceof CNNArchCompilationUnitSymbol){
check((CNNArchCompilationUnitSymbol) sym);
}
else if (sym instanceof IODeclarationSymbol){
check((IODeclarationSymbol) sym);
}
else if (sym instanceof MathExpressionSymbol){
check((MathExpressionSymbol) sym);
}
else{
throw new IllegalStateException("Symbol class is unknown in CNNArchSymbolCoCo: "
+ sym.getClass().getSimpleName());
}
}
public void check(ArchitectureSymbol sym){
//Override if needed
}
public void check(MethodDeclarationSymbol sym){
//Override if needed
}
public void check(LayerSymbol sym){
//Override if needed
}
public void check(ArchExpressionSymbol sym){
//Override if needed
}
public void check(ArchTypeSymbol sym){
//Override if needed
}
public void check(VariableSymbol sym){
//Override if needed
}
public void check(ArgumentSymbol sym){
//Override if needed
}
public void check(CNNArchCompilationUnitSymbol sym){
//Override if needed
}
public void check(IODeclarationSymbol sym){
//Override if needed
}
public void check(MathExpressionSymbol sym){
//Override if needed
}
}
......@@ -20,12 +20,13 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.ScopeSpanningSymbol;
import de.monticore.symboltable.Symbol;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class CNNArchExtendedCoCoChecker{
public class CNNArchSymbolCoCoChecker {
private List<CNNArchSymbolCoCo> cocos = new ArrayList<>();
......@@ -33,17 +34,42 @@ public class CNNArchExtendedCoCoChecker{
return cocos;
}
public CNNArchExtendedCoCoChecker addCoCo(CNNArchSymbolCoCo coco) {
public CNNArchSymbolCoCoChecker addCoCo(CNNArchSymbolCoCo coco) {
getCocos().add(coco);
return this;
}
public void checkAll(ArchitectureSymbol resolvedArchitecture) {
if (!resolvedArchitecture.isResolved()){
throw new IllegalStateException("Architecture has to be resolved to check symboltable cocos");
}
public void checkAll(Symbol sym) {
handle(sym, new HashSet<>());
}
public void check(Symbol sym){
for (CNNArchSymbolCoCo coco : getCocos()){
coco.check(resolvedArchitecture);
coco.check(sym);
}
}
public void handle(Symbol sym, Set<Symbol> checkedSymbols){
if (!checkedSymbols.contains(sym)) {
check(sym);
checkedSymbols.add(sym);
if (sym instanceof ScopeSpanningSymbol) {
traverse(((ScopeSpanningSymbol) sym).getSpannedScope(), checkedSymbols);
}
}
}
public void traverse(Scope scope, Set<Symbol> checkedSymbols){
for (Collection<Symbol> collection : scope.getLocalSymbols().values()){
for (Symbol sym : collection){
handle(sym, checkedSymbols);
}
}
for (Scope subScope : scope.getSubScopes()){
if (!subScope.isSpannedBySymbol()){
traverse(subScope, checkedSymbols);
}
}
}
}
......@@ -20,12 +20,11 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
public class CheckArchitectureFinished implements CNNArchSymbolCoCo {
public class CheckArchitectureFinished extends CNNArchSymbolCoCo {
@Override
public void check(ArchitectureSymbol architecture) {
......
......@@ -20,7 +20,6 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.IODeclarationSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.IOLayerSymbol;
......@@ -34,7 +33,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CheckIOAccessAndIOMissing implements CNNArchSymbolCoCo {
public class CheckIOAccessAndIOMissing extends CNNArchSymbolCoCo {
@Override
public void check(ArchitectureSymbol architecture) {
......
......@@ -20,8 +20,9 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTIOLayer;
import de.monticore.lang.monticar.cnnarch._symboltable.IODeclarationSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.IOLayerSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.LayerSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
......@@ -29,18 +30,24 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class CheckIOName implements CNNArchASTIOLayerCoCo {
public class CheckIOName extends CNNArchSymbolCoCo {
private Set<IODeclarationSymbol> checkedIODeclarations = new HashSet<>();
@Override
public void check(ASTIOLayer node) {
Collection<IODeclarationSymbol> ioDeclarations = node.getEnclosingScope().get().<IODeclarationSymbol>resolveMany(node.getName(), IODeclarationSymbol.KIND);
public void check(LayerSymbol sym) {
if (sym instanceof IOLayerSymbol){
checkIOLayer((IOLayerSymbol) sym);
}
}
public void checkIOLayer(IOLayerSymbol ioLayer) {
Collection<IODeclarationSymbol> ioDeclarations = ioLayer.getEnclosingScope().resolveMany(ioLayer.getName(), IODeclarationSymbol.KIND);
if (ioDeclarations.isEmpty()){
Log.error("0" + ErrorCodes.UNKNOWN_IO + " Unknown input or output name. " +
"The input or output '" + node.getName() + "' does not exist"
, node.get_SourcePositionStart());
"The input or output '" + ioLayer.getName() + "' does not exist"
, ioLayer.getSourcePosition());
}
else {
IODeclarationSymbol ioDeclaration = ioDeclarations.iterator().next();
......
......@@ -20,7 +20,6 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchSimpleExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchTypeSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
......@@ -30,16 +29,16 @@ import de.se_rwth.commons.logging.Log;
import java.util.Optional;
public class CheckIOType implements CNNArchSymbolCoCo {
public class CheckIOType extends CNNArchSymbolCoCo {
@Override
public void check(ArchitectureSymbol architecture) {
for (IODeclarationSymbol ioDeclaration : architecture.getIODeclarations()){
check(ioDeclaration);
checkIO(ioDeclaration);
}
}
public void check(IODeclarationSymbol ioDeclaration) {
public void checkIO(IODeclarationSymbol ioDeclaration) {
ArchTypeSymbol type = ioDeclaration.getType();
if (type.getElementType().isIsComplex() || type.getElementType().isIsBoolean()){
......
......@@ -20,10 +20,9 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
public class CheckLayerInputs implements CNNArchSymbolCoCo {
public class CheckLayerInputs extends CNNArchSymbolCoCo {
@Override
public void check(ArchitectureSymbol architecture) {
......
......@@ -22,7 +22,7 @@ package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathNameExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchSimpleExpression;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchSimpleExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.VariableSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
......@@ -31,18 +31,23 @@ import de.se_rwth.commons.logging.Log;
import java.util.Collection;
public class CheckNameExpression implements CNNArchASTArchSimpleExpressionCoCo {
public class CheckNameExpression extends CNNArchSymbolCoCo {
@Override
public void check(ASTArchSimpleExpression node) {
ArchSimpleExpressionSymbol expression = (ArchSimpleExpressionSymbol) node.getSymbol().get();
public void check(ArchExpressionSymbol sym) {
if (sym instanceof ArchSimpleExpressionSymbol){
checkSimpleExpression((ArchSimpleExpressionSymbol) sym);
}
}
public void checkSimpleExpression(ArchSimpleExpressionSymbol expression) {
if (expression.getMathExpression().isPresent()){
MathExpressionSymbol mathExpression = expression.getMathExpression().get();
for (MathExpressionSymbol subMathExp : Utils.createSubExpressionList(mathExpression)){
if (subMathExp instanceof MathNameExpressionSymbol){
String name = ((MathNameExpressionSymbol) subMathExp).getNameToAccess();
Collection<VariableSymbol> variableCollection = node.getEnclosingScope().get().resolveMany(name, VariableSymbol.KIND);
Collection<VariableSymbol> variableCollection = expression.getEnclosingScope().resolveMany(name, VariableSymbol.KIND);
if (variableCollection.isEmpty()){
Log.error("0" + ErrorCodes.UNKNOWN_VARIABLE_NAME + " Unknown variable name. " +
......
......@@ -21,7 +21,6 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
......
......@@ -20,7 +20,6 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import java.util.ArrayList;
......
......@@ -21,7 +21,6 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import com.google.common.base.Joiner;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import java.util.ArrayList;
......
......@@ -23,9 +23,9 @@ package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.math.math._symboltable.expression.*;
import de.monticore.lang.monticar.cnnarch.helper.Calculator;
import de.monticore.lang.monticar.cnnarch.helper.Utils;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import org.jscience.mathematics.number.Rational;
import java.util.*;
......@@ -246,6 +246,17 @@ public class ArchSimpleExpressionSymbol extends ArchExpressionSymbol {
}
public static ArchSimpleExpressionSymbol of(Rational value){
ArchSimpleExpressionSymbol res = new ArchSimpleExpressionSymbol();
if (value.getDivisor().intValue() == 1){
res.setValue(value.getDividend().intValue());
}
else {
res.setValue(value.doubleValue());
}
return res;
}
public static ArchSimpleExpressionSymbol of(int value){
ArchSimpleExpressionSymbol res = new ArchSimpleExpressionSymbol();
res.setValue(value);
......
......@@ -114,7 +114,7 @@ public class ArchTypeSymbol extends CommonSymbol {
return getChannelsSymbol().getIntValue().get();
}
protected void setDimensionSymbols(List<ArchSimpleExpressionSymbol> dimensions) {
public void setDimensionSymbols(List<ArchSimpleExpressionSymbol> dimensions) {
this.dimensions = dimensions;
}
......
......@@ -41,16 +41,11 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
private List<IOLayerSymbol> inputs = new ArrayList<>();
private List<IOLayerSymbol> outputs = new ArrayList<>();
private Map<String, IODeclarationSymbol> ioDeclarationMap = new HashMap<>();
private boolean isCopy = false;
public ArchitectureSymbol() {
super("", KIND);
}
public ArchitectureSymbol(String name) {
super(name, KIND);
}
public LayerSymbol getBody() {
return body;
}
......@@ -67,10 +62,6 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
return outputs;
}
public boolean isCopy() {
return isCopy;
}
//called in IOLayer to get IODeclaration; only null if error; will be checked in coco CheckIOName
@Nullable
protected IODeclarationSymbol resolveIODeclaration(String name){
......@@ -94,30 +85,17 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
return getSpannedScope().resolveLocally(MethodDeclarationSymbol.KIND);
}
//useful to resolve the architecture in a component instance
public ArchitectureSymbol resolveInScope(Scope scope){
if (isCopy()){
getBody().checkIfResolvable();
try{
getBody().resolveOrError();
}
catch (ArchResolveException e){
//do nothing; error is already logged
}
return this;
public void resolve(){
getBody().checkIfResolvable();
try{
getBody().resolveOrError();
}
else {
ArchitectureSymbol copy = preResolveDeepCopy();
copy.putInScope(scope);
copy.resolve();
return copy;
catch (ArchResolveException e){
//do nothing; error is already logged
}
}
public ArchitectureSymbol resolve(){
return resolveInScope(getEnclosingScope());
}
public List<LayerSymbol> getFirstLayers(){
if (!getBody().isResolved()){
resolve();
......@@ -137,13 +115,18 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
Collection<Symbol> symbolsInScope = scope.getLocalSymbols().get(getName());
if (symbolsInScope == null || !symbolsInScope.contains(this)){
scope.getAsMutableScope().add(this);
getSpannedScope().getAsMutableScope().setResolvingFilters(scope.getResolvingFilters());
Utils.recursiveSetResolvingFilters(getSpannedScope(), scope.getResolvingFilters());
}
}
public ArchitectureSymbol preResolveDeepCopy(){
ArchitectureSymbol copy = new ArchitectureSymbol("instance");
/*
Creates a unresolved copy of this architecture and
adds the copy to the scope given as argument.
Useful to create instances.
This works even if "this" is already resolved.
*/
public ArchitectureSymbol preResolveDeepCopy(Scope enclosingScopeOfCopy){
ArchitectureSymbol copy = new ArchitectureSymbol();
copy.setBody(getBody().preResolveDeepCopy());
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
......@@ -160,7 +143,7 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
}
copy.getBody().putInScope(copy.getSpannedScope());
copy.isCopy = true;
copy.putInScope(enclosingScopeOfCopy);
return copy;
}
}
......@@ -34,7 +34,6 @@ public class CNNArchCompilationUnitSymbol extends CNNArchCompilationUnitSymbolTO
private List<VariableSymbol> parameters;
private List<IODeclarationSymbol> ioDeclarations;
private ArchitectureSymbol architecture;
private boolean isCopy = false;
public CNNArchCompilationUnitSymbol(String name) {
super(name);
......@@ -64,13 +63,10 @@ public class CNNArchCompilationUnitSymbol extends CNNArchCompilationUnitSymbolTO
this.parameters = parameters;
}
public boolean isCopy() {
return isCopy;
}
public ArchitectureSymbol resolve(){
checkParameters();
return getArchitecture().resolve();
getArchitecture().resolve();
return getArchitecture();
}
......@@ -159,11 +155,9 @@ public class CNNArchCompilationUnitSymbol extends CNNArchCompilationUnitSymbolTO
}
copy.setIoDeclarations(ioCopies);
ArchitectureSymbol architecture = getArchitecture().preResolveDeepCopy();
ArchitectureSymbol architecture = getArchitecture().preResolveDeepCopy(copy.getSpannedScope());
copy.setArchitecture(architecture);
architecture.putInScope(copy.getSpannedScope());
Utils.recursiveSetResolvingFilters(copy.getSpannedScope(), getSpannedScope().getResolvingFilters());
copy.isCopy = true;
return copy;
}
}
......@@ -25,7 +25,6 @@ package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.monticar.cnnarch.predefined.AllPredefinedVariables;
import de.monticore.symboltable.CommonScopeSpanningSymbol;
import de.monticore.symboltable.Symbol;
import java.util.ArrayList;
import java.util.List;
......
......@@ -21,7 +21,6 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.se_rwth.commons.logging.Log;
......
......@@ -69,10 +69,10 @@ public class CNNArchGenerator {
System.exit(1);
}
ArchitectureSymbol architecture = CNNArchCocos.checkAll(compilationUnit.get());
CNNArchCocos.checkAll(compilationUnit.get());
try{
generateFiles(architecture);
generateFiles(compilationUnit.get().getArchitecture());
}
catch (IOException e){
Log.error(e.toString());
......
......@@ -52,12 +52,21 @@ public class AbstractSymtabTest {
return scope;
}
protected static ASTCNNArchCompilationUnit getAstNode(String modelPath, String model) {
/* protected static ASTCNNArchCompilationUnit getAstNode(String modelPath, String model) {
Scope symTab = createSymTab(MODEL_PATH + modelPath);
CNNArchCompilationUnitSymbol comp = symTab.<CNNArchCompilationUnitSymbol> resolve(
model, CNNArchCompilationUnitSymbol.KIND).orElse(null);