Search in sources :

Example 21 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class DMLScript method createArgumentsMap.

///////////////////////////////
// private internal utils (argument parsing)
////////
@Deprecated()
protected static /**
	 * Creates an argument map appropriate for consumption by the backend
	 * The only method using this is the legacy {@link MLContext} api.
	 * Once that is removed, this function should be removed as well.
	 * This method uses a fragile position based argument for -args & -nvargs
	 * @param hasNamedArgs true for named arguments, false for positional arguments
	 * @param args in "k=v" format for named arguments and "v" for positional arguments
	 * @return	a map containing either ($K,V) or ($1,V) for named and positional arguments respectively
	 * @throws LanguageException when a named argument is an invalid identifier for DML/PyDML
	 */
Map<String, String> createArgumentsMap(boolean hasNamedArgs, String[] args) throws LanguageException {
    Map<String, String> argMap = new HashMap<String, String>();
    if (args == null)
        return argMap;
    for (int i = 1; i <= args.length; i++) {
        String arg = args[i - 1];
        if (arg.equalsIgnoreCase("-l") || arg.equalsIgnoreCase("-log") || arg.equalsIgnoreCase("-v") || arg.equalsIgnoreCase("-visualize") || arg.equalsIgnoreCase("-explain") || arg.equalsIgnoreCase("-debug") || arg.equalsIgnoreCase("-stats") || arg.equalsIgnoreCase("-exec") || arg.equalsIgnoreCase("-debug") || arg.startsWith("-config")) {
            throw new LanguageException("-args or -nvargs must be the final argument for DMLScript!");
        }
        //parse arguments (named args / args by position)
        if (hasNamedArgs) {
            // CASE: named argument argName=argValue -- must add <argName, argValue> pair to _argVals
            String[] argPieces = arg.split("=");
            if (argPieces.length < 2)
                throw new LanguageException("for -nvargs option, elements in arg list must be named and have form argName=argValue");
            String argName = argPieces[0];
            StringBuilder sb = new StringBuilder();
            for (int jj = 1; jj < argPieces.length; jj++) {
                sb.append(argPieces[jj]);
            }
            String varNameRegex = "^[a-zA-Z]([a-zA-Z0-9_])*$";
            if (!argName.matches(varNameRegex))
                throw new LanguageException("argName " + argName + " must be a valid variable name in DML. Valid variable names in DML start with upper-case or lower-case letter, and contain only letters, digits, or underscores");
            argMap.put("$" + argName, sb.toString());
        } else {
            // CASE: unnamed argument -- use position in arg list for name
            argMap.put("$" + i, arg);
        }
    }
    return argMap;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) HashMap(java.util.HashMap)

Example 22 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class IPAPassApplyStaticHopRewrites method rewriteProgram.

