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;
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations