Repository: systemml
Updated Branches:
  refs/heads/master 607a402bc -> 4a822a22c


[SYSTEMML-2078] Add support for builtin constants (PI, INF, NaN)

Closes #752.


Project: http://git-wip-us.apache.org/repos/asf/systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/4a822a22
Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/4a822a22
Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/4a822a22

Branch: refs/heads/master
Commit: 4a822a22c88b579e935a1b09051c1fe236e18a4b
Parents: 607a402
Author: EdgarLGB <[email protected]>
Authored: Sun Apr 1 17:14:36 2018 -0700
Committer: Matthias Boehm <[email protected]>
Committed: Sun Apr 1 17:14:36 2018 -0700

----------------------------------------------------------------------
 .../apache/sysml/parser/BuiltinConstant.java    | 45 ++++++++++++++++++++
 .../org/apache/sysml/parser/StatementBlock.java | 13 ++++++
 .../parser/common/CommonSyntacticValidator.java | 10 ++++-
 .../integration/mlcontext/MLContextTest.java    |  8 ++++
 .../api/mlcontext/builtin-constants-test.dml    | 39 +++++++++++++++++
 5 files changed, 114 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/4a822a22/src/main/java/org/apache/sysml/parser/BuiltinConstant.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/BuiltinConstant.java 
