Unverified Commit dc832bdd authored by Thomas Michael Timmermanns's avatar Thomas Michael Timmermanns Committed by GitHub
Browse files

Implemented Parameters and added Cocos (#6)

* Added Tests.

* Implemented Parameters and added Cocos.
parent c2bd588f
#
#
# ******************************************************************************
# 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/>.
# *******************************************************************************
#
# Java Maven CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-java/ for more details
......
......@@ -30,7 +30,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>cnn-train</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......
......@@ -2,14 +2,21 @@ package de.monticore.lang.monticar;
grammar CNNTrain extends de.monticore.lang.monticar.Common2, de.monticore.lang.NumberUnit{
CNNTrainCompilationUnit = TrainingConfiguration;
symbol scope TrainingConfiguration = "configuration" name:Name& "{" entries:ConfigEntry* "}";
CNNTrainCompilationUnit = Configuration;
symbol scope Configuration = "configuration"
name:Name&
("(" parameters:(ConfigParameter || ",")* ")")? "{"
entries:ConfigEntry* "}";
ConfigParameter = Name&;
interface Entry;
ast Entry = method String getName(){}
method ASTConfigValue getValue(){};
interface ConfigValue;
interface ConfigEntry extends Entry;
interface VariableReference;
ast VariableReference = method String getName(){};
ContextEntry implements ConfigEntry = name:"context" ":" value:ContextValue;
ModelPathEntry implements ConfigEntry = name:"model_path" ":" value:PathValue;
......@@ -45,7 +52,7 @@ grammar CNNTrain extends de.monticore.lang.monticar.Common2, de.monticore.lang.N
| overwrite:"overwrite"
| noLoad:"no_load");
DataValue implements ConfigValue = (DataVariable | PathValue);
DataVariable = Name&;
DataVariable implements VariableReference = Name&;
IntegerValue implements ConfigValue = Number;
NumberValue implements ConfigValue = Number;
BooleanValue implements ConfigValue = (TRUE:"true" | FALSE:"false");
......
......@@ -26,7 +26,8 @@ public class CNNTrainCocos {
return new CNNTrainCoCoChecker()
.addCoCo(new CheckEntryRepetition())
.addCoCo(new CheckInteger())
.addCoCo(new CheckValidPath());
.addCoCo(new CheckValidPath())
.addCoCo(new CheckNameValue());
}
}
......@@ -21,6 +21,7 @@
package de.monticore.lang.monticar.cnntrain._cocos;
import de.monticore.lang.monticar.cnntrain._ast.ASTEntry;
import de.monticore.lang.monticar.cnntrain.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
import java.util.HashSet;
......@@ -33,7 +34,7 @@ public class CheckEntryRepetition implements CNNTrainASTEntryCoCo {
@Override
public void check(ASTEntry node) {
if (entryNameSet.contains(node.getName())){
Log.error("0xC8853 The parameter '" + node.getName() + "' has multiple values. " +
Log.error("0" + ErrorCodes.ENTRY_REPETITION_CODE +" The parameter '" + node.getName() + "' has multiple values. " +
"Multiple assignments of the same parameter are not allowed",
node.get_SourcePositionStart());
}
......
......@@ -21,6 +21,7 @@
package de.monticore.lang.monticar.cnntrain._cocos;
import de.monticore.lang.monticar.cnntrain._ast.ASTIntegerValue;
import de.monticore.lang.monticar.cnntrain.helper.ErrorCodes;
import de.monticore.lang.numberunit._ast.ASTUnitNumber;
import de.se_rwth.commons.logging.Log;
import org.jscience.mathematics.number.Rational;
......@@ -35,7 +36,7 @@ public class CheckInteger implements CNNTrainASTIntegerValueCoCo {
if (unitNumber.isPresent()){
Rational number = unitNumber.get().getNumber().get();
if (number.getDivisor().intValue() != 1){
Log.error("0xC8851 Value has to be an integer."
Log.error("0" + ErrorCodes.NOT_INTEGER_CODE +" Value has to be an integer."
, 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/>.
* *******************************************************************************
*/
package de.monticore.lang.monticar.cnntrain._cocos;
import de.monticore.lang.monticar.cnntrain._ast.ASTVariableReference;
import de.monticore.lang.monticar.cnntrain._symboltable.ConfigParameterSymbol;
import de.monticore.lang.monticar.cnntrain._symboltable.NameValueSymbol;
import de.monticore.lang.monticar.cnntrain.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
import java.util.Collection;
public class CheckNameValue implements CNNTrainASTVariableReferenceCoCo {
@Override
public void check(ASTVariableReference node) {
Collection<ConfigParameterSymbol> parameterCollection = node.getEnclosingScope().get().resolveMany(node.getName(), ConfigParameterSymbol.KIND);
if (parameterCollection.isEmpty()){
Log.error("0" + ErrorCodes.UNKNOWN_VARIABLE_CODE + " Unknown variable with name '" + node.getName() + "'."
, node.get_SourcePositionStart());
}
else if (parameterCollection.size() > 1){
Log.error("0" + ErrorCodes.PARAMETER_REPETITION_CODE + " Parameter with name '" + node.getName() + "' is declared multiple times"
, node.get_SourcePositionStart());
}
}
}
......@@ -21,6 +21,7 @@
package de.monticore.lang.monticar.cnntrain._cocos;
import de.monticore.lang.monticar.cnntrain._ast.ASTPathValue;
import de.monticore.lang.monticar.cnntrain.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
import java.nio.file.Files;
......@@ -41,7 +42,7 @@ public class CheckValidPath implements CNNTrainASTPathValueCoCo {
}*/
}
catch (InvalidPathException e){
Log.error("0xC8556 Invalid path. " + e.getMessage());
Log.error("0" + ErrorCodes.INVALID_PATH_CODE +" Invalid path. " + e.getMessage());
}
}
......
......@@ -42,6 +42,7 @@ public class CNNTrainLanguage extends CNNTrainLanguageTOP {
addResolvingFilter(new CommonResolvingFilter<Symbol>(EntrySymbol.KIND));
addResolvingFilter(new CommonResolvingFilter<Symbol>(NameValueSymbol.KIND));
addResolvingFilter(new CommonResolvingFilter<Symbol>(ValueSymbol.KIND));
addResolvingFilter(new CommonResolvingFilter<Symbol>(ConfigParameterSymbol.KIND));
setModelNameCalculator(new CNNTrainModelNameCalculator());
}
......
......@@ -35,7 +35,7 @@ import java.util.Optional;
public class CNNTrainSymbolTableCreator extends CNNTrainSymbolTableCreatorTOP {
private String compilationUnitPackage = "";
private TrainingConfigurationSymbol configuration;
private ConfigurationSymbol configuration;
public CNNTrainSymbolTableCreator(final ResolvingConfiguration resolvingConfig,
......@@ -51,7 +51,7 @@ public class CNNTrainSymbolTableCreator extends CNNTrainSymbolTableCreatorTOP {
@Override
public void visit(final ASTCNNTrainCompilationUnit compilationUnit) {
Log.debug("Building Symboltable for Script: " + compilationUnit.getTrainingConfiguration().getName(),
Log.debug("Building Symboltable for Script: " + compilationUnit.getConfiguration().getName(),
CNNTrainSymbolTableCreator.class.getSimpleName());
List<ImportStatement> imports = new ArrayList<>();
......@@ -65,16 +65,27 @@ public class CNNTrainSymbolTableCreator extends CNNTrainSymbolTableCreatorTOP {
}
@Override
public void visit(final ASTTrainingConfiguration node){
configuration = new TrainingConfigurationSymbol(node.getName());
public void visit(final ASTConfiguration node){
configuration = new ConfigurationSymbol(node.getName());
addToScopeAndLinkWithNode(configuration , node);
}
@Override
public void endVisit(final ASTTrainingConfiguration trainingConfiguration) {
public void endVisit(final ASTConfiguration trainingConfiguration) {
List<ConfigParameterSymbol> parameters = new ArrayList<>();
for (ASTConfigParameter astParameter : trainingConfiguration.getParameters()){
parameters.add((ConfigParameterSymbol) astParameter.getSymbol().get());
}
configuration.setParameters(parameters);
removeCurrentScope();
}
@Override
public void endVisit(ASTConfigParameter node) {
ConfigParameterSymbol parameter = new ConfigParameterSymbol(node.getName());
addToScopeAndLinkWithNode(parameter, node);
}
@Override
public void endVisit(ASTEntry node) {
EntrySymbol entry = new EntrySymbol(node.getName());
......
......@@ -20,27 +20,19 @@
*/
package de.monticore.lang.monticar.cnntrain._symboltable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import de.monticore.symboltable.SymbolKind;
public class TrainingConfigurationSymbol extends TrainingConfigurationSymbolTOP {
public class ConfigParameterKind implements SymbolKind {
private Map<String, EntrySymbol> entryMap = new HashMap<>();
private static final String NAME = "de.monticore.lang.monticar.cnntrain._symboltable.ConfigParameterKind";
public TrainingConfigurationSymbol(String name) {
super(name);
@Override
public String getName() {
return NAME;
}
public Map<String, EntrySymbol> getEntryMap() {
return entryMap;
@Override
public boolean isKindOf(SymbolKind kind) {
return NAME.equals(kind.getName()) || SymbolKind.super.isKindOf(kind);
}
public Collection<EntrySymbol> getEntries(){
return getEntryMap().values();
}
public EntrySymbol getEntry(String name){
return getEntryMap().get(name);
}
}
}
\ No newline at end of file
/**
*
* ******************************************************************************
* 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.cnntrain._symboltable;
import de.monticore.lang.monticar.types2._ast.ASTType;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.SymbolKind;
import java.util.Optional;
public class ConfigParameterSymbol extends CommonSymbol {
public static final ConfigParameterKind KIND = new ConfigParameterKind();
private ASTType type;
private Object value = null;
public ConfigParameterSymbol(String name) {
super(name, KIND);
}
public ASTType getType() {
return type;
}
public void setType(ASTType type) {
this.type = type;
}
public Optional<Object> getValue(){
return Optional.ofNullable(value);
}
public void setValue(Object value) {
this.value = value;
}
}
/**
*
* ******************************************************************************
* 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.cnntrain._symboltable;
import de.monticore.lang.monticar.cnntrain.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
import org.jscience.mathematics.number.Rational;
import java.util.*;
public class ConfigurationSymbol extends ConfigurationSymbolTOP {
private Map<String, EntrySymbol> entryMap = new HashMap<>();
private List<ConfigParameterSymbol> parameters = new ArrayList<>();
public ConfigurationSymbol(String name) {
super(name);
}
public Map<String, EntrySymbol> getEntryMap() {
return entryMap;
}
public Collection<EntrySymbol> getEntries(){
return getEntryMap().values();
}
public EntrySymbol getEntry(String name){
return getEntryMap().get(name);
}
public List<ConfigParameterSymbol> getParameters() {
return parameters;
}
public void setParameters(List<ConfigParameterSymbol> parameters) {
this.parameters = parameters;
}
public void checkParameters(){
for (ConfigParameterSymbol parameter : getParameters()){
if (!parameter.getValue().isPresent()){
Log.error("0" + ErrorCodes.MISSING_PARAMETER_VALUE_CODE + " Missing configuration argument. " +
"The parameter '" + parameter.getName() + "' has no value.");
}
}
}
public Optional<ConfigParameterSymbol> getParameter(String name){
for (ConfigParameterSymbol parameter : getParameters()){
if (parameter.getName().equals(name)){
return Optional.of(parameter);
}
}
return Optional.empty();
}
private ConfigParameterSymbol getParameterOrError(String name){
Optional<ConfigParameterSymbol> param = getParameter(name);
if (param.isPresent()){
return param.get();
}
else {
throw new IllegalArgumentException("configuration parameter with name " + name + " does not exist.");
}
}
public void setParameter(String name, Rational value){
ConfigParameterSymbol parameter = getParameterOrError(name);
if (value.getDivisor().intValue() == 1){
parameter.setValue(value.getDividend().intValue());
}
else {
parameter.setValue(value.doubleValue());
}
}
public void setParameter(String name, boolean value){
ConfigParameterSymbol parameter = getParameterOrError(name);
parameter.setValue(value);
}
public void setParameter(String name, int value){
ConfigParameterSymbol parameter = getParameterOrError(name);
parameter.setValue(value);
}
public void setParameter(String name, double value){
ConfigParameterSymbol parameter = getParameterOrError(name);
parameter.setValue(value);
}
public void setParameter(String name, String value){
ConfigParameterSymbol parameter = getParameterOrError(name);
parameter.setValue(value);
}
}
......@@ -23,13 +23,21 @@ package de.monticore.lang.monticar.cnntrain._symboltable;
public class NameValueSymbol extends ValueSymbol {
public static final NameValueKind KIND = new NameValueKind();
private ConfigParameterSymbol parameter;
public NameValueSymbol(String name) {
super(name, KIND);
}
public ConfigParameterSymbol getParameter(){
if (parameter == null){
parameter = (ConfigParameterSymbol) getEnclosingScope().resolve(getName(), ConfigParameterSymbol.KIND).get();
}
return parameter;
}
@Override
public String getValue() {
return getName();
public Object getValue() {
return getParameter().getValue();
}
}
/**
*
* ******************************************************************************
* 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.cnntrain.helper;
public class ErrorCodes {
public static final String ENTRY_REPETITION_CODE = "xC8853";
public static final String NOT_INTEGER_CODE = "xC8851";
public static final String UNKNOWN_VARIABLE_CODE = "xC8558";
public static final String PARAMETER_REPETITION_CODE = "xC8559";
public static final String INVALID_PATH_CODE = "xC8556";
public static final String MISSING_PARAMETER_VALUE_CODE = "xC7293";
}
......@@ -21,7 +21,7 @@
package de.monticore.lang.monticar.cnntrain;
import de.monticore.lang.monticar.cnntrain._parser.CNNTrainParser;
import de.monticore.lang.monticar.cnntrain._symboltable.TrainingConfigurationSymbol;
import de.monticore.lang.monticar.cnntrain._symboltable.ConfigurationSymbol;
import de.monticore.symboltable.Scope;
import org.junit.Test;
......@@ -40,9 +40,9 @@ public class SymtabTest extends AbstractSymtabTest {
@Test
public void testAlexnet(){
Scope symTab = createSymTab("src/test/resources");
TrainingConfigurationSymbol a = symTab.<TrainingConfigurationSymbol>resolve(
ConfigurationSymbol a = symTab.<ConfigurationSymbol>resolve(
"SimpleConfig2",
TrainingConfigurationSymbol.KIND).orElse(null);
ConfigurationSymbol.KIND).orElse(null);
assertNotNull(a);
}
......
......@@ -24,7 +24,7 @@ import de.monticore.lang.monticar.cnntrain.AbstractSymtabTest;
import de.monticore.lang.monticar.cnntrain._ast.ASTCNNTrainNode;
import de.monticore.lang.monticar.cnntrain._cocos.CNNTrainCoCoChecker;
import de.monticore.lang.monticar.cnntrain._cocos.CNNTrainCocos;
import de.monticore.lang.monticar.cnntrain._symboltable.TrainingConfigurationSymbol;
import de.monticore.lang.monticar.cnntrain._symboltable.ConfigurationSymbol;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Finding;
import de.se_rwth.commons.logging.Log;
......@@ -48,8 +48,8 @@ public class AbstractCoCoTest extends AbstractSymtabTest {
protected static ASTCNNTrainNode getAstNode(String modelPath, String model) {
Scope symTab = createSymTab(MODEL_PATH + modelPath);
TrainingConfigurationSymbol comp = symTab.<TrainingConfigurationSymbol> resolve(
model, TrainingConfigurationSymbol.KIND).orElse(null);
ConfigurationSymbol comp = symTab.<ConfigurationSymbol> resolve(
model, ConfigurationSymbol.KIND).orElse(null);
assertNotNull("Could not resolve model " + model, comp);
return (ASTCNNTrainNode) comp.getAstNode().get();
......
......@@ -29,6 +29,7 @@ public class AllCoCoTest extends AbstractCoCoTest{
String baseDir="src/test/resources";
@Test
public void testCoCosSimulator() throws IOException {
//checkValid("", "InvalidName");
checkValid("","SimpleConfig1");
checkValid("","SimpleConfig2");
checkValid("","DataReferenceTest");
......@@ -36,14 +37,17 @@ public class AllCoCoTest extends AbstractCoCoTest{
checkValid("","FullConfig2");
checkInvalid(new CNNTrainCoCoChecker().addCoCo(new CheckEntryRepetition()),
getAstNode("", "DuplicatedParameter"),
getAstNode("", "EntryRepetition"),
new ExpectedErrorInfo(1, "xC8853"));
checkInvalid(new CNNTrainCoCoChecker().addCoCo(new CheckInteger()),