Search in sources :

Example 6 with RuleInfo

use of org.csstudio.display.builder.model.rules.RuleInfo in project org.csstudio.display.builder by kasemir.

the class RulesWidgetProperty method readFromXML.

@Override
public void readFromXML(final ModelReader model_reader, final Element property_xml) throws Exception {
    final Iterable<Element> rule_xml = XMLUtil.getChildElements(property_xml, XMLTags.RULE);
    final List<RuleInfo> rules = new ArrayList<>();
    for (final Element xml : rule_xml) {
        String name, prop_id, out_exp_str;
        try {
            name = xml.getAttribute(XMLTags.NAME);
            if (name.isEmpty())
                logger.log(Level.WARNING, "Missing rule 'name'");
        } catch (Exception ex) {
            name = "unknown";
            logger.log(Level.WARNING, "Failed to find rule name");
        }
        try {
            prop_id = xml.getAttribute("prop_id");
            if (prop_id.isEmpty())
                logger.log(Level.WARNING, "Missing rule 'prop_id'");
        } catch (Exception e) {
            prop_id = "unknown";
            logger.log(Level.WARNING, "Failed to find rule prop_id");
        }
        boolean prop_as_expr = false;
        try {
            out_exp_str = xml.getAttribute("out_exp");
            prop_as_expr = false;
            if (out_exp_str.isEmpty())
                logger.log(Level.WARNING, "Missing rule 'out_exp'");
            else
                prop_as_expr = Boolean.parseBoolean(out_exp_str);
        } catch (Exception e) {
            logger.log(Level.WARNING, "Failed to find rule out_exp");
        }
        List<ExpressionInfo<?>> exprs;
        try {
            exprs = readExpressions(model_reader, prop_id, prop_as_expr, xml);
        } catch (Throwable ex) {
            logger.log(Level.WARNING, "Failure to readExpressions for " + prop_id, ex);
            exprs = Collections.emptyList();
        }
        final List<ScriptPV> pvs = readPVs(xml);
        rules.add(new RuleInfo(name, prop_id, prop_as_expr, exprs, pvs));
    }
    setValue(rules);
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ExprInfoString(org.csstudio.display.builder.model.rules.RuleInfo.ExprInfoString) ExpressionInfo(org.csstudio.display.builder.model.rules.RuleInfo.ExpressionInfo) RuleInfo(org.csstudio.display.builder.model.rules.RuleInfo)

Example 7 with RuleInfo

use of org.csstudio.display.builder.model.rules.RuleInfo in project org.csstudio.display.builder by kasemir.

the class WidgetRuntime method startScripts.

/**
 * Start Scripts
 */
private void startScripts() {
    // Start scripts triggered by PVs
    final List<ScriptInfo> script_infos = widget.propScripts().getValue();
    final List<RuleInfo> rule_infos = widget.propRules().getValue();
    if ((script_infos.size() > 0) || (rule_infos.size() > 0)) {
        final List<RuntimeScriptHandler> handlers = new ArrayList<>(script_infos.size() + rule_infos.size());
        for (final ScriptInfo script_info : script_infos) {
            try {
                handlers.add(new RuntimeScriptHandler(widget, script_info));
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Script failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(script_info.getPath());
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        for (final RuleInfo rule_info : rule_infos) {
            try {
                handlers.add(new RuntimeScriptHandler(widget, rule_info));
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Rule failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(rule_info.getName());
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        script_handlers = handlers;
    }
    // Compile scripts invoked by actions
    final List<ActionInfo> actions = widget.propActions().getValue().getActions();
    if (actions.size() > 0) {
        final Map<ExecuteScriptActionInfo, Script> scripts = new HashMap<>();
        for (ActionInfo action_info : actions) {
            if (!(action_info instanceof ExecuteScriptActionInfo))
                continue;
            final ExecuteScriptActionInfo script_action = (ExecuteScriptActionInfo) action_info;
            try {
                final MacroValueProvider macros = widget.getMacrosOrProperties();
                final Script script = RuntimeScriptHandler.compileScript(widget, macros, script_action.getInfo());
                scripts.put(script_action, script);
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Script for action failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(script_action);
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        if (scripts.size() > 0)
            action_scripts = scripts;
    }
    // Signal that start() has completed
    started.countDown();
}
Also used : Script(org.csstudio.display.builder.runtime.script.internal.Script) HashMap(java.util.HashMap) ScriptInfo(org.csstudio.display.builder.model.properties.ScriptInfo) ArrayList(java.util.ArrayList) ActionInfo(org.csstudio.display.builder.model.properties.ActionInfo) WritePVActionInfo(org.csstudio.display.builder.model.properties.WritePVActionInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) MacroValueProvider(org.csstudio.display.builder.model.macros.MacroValueProvider) DisplayModel(org.csstudio.display.builder.model.DisplayModel) RuntimeScriptHandler(org.csstudio.display.builder.runtime.script.internal.RuntimeScriptHandler) RuleInfo(org.csstudio.display.builder.model.rules.RuleInfo)

Aggregations

RuleInfo (org.csstudio.display.builder.model.rules.RuleInfo)7 ScriptPV (org.csstudio.display.builder.model.properties.ScriptPV)3 LabelWidget (org.csstudio.display.builder.model.widgets.LabelWidget)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Widget (org.csstudio.display.builder.model.Widget)2 ExprInfoString (org.csstudio.display.builder.model.rules.RuleInfo.ExprInfoString)2 ImageWidget (org.csstudio.display.builder.model.widgets.plots.ImageWidget)2 HashMap (java.util.HashMap)1 DisplayModel (org.csstudio.display.builder.model.DisplayModel)1 WidgetProperty (org.csstudio.display.builder.model.WidgetProperty)1 MacroValueProvider (org.csstudio.display.builder.model.macros.MacroValueProvider)1 ActionInfo (org.csstudio.display.builder.model.properties.ActionInfo)1 ExecuteScriptActionInfo (org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo)1 ScriptInfo (org.csstudio.display.builder.model.properties.ScriptInfo)1 WidgetColor (org.csstudio.display.builder.model.properties.WidgetColor)1 WritePVActionInfo (org.csstudio.display.builder.model.properties.WritePVActionInfo)1 ExpressionInfo (org.csstudio.display.builder.model.rules.RuleInfo.ExpressionInfo)1 AutocompleteMenu (org.csstudio.display.builder.representation.javafx.AutocompleteMenu)1