Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system.util.evalExpression #7

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,12 +1,29 @@
package org.imdc.extensions.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.inductiveautomation.ignition.common.PyUtilities;
import com.inductiveautomation.ignition.common.TypeUtilities;
import com.inductiveautomation.ignition.common.expressions.ConstantExpression;
import com.inductiveautomation.ignition.common.expressions.Expression;
import com.inductiveautomation.ignition.common.expressions.ExpressionParseContext;
import com.inductiveautomation.ignition.common.expressions.FunctionFactory;
import com.inductiveautomation.ignition.common.expressions.parsing.ELParserHarness;
import com.inductiveautomation.ignition.common.model.CommonContext;
import com.inductiveautomation.ignition.common.model.values.QualifiedValue;
import com.inductiveautomation.ignition.common.script.PyArgParser;
import com.inductiveautomation.ignition.common.script.ScriptContext;
import com.inductiveautomation.ignition.common.script.builtin.KeywordArgs;
import com.inductiveautomation.ignition.common.script.hints.ScriptFunction;
import com.inductiveautomation.ignition.common.tags.model.TagPath;
import com.inductiveautomation.ignition.common.tags.paths.parser.TagPathParser;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.python.core.Py;
Expand All @@ -17,6 +34,8 @@
public class UtilitiesExtensions {
private final CommonContext context;

private static final ELParserHarness EXPRESSION_PARSER = new ELParserHarness();

public UtilitiesExtensions(CommonContext context) {
this.context = context;
}
Expand Down Expand Up @@ -55,4 +74,59 @@ private static PyObject recursiveConvert(@NotNull PyObject object) {
}
}
}

@ScriptFunction(docBundlePrefix = "UtilitiesExtensions")
@KeywordArgs(names = {"expression"}, types = {String.class})
public QualifiedValue evalExpression(PyObject[] args, String[] keywords) throws Exception {
if (args.length == 0) {
throw Py.ValueError("Must supply at least one argument to evalExpression");
}

String expression = TypeUtilities.toString(TypeUtilities.pyToJava(args[0]));

var keywordMap = new HashMap<String, Object>();
for (int i = 0; i < keywords.length; i++) {
keywordMap.put(keywords[i], TypeUtilities.pyToJava(args[i + 1]));
}
ExpressionParseContext parseContext = new KeywordParseContext(keywordMap);

Expression actualExpression = EXPRESSION_PARSER.parse(expression, parseContext);
try {
actualExpression.startup();
return actualExpression.execute();
} finally {
actualExpression.shutdown();
}
}

private class KeywordParseContext implements ExpressionParseContext {
private final Map<String, Object> keywords;

private KeywordParseContext(Map<String, Object> keywords) {
this.keywords = keywords;
}

@Override
public Expression createBoundExpression(String reference) throws RuntimeException {
if (reference == null || reference.isEmpty()) {
throw new IllegalArgumentException("Invalid path " + reference);
}
if (keywords.containsKey(reference)) {
return new ConstantExpression(keywords.get(reference));
} else {
try {
TagPath path = TagPathParser.parse(ScriptContext.defaultTagProvider(), reference);
var tagValues = context.getTagManager().readAsync(List.of(path)).get(30, TimeUnit.SECONDS);
return new ConstantExpression(tagValues.get(0).getValue());
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
throw new RuntimeException(e);
}
}
}

@Override
public FunctionFactory getFunctionFactory() {
return context.getExpressionFunctionFactory();
}
}
}
@@ -1,6 +1,8 @@
getContext.desc=Returns the current scope's context object directly.
getContext.returns=The current scope's context.

deepCopy.desc=Deep copies the inner object structure into plain Python lists, dictionaries, and primitives.
deepCopy.param.object=The object to convert.
deepCopy.returns=A plain Python primitive object.
evalExpression.desc=Evaluates the supplied expression. Provide keyword arguments to populate values to curly braces.
evalExpression.param.expression=The expression to evaluate.
evalExpression.returns=A QualifiedValue with the result of the provided expression.
1 change: 1 addition & 0 deletions docker-compose.yml
Expand Up @@ -3,6 +3,7 @@ services:
image: inductiveautomation/ignition:8.1.20
ports:
- 18088:8088
- 18000:8000
environment:
GATEWAY_ADMIN_PASSWORD: password
IGNITION_EDITION: standard
Expand Down