-
Notifications
You must be signed in to change notification settings - Fork 0
Rule action persistence policies #8
Changes from all commits
f7a27c5
896041a
5a4a3ae
4d02198
578f044
02eadca
f55f1e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package com.team766.framework3; | ||
|
|
||
| import static com.team766.framework3.RulePersistence.ONCE_AND_HOLD; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.collect.BiMap; | ||
| import com.google.common.collect.HashBiMap; | ||
|
|
@@ -14,6 +16,7 @@ | |
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.BooleanSupplier; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
|
|
@@ -43,18 +46,87 @@ public Category getLoggerCategory() { | |
| return Category.RULES; | ||
| } | ||
|
|
||
| protected Rule addRule(String name, BooleanSupplier condition, Supplier<Procedure> action) { | ||
| Rule rule = new Rule(name, condition, action); | ||
| protected Rule addRule( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we hold off on merging this PR until the RuleEngine API simplification goes through? I think that would remove the need for a lot of these overloaded versions of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. n/m about the first paragraph since this is based on that other PR.. do you think we could reduce the overloading and let devs more simply specify a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
does standard Java style discourage the use of overloads?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not at all - there are just a lot of variants. it's too bad java doesn't have named and optoinal parameters. :) just looked - there are articles like this that also speak to alternatives to method overloading when there are a larger number of parameters. I'm fine either way - was just wondering how to keep this scalable esp if and as we do add more parameters.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a good point. we can discuss again if we add more parameters |
||
| String name, | ||
| BooleanSupplier condition, | ||
| RulePersistence rulePersistence, | ||
| Supplier<Procedure> action) { | ||
| Rule rule = new Rule(name, condition, rulePersistence, action); | ||
| rules.put(name, rule); | ||
| int priority = rulePriorities.size(); | ||
| rulePriorities.put(rule, priority); | ||
| return rule; | ||
| } | ||
|
|
||
| protected Rule addRule(String name, BooleanSupplier condition, Supplier<Procedure> action) { | ||
| return addRule(name, condition, ONCE_AND_HOLD, action); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, BooleanSupplier condition, Mechanism mechanism, Runnable action) { | ||
| String name, | ||
| BooleanSupplier condition, | ||
| RulePersistence rulePersistence, | ||
| Set<Mechanism> mechanisms, | ||
| Consumer<Context> action) { | ||
| return addRule( | ||
| name, | ||
| condition, | ||
| rulePersistence, | ||
| () -> new FunctionalProcedure(mechanisms, action)); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, | ||
| BooleanSupplier condition, | ||
| Set<Mechanism> mechanisms, | ||
| Consumer<Context> action) { | ||
| return addRule(name, condition, ONCE_AND_HOLD, mechanisms, action); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, | ||
| BooleanSupplier condition, | ||
| RulePersistence rulePersistence, | ||
| Mechanism mechanism, | ||
| Consumer<Context> action) { | ||
| return addRule(name, condition, rulePersistence, Set.of(mechanism), action); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, BooleanSupplier condition, Mechanism mechanism, Consumer<Context> action) { | ||
| return addRule(name, condition, ONCE_AND_HOLD, mechanism, action); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, | ||
| BooleanSupplier condition, | ||
| RulePersistence rulePersistence, | ||
| Set<Mechanism> mechanisms, | ||
| Runnable action) { | ||
| return addRule( | ||
| name, condition, () -> new FunctionalInstantProcedure(Set.of(mechanism), action)); | ||
| name, | ||
| condition, | ||
| rulePersistence, | ||
| () -> new FunctionalInstantProcedure(mechanisms, action)); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, BooleanSupplier condition, Set<Mechanism> mechanisms, Runnable action) { | ||
| return addRule(name, condition, ONCE_AND_HOLD, mechanisms, action); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, | ||
| BooleanSupplier condition, | ||
| RulePersistence rulePersistence, | ||
| Mechanism mechanism, | ||
| Runnable action) { | ||
| return addRule(name, condition, rulePersistence, Set.of(mechanism), action); | ||
| } | ||
|
|
||
| protected Rule addRule( | ||
| String name, BooleanSupplier condition, Mechanism mechanism, Runnable action) { | ||
| return addRule(name, condition, ONCE_AND_HOLD, mechanism, action); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
|
|
@@ -107,7 +179,7 @@ public final void run() { | |
| rule.evaluate(); | ||
|
|
||
| // see if the rule is triggering | ||
| Rule.TriggerType triggerType = rule.getCurrentTriggerType(); | ||
| final Rule.TriggerType triggerType = rule.getCurrentTriggerType(); | ||
| if (triggerType != Rule.TriggerType.NONE) { | ||
| log(Severity.INFO, "Rule " + rule.getName() + " triggering: " + triggerType); | ||
|
|
||
|
|
@@ -169,6 +241,17 @@ public final void run() { | |
| } | ||
|
|
||
| // we're good to proceed | ||
|
|
||
| if (triggerType == Rule.TriggerType.FINISHED | ||
| && rule.getCancellationOnFinish() | ||
| == Rule.Cancellation.CANCEL_NEWLY_ACTION) { | ||
| var newlyCommand = | ||
| ruleMap.inverse().get(new RuleAction(rule, Rule.TriggerType.NEWLY)); | ||
| if (newlyCommand != null) { | ||
| newlyCommand.cancel(); | ||
| } | ||
| } | ||
|
|
||
| Procedure procedure = rule.getProcedureToRun(); | ||
| if (procedure == null) { | ||
| continue; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.team766.framework3; | ||
|
|
||
| /** | ||
| * Policies for how to handle a Rule's action when the action completes or the Rule stops triggering. | ||
| */ | ||
| public enum RulePersistence { | ||
| /** | ||
| * When the action completes, don't do anything. Any Mechanism reservations that the action held | ||
| * are released. Also, the action may continue running after the Rule stops triggering. | ||
| */ | ||
| ONCE, | ||
| /** | ||
| * When the action completes, don't do anything but retain the Mechanism reservations that the | ||
| * action held until the Rule stops triggering. If the Rule stops triggering before the action | ||
| * has completed, then the action will be terminated. | ||
| */ | ||
| ONCE_AND_HOLD, | ||
| /** | ||
| * When the action completes, start executing the action again, until the Rule stops triggering. | ||
| * The action will be terminated when the Rule stops triggering. | ||
| */ | ||
| REPEATEDLY, | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.