use of org.apache.sysml.parser.ImportStatement in project incubator-systemml by apache.
the class PyDMLParserWrapper method createDMLProgram.
private 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));
}
dmlPgm.mergeStatementBlocks();
return dmlPgm;
}
use of org.apache.sysml.parser.ImportStatement in project incubator-systemml by apache.
the class DMLParserWrapper method createDMLProgram.
private 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) {
org.apache.sysml.parser.Statement current = stmtCtx.info.stmt;
if (current == null) {
LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
return null;
}
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));
}
dmlPgm.mergeStatementBlocks();
return dmlPgm;
}
use of org.apache.sysml.parser.ImportStatement in project incubator-systemml by apache.
the class DmlSyntacticValidator method exitImportStatement.
// -----------------------------------------------------------------
// "src" statment
// -----------------------------------------------------------------
@Override
public void exitImportStatement(ImportStatementContext ctx) {
//prepare import filepath
String filePath = ctx.filePath.getText();
String namespace = DMLProgram.DEFAULT_NAMESPACE;
if (ctx.namespace != null && ctx.namespace.getText() != null && !ctx.namespace.getText().isEmpty()) {
namespace = ctx.namespace.getText();
}
if ((filePath.startsWith("\"") && filePath.endsWith("\"")) || filePath.startsWith("'") && filePath.endsWith("'")) {
filePath = filePath.substring(1, filePath.length() - 1);
}
File file = new File(filePath);
if (!file.isAbsolute()) {
//concatenate working directory to filepath
filePath = _workingDir + File.separator + filePath;
}
validateNamespace(namespace, filePath, ctx);
String scriptID = DMLProgram.constructFunctionKey(namespace, filePath);
DMLProgram prog = null;
if (!_scripts.get().containsKey(scriptID)) {
_scripts.get().put(scriptID, namespace);
try {
prog = (new DMLParserWrapper()).doParse(filePath, null, getQualifiedNamespace(namespace), argVals);
} catch (ParseException e) {
notifyErrorListeners(e.getMessage(), ctx.start);
return;
}
// Custom logic whether to proceed ahead or not. Better than the current exception handling mechanism
if (prog == null) {
notifyErrorListeners("One or more errors found during importing a program from file " + filePath, ctx.start);
return;
} else {
ctx.info.namespaces = new HashMap<String, DMLProgram>();
ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
ctx.info.stmt = new ImportStatement();
((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
((ImportStatement) ctx.info.stmt).setNamespace(namespace);
}
} else {
// Skip redundant parsing (to prevent potential infinite recursion) and
// create empty program for this context to allow processing to continue.
prog = new DMLProgram();
ctx.info.namespaces = new HashMap<String, DMLProgram>();
ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
ctx.info.stmt = new ImportStatement();
((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
((ImportStatement) ctx.info.stmt).setNamespace(namespace);
}
}
use of org.apache.sysml.parser.ImportStatement in project incubator-systemml by apache.
the class PydmlSyntacticValidator method exitImportStatement.
// -----------------------------------------------------------------
// "src" statment
// -----------------------------------------------------------------
@Override
public void exitImportStatement(ImportStatementContext ctx) {
//prepare import filepath
String filePath = ctx.filePath.getText();
String namespace = DMLProgram.DEFAULT_NAMESPACE;
if (ctx.namespace != null && ctx.namespace.getText() != null && !ctx.namespace.getText().isEmpty()) {
namespace = ctx.namespace.getText();
}
if ((filePath.startsWith("\"") && filePath.endsWith("\"")) || filePath.startsWith("'") && filePath.endsWith("'")) {
filePath = filePath.substring(1, filePath.length() - 1);
}
File file = new File(filePath);
if (!file.isAbsolute()) {
//concatenate working directory to filepath
filePath = _workingDir + File.separator + filePath;
}
validateNamespace(namespace, filePath, ctx);
String scriptID = DMLProgram.constructFunctionKey(namespace, filePath);
DMLProgram prog = null;
if (!_scripts.get().containsKey(scriptID)) {
_scripts.get().put(scriptID, namespace);
try {
prog = (new PyDMLParserWrapper()).doParse(filePath, null, getQualifiedNamespace(namespace), argVals);
} catch (ParseException e) {
notifyErrorListeners(e.getMessage(), ctx.start);
return;
}
// Custom logic whether to proceed ahead or not. Better than the current exception handling mechanism
if (prog == null) {
notifyErrorListeners("One or more errors found during importing a program from file " + filePath, ctx.start);
return;
} else {
ctx.info.namespaces = new HashMap<String, DMLProgram>();
ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
ctx.info.stmt = new ImportStatement();
((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
((ImportStatement) ctx.info.stmt).setNamespace(namespace);
}
} else {
// Skip redundant parsing (to prevent potential infinite recursion) and
// create empty program for this context to allow processing to continue.
prog = new DMLProgram();
ctx.info.namespaces = new HashMap<String, DMLProgram>();
ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
ctx.info.stmt = new ImportStatement();
((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
((ImportStatement) ctx.info.stmt).setNamespace(namespace);
}
}
Aggregations