Package it.freedomotic.reactions

Examples of it.freedomotic.reactions.Reaction


     */
    public Reaction resolve(Reaction r) {
        this.reaction = r;

        if ((context != null) && (reaction != null)) {
            Reaction clone =
                    new Reaction(reaction.getTrigger(),
                    performSubstitutionInCommands(reaction.getCommands()));

            return clone;
        }

View Full Code Here


                //Searching for reactions using this trigger
                boolean found = false;

                while (it.hasNext()) {
                    Reaction reaction = it.next();
                    Trigger reactionTrigger = reaction.getTrigger();

                    //found a related reaction. This must be executed
                    if (trigger.equals(reactionTrigger) && !reaction.getCommands().isEmpty()) {
                        if (!checkAdditionalConditions(reaction)) {
                            LOG.log(Level.INFO,
                                    "Additional conditions test failed in reaction {0}", reaction.toString());
                            return;
                        }
                        reactionTrigger.setExecuted();
                        found = true;
                        LOG.log(Level.FINE, "Try to execute reaction {0}", reaction.toString());

                        try {
                            //executes the commands in sequence (only the first sequence is used)
                            //if more then one sequence is needed it can be done with two reactions with the same trigger
                            Resolver commandResolver = new Resolver();
                            event.getPayload().addStatement("description",
                                    trigger.getDescription()); //embedd the trigger description to the event payload
                            commandResolver.addContext("event.",
                                    event.getPayload());

                            for (final Command command : reaction.getCommands()) {
                                if (command == null) {
                                    continue; //skip this loop
                                }

                                if (command.getReceiver()
                                        .equalsIgnoreCase(BehaviorManager.getMessagingChannel())) {
                                    //this command is for an object so it needs only to know only about event parameters
                                    Command resolvedCommand = commandResolver.resolve(command);
                                    //doing so we bypass messaging system gaining better performances
                                    BehaviorManager.parseCommand(resolvedCommand);
                                } else {
                                    //if the event has a target object we include also object info
                                    EnvObjectLogic targetObject =
                                            EnvObjectPersistence.getObjectByName(event.getProperty("object.name"));

                                    if (targetObject != null) {
                                        commandResolver.addContext("current.",
                                                targetObject.getExposedProperties());
                                        commandResolver.addContext("current.",
                                                targetObject.getExposedBehaviors());
                                    }

                                    final Command resolvedCommand = commandResolver.resolve(command);

                                    //it's not a user level command for objects (eg: turn it on), it is for another kind of actuator
                                    Command reply = busService.send(resolvedCommand); //blocking wait until executed

                                    if (reply == null) {
                                        LOG.log(Level.WARNING,
                                                "Unreceived reply within given time ({0}ms) for command {1}",
                                                new Object[]{command.getReplyTimeout(), command.getName()});
                                    } else {
                                        if (reply.isExecuted()) {
                                            LOG.log(Level.FINE, "Executed succesfully {0}", command.getName());
                                        } else {
                                            LOG.log(Level.WARNING, "Unable to execute command{0}", command.getName());
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            LOG.severe("Exception while merging event parameters into reaction.\n");
                            LOG.severe(Freedomotic.getStackTraceInfo(e));

                            return;
                        }

                        String info =
                                "Executing automation '" + reaction.toString() + "' takes "
                                + (System.currentTimeMillis() - event.getCreation()) + "ms.";
                        LOG.info(info);

                        MessageEvent message = new MessageEvent(null, info);
                        message.setType("callout"); //display as callout on frontends
View Full Code Here

    }

    public ReactionEditor(I18n i18n) {
        this.I18n = i18n;
        initComponents();
        this.reaction = new Reaction();
        init();
    }
View Full Code Here

                }

                if (!found) { //add an empty reaction if none
                    pos = panel.getComponentCount();

                    ReactionEditor editor = new ReactionEditor(I18n, new Reaction(trigger),
                            this);
                    panel.add(editor, pos++);
                }

                panel.add(new JSeparator(),
View Full Code Here

                        }
                    }

                    if (!alreadyStored) { //add an empty reaction if none

                        ReactionEditor editor = new ReactionEditor(I18n, new Reaction(trigger),
                                this);
                        panel.add(editor);
                    }
                }
            }
View Full Code Here

    }

    private String manageMessage(String mess) {
        Command c;
        Trigger t = null;
        Reaction r;
        NaturalLanguageProcessor nlp2 = new NaturalLanguageProcessor();
        //  String sentenceMess[] = nlp.getSentenceDetector().sentDetect(mess);
        String tokenMess[] = mess.split(" "); //nlp.getTokenizer().tokenize(sentenceMess[0]);
        String triggername = "";
        int conditionSep = 0;
        if (tokenMess[0].equalsIgnoreCase(HELP)) {
            return help(tokenMess);
        }
        if (tokenMess[0].equalsIgnoreCase(LIST)) {
            return list(tokenMess);
        }

        if (tokenMess[0].equalsIgnoreCase(IF) || tokenMess[0].equalsIgnoreCase(WHEN)) {
            for (int i = 1; i < tokenMess.length; i++) {
                if (tokenMess[i].equalsIgnoreCase(THEN)) {
                    triggername = unsplit(tokenMess, 1, i - 1, " ");
                    conditionSep = i + 1;
                    break;
                }
            }
            t = TriggerPersistence.getTrigger(triggername);
        }

        String commandName = unsplit(tokenMess, conditionSep, tokenMess.length - conditionSep, " ");
        List<NaturalLanguageProcessor.Rank> mostSimilar = nlp2.getMostSimilarCommand(commandName, 10);
        // user is asking for help
        if (commandName.contains("*")) {
            String response = "";
            for (NaturalLanguageProcessor.Rank nlpr : mostSimilar) {
                response += "? " + nlpr.getCommand().getName() + "\n";
            }
            return response;
        }
        if (!mostSimilar.isEmpty() && mostSimilar.get(0).getSimilarity() > 0) {
            c = mostSimilar.get(0).getCommand();
        } else {
            return "No available commands similar to: " + commandName;
        }
        if (tokenMess[0].equalsIgnoreCase(IF)) {
            Trigger NEWt = t.clone();
            NEWt.setNumberOfExecutions(1);
            r = new Reaction(NEWt, c);
            ReactionPersistence.add(r);
        } else if (tokenMess[0].equalsIgnoreCase(WHEN)) {
            // do something
            r = new Reaction(t, c);
            ReactionPersistence.add(r);
        } else {
            send(c);
            return c.getName() + "\n DONE.";
        }
View Full Code Here

TOP

Related Classes of it.freedomotic.reactions.Reaction

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.