b/src/main/java/org/apache/sysml/parser/BuiltinConstant.java
new file mode 100644
index 0000000..5325e80
--- /dev/null
+++ b/src/main/java/org/apache/sysml/parser/BuiltinConstant.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sysml.parser;
+
+import org.apache.commons.lang3.EnumUtils;
+
+/**
+ * These are the builtin constants
+ */
+public enum BuiltinConstant {
+       PI(Math.PI),
+       INF(Double.POSITIVE_INFINITY),
+       NaN(Double.NaN);
+
+       private DoubleIdentifier _id;
+
+       private BuiltinConstant(double d) {
+               this._id = new DoubleIdentifier(d);
+       }
+
+       public DoubleIdentifier get() {
+               return this._id;
+       }
+
+       public static boolean contains(String name) {
+               return EnumUtils.isValidEnum(BuiltinConstant.class, name);
+       }
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/4a822a22/src/main/java/org/apache/sysml/parser/StatementBlock.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/StatementBlock.java 
b/src/main/java/org/apache/sysml/parser/StatementBlock.java
index 1f0e81c..2957482 100644
--- a/src/main/java/org/apache/sysml/parser/StatementBlock.java
+++ b/src/main/java/org/apache/sysml/parser/StatementBlock.java
@@ -784,6 +784,12 @@ public class StatementBlock extends LiveVariableAnalysis 
implements ParseInfo
                DataIdentifier target = as.getTarget();
                Expression source = as.getSource();
                
+               // check if target is builtin constant
+               if (target != null && 
BuiltinConstant.contains(target.getName())) {
+                       target.raiseValidateError(String.format(
+                               "Cannot assign a value to the builtin constant 
%s.", target.getName()), false);
+               }
+               
                if (source instanceof FunctionCallIdentifier) {
                        ((FunctionCallIdentifier) source).validateExpression(
                                dmlProg, ids.getVariables(),currConstVars, 
conditional);
@@ -899,6 +905,13 @@ public class StatementBlock extends LiveVariableAnalysis 
implements ParseInfo
                MultiAssignmentStatement mas = (MultiAssignmentStatement) 
current;
                ArrayList<DataIdentifier> targetList = mas.getTargetList();
                Expression source = mas.getSource();
+
+               // check if target list contains builtin constant
+               targetList.forEach(target -> {
+                       if (target != null && 
BuiltinConstant.contains(target.getName()))
+                               target.raiseValidateError(String.format(
+                                       "Cannot assign a value to the builtin 
constant %s.", target.getName()), false);
+               });
                
                //MultiAssignmentStatments currently supports only External,
                //User-defined, and Multi-return Builtin function expressions

http://git-wip-us.apache.org/repos/asf/systemml/blob/4a822a22/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java 
b/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
index cc67ebe..d1b8cf6 100644
--- a/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
+++ b/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
@@ -55,6 +55,7 @@ import org.apache.sysml.parser.Statement;
 import org.apache.sysml.parser.StringIdentifier;
 import org.apache.sysml.parser.dml.DmlSyntacticValidator;
 import org.apache.sysml.parser.pydml.PydmlSyntacticValidator;
+import org.apache.sysml.parser.BuiltinConstant;
 
 /**
  * Contains fields and (helper) methods common to {@link 
DmlSyntacticValidator} and {@link PydmlSyntacticValidator}
@@ -328,6 +329,13 @@ public abstract class CommonSyntacticValidator {
        }
 
        protected void exitDataIdExpressionHelper(ParserRuleContext ctx, 
ExpressionInfo me, ExpressionInfo dataInfo) {
+               // inject builtin constant
+               if (dataInfo.expr instanceof DataIdentifier) {
+                       DataIdentifier id = ((DataIdentifier) dataInfo.expr);
+                       if (BuiltinConstant.contains(id.getName())) { 
+                               dataInfo.expr = 
BuiltinConstant.valueOf(id.getName()).get();
+                       }
+               }
                me.expr = dataInfo.expr;
                // If "The parameter $X either needs to be passed through 
commandline or initialized to default value" validation
                // error occurs, then dataInfo.expr is null which would cause a 
null pointer exception with the following code.
@@ -430,7 +438,7 @@ public abstract class CommonSyntacticValidator {
                                info.stmt = new AssignmentStatement(ctx, 
target, source, currentFile);
                        } catch (LanguageException e) {
                                // TODO: extract more meaningful info from this 
exception.
-                               notifyErrorListeners("invalid assignment", 
lhsStart);
+                               notifyErrorListeners("invalid assignment: " + 
e.getMessage(), lhsStart);
                                return;
                        }
                }

http://git-wip-us.apache.org/repos/asf/systemml/blob/4a822a22/src/test/java/org/apache/sysml/test/integration/mlcontext/MLContextTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/sysml/test/integration/mlcontext/MLContextTest.java 
b/src/test/java/org/apache/sysml/test/integration/mlcontext/MLContextTest.java
index 39b9652..a9dcb23 100644
--- 
a/src/test/java/org/apache/sysml/test/integration/mlcontext/MLContextTest.java
+++ 
b/src/test/java/org/apache/sysml/test/integration/mlcontext/MLContextTest.java
@@ -90,6 +90,14 @@ import scala.collection.Seq;
 public class MLContextTest extends MLContextTestBase {
 
        @Test
+       public void testBuiltinConstantsTest() {
+               System.out.println("MLContextTest - basic builtin constants 
test");
+               Script script = dmlFromFile(baseDirectory + File.separator + 
"builtin-constants-test.dml");
+               ml.execute(script);
+               Assert.assertTrue(Statistics.getNoOfExecutedSPInst() == 0);
+       }
+       
+       @Test
        public void testBasicExecuteEvalTest() {
                System.out.println("MLContextTest - basic eval test");
                setExpectedStdOut("10");

http://git-wip-us.apache.org/repos/asf/systemml/blob/4a822a22/src/test/scripts/org/apache/sysml/api/mlcontext/builtin-constants-test.dml
----------------------------------------------------------------------
diff --git 
a/src/test/scripts/org/apache/sysml/api/mlcontext/builtin-constants-test.dml 
b/src/test/scripts/org/apache/sysml/api/mlcontext/builtin-constants-test.dml
new file mode 100644
index 0000000..05b832e
--- /dev/null
+++ b/src/test/scripts/org/apache/sysml/api/mlcontext/builtin-constants-test.dml
@@ -0,0 +1,39 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+# a func using builtin constant PI
+f1 = function (double r) return (double res) {
+  res = PI * r * r
+}
+
+# use builtin constant PI in main func
+res = PI * 1000
+print(res)
+
+# use builtin constant NaN
+print(NaN + 1)
+
+# use builtin constant INF
+if (1 / 0 == INF) {
+  print("1 / 0 is an infinity.")
+}
+
+print(f1(100))

Reply via email to