Commit 498f60db authored by vonWenckstern's avatar vonWenckstern

added new flag and new functions to compile Java source code directly to JavaScript

parent cc594a56
Pipeline #92133 failed with stage
in 2 minutes and 51 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/>.
* *******************************************************************************
*/
import java.io.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
// this file is needed for the JavaScript version of OCLFiddle
// see https://github.com/leaningtech/cheerpj-meta/issues/53#issuecomment-448426101
// and https://github.com/EmbeddedMontiArc/webspace/blob/297c0d533c637ad45887bda3960e17597f6a674a/OCL/resources/js/cheerpj.js#L26
public class ReadFileContent {
public static String read(String path) throws IOException {
File file = new File(path);
int len = (int)file.length();
InputStreamReader fr = new InputStreamReader(new FileInputStream(path),"UTF-8");
char[] buffer = new char[len];
fr.read(buffer);
fr.close();
return new String(buffer);
}
public static String getAllFiles(String path) throws IOException {
ArrayList<String> files = new ArrayList<>();
File file = new File(path);
addFile(file, files);
return files.stream().collect(Collectors.joining("\n"));
}
private static void addFile(File directory, ArrayList<String> files) {
String[] elements = directory.list();
for (String el : elements) {
File f = new File(directory.getPath() + "/" + el);
if (f.isFile())
files.add(f.getPath());
else if (f.isDirectory())
addFile(f, files);
}
}
}
/**
* ******************************************************************************
* 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/>.
* *******************************************************************************
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
// this file is needed for the JavaScript version of OCLFiddle
// see https://github.com/leaningtech/cheerpj-meta/issues/53#issuecomment-448426101
// and https://github.com/EmbeddedMontiArc/webspace/blob/297c0d533c637ad45887bda3960e17597f6a674a/OCL/resources/js/cheerpj.js#L26
public class WriteFileContent {
/**
* This class shows how to write file in java
* @param args
* @throws IOException
*/
public static void main(String[] args) {
String path = args[0];
String[] paths = path.split("/");
String s = "";
for (int i = 0; i < paths.length - 1; i++) {
String p = paths[i];
s += p + "/";
File f = new File(s.substring(0, s.length()-1));
if (!f.exists()) {
if(!f.mkdir()) {
System.out.println("could not create directory " + p);
break;
}
}
}
String data = args[1];
writeUsingFileWriter(path, data);
}
/**
* Use WriteFileContent when number of write operations are less
* @param data
*/
private static void writeUsingFileWriter(String path, String data) {
File file = new File(path);
java.io.OutputStreamWriter fr = null;
try {
fr = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
fr.write(data);
} catch (IOException e) {
e.printStackTrace();
}finally{
//close resources
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
......@@ -24,6 +24,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import de.monticore.ModelingLanguageFamily;
import de.monticore.io.paths.ModelPath;
......@@ -31,6 +32,7 @@ import de.monticore.prettyprint.IndentPrinter;
import de.monticore.symboltable.GlobalScope;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.ResolvingConfiguration;
import de.monticore.types.types._ast.ASTImportStatement;
import de.monticore.umlcd4a.CD4AnalysisLanguage;
import de.monticore.umlcd4a.cd4analysis._ast.ASTCDCompilationUnit;
import de.monticore.umlcd4a.cd4analysis._parser.CD4AnalysisParser;
......@@ -62,6 +64,8 @@ public class OCLCDTool {
Options options = new Options();
Option preload = new Option("preloadCD", "preloadCD", false, "preload classdiagrams in browser");
options.addOption(preload);
Option path = new Option("path", "project-path", true, "absolute path to project, " +
"required when ocl given as qualified name");
options.addOption(path);
......@@ -110,6 +114,7 @@ public class OCLCDTool {
String cdPath = cmd.getOptionValue("printTgt");
Boolean verbose = cmd.hasOption("verbose");
Boolean parse = cmd.hasOption("parseOnly");
Boolean preloadCD = cmd.hasOption("preloadCD");
// Disable verbose logging
if (!verbose) {
......@@ -118,7 +123,7 @@ public class OCLCDTool {
if (cmd.hasOption("path") && cmd.hasOption("ocl") && isQualifiedName(oclModel)) {
if(!parse) {
loadOclModel(parentDir, oclModel);
loadOclModel(parentDir, oclModel,preloadCD);
} else {
parseOcl(parentDir, oclModel);
}
......@@ -256,7 +261,7 @@ public class OCLCDTool {
}
protected static ASTCompilationUnit loadOclModel(String parentDirectory, String modelFullQualifiedFilename) {
protected static ASTCompilationUnit loadOclModel(String parentDirectory, String modelFullQualifiedFilename, boolean preloadCD) {
final OCLLanguage ocllang = new OCLLanguage();
final CD4AnalysisLanguage cd4AnalysisLang = new CD4AnalysisLanguage();
......@@ -272,13 +277,41 @@ public class OCLCDTool {
resolvingConfiguration.addDefaultFilters(ocllang.getResolvingFilters());
resolvingConfiguration.addDefaultFilters(cd4AnalysisLang.getResolvingFilters());
OCLSymbolTableCreator oclSymbolTableCreator = ocllang.getSymbolTableCreator(resolvingConfiguration, globalScope).get();
Optional<ASTCompilationUnit> astOCLCompilationUnit = ocllang.getModelLoader().loadModel(modelFullQualifiedFilename, modelPath);
CD4AnalysisSymbolTableCreator cdSymbolTableCreator = cd4AnalysisLang.getSymbolTableCreator(resolvingConfiguration, globalScope).get();
String path = parentDirectory + "/" + modelFullQualifiedFilename.replace(".", "/") + ".ocl";
if (path.startsWith("/"))
path = path.substring(1);
//System.out.println("path: " + path);
OCLParser parser = new OCLParser();
Optional<ASTCompilationUnit> astOCLCompilationUnit = //ocllang.getModelLoader().loadModel(modelFullQualifiedFilename, modelPath);
parser.parse(path);
//System.out.println("astOCLCompilationUnit.isPresent(): " + astOCLCompilationUnit.isPresent());
if (astOCLCompilationUnit.isPresent()) {
if (preloadCD) {
for (ASTImportStatement importS : astOCLCompilationUnit.get().getImportStatementList()) {
// need to reload all cd4 files and add symbols, b/c the model loader does not work in JS file system (anything is wrong with path calculation)
// ignore here .* imports -> this is only for the fiddle
String cdPath = parentDirectory + "/" + importS.getImportList().stream().collect(Collectors.joining("/")) + ".cd";
if (cdPath.startsWith("/"))
cdPath = cdPath.substring(1);
//System.out.println("cdPath: " + cdPath);
CD4AnalysisParser cdParser = new CD4AnalysisParser();
Optional<ASTCDCompilationUnit> astcdCompilationUnit = cdParser.parse(cdPath);
//System.out.println("astcdCompilationUnit.isPresent(): " + astcdCompilationUnit.isPresent());
if (astcdCompilationUnit.isPresent()) {
astcdCompilationUnit.get().accept(cdSymbolTableCreator);
}
}
}
astOCLCompilationUnit.get().accept(oclSymbolTableCreator);
OCLCoCoChecker checker = OCLCoCos.createChecker();
checker.checkAll(astOCLCompilationUnit.get());
return astOCLCompilationUnit.get();
} else {
Log.error("Could not create AST (probably model could not be loaded).");
}
} catch (Exception e) {
e.printStackTrace();
......
......@@ -162,6 +162,17 @@ public class OCLCDToolTest {
Assert.assertEquals(0, Log.getErrorCount());
}
@Ignore("This test fails here, but it works in the browser -> the browser cannot load models via model loader so the CD models must be pre-loaded")
@Test
public void cheerpJTest() {
String[] args = new String[] {"-path", "", "-ocl", "example.ocl.Demo", "-preloadCD"};
try {
OCLCDTool.main(args);
} catch (Exception e) {
Log.error(e.getMessage());
}
}
@Test
public void oclMvWPhdTest() {
......
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