2 * Reactor - Engine.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.reactor.engine;
22 import java.util.SortedMap;
23 import java.util.concurrent.TimeUnit;
25 import net.pterodactylus.reactor.Filter;
26 import net.pterodactylus.reactor.Query;
27 import net.pterodactylus.reactor.Reaction;
28 import net.pterodactylus.reactor.Trigger;
29 import net.pterodactylus.reactor.states.AbstractState;
30 import net.pterodactylus.reactor.states.FailedState;
32 import org.apache.log4j.Logger;
34 import com.google.common.collect.Maps;
35 import com.google.common.collect.Sets;
36 import com.google.common.util.concurrent.AbstractExecutionThreadService;
37 import com.google.common.util.concurrent.Uninterruptibles;
40 * Reactor main engine.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class Engine extends AbstractExecutionThreadService {
47 private static final Logger logger = Logger.getLogger(Engine.class);
49 /** All defined reactions. */
50 private final Set<Reaction> reactions = Sets.newHashSet();
52 /** Reaction states. */
53 private final Map<Reaction, ReactionExecution> reactionExecutions = Maps.newHashMap();
60 * Adds the given reaction to this engine.
63 * The reaction to add to this engine
65 @SuppressWarnings("synthetic-access")
66 public void addReaction(Reaction reaction) {
67 reactions.add(reaction);
68 reactionExecutions.put(reaction, new ReactionExecution());
72 // ABSTRACTSERVICE METHODS
82 /* delay if we have no reactions. */
83 if (reactions.isEmpty()) {
84 logger.trace("Sleeping for 1 second while no Reactions available.");
85 Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
89 /* find next reaction. */
90 SortedMap<Long, Reaction> nextReactions = Maps.newTreeMap();
91 for (Reaction reaction : reactions) {
92 ReactionExecution reactionExecution = reactionExecutions.get(reaction);
93 nextReactions.put(reactionExecution.lastExecutionTime() + reaction.updateInterval(), reaction);
95 Reaction nextReaction = nextReactions.get(nextReactions.firstKey());
96 ReactionExecution reactionExecution = reactionExecutions.get(nextReaction);
97 logger.debug(String.format("Next Reaction: %s.", nextReaction));
99 /* wait until the next reaction has to run. */
100 while (isRunning()) {
101 long waitTime = (reactionExecution.lastExecutionTime() + nextReaction.updateInterval()) - System.currentTimeMillis();
102 logger.debug(String.format("Time to wait for next Reaction: %d millseconds.", waitTime));
107 logger.debug(String.format("Waiting for %d milliseconds.", waitTime));
108 TimeUnit.MILLISECONDS.sleep(waitTime);
109 } catch (InterruptedException ie1) {
114 /* are we still running? */
120 reactionExecution.setLastExecutionTime(System.currentTimeMillis());
121 Query query = nextReaction.query();
122 net.pterodactylus.reactor.State state;
124 logger.debug("Querying system...");
125 state = query.state();
127 state = FailedState.INSTANCE;
129 logger.debug("System queried.");
130 } catch (Throwable t1) {
131 logger.warn("Querying system failed!", t1);
132 state = new AbstractState(t1) {
133 /* no further state. */
136 logger.debug(String.format("State is %s.", state));
138 /* convert states. */
139 for (Filter filter : nextReaction.filters()) {
140 net.pterodactylus.reactor.State newState = filter.filter(state);
141 logger.debug(String.format("Old state is %s, new state is %s.", state, newState));
144 reactionExecution.addState(state);
146 /* only run trigger if we have collected two states. */
147 Trigger trigger = nextReaction.trigger();
148 boolean triggerHit = false;
149 if (reactionExecution.previousState() != null) {
150 logger.debug("Checking Trigger for changes...");
151 triggerHit = trigger.triggers(reactionExecution.currentState(), reactionExecution.previousState());
154 /* run action if trigger was hit. */
155 logger.debug(String.format("Trigger was hit: %s.", triggerHit));
157 logger.info("Executing Action...");
158 nextReaction.action().execute(trigger.trigger());
165 * Stores execution states of a {@link Reaction}.
167 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
169 private static class ReactionExecution {
171 /** The time the reaction was last executed. */
172 private long lastExecutionTime;
174 /** The previous state of the reaction. */
175 private net.pterodactylus.reactor.State previousState;
177 /** The current state of the reaction. */
178 private net.pterodactylus.reactor.State currentState;
185 * Returns the time the reaction was last executed. If the reaction was
186 * not yet executed, this method returns {@code 0}.
188 * @return The last execution time of the reaction (in milliseconds
189 * since Jan 1, 1970 UTC)
191 public long lastExecutionTime() {
192 return lastExecutionTime;
196 * Returns the current state of the reaction. If the reaction was not
197 * yet executed, this method returns {@code null}.
199 * @return The current state of the reaction
201 public net.pterodactylus.reactor.State currentState() {
206 * Returns the previous state of the reaction. If the reaction was not
207 * yet executed at least twice, this method returns {@code null}.
209 * @return The previous state of the reaction
211 public net.pterodactylus.reactor.State previousState() {
212 return previousState;
216 * Sets the last execution time of the reaction.
218 * @param lastExecutionTime
219 * The last execution time of the reaction (in milliseconds
220 * since Jan 1, 1970 UTC)
221 * @return This execution
223 public ReactionExecution setLastExecutionTime(long lastExecutionTime) {
224 this.lastExecutionTime = lastExecutionTime;
233 * Adds the given state as current state and moves the current state
234 * into the previous state.
237 * The new current state
238 * @return This execution
240 public ReactionExecution addState(net.pterodactylus.reactor.State state) {
241 previousState = currentState;
242 currentState = state;