Commit e6700112 authored by Thomas Michael Timmermanns's avatar Thomas Michael Timmermanns
Browse files

Added first classes of SMI.

Started implementation of ArchValueSymbol and VariableSymbol.
parent 997c8052
......@@ -42,7 +42,7 @@
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
| elements (elements used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
......
......@@ -6,7 +6,7 @@ grammar CNNArch extends de.monticore.lang.math.Math {
symbol scope Architecture = "architecture"
name:Name& "{"
defs:ArchDeclaration*
declarations:ArchDeclaration*
body:ArchBody "}";
interface ArchDeclaration;
......@@ -19,45 +19,49 @@ grammar CNNArch extends de.monticore.lang.math.Math {
Name&
(ArrayDeclaration)?;
ArchType implements Type = (ElementType ("^" "{" (IOVariable || ",")+ "}")?)?;
ArchType implements Type = (ElementType ("^" "{" (Dimension || ",")+ "}")?)?;
symbol IOVariable implements Variable = Name&;
Dimension = IOVariable | intLiteral:UnitNumberResolution;
symbol Constant implements Variable = "def" Name& "=" rhs:ArchRhs;
IOVariable implements Variable = Name&;
Constant implements Variable = "def" Name& "=" rhs:ArchExpression;
symbol scope MethodDeclaration implements ArchDeclaration = "def"
Name& "("
parameters:(Parameter || ",")* ")" "{"
body:ArchBody "}";
symbol Parameter implements Variable = Name& ("=" default:ArchRhs)?;
Parameter implements Variable = Name& ("=" default:ArchExpression)?;
ArchBody = elements:(ArchitectureElement || "->")*;
scope ArchBody = elements:(ArchitectureElement || "->")*;
IOLayer implements ArchitectureElement = Name& ("[" index:ArchExpression "]")?;
IOLayer implements ArchitectureElement = Name& ("[" index:ArchSimpleExpression "]")?;
MethodLayer implements ArchitectureElement = Name& "(" arguments:(Argument || ",")* ")";
Argument = Name "=" rhs:ArchRhs;
Argument = Name "=" rhs:ArchExpression;
ArchRhs = (expression:ArchExpression | sequence:ArchValueSequence);
ParallelLayer implements ArchitectureElement = "(" groups:(ArchBody || "|")+ ")";
ArchValueSequence = "[" parallelValues:(SerialValues || "|")* "]";
ArrayAccessLayer implements ArchitectureElement = "[" index:ArchSimpleExpression "]";
SerialValues = (ArchExpression || "->")+;
ArchValueRange extends ArchValueSequence = "[" start:ArchExpression
(serial:"->" | parallel:"|")
(":" step:ArchExpression)?
":" end:ArchExpression "]";
ArchExpression = (expression:ArchSimpleExpression | sequence:ArchValueSequence);
ParallelLayer implements ArchitectureElement = "(" groups:(ArchBody || "|")+ ")";
ArchValueSequence = "[" parallelValues:(ArchSerialSequence || "|")* "]";
ArchSerialSequence = serialValues:(ArchSimpleExpression || "->")+;
ArchValueRange extends ArchValueSequence = "[" start:ArchSimpleExpression
(serial:"->" | parallel:"|")
(":" step:ArchSimpleExpression)?
":" end:ArchSimpleExpression "]";
ArrayAccessLayer implements ArchitectureElement = "[" index:ArchExpression "]";
ArchExpression = (arithmeticExpression:MathArithmeticExpression
| booleanExpression:MathBooleanExpression
| tupleExpression:TupleExpression);
ArchSimpleExpression = (arithmeticExpression:MathArithmeticExpression
| booleanExpression:MathBooleanExpression
| tupleExpression:TupleExpression);
TupleExpression implements MathExpression = "(" expressions:MathArithmeticExpression "," expressions:(MathArithmeticExpression || ",")* ")";
......
/**
*
* ******************************************************************************
* 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;
public class ErrorMessages {
public static final String OUTPUT_UNDEFINED_CODE = "x0601D";
public static final String OUTPUT_UNDEFINED_MSG = "0" + OUTPUT_UNDEFINED_CODE + " Undefined output. ";
public static final String OUTPUT_MISSING_CODE = "x0601C";
public static final String OUTPUT_MISSING_MSG = "0" + OUTPUT_MISSING_CODE + " Missing output. Architecture has to declare at least one output.";
public static final String OUTPUT_UNUSED_CODE = "x0601E";
public static final String OUTPUT_UNUSED_MSG = "0" + OUTPUT_UNUSED_CODE + " ";
public static final String OUTPUT_DUPLICATE_CODE = "x0601B";
public static final String OUTPUT_DUPLICATE_MSG = "0"+OUTPUT_DUPLICATE_CODE+" ";
public static final String MISSING_ARG_CODE = "x06021";
public static final String MISSING_ARG_MSG = "0"+MISSING_ARG_CODE+" Missing argument. ";
public static final String PLACEHOLDER_ARG_CODE = "x0303B";
public static final String PLACEHOLDER_ARG_MSG = "0"+PLACEHOLDER_ARG_CODE+" \"_placeholder\" is not an argument. ";
public static final String DUPLICATE_ARG_CODE = "x03031";
public static final String DUPLICATE_ARG_MSG = "0"+DUPLICATE_ARG_CODE+" Multiple assignments of the same argument are not allowed. ";
public static final String MISSING_VAR_VALUE_CODE = "x02572";
public static final String INCORRECT_ARG_TYPE_CODE = "x03444";
public static final String INCORRECT_ARG_TYPE_MSG = "0"+INCORRECT_ARG_TYPE_CODE+" Incorrect argument type. ";
public static final String OUTPUT_MISSING_VAR_FC_CODE = "x06068";
public static final String OUTPUT_MISSING_VAR_FC_MSG = "0" + OUTPUT_MISSING_VAR_FC_CODE +
" The output structure has to contain a fullyConnected-Layer without the argument 'units'. " +
"This is because the number of outputs is variable for all architectures. " +
"Example: output{ fullyConnected() activation.softmax() } -> out";
}
/**
*
* ******************************************************************************
* 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 java.util.Optional;
abstract public class ArchAbstractSequenceValue extends ArchValueSymbol {
public ArchAbstractSequenceValue() {
super();
}
abstract public boolean isParallelSequence();
abstract public boolean isSerialSequence();
abstract public Optional<Integer> getParallelLength();
abstract public Optional<Integer> getSerialLength();
}
/**
*
* ******************************************************************************
* 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 java.util.Optional;
public class ArchRangeValueSymbol extends ArchAbstractSequenceValue {
private ArchSimpleValueSymbol startSymbol;
private ArchSimpleValueSymbol endSymbol;
private boolean parallel;
public ArchRangeValueSymbol() {
super();
}
public ArchRangeValueSymbol(ArchSimpleValueSymbol startSymbol, ArchSimpleValueSymbol endSymbol, boolean parallel) {
this.startSymbol = startSymbol;
this.endSymbol = endSymbol;
this.parallel = parallel;
}
public ArchSimpleValueSymbol getStartSymbol() {
return startSymbol;
}
public void setStartSymbol(ArchSimpleValueSymbol startSymbol) {
this.startSymbol = startSymbol;
}
public ArchSimpleValueSymbol getEndSymbol() {
return endSymbol;
}
public void setEndSymbol(ArchSimpleValueSymbol endSymbol) {
this.endSymbol = endSymbol;
}
protected boolean isParallel() {
return parallel;
}
public void setParallel(boolean parallel) {
this.parallel = parallel;
}
@Override
public boolean isParallelSequence() {
return isParallel();
}
@Override
public boolean isSerialSequence() {
return !isParallel();
}
private Optional<Integer> getLength(){
Optional<Integer> optLength = Optional.empty();
ArchRangeValueSymbol resolvedSymbol = resolve();
if (resolvedSymbol.isFullyResolved()){
Object startValue = resolvedSymbol.getEndSymbol().getValue().get();
Object endValue = resolvedSymbol.getEndSymbol().getValue().get();
if (startValue instanceof Integer && endValue instanceof Integer){
int start = (Integer)startValue;
int end = (Integer) endValue;
optLength = Optional.of(Math.abs(end - start) + 1);
}
}
return optLength;
}
@Override
public Optional<Integer> getParallelLength() {
if (isParallelSequence()) {
return getLength();
}
else {
return Optional.of(1);
}
}
@Override
public Optional<Integer> getSerialLength() {
if (isSerialSequence()) {
return getLength();
}
else {
return Optional.of(1);
}
}
@Override
public Optional<Object> getValue() {
return null;
}
@Override
public ArchRangeValueSymbol resolve() {
return null;
}
}
/**
*
* ******************************************************************************
* 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 java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class ArchSequenceValueSymbol extends ArchAbstractSequenceValue {
private List<List<ArchSimpleValueSymbol>> elements;
public ArchSequenceValueSymbol(List<List<ArchSimpleValueSymbol>> elements) {
this.elements = elements;
}
public List<List<ArchSimpleValueSymbol>> getElements() {
return elements;
}
public void setElements(List<List<ArchSimpleValueSymbol>> elements) {
this.elements = elements;
}
@Override
public boolean isSerialSequence(){
boolean isSerial = !isParallelSequence();
for (List<ArchSimpleValueSymbol> serialElement : getElements()){
if (serialElement.size() >= 2){
isSerial = true;
}
}
return isSerial;
}
@Override
public boolean isParallelSequence(){
return elements.size() >= 2;
}
@Override
public Optional<Integer> getParallelLength() {
return Optional.of(elements.size());
}
@Override
public Optional<Integer> getSerialLength() {
if (!elements.isEmpty()){
int maxLenght = 0;
for (List<ArchSimpleValueSymbol> element : getElements()){
if (maxLenght < element.size()){
maxLenght = element.size();
}
}
return Optional.of(maxLenght);
}
else {
return Optional.of(0);
}
}
@Override
public Optional<Object> getValue() {
ArchSequenceValueSymbol resolvedSymbol = resolve();
if (resolvedSymbol.isFullyResolved()){
List<List<Object>> valueLists = new ArrayList<>(4);
for (List<ArchSimpleValueSymbol> serialList : resolvedSymbol.getElements()) {
List<Object> values = new ArrayList<>(4);
for (ArchSimpleValueSymbol element : serialList) {
values.add(element.getValue().get());
}
valueLists.add(values);
}
if (isParallelSequence() || valueLists.isEmpty()) {
return Optional.of(valueLists);
}
else {
return Optional.of(valueLists.get(0));
}
}
else{
return Optional.empty();
}
}
@Override
public ArchSequenceValueSymbol resolve() {
if (isFullyResolved()){
return this;
}
else {
boolean isFullyResolved = true;
List<List<ArchSimpleValueSymbol>> resolvedElements = new ArrayList<>(4);
for (List<ArchSimpleValueSymbol> serialList : getElements()) {
List<ArchSimpleValueSymbol> resolvedSerialList = new ArrayList<>(4);
for (ArchSimpleValueSymbol element : serialList) {
ArchSimpleValueSymbol resolvedElement = element.resolve();
if (!resolvedElement.isFullyResolved()) {
isFullyResolved = false;
}
resolvedSerialList.add(element.resolve());
}
resolvedElements.add(resolvedSerialList);
}
ArchSequenceValueSymbol resolvedCopy = new ArchSequenceValueSymbol(resolvedElements);
resolvedCopy.setFullyResolved(isFullyResolved);
return resolvedCopy;
}
}
}
/**
*
* ******************************************************************************
* 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.lang.math.math._symboltable.expression.MathArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathCompareExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.interfaces.TextualExpression;
import java.util.Optional;
public class ArchSimpleValueSymbol extends ArchValueSymbol implements TextualExpression {
private MathExpressionSymbol expression;
public ArchSimpleValueSymbol() {
super();
}
public MathExpressionSymbol getExpression() {
return expression;
}
public void setExpression(MathExpressionSymbol expression) {
this.expression = expression;
}
@Override
public boolean isBoolean(){
return (expression instanceof MathCompareExpressionSymbol);
}
@Override
public boolean isNumber(){
return (expression instanceof MathArithmeticExpressionSymbol);
}
@Override
public boolean isTuple(){
return (expression instanceof TupleExpressionSymbol);
}
@Override
public Optional<Boolean> isInt(){
Optional<Boolean> isInt = Optional.empty();
if (isNumber()){
Optional<Object> optValue = getValue();
if (optValue.isPresent()){
/*if (optValue.get() instanceof Double){
Double value = (Double) optValue.get();
isInt = Optional.of(Math.round(value) == value);
}*/
if (optValue.get() instanceof Integer){
isInt = Optional.of(true);
}
else {
isInt = Optional.of(false);
}
}
}
return isInt;
}
@Override
public boolean isSimpleValue() {
return true;
}
@Override
public Optional<Object> getValue() {
//todo
return null;
}
@Override
public ArchSimpleValueSymbol resolve() {
//todo
return null;
}
@Override
public String getTextualRepresentation() {
return expression.getTextualRepresentation();
}
}
/**
*
* ******************************************************************************
* 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