Package it.freedomotic.reactions

Examples of it.freedomotic.reactions.Statement


        Iterator it = t.getPayload().iterator();
        int row = 0;

        while (it.hasNext()) {
            Statement statement = (Statement) it.next();
            List list = new ArrayList();
            list.add(statement.getLogical());
            list.add(statement.getAttribute());
            list.add(statement.getOperand());
            list.add(statement.getValue());
            model.insertRow(row,
                    list.toArray());
        }
    }
View Full Code Here


        Iterator it = trigger.getPayload().iterator();
        int row = 0;

        while (it.hasNext()) {
            Statement statement = (Statement) it.next();
            panel.addElement(new JTextField(statement.getLogical()),
                    row,
                    0);
            panel.addElement(new JTextField(statement.getAttribute()),
                    row,
                    1);
            panel.addElement(new JTextField(statement.getOperand()),
                    row,
                    2);
            panel.addElement(new JTextField(statement.getValue()),
                    row,
                    3);
            row++;
        }
View Full Code Here

    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
        Payload payload = (Payload) o;
        writer.startNode("payload");
        Iterator<Statement> it = payload.iterator();
        while (it.hasNext()) {
            Statement statement = it.next();
            writer.startNode("it.freedomotic.reactions.Statement");
            writer.startNode("logical");
            writer.setValue(statement.getLogical());
            writer.endNode(); //</logical>
            writer.startNode("attribute");
            writer.setValue(statement.getAttribute());
            writer.endNode(); //</attribute>
            writer.startNode("operand");
            writer.setValue(statement.getOperand());
            writer.endNode(); //</operand>
            writer.startNode("value");
            writer.setValue(statement.getValue());
            writer.endNode(); //</value>
            writer.endNode(); //</it.freedomotic.reactions.Statement>
        }

        writer.endNode(); //</payload>
View Full Code Here

     */
    private void performSubstitutionInTrigger(Trigger trigger) throws VariableResolutionException {
        Iterator it = trigger.getPayload().iterator();

        while (it.hasNext()) {
            Statement statement = (Statement) it.next();
            String key = (String) statement.getAttribute();
            String propertyValue = (String) statement.getValue();

            for (final String PREFIX : prefixes) {
                Pattern pattern = Pattern.compile("@" + PREFIX + "[.A-Za-z0-9_-]*\\b(#)?"); //find any @token
                Matcher matcher = pattern.matcher(propertyValue);
                StringBuffer result = new StringBuffer(propertyValue.length());

                while (matcher.find()) {
                    matcher.appendReplacement(result, "");

                    String tokenKey = matcher.group();

                    if (tokenKey.endsWith("#")) {
                        tokenKey = tokenKey.substring(0, tokenKey.length() - 1); //cutting out the optional last '#'
                    }

                    tokenKey =
                            tokenKey.substring(1,
                            tokenKey.length()); //cutting out the first char '@'

                    String tokenValue = trigger.getPayload().getStatementValue(tokenKey);

                    if (tokenValue == null) {
                        throw new VariableResolutionException("Variable '" + tokenValue + "' cannot be resolved in trigger '"
                                + trigger.getName() + "'.\n" + "Availabe tokens are: "
                                + context.toString());
                    }

                    //replace an @token.property with its real value
                    //System.out.println("Replace all " + tokenKey + " with " + tokenValue + " in " + propertyValue);
                    result.append(tokenValue);
                }

                matcher.appendTail(result);
                statement.setValue(result.toString());
            }

            //all references are replaced with real values in the current statement, now perform scripting
            String possibleScript = (String) statement.getValue().trim();
            boolean success = false;

            if (possibleScript.startsWith("=")) {
                //this is a javascript
                try {
                    ScriptEngineManager mgr = new ScriptEngineManager();
                    ScriptEngine js = mgr.getEngineByName("JavaScript");
                    //removing equal sign on the head
                    String script = possibleScript.substring(1);

                    if (js == null) {
                        LOG.severe("Cannot instatiate a JavaScript engine");
                    }

                    try {
                        js.eval(script);
                    } catch (ScriptException scriptException) {
                        LOG.severe(scriptException.getMessage());
                    }

                    if (js.get(key) == null) {
                        LOG.log(Level.SEVERE,
                                "Script evaluation in trigger ''{0}'' has returned a null value, maybe the key ''{1}'' is not evaluated properly.",
                                new Object[]{trigger.getName(), key});
                    }

                    statement.setValue(js.get(key).toString());
                    success = true;
                } catch (Exception ex) {
                    success = false;
                    LOG.severe(ex.getMessage());
                }
            }

            if (!success) {
                //fall back to the value before scripting evaluation
                statement.setValue(possibleScript);
            }
        }
    }
