Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
remove Rule.Builder and simplify RuleEngine.addRule(), letting it ret…
Browse files Browse the repository at this point in the history
…urn a Rule that can be modified further, eg with a finishedTriggeringProcedure.
  • Loading branch information
dejabot committed Jan 5, 2025
1 parent c1bde3b commit f7a27c5
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 260 deletions.
80 changes: 15 additions & 65 deletions src/main/java/com/team766/framework3/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
* public class MyRules extends RuleEngine {
* public MyRules() {
* // add rule to spin up the shooter when the boxop presses the right trigger on the gamepad
* rules.add(Rule.create("spin up shooter", gamepad.getButton(InputConstants.XBOX_RT)).
* withNewlyTriggeringProcedure(() -> new ShooterSpin(shooter)));
* rules.add("spin up shooter", gamepad.getButton(InputConstants.XBOX_RT),
* () -> new ShooterSpin(shooter)));
* ...
* }
* }
Expand All @@ -49,55 +49,6 @@ enum TriggerType {
FINISHED
}

/**
* Simple Builder for {@link Rule}s. Configure Rules via this Builder; these fields will be immutable
* in the rule the Builder constructs.
*
* Instances of this Builder are created via {@link Rule#create} to simplify syntax.
*/
public static class Builder {
private final String name;
private final BooleanSupplier predicate;
private Supplier<Procedure> newlyTriggeringProcedure;
private Supplier<Procedure> finishedTriggeringProcedure;

private Builder(String name, BooleanSupplier predicate) {
this.name = name;
this.predicate = predicate;
}

/** Specify a creator for the Procedure that should be run when this rule starts triggering. */
public Builder withNewlyTriggeringProcedure(Supplier<Procedure> action) {
this.newlyTriggeringProcedure = action;
return this;
}

public Builder withNewlyTriggeringProcedure(
Set<Mechanism<?>> reservations, Runnable action) {
this.newlyTriggeringProcedure =
() -> new FunctionalInstantProcedure(reservations, action);
return this;
}

/** Specify a creator for the Procedure that should be run when this rule was triggering before and is no longer triggering. */
public Builder withFinishedTriggeringProcedure(Supplier<Procedure> action) {
this.finishedTriggeringProcedure = action;
return this;
}

public Builder withFinishedTriggeringProcedure(
Set<Mechanism<?>> reservations, Runnable action) {
this.finishedTriggeringProcedure =
() -> new FunctionalInstantProcedure(reservations, action);
return this;
}

// called by {@link RuleEngine#addRule}.
/* package */ Rule build() {
return new Rule(name, predicate, newlyTriggeringProcedure, finishedTriggeringProcedure);
}
}

private final String name;
private final BooleanSupplier predicate;
private final Map<TriggerType, Supplier<Procedure>> triggerProcedures =
Expand All @@ -107,15 +58,8 @@ public Builder withFinishedTriggeringProcedure(

private TriggerType currentTriggerType = TriggerType.NONE;

public static Builder create(String name, BooleanSupplier predicate) {
return new Builder(name, predicate);
}

private Rule(
String name,
BooleanSupplier predicate,
Supplier<Procedure> newlyTriggeringProcedure,
Supplier<Procedure> finishedTriggeringProcedure) {
/* package */ Rule(
String name, BooleanSupplier predicate, Supplier<Procedure> newlyTriggeringProcedure) {
if (predicate == null) {
throw new IllegalArgumentException("Rule predicate has not been set.");
}
Expand All @@ -131,12 +75,18 @@ private Rule(
triggerReservations.put(
TriggerType.NEWLY, getReservationsForProcedure(newlyTriggeringProcedure));
}
}

if (finishedTriggeringProcedure != null) {
triggerProcedures.put(TriggerType.FINISHED, finishedTriggeringProcedure);
triggerReservations.put(
TriggerType.FINISHED, getReservationsForProcedure(finishedTriggeringProcedure));
}
/** Specify a creator for the Procedure that should be run when this rule was triggering before and is no longer triggering. */
public Rule withFinishedTriggeringProcedure(Supplier<Procedure> action) {
triggerProcedures.put(TriggerType.FINISHED, action);
triggerReservations.put(TriggerType.FINISHED, getReservationsForProcedure(action));
return this;
}

public Rule withFinishedTriggeringProcedure(Set<Mechanism<?>> reservations, Runnable action) {
return withFinishedTriggeringProcedure(
() -> new FunctionalInstantProcedure(reservations, action));
}

private Set<Mechanism<?>> getReservationsForProcedure(Supplier<Procedure> supplier) {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/team766/framework3/RuleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

/**
* {@link RuleEngine}s manage and process a set of {@link Rule}s. Subclasses should add rules via
Expand Down Expand Up @@ -41,11 +43,18 @@ public Category getLoggerCategory() {
return Category.RULES;
}

protected void addRule(Rule.Builder builder) {
Rule rule = builder.build();
protected Rule addRule(String name, BooleanSupplier condition, Supplier<Procedure> action) {
Rule rule = new Rule(name, condition, action);
rules.add(rule);
int priority = rulePriorities.size();
rulePriorities.put(rule, priority);
return rule;
}

protected Rule addRule(
String name, BooleanSupplier condition, Mechanism<?> mechanism, Runnable action) {
return addRule(
name, condition, () -> new FunctionalInstantProcedure(Set.of(mechanism), action));
}

@VisibleForTesting
Expand Down
Loading

0 comments on commit f7a27c5

Please sign in to comment.