Search in sources :

Example 1 with Statement

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;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ImportStatement(org.apache.sysml.parser.ImportStatement) Statement(org.apache.sysml.parser.Statement) DMLProgram(org.apache.sysml.parser.DMLProgram) ImportStatement(org.apache.sysml.parser.ImportStatement) Map(java.util.Map) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) StatementContext(org.apache.sysml.parser.pydml.PydmlParser.StatementContext)

Example 2 with Statement

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;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ImportStatement(org.apache.sysml.parser.ImportStatement) Statement(org.apache.sysml.parser.Statement) DMLProgram(org.apache.sysml.parser.DMLProgram) ImportStatement(org.apache.sysml.parser.ImportStatement) Map(java.util.Map) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) StatementContext(org.apache.sysml.parser.pydml.PydmlParser.StatementContext)

Example 3 with Statement

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());
    }
}
Also used : FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) FunctionStatement(org.apache.sysml.parser.FunctionStatement) Statement(org.apache.sysml.parser.Statement) ArrayList(java.util.ArrayList) CannotCompileException(javassist.CannotCompileException) LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatement(org.apache.sysml.parser.FunctionStatement) DMLProgram(org.apache.sysml.parser.DMLProgram) CtMethod(javassist.CtMethod)

Example 4 with Statement

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());
    }
}
Also used : FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) FunctionStatement(org.apache.sysml.parser.FunctionStatement) Statement(org.apache.sysml.parser.Statement) ArrayList(java.util.ArrayList) CannotCompileException(javassist.CannotCompileException) LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatement(org.apache.sysml.parser.FunctionStatement) DMLProgram(org.apache.sysml.parser.DMLProgram) CtMethod(javassist.CtMethod)

Aggregations

DMLProgram (org.apache.sysml.parser.DMLProgram)4 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)4 LanguageException (org.apache.sysml.parser.LanguageException)4 Statement (org.apache.sysml.parser.Statement)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CannotCompileException (javassist.CannotCompileException)2 CtMethod (javassist.CtMethod)2 FunctionStatement (org.apache.sysml.parser.FunctionStatement)2 ImportStatement (org.apache.sysml.parser.ImportStatement)2 FunctionStatementContext (org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext)2 StatementContext (org.apache.sysml.parser.pydml.PydmlParser.StatementContext)2