- synchronized (reactions) {
- if (!reactions.containsKey(name)) {
- return;
- }
- reactions.remove(name);
- reactions.notifyAll();
- }
- }
-
- //
- // ABSTRACTSERVICE METHODS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- while (isRunning()) {
- Optional<NextReaction> nextReaction = getNextReaction();
- if (!nextReaction.isPresent()) {
- continue;
- }
-
- String reactionName = nextReaction.get().getKey();
- logger.debug(format("Next Reaction: %s.", reactionName));
-
- /* wait until the next reaction has to run. */
- Optional<net.pterodactylus.rhynodge.State> lastState = stateManager.loadLastState(reactionName);
- int lastStateFailCount = lastState.isPresent() ? lastState.get().failCount() : 0;
- long waitTime = nextReaction.get().getNextTime() - System.currentTimeMillis();
- logger.debug(format("Time to wait for next Reaction: %d millseconds.", waitTime));
- if (waitTime > 0) {
- synchronized (reactions) {
- try {
- logger.info(format("Waiting until %tc.", nextReaction.get().getNextTime()));
- reactions.wait(waitTime);
- } catch (InterruptedException ie1) {
- /* we’re looping! */
- }
- }
-
- /* re-start loop to check for new reactions. */
- continue;
- }
-
- /* run reaction. */
- logger.info(format("Running Query for %s...", reactionName));
- Query query = nextReaction.get().getReaction().query();
- net.pterodactylus.rhynodge.State state;
- try {
- logger.debug("Querying system...");
- state = query.state();
- if (state == null) {
- state = FailedState.INSTANCE;
- }
- logger.debug("System queried.");
- } catch (Throwable t1) {
- logger.warn("Querying system failed!", t1);
- state = new AbstractState(t1) {
- /* no further state. */
- };
- }
- logger.debug(format("State is %s.", state));
-
- /* convert states. */
- for (Filter filter : nextReaction.get().getReaction().filters()) {
- if (state.success()) {
- net.pterodactylus.rhynodge.State newState = filter.filter(state);
- //logger.debug(String.format("Old state is %s, new state is %s.", state, newState));
- state = newState;
- }
- }
- if (!state.success()) {
- state.setFailCount(lastStateFailCount + 1);
- }
- Optional<net.pterodactylus.rhynodge.State> lastSuccessfulState = stateManager.loadLastSuccessfulState(reactionName);
-
- /* merge states. */
- boolean triggerHit = false;
- Trigger trigger = nextReaction.get().getReaction().trigger();
- if (lastSuccessfulState.isPresent() && lastSuccessfulState.get().success() && state.success()) {
- net.pterodactylus.rhynodge.State newState = trigger.mergeStates(lastSuccessfulState.get(), state);
-
- /* save new state. */
- stateManager.saveState(reactionName, newState);
-
- triggerHit = trigger.triggers();
- } else {
- /* save first or error state. */
- stateManager.saveState(reactionName, state);
- }
-
- /* run action if trigger was hit. */
- logger.debug(format("Trigger was hit: %s.", triggerHit));
- if (triggerHit) {
- logger.info("Executing Action...");
- nextReaction.get().getReaction().action().execute(trigger.output(nextReaction.get().getReaction()));
- }
-