Commit 0916081f authored by Sebastian Nickels's avatar Sebastian Nickels

Moved generator-related code to CNNArch2X

parent cd421d5f
Pipeline #157642 passed with stages
in 19 minutes and 3 seconds
/**
*
* ******************************************************************************
* 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;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.CNNArchLanguage;
import de.monticore.symboltable.GlobalScope;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
public abstract class CNNArchGenerator {
private String generationTargetPath;
private String modelsDirPath;
public static void quitGeneration(){
Log.error("Code generation is aborted");
System.exit(1);
}
public boolean isCMakeRequired() {
return true;
}
public String getGenerationTargetPath(){
if (generationTargetPath.charAt(generationTargetPath.length() - 1) != '/') {
this.generationTargetPath = generationTargetPath + "/";
}
return generationTargetPath;
}
public void setGenerationTargetPath(String generationTargetPath){
this.generationTargetPath = generationTargetPath;
}
protected String getModelsDirPath() {
return this.modelsDirPath;
}
public void generate(Path modelsDirPath, String rootModelName){
this.modelsDirPath = modelsDirPath.toString();
final ModelPath mp = new ModelPath(modelsDirPath);
GlobalScope scope = new GlobalScope(mp, new CNNArchLanguage());
generate(scope, rootModelName);
}
public abstract boolean check(ArchitectureSymbol architecture);
public abstract void generate(Scope scope, String rootModelName);
//check cocos with CNNArchCocos.checkAll(architecture) before calling this method.
public abstract Map<String, String> generateStrings(ArchitectureSymbol architecture);
//check cocos with CNNArchCocos.checkAll(architecture) before calling this method.
public void generateFiles(ArchitectureSymbol architecture) throws IOException{
Map<String, String> fileContentMap = generateStrings(architecture);
generateFromFilecontentsMap(fileContentMap);
}
public abstract void generateFromFilecontentsMap(Map<String, String> fileContentMap) throws IOException;
public void generateCMake(String rootModelName){
Map<String, String> fileContentMap = generateCMakeContent(rootModelName);
try {
generateFromFilecontentsMap(fileContentMap);
} catch (IOException e) {
Log.error("CMake file could not be generated" + e.getMessage());
}
}
public abstract Map<String, String> generateCMakeContent(String rootModelName);
}
/**
*
* ******************************************************************************
* 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;
import de.se_rwth.commons.logging.Log;
import java.io.*;
import java.net.URL;
import java.util.Objects;
import java.util.Properties;
public class DataPathConfigParser{
private String configTargetPath;
private String configFileName;
private Properties properties;
public DataPathConfigParser(String configPath) {
setConfigPath(configPath);
properties = new Properties();
try
{
properties.load(new FileInputStream(configTargetPath));
} catch(IOException e)
{
Log.error("Config file " + configPath + " could not be found");
}
}
public String getConfigPath() {
if (configTargetPath.charAt(configTargetPath.length() - 1) != '/') {
this.configTargetPath = configTargetPath + "/";
}
return configTargetPath;
}
public void setConfigPath(String configTargetPath){
this.configTargetPath = configTargetPath;
}
public String getDataPath(String modelName) {
String path = properties.getProperty(modelName);
if(path == null) {
Log.error("Data path config file did not specify a path for component '" + modelName + "'");
}
return path;
}
}
/**
*
* ******************************************************************************
* 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;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import de.monticore.lang.monticar.cnnarch.predefined.AllPredefinedVariables;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
import org.junit.Before;
import org.junit.Test;
import static de.monticore.lang.monticar.cnnarch.ParserTest.ENABLE_FAIL_QUICK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class DataPathConfigParserTest extends AbstractSymtabTest {
@Before
public void setUp() {
// ensure an empty log
Log.getFindings().clear();
Log.enableFailQuick(ENABLE_FAIL_QUICK);
}
@Test
public void testDataPathConfigParserValidComponent() {
DataPathConfigParser parser = new DataPathConfigParser("src/test/resources/architectures/data_paths.txt");
String data_path = parser.getDataPath("ComponentName");
assertTrue("Wrong data path returned", data_path.equals("/path/to/training/data"));
}
@Test
public void testDataPathConfigParserInvalidComponent() {
DataPathConfigParser parser = new DataPathConfigParser("src/test/resources/architectures/data_paths.txt");
String data_path = parser.getDataPath("NotExistingComponent");
assertTrue("For not listed components, null should be returned", data_path == null);
assertTrue(Log.getFindings().size() == 1);
}
@Test
public void testDataPathConfigParserInvalidPath() {
DataPathConfigParser parser = new DataPathConfigParser("invalid/path/data_paths.txt");
assertTrue(Log.getFindings().size() == 1);
}
}
ComponentName /path/to/training/data
\ No newline at end of file
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