use of org.apache.sysml.parser.Statement in project incubator-systemml by apache.
the class PyDMLParserWrapper method createDMLProgram.
private static DMLProgram createDMLProgram(ProgramrootContext ast, String sourceNamespace) {
DMLProgram dmlPgm = new DMLProgram();
String namespace = (sourceNamespace != null && sourceNamespace.length() > 0) ? sourceNamespace : DMLProgram.DEFAULT_NAMESPACE;
dmlPgm.getNamespaces().put(namespace, dmlPgm);
// First add all the functions
for (FunctionStatementContext fn : ast.functionBlocks) {
FunctionStatementBlock functionStmtBlk = new FunctionStatementBlock();
functionStmtBlk.addStatement(fn.info.stmt);
try {
dmlPgm.addFunctionStatementBlock(namespace, fn.info.functionName, functionStmtBlk);
} catch (LanguageException e) {
LOG.error("line: " + fn.start.getLine() + ":" + fn.start.getCharPositionInLine() + " cannot process the function " + fn.info.functionName);
return null;
}
}
// Then add all the statements
for (StatementContext stmtCtx : ast.blocks) {
Statement current = stmtCtx.info.stmt;
if (current == null) {
LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
return null;
}
// Ignore Newline logic
if (current.isEmptyNewLineStatement()) {
continue;
}
if (current instanceof ImportStatement) {
// Handle import statements separately
if (stmtCtx.info.namespaces != null) {
// Add the DMLProgram entries into current program
for (Map.Entry<String, DMLProgram> entry : stmtCtx.info.namespaces.entrySet()) {
// TODO handle namespace key already exists for different program value instead of overwriting
DMLProgram prog = entry.getValue();
if (prog != null && prog.getNamespaces().size() > 0) {
dmlPgm.getNamespaces().put(entry.getKey(), prog);
}
// Add dependent programs (handle imported script that also imports scripts)
for (Map.Entry<String, DMLProgram> dependency : entry.getValue().getNamespaces().entrySet()) {
String depNamespace = dependency.getKey();
DMLProgram depProgram = dependency.getValue();
if (dmlPgm.getNamespaces().get(depNamespace) == null) {
dmlPgm.getNamespaces().put(depNamespace, depProgram);
}
}
}
} else {
LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the import statement");
return null;
}
}
// Now wrap statement into individual statement block
// merge statement will take care of merging these blocks
dmlPgm.addStatementBlock(getStatementBlock(current));
}
// post-processing
dmlPgm.hoistFunctionCallsFromExpressions();
dmlPgm.mergeStatementBlocks();
return dmlPgm;
}
use of org.apache.sysml.parser.Statement in project systemml by apache.
the class PyDMLParserWrapper method createDMLProgram.
private static DMLProgram createDMLProgram(ProgramrootContext ast, String sourceNamespace) {
DMLProgram dmlPgm = new DMLProgram();
String namespace = (sourceNamespace != null && sourceNamespace.length() > 0) ? sourceNamespace : DMLProgram.DEFAULT_NAMESPACE;
dmlPgm.getNamespaces().put(namespace, dmlPgm);
// First add all the functions
for (FunctionStatementContext fn : ast.functionBlocks) {
FunctionStatementBlock functionStmtBlk = new FunctionStatementBlock();
functionStmtBlk.addStatement(fn.info.stmt);
try {
dmlPgm.addFunctionStatementBlock(namespace, fn.info.functionName, functionStmtBlk);
} catch (LanguageException e) {
LOG.error("line: " + fn.start.getLine() + ":" + fn.start.getCharPositionInLine() + " cannot process the function " + fn.info.functionName);
return null;
}
}
// Then add all the statements
for (StatementContext stmtCtx : ast.blocks) {
Statement current = stmtCtx.info.stmt;
if (current == null) {
LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
return null;
}
// Ignore Newline logic
if (current.isEmptyNewLineStatement()) {
continue;
}
if (current instanceof ImportStatement) {
// Handle import statements separately
if (stmtCtx.info.namespaces != null) {
// Add the DMLProgram entries into current program
for (Map.Entry<String, DMLProgram> entry : stmtCtx.info.namespaces.entrySet()) {
// TODO handle namespace key already exists for different program value instead of overwriting
DMLProgram prog = entry.getValue();
if (prog != null && prog.getNamespaces().size() > 0) {
dmlPgm.getNamespaces().put(entry.getKey(), prog);
}
// Add dependent programs (handle imported script that also imports scripts)
for (Map.Entry<String, DMLProgram> dependency : entry.getValue().getNamespaces().entrySet()) {
String depNamespace = dependency.getKey();
DMLProgram depProgram = dependency.getValue();
if (dmlPgm.getNamespaces().get(depNamespace) == null) {
dmlPgm.getNamespaces().put(depNamespace, depProgram);
}
}
}
} else {
LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the import statement");
return null;
}
}
// Now wrap statement into individual statement block
// merge statement will take care of merging these blocks
dmlPgm.addStatementBlock(getStatementBlock(current));
}
// post-processing
dmlPgm.hoistFunctionCallsFromExpressions();
dmlPgm.mergeStatementBlocks();
return dmlPgm;
}
use of org.apache.sysml.parser.Statement in project systemml by apache.
the class GenerateClassesForMLContext method addFunctionMethods.
/**
* Add methods to a derived script class to allow invocation of script
* functions.
*
* @param scriptFilePath
* the path to a script file
* @param ctNewScript
* the javassist compile-time class representation of a script
*/
public static void addFunctionMethods(String scriptFilePath, CtClass ctNewScript) {
try {
DMLProgram dmlProgram = dmlProgramFromScriptFilePath(scriptFilePath);
if (dmlProgram == null) {
System.out.println("Could not generate DML Program for: " + scriptFilePath);
return;
}
Map<String, FunctionStatementBlock> defaultNsFsbsMap = dmlProgram.getFunctionStatementBlocks(DMLProgram.DEFAULT_NAMESPACE);
List<FunctionStatementBlock> fsbs = new ArrayList<>();
fsbs.addAll(defaultNsFsbsMap.values());
for (FunctionStatementBlock fsb : fsbs) {
ArrayList<Statement> sts = fsb.getStatements();
for (Statement st : sts) {
if (!(st instanceof FunctionStatement)) {
continue;
}
FunctionStatement fs = (FunctionStatement) st;
String dmlFunctionCall = generateDmlFunctionCall(scriptFilePath, fs);
String functionCallMethod = generateFunctionCallMethod(scriptFilePath, fs, dmlFunctionCall);
CtMethod m = CtNewMethod.make(functionCallMethod, ctNewScript);
ctNewScript.addMethod(m);
addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, false);
addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, true);
}
}
} catch (LanguageException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
} catch (CannotCompileException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
} catch (RuntimeException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
}
}
use of org.apache.sysml.parser.Statement in project incubator-systemml by apache.
the class GenerateClassesForMLContext method addFunctionMethods.
/**
* Add methods to a derived script class to allow invocation of script
* functions.
*
* @param scriptFilePath
* the path to a script file
* @param ctNewScript
* the javassist compile-time class representation of a script
*/
public static void addFunctionMethods(String scriptFilePath, CtClass ctNewScript) {
try {
DMLProgram dmlProgram = dmlProgramFromScriptFilePath(scriptFilePath);
if (dmlProgram == null) {
System.out.println("Could not generate DML Program for: " + scriptFilePath);
return;
}
Map<String, FunctionStatementBlock> defaultNsFsbsMap = dmlProgram.getFunctionStatementBlocks(DMLProgram.DEFAULT_NAMESPACE);
List<FunctionStatementBlock> fsbs = new ArrayList<>();
fsbs.addAll(defaultNsFsbsMap.values());
for (FunctionStatementBlock fsb : fsbs) {
ArrayList<Statement> sts = fsb.getStatements();
for (Statement st : sts) {
if (!(st instanceof FunctionStatement)) {
continue;
}
FunctionStatement fs = (FunctionStatement) st;
String dmlFunctionCall = generateDmlFunctionCall(scriptFilePath, fs);
String functionCallMethod = generateFunctionCallMethod(scriptFilePath, fs, dmlFunctionCall);
CtMethod m = CtNewMethod.make(functionCallMethod, ctNewScript);
ctNewScript.addMethod(m);
addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, false);
addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, true);
}
}
} catch (LanguageException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
} catch (CannotCompileException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
} catch (RuntimeException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
}
}
Aggregations