Commit 242d221d authored by Abdallah Atouani's avatar Abdallah Atouani

Merge remote-tracking branch 'origin/feature/path_tagging' into feature/path_tagging

# Conflicts:
#	src/main/java/de/monticore/lang/monticar/emadl/generator/EMADLGenerator.java
#	src/test/java/de/monticore/lang/monticar/emadl/GenerationTest.java
#	src/test/resources/models/tagging/AlexNet.tag
parents 663f0619 c10db833
......@@ -26,7 +26,7 @@ import com.google.common.base.Splitter;
import com.google.common.io.Resources;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.cncModel.EMAComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.cncModel.EMADynamicComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstantiationSymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.monticar.cnnarch.CNNArchGenerator;
import de.monticore.lang.monticar.cnnarch.DataPathConfigParser;
......@@ -34,6 +34,8 @@ import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnntrain.CNNTrainGenerator;
import de.monticore.lang.monticar.cnntrain._symboltable.ConfigurationSymbol;
import de.monticore.lang.monticar.emadl._cocos.EMADLCocos;
import de.monticore.lang.monticar.emadl._cocos.DataPathCocos;
import de.monticore.lang.monticar.emadl._cocos.CheckArchitecture;
import de.monticore.lang.monticar.emadl.tagging.dltag.DataPathSymbol;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.cpp.ArmadilloHelper;
......@@ -47,19 +49,17 @@ import de.monticore.symboltable.Scope;
import de.se_rwth.commons.Splitters;
import de.se_rwth.commons.logging.Log;
import freemarker.template.TemplateException;
import de.monticore.lang.monticar.emadl.tagging.dltag.DataPathSymbol;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.DigestInputStream;
import javax.xml.bind.DatatypeConverter;
import java.util.*;
public class EMADLGenerator {
......@@ -69,8 +69,6 @@ public class EMADLGenerator {
private Backend backend;
private String modelsPath;
public EMADLGenerator(Backend backend) {
this.backend = backend;
......@@ -86,24 +84,22 @@ public class EMADLGenerator {
}
public void setModelsPath(String modelsPath) {
if (!(modelsPath.substring(modelsPath.length() - 1).equals("/"))){
if (!(modelsPath.substring(modelsPath.length() - 1).equals("/"))) {
this.modelsPath = modelsPath + "/";
}
else {
} else {
this.modelsPath = modelsPath;
}
}
public void setGenerationTargetPath(String generationTargetPath){
if (!(generationTargetPath.substring(generationTargetPath.length() - 1).equals("/"))){
public void setGenerationTargetPath(String generationTargetPath) {
if (!(generationTargetPath.substring(generationTargetPath.length() - 1).equals("/"))) {
getEmamGen().setGenerationTargetPath(generationTargetPath + "/");
}
else {
} else {
getEmamGen().setGenerationTargetPath(generationTargetPath);
}
}
public String getGenerationTargetPath(){
public String getGenerationTargetPath() {
return getEmamGen().getGenerationTargetPath();
}
......@@ -111,24 +107,27 @@ public class EMADLGenerator {
return emamGen;
}
public void generate(String modelPath, String qualifiedName, String pythonPath, String forced, boolean doCompile) throws IOException, TemplateException {
setModelsPath( modelPath );
public void generate(String modelPath, String qualifiedName, String pythonPath, String forced, boolean doCompile)
throws IOException, TemplateException {
setModelsPath(modelPath);
TaggingResolver symtab = EMADLAbstractSymtab.createSymTabAndTaggingResolver(getModelsPath());
EMAComponentSymbol component = symtab.<EMAComponentSymbol>resolve(qualifiedName, EMAComponentSymbol.KIND).orElse(null);
EMAComponentSymbol component = symtab.<EMAComponentSymbol>resolve(qualifiedName, EMAComponentSymbol.KIND)
.orElse(null);
List<String> splitName = Splitters.DOT.splitToList(qualifiedName);
String componentName = splitName.get(splitName.size() - 1);
String instanceName = componentName.substring(0, 1).toLowerCase() + componentName.substring(1);
if (component == null){
if (component == null) {
Log.error("Component with name '" + componentName + "' does not exist.");
System.exit(1);
}
EMAComponentInstanceSymbol instance = component.getEnclosingScope().<EMAComponentInstanceSymbol>resolve(instanceName, EMAComponentInstanceSymbol.KIND).get();
EMAComponentInstanceSymbol instance = component.getEnclosingScope()
.<EMAComponentInstanceSymbol>resolve(instanceName, EMAComponentInstanceSymbol.KIND).get();
generateFiles(symtab, instance, symtab, pythonPath, forced);
if (doCompile) {
compile();
}
......@@ -141,11 +140,11 @@ public class EMADLGenerator {
pb.inheritIO();
Process process = pb.start();
int returnCode = process.waitFor();
if(returnCode != 0) {
if (returnCode != 0) {
Log.error("During compilation, an error occured. See above for more details.");
System.exit(1);
}
}catch(Exception e){
} catch (Exception e) {
Log.error("During compilation, the following error occured: '" + e.toString() + "'");
System.exit(1);
} finally {
......@@ -153,11 +152,10 @@ public class EMADLGenerator {
}
}
public File createTempScript() throws IOException{
public File createTempScript() throws IOException {
File tempScript = File.createTempFile("script", null);
try{
Writer streamWriter = new OutputStreamWriter(new FileOutputStream(
tempScript));
try {
Writer streamWriter = new OutputStreamWriter(new FileOutputStream(tempScript));
PrintWriter printWriter = new PrintWriter(streamWriter);
printWriter.println("#!/bin/bash");
......@@ -168,7 +166,7 @@ public class EMADLGenerator {
printWriter.println("make");
printWriter.close();
}catch(Exception e){
} catch (Exception e) {
System.out.println(e);
}
......@@ -191,9 +189,11 @@ public class EMADLGenerator {
}
}
public void generateFiles(TaggingResolver taggingResolver, EMAComponentInstanceSymbol EMAComponentSymbol, Scope symtab, String pythonPath, String forced) throws IOException {
public void generateFiles(TaggingResolver taggingResolver, EMAComponentInstanceSymbol EMAComponentSymbol,
Scope symtab, String pythonPath, String forced) throws IOException {
Set<EMAComponentInstanceSymbol> allInstances = new HashSet<>();
List<FileContent> fileContents = generateStrings(taggingResolver, EMAComponentSymbol, symtab, allInstances, forced);
List<FileContent> fileContents = generateStrings(taggingResolver, EMAComponentSymbol, symtab, allInstances,
forced);
for (FileContent fileContent : fileContents) {
emamGen.generateFile(fileContent);
......@@ -201,74 +201,77 @@ public class EMADLGenerator {
// train
Map<String, String> fileContentMap = new HashMap<>();
for(FileContent f : fileContents) {
for (FileContent f : fileContents) {
fileContentMap.put(f.getFileName(), f.getFileContent());
}
List<FileContent> fileContentsTrainingHashes = new ArrayList<>();
List<String> newHashes = new ArrayList<>();
for (EMAComponentInstanceSymbol componentInstance : allInstances) {
Optional<ArchitectureSymbol> architecture = componentInstance.getSpannedScope().resolve("", ArchitectureSymbol.KIND);
Optional<ArchitectureSymbol> architecture = componentInstance.getSpannedScope().resolve("",
ArchitectureSymbol.KIND);
if(!architecture.isPresent()) {
if (!architecture.isPresent()) {
continue;
}
if(forced.equals("n")) {
if (forced.equals("n")) {
continue;
}
String configFilename = getConfigFilename(componentInstance.getComponentType().getFullName(), componentInstance.getFullName(), componentInstance.getName());
String configFilename = getConfigFilename(componentInstance.getComponentType().getFullName(),
componentInstance.getFullName(), componentInstance.getName());
String emadlPath = getModelsPath() + configFilename + ".emadl";
String cnntPath = getModelsPath() + configFilename + ".cnnt";
String emadlHash = getChecksumForFile(emadlPath);
String cnntHash = getChecksumForFile(cnntPath);
String componentConfigFilename = componentInstance.getComponentType().getReferencedSymbol().getFullName().replaceAll("\\.", "/");
String componentConfigFilename = componentInstance.getComponentType().getReferencedSymbol().getFullName()
.replaceAll("\\.", "/");
String b = backend.getBackendString(backend);
String trainingDataHash = "";
String testDataHash = "";
if(b.equals("CAFFE2")){
if (b.equals("CAFFE2")) {
trainingDataHash = getChecksumForFile(architecture.get().getDataPath() + "/train_lmdb/data.mdb");
testDataHash = getChecksumForFile(architecture.get().getDataPath() + "/test_lmdb/data.mdb");
}else{
} else {
trainingDataHash = getChecksumForFile(architecture.get().getDataPath() + "/train.h5");
testDataHash = getChecksumForFile(architecture.get().getDataPath() + "/test.h5");
}
String trainingHash = emadlHash + "#" + cnntHash + "#" + trainingDataHash + "#" + testDataHash;
boolean alreadyTrained = newHashes.contains(trainingHash) || isAlreadyTrained(trainingHash, componentInstance);
if(alreadyTrained && !forced.equals("y")) {
boolean alreadyTrained = newHashes.contains(trainingHash)
|| isAlreadyTrained(trainingHash, componentInstance);
if (alreadyTrained && !forced.equals("y")) {
Log.warn("Training of model " + componentInstance.getFullName() + " skipped");
}
else {
String parsedFullName = componentInstance.getFullName().substring(0, 1).toLowerCase() + componentInstance.getFullName().substring(1).replaceAll("\\.", "_");
} else {
String parsedFullName = componentInstance.getFullName().substring(0, 1).toLowerCase()
+ componentInstance.getFullName().substring(1).replaceAll("\\.", "_");
String trainerScriptName = "CNNTrainer_" + parsedFullName + ".py";
String trainingPath = getGenerationTargetPath() + trainerScriptName;
if(Files.exists(Paths.get(trainingPath))){
if (Files.exists(Paths.get(trainingPath))) {
ProcessBuilder pb = new ProcessBuilder(Arrays.asList(pythonPath, trainingPath)).inheritIO();
Process p = pb.start();
int exitCode = 0;
try {
exitCode = p.waitFor();
}
catch(InterruptedException e) {
} catch (InterruptedException e) {
Log.error("Training aborted: exit code " + Integer.toString(exitCode));
System.exit(1);
}
if(exitCode != 0) {
if (exitCode != 0) {
Log.error("Training failed: exit code " + Integer.toString(exitCode));
System.exit(1);
}
fileContentsTrainingHashes.add(new FileContent(trainingHash, componentConfigFilename + ".training_hash"));
fileContentsTrainingHashes
.add(new FileContent(trainingHash, componentConfigFilename + ".training_hash"));
newHashes.add(trainingHash);
}
else{
} else {
System.out.println("Trainingfile " + trainingPath + " not found.");
}
}
......@@ -283,8 +286,7 @@ public class EMADLGenerator {
private static String convertByteArrayToHexString(byte[] arrayBytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < arrayBytes.length; i++) {
stringBuffer.append(Integer.toString((arrayBytes[i] & 0xff) + 0x100, 16)
.substring(1));
stringBuffer.append(Integer.toString((arrayBytes[i] & 0xff) + 0x100, 16).substring(1));
}
return stringBuffer.toString();
}
......@@ -295,24 +297,25 @@ public class EMADLGenerator {
String componentConfigFilename = component.getFullName().replaceAll("\\.", "/");
String checkFilePathString = getGenerationTargetPath() + componentConfigFilename + ".training_hash";
Path checkFilePath = Paths.get( checkFilePathString);
if(Files.exists(checkFilePath)) {
Path checkFilePath = Paths.get(checkFilePathString);
if (Files.exists(checkFilePath)) {
List<String> hashes = Files.readAllLines(checkFilePath);
for(String hash : hashes) {
if(hash.equals(trainingHash)) {
for (String hash : hashes) {
if (hash.equals(trainingHash)) {
return true;
}
}
}
return false;
}
catch(Exception e) {
} catch (Exception e) {
return false;
}
}
public List<FileContent> generateStrings(TaggingResolver taggingResolver, EMAComponentInstanceSymbol componentInstanceSymbol, Scope symtab, Set<EMAComponentInstanceSymbol> allInstances, String forced){
public List<FileContent> generateStrings(TaggingResolver taggingResolver,
EMAComponentInstanceSymbol componentInstanceSymbol, Scope symtab,
Set<EMAComponentInstanceSymbol> allInstances, String forced) {
List<FileContent> fileContents = new ArrayList<>();
generateComponent(fileContents, allInstances, taggingResolver, componentInstanceSymbol, symtab);
......@@ -326,14 +329,16 @@ public class EMADLGenerator {
if (cnnArchGenerator.isCMakeRequired()) {
cnnArchGenerator.setGenerationTargetPath(getGenerationTargetPath());
Map<String, String> cmakeContentsMap = cnnArchGenerator.generateCMakeContent(componentInstanceSymbol.getFullName());
for (String fileName : cmakeContentsMap.keySet()){
Map<String, String> cmakeContentsMap = cnnArchGenerator
.generateCMakeContent(componentInstanceSymbol.getFullName());
for (String fileName : cmakeContentsMap.keySet()) {
fileContents.add(new FileContent(cmakeContentsMap.get(fileName), fileName));
}
}
if (emamGen.shouldGenerateMainClass()) {
//fileContents.add(emamGen.getMainClassFileContent(componentInstanceSymbol, fileContents.get(0)));
// fileContents.add(emamGen.getMainClassFileContent(componentInstanceSymbol,
// fileContents.get(0)));
} else if (emamGen.shouldGenerateSimulatorInterface()) {
fileContents.addAll(SimulatorIntegrationHelper.getSimulatorIntegrationHelperFileContent());
}
......@@ -343,17 +348,41 @@ public class EMADLGenerator {
return fileContents;
}
protected String getDataPath(TaggingResolver taggingResolver, EMAComponentSymbol component, EMAComponentInstanceSymbol instance){
/**
* returns data path either from tags or data_paths.txt
*/
protected String getDataPath(TaggingResolver taggingResolver, EMAComponentSymbol component,
EMAComponentInstanceSymbol instance) {
List<TagSymbol> instanceTags = new LinkedList<>();
boolean isChildComponent = instance.getEnclosingComponent().isPresent();
if (isChildComponent) {
// get all instantiated components of parent
List<EMAComponentInstantiationSymbol> instantiationSymbols = (List<EMAComponentInstantiationSymbol>) instance
.getEnclosingComponent().get().getComponentType().getReferencedSymbol().getSubComponents();
// filter corresponding instantiation of instance and add tags
instantiationSymbols.stream().filter(e -> e.getName().equals(instance.getName())).findFirst()
.ifPresent(symbol -> instanceTags.addAll(taggingResolver.getTags(symbol, DataPathSymbol.KIND)));
}
// instance tags have priority
List<TagSymbol> instanceTags = (List<TagSymbol>) taggingResolver.getTags(instance, DataPathSymbol.KIND);
List<TagSymbol> tags = !instanceTags.isEmpty() ? instanceTags :
(List<TagSymbol>) taggingResolver.getTags(component, DataPathSymbol.KIND);
List<TagSymbol> tags = !instanceTags.isEmpty() ? instanceTags
: (List<TagSymbol>) taggingResolver.getTags(component, DataPathSymbol.KIND);
String dataPath;
if (!tags.isEmpty()) {
dataPath = (String) tags.get(0).getValues().get(0);
}
else {
DataPathSymbol dataPathSymbol = (DataPathSymbol) tags.get(0);
DataPathCocos.check(dataPathSymbol);
dataPath = dataPathSymbol.getPath();
// TODO: Replace warinings with errors, until then use this method
stopGeneratorIfWarning();
Log.warn("Tagging info for symbol was found, ignoring data_paths.txt: " + dataPath);
} else {
DataPathConfigParser newParserConfig = new DataPathConfigParser(getModelsPath() + "data_paths.txt");
dataPath = newParserConfig.getDataPath(component.getFullName());
}
......@@ -361,103 +390,104 @@ public class EMADLGenerator {
return dataPath;
}
protected void generateComponent(List<FileContent> fileContents,
Set<EMAComponentInstanceSymbol> allInstances,
TaggingResolver taggingResolver,
EMAComponentInstanceSymbol componentInstanceSymbol,
Scope symtab){
protected void generateComponent(List<FileContent> fileContents, Set<EMAComponentInstanceSymbol> allInstances,
TaggingResolver taggingResolver, EMAComponentInstanceSymbol componentInstanceSymbol, Scope symtab) {
allInstances.add(componentInstanceSymbol);
EMAComponentSymbol EMAComponentSymbol = componentInstanceSymbol.getComponentType().getReferencedSymbol();
/* remove the following two lines if the component symbol full name bug with generic variables is fixed */
/*
* remove the following two lines if the component symbol full name bug with
* generic variables is fixed
*/
EMAComponentSymbol.setFullName(null);
EMAComponentSymbol.getFullName();
/* */
Optional<ArchitectureSymbol> architecture = componentInstanceSymbol.getSpannedScope().resolve("", ArchitectureSymbol.KIND);
Optional<MathStatementsSymbol> mathStatements = EMAComponentSymbol.getSpannedScope().resolve("MathStatements", MathStatementsSymbol.KIND);
Optional<ArchitectureSymbol> architecture = componentInstanceSymbol.getSpannedScope().resolve("",
ArchitectureSymbol.KIND);
Optional<MathStatementsSymbol> mathStatements = EMAComponentSymbol.getSpannedScope().resolve("MathStatements",
MathStatementsSymbol.KIND);
EMADLCocos.checkAll(componentInstanceSymbol);
if (architecture.isPresent()){
if (architecture.isPresent()) {
String dPath = getDataPath(taggingResolver, EMAComponentSymbol, componentInstanceSymbol);
architecture.get().setDataPath(dPath);
architecture.get().setComponentName(EMAComponentSymbol.getFullName());
generateCNN(fileContents, taggingResolver, componentInstanceSymbol, architecture.get());
}
else if (mathStatements.isPresent()){
} else if (mathStatements.isPresent()) {
generateMathComponent(fileContents, taggingResolver, componentInstanceSymbol, mathStatements.get());
}
else {
} else {
generateSubComponents(fileContents, allInstances, taggingResolver, componentInstanceSymbol, symtab);
}
}
private void fixArmadilloImports(List<FileContent> fileContents){
for (FileContent fileContent : fileContents){
fileContent.setFileContent(fileContent.getFileContent()
.replaceFirst("#include \"armadillo.h\"",
"#include \"armadillo\""));
private void fixArmadilloImports(List<FileContent> fileContents) {
for (FileContent fileContent : fileContents) {
fileContent.setFileContent(
fileContent.getFileContent().replaceFirst("#include \"armadillo.h\"", "#include \"armadillo\""));
}
}
public void generateCNN(List<FileContent> fileContents, TaggingResolver taggingResolver, EMAComponentInstanceSymbol instance, ArchitectureSymbol architecture){
Map<String,String> contentMap = cnnArchGenerator.generateStrings(architecture);
public void generateCNN(List<FileContent> fileContents, TaggingResolver taggingResolver,
EMAComponentInstanceSymbol instance, ArchitectureSymbol architecture) {
Map<String, String> contentMap = cnnArchGenerator.generateStrings(architecture);
String fullName = instance.getFullName().replaceAll("\\.", "_");
//get the components execute method
// get the components execute method
String executeKey = "execute_" + fullName;
String executeMethod = contentMap.get(executeKey);
if (executeMethod == null){
if (executeMethod == null) {
throw new IllegalStateException("execute method of " + fullName + " not found");
}
contentMap.remove(executeKey);
String component = emamGen.generateString(taggingResolver, instance, (MathStatementsSymbol) null);
FileContent componentFileContent = new FileContent(
transformComponent(component, "CNNPredictor_" + fullName, executeMethod),
instance);
transformComponent(component, "CNNPredictor_" + fullName, executeMethod), instance);
for (String fileName : contentMap.keySet()){
for (String fileName : contentMap.keySet()) {
fileContents.add(new FileContent(contentMap.get(fileName), fileName));
}
fileContents.add(componentFileContent);
fileContents.add(new FileContent(readResource("CNNTranslator.h", Charsets.UTF_8), "CNNTranslator.h"));
}
protected String transformComponent(String component, String predictorClassName, String executeMethod){
protected String transformComponent(String component, String predictorClassName, String executeMethod) {
String networkVariableName = "_cnn_";
//insert includes
component = component.replaceFirst("using namespace",
"#include \"" + predictorClassName + ".h" + "\"\n" +
"#include \"CNNTranslator.h\"\n" +
"using namespace");
// insert includes
component = component.replaceFirst("using namespace", "#include \"" + predictorClassName + ".h" + "\"\n"
+ "#include \"CNNTranslator.h\"\n" + "using namespace");
//insert network attribute
// insert network attribute
component = component.replaceFirst("public:",
"public:\n" + predictorClassName + " " + networkVariableName + ";");
//insert execute method
// insert execute method
component = component.replaceFirst("void execute\\(\\)\\s\\{\\s\\}",
"void execute(){\n" + executeMethod + "\n}");
return component;
}
public void generateMathComponent(List<FileContent> fileContents, TaggingResolver taggingResolver, EMAComponentInstanceSymbol EMAComponentSymbol, MathStatementsSymbol mathStatementsSymbol){
public void generateMathComponent(List<FileContent> fileContents, TaggingResolver taggingResolver,
EMAComponentInstanceSymbol EMAComponentSymbol, MathStatementsSymbol mathStatementsSymbol) {
fileContents.add(new FileContent(
emamGen.generateString(taggingResolver, EMAComponentSymbol, mathStatementsSymbol),
EMAComponentSymbol));
emamGen.generateString(taggingResolver, EMAComponentSymbol, mathStatementsSymbol), EMAComponentSymbol));
}
public void generateSubComponents(List<FileContent> fileContents, Set<EMAComponentInstanceSymbol> allInstances, TaggingResolver taggingResolver, EMAComponentInstanceSymbol componentInstanceSymbol, Scope symtab){
fileContents.add(new FileContent(emamGen.generateString(taggingResolver, componentInstanceSymbol, (MathStatementsSymbol) null), componentInstanceSymbol));
public void generateSubComponents(List<FileContent> fileContents, Set<EMAComponentInstanceSymbol> allInstances,
TaggingResolver taggingResolver, EMAComponentInstanceSymbol componentInstanceSymbol, Scope symtab) {
fileContents.add(new FileContent(
emamGen.generateString(taggingResolver, componentInstanceSymbol, (MathStatementsSymbol) null),
componentInstanceSymbol));
String lastNameWithoutArrayPart = "";
for (EMAComponentInstanceSymbol instanceSymbol : componentInstanceSymbol.getSubComponents()) {
int arrayBracketIndex = instanceSymbol.getName().indexOf("[");
boolean generateComponentInstance = true;
if (arrayBracketIndex != -1) {
generateComponentInstance = !instanceSymbol.getName().substring(0, arrayBracketIndex).equals(lastNameWithoutArrayPart);
generateComponentInstance = !instanceSymbol.getName().substring(0, arrayBracketIndex)
.equals(lastNameWithoutArrayPart);
lastNameWithoutArrayPart = instanceSymbol.getName().substring(0, arrayBracketIndex);
Log.info(lastNameWithoutArrayPart, "Without:");
Log.info(generateComponentInstance + "", "Bool:");
......@@ -472,45 +502,45 @@ public class EMADLGenerator {
String trainConfigFilename;
String mainComponentConfigFilename = mainComponentName.replaceAll("\\.", "/");
String componentConfigFilename = componentFullName.replaceAll("\\.", "/");
String instanceConfigFilename = componentFullName.replaceAll("\\.", "/") + "_" + componentName;
if (Files.exists(Paths.get( getModelsPath() + instanceConfigFilename + ".cnnt"))) {
String instanceConfigFilename = componentFullName.replaceAll("\\.", "/") + "_" + componentName;
if (Files.exists(Paths.get(getModelsPath() + instanceConfigFilename + ".cnnt"))) {
trainConfigFilename = instanceConfigFilename;
}
else if (Files.exists(Paths.get( getModelsPath() + componentConfigFilename + ".cnnt"))){
} else if (Files.exists(Paths.get(getModelsPath() + componentConfigFilename + ".cnnt"))) {
trainConfigFilename = componentConfigFilename;
}
else if (Files.exists(Paths.get( getModelsPath() + mainComponentConfigFilename + ".cnnt"))){
} else if (Files.exists(Paths.get(getModelsPath() + mainComponentConfigFilename + ".cnnt"))) {
trainConfigFilename = mainComponentConfigFilename;
}
else{
Log.error("Missing configuration file. " +
"Could not find a file with any of the following names (only one needed): '"
+ getModelsPath() + instanceConfigFilename + ".cnnt', '"
+ getModelsPath() + componentConfigFilename + ".cnnt', '"
+ getModelsPath() + mainComponentConfigFilename + ".cnnt'." +
" These files denote respectively the configuration for the single instance, the component or the whole system.");
} else {
Log.error("Missing configuration file. "
+ "Could not find a file with any of the following names (only one needed): '" + getModelsPath()
+ instanceConfigFilename + ".cnnt', '" + getModelsPath() + componentConfigFilename + ".cnnt', '"
+ getModelsPath() + mainComponentConfigFilename + ".cnnt'."
+ " These files denote respectively the configuration for the single instance, the component or the whole system.");
return null;
}
return trainConfigFilename;
}
public List<FileContent> generateCNNTrainer(Set<EMAComponentInstanceSymbol> allInstances, String mainComponentName) {
public List<FileContent> generateCNNTrainer(Set<EMAComponentInstanceSymbol> allInstances,
String mainComponentName) {
List<FileContent> fileContents = new ArrayList<>();
for (EMAComponentInstanceSymbol componentInstance : allInstances) {
EMAComponentSymbol component = componentInstance.getComponentType().getReferencedSymbol();
Optional<ArchitectureSymbol> architecture = component.getSpannedScope().resolve("", ArchitectureSymbol.KIND);
Optional<ArchitectureSymbol> architecture = component.getSpannedScope().resolve("",
ArchitectureSymbol.KIND);
if (architecture.isPresent()) {
String trainConfigFilename = getConfigFilename(mainComponentName, component.getFullName(), component.getName());
String trainConfigFilename = getConfigFilename(mainComponentName, component.getFullName(),
component.getName());
//should be removed when CNNTrain supports packages
// should be removed when CNNTrain supports packages
List<String> names = Splitter.on("/").splitToList(trainConfigFilename);
trainConfigFilename = names.get(names.size()-1);
Path modelPath = Paths.get(getModelsPath() + Joiner.on("/").join(names.subList(0,names.size()-1)));
ConfigurationSymbol configuration = cnnTrainGenerator.getConfigurationSymbol(modelPath, trainConfigFilename);
trainConfigFilename = names.get(names.size() - 1);
Path modelPath = Paths.get(getModelsPath() + Joiner.on("/").join(names.subList(0, names.size() - 1)));
ConfigurationSymbol configuration = cnnTrainGenerator.getConfigurationSymbol(modelPath,
trainConfigFilename);
cnnTrainGenerator.setInstanceName(componentInstance.getFullName().replaceAll("\\.", "_"));
Map<String, String> fileContentMap = cnnTrainGenerator.generateStrings(configuration);
for (String fileName : fileContentMap.keySet()){