@Override
public void rewriteProgram(DMLProgram prog, FunctionCallGraph fgraph, FunctionCallSizeInfo fcallSizes) {
    try {
        // construct rewriter w/o checkpoint injection to avoid redundancy
        ProgramRewriter rewriter = new ProgramRewriter(true, false);
        rewriter.removeStatementBlockRewrite(RewriteInjectSparkLoopCheckpointing.class);
        // rewrite program hop dags and statement blocks
        // rewrite and split
        rewriter.rewriteProgramHopDAGs(prog, true);
    } catch (LanguageException ex) {
        throw new HopsException(ex);
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) HopsException(org.apache.sysml.hops.HopsException)

Example 23 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class IPAPassRemoveUnusedFunctions method rewriteProgram.

@Override
public void rewriteProgram(DMLProgram prog, FunctionCallGraph fgraph, FunctionCallSizeInfo fcallSizes) {
    try {
        Set<String> fnamespaces = prog.getNamespaces().keySet();
        for (String fnspace : fnamespaces) {
            HashMap<String, FunctionStatementBlock> fsbs = prog.getFunctionStatementBlocks(fnspace);
            Iterator<Entry<String, FunctionStatementBlock>> iter = fsbs.entrySet().iterator();
            while (iter.hasNext()) {
                Entry<String, FunctionStatementBlock> e = iter.next();
                if (!fgraph.isReachableFunction(fnspace, e.getKey())) {
                    iter.remove();
                    if (LOG.isDebugEnabled())
                        LOG.debug("IPA: Removed unused function: " + DMLProgram.constructFunctionKey(fnspace, e.getKey()));
                }
            }
        }
    } catch (LanguageException ex) {
        throw new HopsException(ex);
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) Entry(java.util.Map.Entry) HopsException(org.apache.sysml.hops.HopsException)

Example 24 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class DMLScript method readDMLScript.

/**
 * Reads the DML/PyDML script into a String
 * @param isFile	Whether the string argument is a path to a file or the script itself
 * @param scriptOrFilename script or filename
 * @return a string representation of the script
 * @throws IOException	if error
 */
protected static String readDMLScript(boolean isFile, String scriptOrFilename) throws IOException {
    String dmlScriptStr;
    if (isFile) {
        String fileName = scriptOrFilename;
        // read DML script from file
        if (fileName == null)
            throw new LanguageException("DML script path was not specified!");
        StringBuilder sb = new StringBuilder();
        BufferedReader in = null;
        try {
            // read from hdfs or gpfs file system
            if (fileName.startsWith("hdfs:") || fileName.startsWith("gpfs:") || IOUtilFunctions.isObjectStoreFileScheme(new Path(fileName))) {
                Path scriptPath = new Path(fileName);
                FileSystem fs = IOUtilFunctions.getFileSystem(scriptPath);
                in = new BufferedReader(new InputStreamReader(fs.open(scriptPath)));
            } else // from local file system
            {
                in = new BufferedReader(new FileReader(fileName));
            }
            // core script reading
            String tmp = null;
            while ((tmp = in.readLine()) != null) {
                sb.append(tmp);
                sb.append("\n");
            }
        } catch (IOException ex) {
            LOG.error("Failed to read the script from the file system", ex);
            throw ex;
        } finally {
            IOUtilFunctions.closeSilently(in);
        }
        dmlScriptStr = sb.toString();
    } else {
        String scriptString = scriptOrFilename;
        // parse given script string
        if (scriptString == null)
            throw new LanguageException("DML script was not specified!");
        InputStream is = new ByteArrayInputStream(scriptString.getBytes());
        Scanner scan = new Scanner(is);
        dmlScriptStr = scan.useDelimiter("\\A").next();
        scan.close();
    }
    return dmlScriptStr;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) Path(org.apache.hadoop.fs.Path) Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileSystem(org.apache.hadoop.fs.FileSystem) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 25 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class DataTypeChangeTest method runValidateTest.

private void runValidateTest(String fullTestName, boolean expectedException) {
    boolean raisedException = false;
    try {
        // Tell the superclass about the name of this test, so that the superclass can
        // create temporary directories.
        TestConfiguration testConfig = new TestConfiguration(TEST_CLASS_DIR, fullTestName, new String[] {});
        addTestConfiguration(fullTestName, testConfig);
        loadTestConfiguration(testConfig);
        DMLConfig conf = new DMLConfig(getCurConfigFile().getPath());
        ConfigurationManager.setLocalConfig(conf);
        String dmlScriptString = "";
        HashMap<String, String> argVals = new HashMap<String, String>();
        // read script
        try (BufferedReader in = new BufferedReader(new FileReader(fullTestName))) {
            String s1 = null;
            while ((s1 = in.readLine()) != null) dmlScriptString += s1 + "\n";
        }
        // parsing and dependency analysis
        ParserWrapper parser = ParserFactory.createParser(org.apache.sysml.api.mlcontext.ScriptType.DML);
        DMLProgram prog = parser.parse(DMLScript.DML_FILE_PATH_ANTLR_PARSER, dmlScriptString, argVals);
        DMLTranslator dmlt = new DMLTranslator(prog);
        dmlt.liveVariableAnalysis(prog);
        dmlt.validateParseTree(prog);
    } catch (LanguageException ex) {
        raisedException = true;
        if (raisedException != expectedException)
            ex.printStackTrace();
    } catch (Exception ex2) {
        ex2.printStackTrace();
        throw new RuntimeException(ex2);
    // Assert.fail( "Unexpected exception occured during test run." );
    }
    // check correctness
    Assert.assertEquals(expectedException, raisedException);
}
Also used : DMLConfig(org.apache.sysml.conf.DMLConfig) HashMap(java.util.HashMap) TestConfiguration(org.apache.sysml.test.integration.TestConfiguration) DMLTranslator(org.apache.sysml.parser.DMLTranslator) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) LanguageException(org.apache.sysml.parser.LanguageException) BufferedReader(java.io.BufferedReader) DMLProgram(org.apache.sysml.parser.DMLProgram) FileReader(java.io.FileReader) ParserWrapper(org.apache.sysml.parser.ParserWrapper)

Aggregations

LanguageException (org.apache.sysml.parser.LanguageException)38 DMLProgram (org.apache.sysml.parser.DMLProgram)16 AssignmentStatement (org.apache.sysml.parser.AssignmentStatement)9 BufferedReader (java.io.BufferedReader)8 FileReader (java.io.FileReader)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 DMLTranslator (org.apache.sysml.parser.DMLTranslator)8 Expression (org.apache.sysml.parser.Expression)8 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)8 ParameterExpression (org.apache.sysml.parser.ParameterExpression)8 HashMap (java.util.HashMap)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DMLConfig (org.apache.sysml.conf.DMLConfig)6 HopsException (org.apache.sysml.hops.HopsException)6 ProgramRewriter (org.apache.sysml.hops.rewrite.ProgramRewriter)6 BinaryExpression (org.apache.sysml.parser.BinaryExpression)6 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)6 DataExpression (org.apache.sysml.parser.DataExpression)6 DataIdentifier (org.apache.sysml.parser.DataIdentifier)6