Skip to content

Commit

Permalink
Added eq and not expressions to ActionLang, deprecated RoverBoostVolt…
Browse files Browse the repository at this point in the history
…ageActionNode for removal
  • Loading branch information
retrodaredevil committed Dec 13, 2024
1 parent 8cf4593 commit b49393b
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public final class ActionLangUtil {
// expressions
configMap.put("const", builder.copy().args("value").build());
configMap.put("str", builder.copy().linkedNode("expression").build());
configMap.put("eq", builder.copy().args("lhs", "rhs").build());
configMap.put("not", builder.copy().linkedNode("expression").build());
configMap.put("ref", builder.copy().args("name").build());
configMap.put("eval", builder.copy().args("name").build());
configMap.put("join", builder.copy().linkedNode("expression").build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.retrodaredevil.action.node.expression;

import me.retrodaredevil.action.node.expression.result.BooleanExpressionResult;
import me.retrodaredevil.action.node.expression.result.ExpressionResult;

import java.util.ArrayList;
import java.util.List;

public class EqualsExpression implements BooleanExpression {
private final Expression lhs;
private final Expression rhs;

public EqualsExpression(Expression lhs, Expression rhs) {
this.lhs = lhs;
this.rhs = rhs;
}

@Override
public List<? extends BooleanExpressionResult> evaluate() {
List<? extends ExpressionResult> leftResultList = lhs.evaluate();
List<? extends ExpressionResult> rightResultList = rhs.evaluate();
List<BooleanExpressionResult> resultList = new ArrayList<>();
for (ExpressionResult leftResult : leftResultList) {
for (ExpressionResult rightResult : rightResultList) {
resultList.add(BooleanExpressionResult.get(leftResult.equals(rightResult)));
}
}
return resultList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static <T extends ExpressionResult> List<T> convertTo(List<? extends Expr
throw new IllegalArgumentException("Could not cast to: " + to, e);
}
})
// TODO use unmodifiable list
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.retrodaredevil.action.node.expression;

import me.retrodaredevil.action.node.expression.result.BooleanExpressionResult;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;

public class NotExpression implements BooleanExpression {
private final Expression expression;

public NotExpression(Expression expression) {
this.expression = requireNonNull(expression);
}

@Override
public List<? extends BooleanExpressionResult> evaluate() {
List<? extends BooleanExpressionResult> results = ExpressionConvert.convertTo(expression.evaluate(), BooleanExpressionResult.class);
// TODO use unmodifiable list
return results.stream()
.map(BooleanExpressionResult::not)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.retrodaredevil.action.node.expression.node;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import me.retrodaredevil.action.node.environment.ActionEnvironment;
import me.retrodaredevil.action.node.expression.EqualsExpression;
import me.retrodaredevil.action.node.expression.Expression;

import static java.util.Objects.requireNonNull;

@JsonTypeName("eq")
public class EqualsExpressionNode implements ExpressionNode {
private final ExpressionNode lhs;
private final ExpressionNode rhs;

@JsonCreator
public EqualsExpressionNode(
@JsonProperty(value = "lhs", required = true) ExpressionNode lhs,
@JsonProperty(value = "rhs", required = true) ExpressionNode rhs) {
this.lhs = requireNonNull(lhs);
this.rhs = requireNonNull(rhs);
}

@Override
public Expression createExpression(ActionEnvironment actionEnvironment) {
return new EqualsExpression(
lhs.createExpression(actionEnvironment),
rhs.createExpression(actionEnvironment)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

@JsonSubTypes({
@JsonSubTypes.Type(ComparisonExpressionNode.class),
@JsonSubTypes.Type(EqualsExpressionNode.class),
@JsonSubTypes.Type(NotExpressionNode.class),
@JsonSubTypes.Type(ConstantExpressionNode.class),
@JsonSubTypes.Type(VariableReferenceExpressionNode.class),
@JsonSubTypes.Type(ToStringExpressionNode.class),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.retrodaredevil.action.node.expression.node;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import me.retrodaredevil.action.node.environment.ActionEnvironment;
import me.retrodaredevil.action.node.expression.Expression;
import me.retrodaredevil.action.node.expression.NotExpression;

import static java.util.Objects.requireNonNull;

@JsonTypeName("not")
public class NotExpressionNode implements ExpressionNode {
private final ExpressionNode expressionNode;

@JsonCreator
public NotExpressionNode(
@JsonProperty(value = "expression", required = true) ExpressionNode expressionNode) {
this.expressionNode = requireNonNull(expressionNode);
}

@Override
public Expression createExpression(ActionEnvironment actionEnvironment) {
return new NotExpression(expressionNode.createExpression(actionEnvironment));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public boolean getBoolean() {
return value;
}

public BooleanExpressionResult not() {
return this == TRUE ? FALSE : TRUE;
}

public static BooleanExpressionResult get(boolean result) {
return result ? TRUE : FALSE;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package me.retrodaredevil.action.node.expression.result;

public interface ExpressionResult {
@Override
boolean equals(Object other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import org.jetbrains.annotations.NotNull;

/**
* A {@link NumericExpressionResult} contains a {@link Number} of any implementation.
* Equality is determined by comparing the equality of the {@link Number#doubleValue()}
*/
public interface NumericExpressionResult extends ExpressionResult, Comparable<NumericExpressionResult> {
Number getNumber();

static NumericExpressionResult create(Number number) {
return () -> number;
return new SimpleNumericExpressionResult(number);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.retrodaredevil.action.node.expression.result;

import java.util.Objects;

import static java.util.Objects.requireNonNull;

final class SimpleNumericExpressionResult implements NumericExpressionResult {
private final Number number;

SimpleNumericExpressionResult(Number number) {
this.number = requireNonNull(number);
}

@Override
public Number getNumber() {
return number;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof NumericExpressionResult)) return false;
NumericExpressionResult that = (NumericExpressionResult) o;
Number thatNumber = that.getNumber();
return number.equals(thatNumber) || number.doubleValue() == thatNumber.doubleValue();
}

@Override
public int hashCode() {
return Objects.hashCode(number.doubleValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.retrodaredevil.action.node.expression.result;

import java.util.Objects;

import static java.util.Objects.requireNonNull;

public final class StringExpressionResult implements ExpressionResult {
Expand All @@ -12,4 +14,17 @@ public StringExpressionResult(String value) {
public String getString() {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StringExpressionResult)) return false;
StringExpressionResult that = (StringExpressionResult) o;
return value.equals(that.value);
}

@Override
public int hashCode() {
return Objects.hashCode(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Deprecated
@Deprecated(forRemoval = true)
@JsonTypeName("roverboostvoltage")
public class RoverBoostVoltageActionNode implements ActionNode {
private static final Logger LOGGER = LoggerFactory.getLogger(RoverBoostVoltageActionNode.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import me.retrodaredevil.solarthing.actions.message.MessageSenderActionNode;
import me.retrodaredevil.solarthing.actions.message.SendMessageActionNode;
import me.retrodaredevil.solarthing.actions.rover.RoverBoostSetActionNode;
import me.retrodaredevil.solarthing.actions.rover.RoverBoostVoltageActionNode;
import me.retrodaredevil.solarthing.actions.rover.RoverLoadActionNode;
import me.retrodaredevil.solarthing.actions.rover.modbus.RoverModbusActionNode;
import me.retrodaredevil.solarthing.actions.solcast.SolcastActionNode;
Expand Down Expand Up @@ -51,7 +50,8 @@ public static ObjectMapper registerActionNodes(ObjectMapper objectMapper) {
RoverModbusActionNode.class,
RoverLoadActionNode.class,
RoverBoostSetActionNode.class,
RoverBoostVoltageActionNode.class,
// RoverBoostVoltageActionNode commented out until we confirm that replacing it works as intended
// RoverBoostVoltageActionNode.class,

TracerModbusActionNode.class,
TracerLoadActionNode.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* some properties may not be useful.
* Usually many of these properties become more useful for the shorter periods of time that are focused on.
*
* @param identifier
* @param averageChargerWattage
* @param averageBuyWattage
* @param identifier TODO
* @param averageChargerWattage TODO
* @param averageBuyWattage TODO
* @deprecated Not yet implemented
*/
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class RequestMain {
public static int startRequestProgram(RequestProgramOptions options, boolean isValidate) throws Exception {
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Beginning request program");
AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled());
return startRequestProgram(options, analyticsManager, options.getPeriod(), options.getMinimumWait(), isValidate);
return startRequestProgram(options, isValidate, analyticsManager, options.getPeriod(), options.getMinimumWait());
}

private static int startRequestProgram(RequestProgramOptions options, AnalyticsManager analyticsManager, Duration period, Duration minimumWait, boolean isValidate) throws Exception {
private static int startRequestProgram(RequestProgramOptions options, boolean isValidate, AnalyticsManager analyticsManager, Duration period, Duration minimumWait) throws Exception {
// Note this is very similar to code in OutbackMateMain and could eventually be refactored
EnvironmentUpdater[] environmentUpdaterReference = new EnvironmentUpdater[1];
PacketHandlerInit.Result handlersResult = PacketHandlerInit.initHandlers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class CustomWmfCouchDbEdit20240318 {
);
private final CouchDbInstance instance;
private final PrintStream out;
@SuppressWarnings("FieldCanBeLocal") // we suppress this warning because this class should probably be removed at some point
private final CouchDbSetupMain.Prompt prompt;

public CustomWmfCouchDbEdit20240318(CouchDbInstance instance, PrintStream out, CouchDbSetupMain.Prompt prompt) {
Expand Down
21 changes: 21 additions & 0 deletions config_templates/actions-ns/equality_test.ns
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
queue {
race {
racer(all : eq(const(5), const(5.0))) : print("5 = 5.0!")
racer(pass) : print("This should not happen!")
}
race {
racer(all : eq(const(4), const(5.0))) : print("4 = 5.0! (bad)")
racer(all : not : eq(const(4), const(5.0))) : print("4 != 5.0!")
racer(pass) : print("This should not happen!")
}

race {
racer(all : eq(const("hello"), const hello)) : print("hello = hello")
racer(pass) : print("This should not happen!")
}
race {
racer(all : eq(const("Hello"), const hello)) : print("Hello = hello (bad)")
racer(all : not : eq(const("Hello"), const hello)) : print("Hello != hello")
racer(pass) : print("This should not happen!")
}
}

0 comments on commit b49393b

Please sign in to comment.