View Full Code Here

    private void mergeContextParamsIntoCommand(Command c) {
        //adding  parameters to command parameters with a  prefix
        Iterator<Statement> it = context.iterator();
        while (it.hasNext()) {
            Statement statement = it.next();
            c.setProperty(statement.getAttribute(), statement.getValue());
        }
    }
View Full Code Here

        Iterator it = aContext.iterator();

        while (it.hasNext()) {
            String key;
            Statement statement = (Statement) it.next();

            //removing the prefix of the properties if already exists
            //to avoid dublicate prefixes like @event.event.object.name
            if (statement.getAttribute().startsWith(PREFIX)) {
                key = statement.getAttribute().substring(PREFIX.length());
            } else {
                key = statement.getAttribute().toString();
            }

            context.addStatement(PREFIX + key,
                    statement.getValue());
        }
    }
View Full Code Here

            private boolean checkAdditionalConditions(Reaction rea) {
                boolean result = true;
                for (Condition condition : rea.getConditions()) {
                    //System.out.println("DEBUG: check condition " + condition.getTarget());
                    EnvObjectLogic object = EnvObjectPersistence.getObjectByName(condition.getTarget());
                    Statement statement = condition.getStatement();
                    if (object != null) {
                        BehaviorLogic behavior = object.getBehavior(statement.getAttribute());
                        //System.out.println("DEBUG: " + object.getPojo().getName() + " "
                        //+ " behavior: " + behavior.getName() + " " + behavior.getValueAsString());
                        boolean eval = behavior.getValueAsString().equalsIgnoreCase(statement.getValue());
                        if (statement.getLogical().equalsIgnoreCase("AND")) {
                            result = result && eval;
                            //System.out.println("DEBUG: result and: " + result + "(" + eval +")");
                        } else {
                            result = result || eval;
                            //System.out.println("DEBUG: result or: " + result + "(" + eval +")");
View Full Code Here

            if (behavior.isEmpty()) {
                return false;
            }
        }

        Statement valueStatement = t.getPayload().getStatements("behaviorValue").get(0);

        if (valueStatement == null) {
            LOG.log(Level.WARNING,
                    "No value in hardware trigger ''{0}'' to apply to object action ''{1}'' of object {2}",
                    new Object[]{t.getName(), behavior, getPojo().getName()});

            return false;
        }

        LOG.log(Level.CONFIG,
                "Sensors notification ''{0}'' has changed ''{1}'' behavior ''{2}'' to {3}",
                new Object[]{t.getName(), getPojo().getName(), behavior, valueStatement.getValue()});

        Config params = new Config();
        params.setProperty("value",
                valueStatement.getValue());
        getBehavior(behavior).filterParams(params, false); //false means not fire commands, only change behavior value

        return true;
    }
View Full Code Here

                t.setName(t.getName().replace(oldName, newName));
                LOG.log(Level.WARNING, "trigger name renamed to {0}", t.getName());
            }
            Iterator<Statement> it = t.getPayload().iterator();
            while (it.hasNext()) {
                Statement statement = it.next();
                if (statement.getValue().contains(oldName)) {
                    statement.setValue(statement.getValue().replace(oldName, newName));
                    LOG.log(Level.WARNING, "Trigger value in payload renamed to {0}", statement.getValue());
                }
            }
        }
    }
View Full Code Here

            if (!trigger.isHardwareLevel()) {
                Iterator it = trigger.getPayload().iterator();

                //chack if this trigger is related toi the object and set a flag
                while (it.hasNext()) {
                    Statement statement = (Statement) it.next();

                    if (statement.getValue().contains(object.getPojo().getName())) {
                        isRelated = true; //is a trigger related with this object

                        break; //no need to check the other statements in current trigger
                    }
                }
View Full Code Here

TOP

Related Classes of it.freedomotic.reactions.Statement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.