Commit 1a8d1f6c authored by Svetlana Pavlitskaya's avatar Svetlana Pavlitskaya Committed by Thomas Michael Timmermanns

grammar updated, cocos tests

parent dc832bdd
......@@ -18,22 +18,11 @@ grammar CNNTrain extends de.monticore.lang.monticar.Common2, de.monticore.lang.N
interface VariableReference;
ast VariableReference = method String getName(){};
ContextEntry implements ConfigEntry = name:"context" ":" value:ContextValue;
ModelPathEntry implements ConfigEntry = name:"model_path" ":" value:PathValue;
TrainDataEntry implements ConfigEntry = name:"train_data" ":" value:DataValue;
TrainLabelEntry implements ConfigEntry = name:"train_label" ":" value:DataValue;
TestDataEntry implements ConfigEntry = name:"test_data" ":" value:DataValue;
TestLabelEntry implements ConfigEntry = name:"test_label" ":" value:DataValue;
LoadingModeEntry implements ConfigEntry = name:"loading_mode" ":" value:LoadingModeValue;
OptimizerEntry implements ConfigEntry = name:"optimizer" ":" value:OptimizerValue;
ValidationSplitEntry implements ConfigEntry = name:"validation_split" ":" value:NumberValue;
EvalMetricEntry implements ConfigEntry = name:"eval_metric" ":" value:EvalMetricValue;
NumEpochEntry implements ConfigEntry = name:"num_epoch" ":" value:IntegerValue;
BatchSizeEntry implements ConfigEntry = name:"batch_size" ":" value:IntegerValue;
CheckpointEntry implements ConfigEntry = name:"checkpoint" ":" value:IntegerValue;
PathValue implements ConfigValue = path:StringLiteral;
ContextValue implements ConfigValue = (GPU:"gpu" | CPU:"cpu");
EvalMetricValue implements ConfigValue =(accuracy:"accuracy"
| crossEntropy:"ce"
| f1:"f1"
......@@ -47,11 +36,8 @@ grammar CNNTrain extends de.monticore.lang.monticar.Common2, de.monticore.lang.N
| inv:"inv"
| poly:"poly"
| sigmoid:"sigmoid");
LoadingModeValue implements ConfigValue =(loadOnly:"load_only"
| loadAndTrain:"load_and_train"
| overwrite:"overwrite"
| noLoad:"no_load");
DataValue implements ConfigValue = (DataVariable | PathValue);
DataVariable implements VariableReference = Name&;
IntegerValue implements ConfigValue = Number;
NumberValue implements ConfigValue = Number;
......
......@@ -25,9 +25,7 @@ public class CNNTrainCocos {
public static CNNTrainCoCoChecker createChecker() {
return new CNNTrainCoCoChecker()
.addCoCo(new CheckEntryRepetition())
.addCoCo(new CheckInteger())
.addCoCo(new CheckValidPath())
.addCoCo(new CheckNameValue());
.addCoCo(new CheckInteger());
}
}
/**
*
* ******************************************************************************
* 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());
}
}
}
/**
*
* ******************************************************************************
* 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.ASTPathValue;
import de.monticore.lang.monticar.cnntrain.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CheckValidPath implements CNNTrainASTPathValueCoCo {
@Override
public void check(ASTPathValue node) {
try{
Path path = Paths.get(node.getPath().getValue().replaceAll("\"", ""));
/*if (!Files.exists(path)){
Log.error("0xC8855 File with path '" + node.getPath().getValue() + "' does not exist."
, node.get_SourcePositionStart());
}*/
}
catch (InvalidPathException e){
Log.error("0" + ErrorCodes.INVALID_PATH_CODE +" Invalid path. " + e.getMessage());
}
}
}
......@@ -72,20 +72,9 @@ public class CNNTrainSymbolTableCreator extends CNNTrainSymbolTableCreatorTOP {
@Override
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());
......@@ -95,25 +84,6 @@ public class CNNTrainSymbolTableCreator extends CNNTrainSymbolTableCreatorTOP {
configuration.getEntryMap().put(node.getName(), entry);
}
@Override
public void endVisit(ASTDataValue node){
ValueSymbol value;
if (node.getDataVariable().isPresent()){
value = (ValueSymbol) node.getDataVariable().get().getSymbol().get();
}
else {
value = (ValueSymbol) node.getPathValue().get().getSymbol().get();
}
node.setSymbol(value);
}
@Override
public void endVisit(ASTPathValue node) {
ValueSymbol value = new ValueSymbol();
value.setValue(node.getPath().getValue().replaceAll("\"", ""));
addToScopeAndLinkWithNode(value, node);
}
@Override
public void endVisit(ASTNumberValue node) {
ValueSymbol value = new ValueSymbol();
......@@ -142,18 +112,6 @@ public class CNNTrainSymbolTableCreator extends CNNTrainSymbolTableCreatorTOP {
addToScopeAndLinkWithNode(value, node);
}
@Override
public void endVisit(ASTContextValue node) {
ValueSymbol value = new ValueSymbol();
if (node.getCPU().isPresent()){
value.setValue(Context.CPU);
}
else if (node.getGPU().isPresent()){
value.setValue(Context.GPU);
}
addToScopeAndLinkWithNode(value, node);
}
@Override
public void endVisit(ASTEvalMetricValue node) {
ValueSymbol value = new ValueSymbol();
......@@ -205,24 +163,6 @@ public class CNNTrainSymbolTableCreator extends CNNTrainSymbolTableCreatorTOP {
addToScopeAndLinkWithNode(value, node);
}
@Override
public void endVisit(ASTLoadingModeValue node) {
ValueSymbol value = new ValueSymbol();
if (node.getLoadAndTrain().isPresent()){
value.setValue(LoadingMode.LOAD_AND_TRAIN);
}
else if (node.getLoadOnly().isPresent()){
value.setValue(LoadingMode.LOAD_ONLY);
}
else if (node.getNoLoad().isPresent()){
value.setValue(LoadingMode.NO_LOAD);
}
else if (node.getOverwrite().isPresent()){
value.setValue(LoadingMode.OVERWRITE);
}
addToScopeAndLinkWithNode(value, node);
}
public void endVisit(ASTOptimizerValue node){
ValueSymbol value = new ValueSymbol();
value.setValue(node.getName());
......
......@@ -55,15 +55,6 @@ public class ConfigurationSymbol extends ConfigurationSymbolTOP {
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)){
......
/**
*
* ******************************************************************************
* 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;
public enum LoadingMode {
LOAD_AND_TRAIN,
LOAD_ONLY,
NO_LOAD,
OVERWRITE;
}
......@@ -25,8 +25,5 @@ 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";
}
......@@ -25,11 +25,13 @@ import de.monticore.io.paths.ModelPath;
import de.monticore.lang.monticar.cnntrain._symboltable.CNNTrainLanguage;
import de.monticore.symboltable.GlobalScope;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
import java.nio.file.Paths;
public class AbstractSymtabTest {
protected static Scope createSymTab(String... modelPath) {
// Log.enableFailQuick(false);
ModelingLanguageFamily fam = new ModelingLanguageFamily();
fam.addModelingLanguage(new CNNTrainLanguage());
......
......@@ -34,18 +34,21 @@ import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertTrue;
public class ParserTest {
public static final boolean ENABLE_FAIL_QUICK = false;
private static List<String> expectedParseErrorModels = Arrays.asList(
"src/test/resources/WrongParameterName.cnnt",
"src/test/resources/InvalidType.cnnt",
"src/test/resources/InvalidOptimizer.cnnt")
.stream().map(s -> Paths.get(s).toString())
private static List<String> expectedParseErrorModels = Stream.of(
"src/test/resources/invalid_parser_tests/WrongParameterName.cnnt",
"src/test/resources/invalid_parser_tests/WrongParameterName2.cnnt",
"src/test/resources/invalid_parser_tests/InvalidType.cnnt",
"src/test/resources/invalid_parser_tests/InvalidOptimizer.cnnt",
"src/test/resources/invalid_parser_tests/MissingColon.cnnt")
.map(s -> Paths.get(s).toString())
.collect(Collectors.toList());
@Before
......@@ -65,7 +68,7 @@ public class ParserTest {
private void test(String fileEnding) throws IOException {
ParseTest parserTest = new ParseTest("." + fileEnding);
Files.walkFileTree(Paths.get("src/test/resources"), parserTest);
Files.walkFileTree(Paths.get("src/test/resources/"), parserTest);
if (!parserTest.getModelsInError().isEmpty()) {
Log.debug("Models in error", "ParserTest");
......
......@@ -34,12 +34,12 @@ public class SymtabTest extends AbstractSymtabTest {
@Test
public void testParsing() throws Exception {
CNNTrainParser parser = new CNNTrainParser();
assertTrue(parser.parse("src/test/resources/SimpleConfig1.cnnt").isPresent());
assertTrue(parser.parse("src/test/resources/valid_tests/SimpleConfig1.cnnt").isPresent());
}
@Test
public void testAlexnet(){
Scope symTab = createSymTab("src/test/resources");
Scope symTab = createSymTab("src/test/resources/valid_tests/");
ConfigurationSymbol a = symTab.<ConfigurationSymbol>resolve(
"SimpleConfig2",
ConfigurationSymbol.KIND).orElse(null);
......
......@@ -83,11 +83,14 @@ public class AbstractCoCoTest extends AbstractSymtabTest {
* the expected errors are present; once only with the given cocos, checking that no additional
* errors are present.
*/
protected static void checkInvalid(CNNTrainCoCoChecker cocos, ASTCNNTrainNode node,
protected static void checkInvalid(CNNTrainCoCoChecker cocos,
String modelPath,
String model,
ExpectedErrorInfo expectedErrors) {
// check whether all the expected errors are present when using all cocos
Log.getFindings().clear();
ASTCNNTrainNode node = getAstNode(modelPath, model);
CNNTrainCocos.createChecker().checkAll(node);
expectedErrors.checkExpectedPresent(Log.getFindings(), "Got no findings when checking all "
+ "cocos. Did you forget to add the new coco to MontiArcCocos?");
......
......@@ -21,33 +21,33 @@
package de.monticore.lang.monticar.cnntrain.cocos;
import de.monticore.lang.monticar.cnntrain._cocos.*;
import de.monticore.lang.monticar.cnntrain.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
import org.junit.Test;
import java.io.IOException;
public class AllCoCoTest extends AbstractCoCoTest{
String baseDir="src/test/resources";
public AllCoCoTest() {
Log.enableFailQuick(false);
}
@Test
public void testCoCosSimulator() throws IOException {
//checkValid("", "InvalidName");
checkValid("","SimpleConfig1");
checkValid("","SimpleConfig2");
checkValid("","DataReferenceTest");
checkValid("","FullConfig");
checkValid("","FullConfig2");
public void testValidCoCos() throws IOException {
checkValid("valid_tests","SimpleConfig1");
checkValid("valid_tests","SimpleConfig2");
checkValid("valid_tests","FullConfig");
checkValid("valid_tests","FullConfig2");
}
@Test
public void testInvalidCoCos() throws IOException {
checkInvalid(new CNNTrainCoCoChecker().addCoCo(new CheckEntryRepetition()),
getAstNode("", "EntryRepetition"),
new ExpectedErrorInfo(1, "xC8853"));
"invalid_cocos_tests", "EntryRepetition",
new ExpectedErrorInfo(1, ErrorCodes.ENTRY_REPETITION_CODE));
checkInvalid(new CNNTrainCoCoChecker().addCoCo(new CheckInteger()),
getAstNode("", "IntegerTest"),
new ExpectedErrorInfo(1, "xC8851"));
checkInvalid(new CNNTrainCoCoChecker().addCoCo(new CheckNameValue()),
getAstNode("", "InvalidName"),
new ExpectedErrorInfo(2, "xC8558"));
checkInvalid(new CNNTrainCoCoChecker().addCoCo(new CheckNameValue()),
getAstNode("", "ParameterRepetition"),
new ExpectedErrorInfo(1, "xC8559"));
"invalid_cocos_tests", "IntegerTest",
new ExpectedErrorInfo(1, ErrorCodes.NOT_INTEGER_CODE));
}
}
configuration DataReferenceTest(inputData, labelData){
train_data:inputData
train_label:labelData
num_epoch : 5
batch_size : 100
optimizer : adam{
learning_rate : 0.001
}
}
\ No newline at end of file
configuration InvalidDataPath{
train_data:"./data//mnist_train"
num_epoch : 5
batch_size : 100
optimizer : adam
}
\ No newline at end of file
configuration InvalidName{
train_data:inputData
train_label:labelData
num_epoch : 5
batch_size : 100
optimizer : adam{
learning_rate : 0.001
}
}
\ No newline at end of file
configuration MissingParameter1{
train_data:"./data/mnist_train"
}
\ No newline at end of file
configuration MissingParameter2{
num_epoch : 5
batch_size : 100
}
\ No newline at end of file
configuration ParameterRepetition(inputData, labelData, inputData){
train_data:inputData
train_label:labelData
num_epoch : 5
batch_size : 100
optimizer : adam{
learning_rate : 0.001
}
}
\ No newline at end of file
configuration EntryRepetition{
train_data:"./data/mnist_train"
num_epoch : 5
batch_size : 100
optimizer : adam
......
configuration IntegerTest{
train_data:"./data/mnist_train"
num_epoch : 5.1
batch_size : 100
optimizer : adam{
......
configuration SimpleConfig1{
train_data:"./data/mnist_train"
num_epoch : 5
batch_size : 100
optimizer : adamsd
......
configuration InvalidType{
train_data:1
num_epoch : 5.1
batch_size : 100.5
num_epoch : 5
batch_size : 100
optimizer : 2
}
\ No newline at end of file
configuration SimpleConfig1{
num_epoch 5
batch_size : 100
optimizer : adam{
learning_rate : 0.001
}
}
configuration WrongParameterName{
train_data:"./data/mnist_train"
num_epoch : 5
batch_size : 100
optmizer : adam
......
configuration A{
num_epoch : 5
batch_size : 100
eval_metric : cross_entropy
optimizer:rmsprop
}
configuration FullConfig(trainData, trainLabel, testData, testLabel){
train_data:trainData
train_label:trainLabel
test_data:testData
test_label:testLabel
configuration FullConfig{
num_epoch : 5
batch_size : 100
loading_mode:load_and_train
eval_metric:ce
checkpoint:1000
optimizer:rmsprop{
learning_rate:0.001
weight_decay:0.01
......
configuration FullConfig2(trainData, trainLabel){
train_data:trainData
train_label:trainLabel
validation_split:0.9
configuration FullConfig2{
num_epoch:10
batch_size:100
loading_mode:load_and_train
eval_metric:ce
checkpoint:1000
optimizer:adam{
learning_rate:0.001
weight_decay:0.01
......
configuration SimpleConfig1{
train_data:"./data/mnist_train"
num_epoch : 5
batch_size : 100
optimizer : adam{
......