Commit 35f07285 authored by Alexander Weigl's avatar Alexander Weigl

magic variables for rule command

parent 26e50bd5
Pipeline #22014 passed with stages
in 5 minutes and 37 seconds
...@@ -41,6 +41,8 @@ import org.antlr.v4.runtime.Token; ...@@ -41,6 +41,8 @@ import org.antlr.v4.runtime.Token;
@RequiredArgsConstructor @RequiredArgsConstructor
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public class Variable extends Literal implements Comparable<Variable> { public class Variable extends Literal implements Comparable<Variable> {
public static final String MAGIC_PREFIX = "#";
@NonNull private String identifier; @NonNull private String identifier;
public Variable(Token variable) { public Variable(Token variable) {
......
...@@ -24,6 +24,7 @@ import edu.kit.iti.formal.psdbg.interpreter.data.State; ...@@ -24,6 +24,7 @@ import edu.kit.iti.formal.psdbg.interpreter.data.State;
import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment; import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.interpreter.exceptions.ScriptCommandNotApplicableException; import edu.kit.iti.formal.psdbg.interpreter.exceptions.ScriptCommandNotApplicableException;
import edu.kit.iti.formal.psdbg.parser.ast.CallStatement; import edu.kit.iti.formal.psdbg.parser.ast.CallStatement;
import edu.kit.iti.formal.psdbg.parser.ast.Variable;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
...@@ -41,9 +42,11 @@ import java.util.Set; ...@@ -41,9 +42,11 @@ import java.util.Set;
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
public class RuleCommandHandler implements CommandHandler<KeyData> { public class RuleCommandHandler implements CommandHandler<KeyData> {
public static final String[] MAGIC_PARAMETER_NAMES = new String[]{
"on", "formula"
};
private static final Logger LOGGER = LogManager.getLogger(RuleCommandHandler.class); private static final Logger LOGGER = LogManager.getLogger(RuleCommandHandler.class);
@Getter @Getter
private final Map<String, Rule> rules; private final Map<String, Rule> rules;
...@@ -85,15 +88,15 @@ public class RuleCommandHandler implements CommandHandler<KeyData> { ...@@ -85,15 +88,15 @@ public class RuleCommandHandler implements CommandHandler<KeyData> {
@Override @Override
public boolean handles(CallStatement call, KeyData data) throws IllegalArgumentException { public boolean handles(CallStatement call, KeyData data) throws IllegalArgumentException {
if (rules.containsKey(call.getCommand())) return true;//static/rigid rules if (rules.containsKey(call.getCommand())) return true;//static/rigid rules
try{ try {
if (data != null) { if (data != null) {
Goal goal = data.getGoal(); Goal goal = data.getGoal();
Set<String> rules = findTaclets(data.getProof(), goal); Set<String> rules = findTaclets(data.getProof(), goal);
return rules.contains(call.getCommand()); return rules.contains(call.getCommand());
} }
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
System.out.println("npe = " + npe); System.out.println("npe = " + npe);
return false; return false;
} }
return false; return false;
} }
...@@ -111,7 +114,8 @@ public class RuleCommandHandler implements CommandHandler<KeyData> { ...@@ -111,7 +114,8 @@ public class RuleCommandHandler implements CommandHandler<KeyData> {
State<KeyData> state = interpreter.getCurrentState(); State<KeyData> state = interpreter.getCurrentState();
GoalNode<KeyData> expandedNode = state.getSelectedGoalNode(); GoalNode<KeyData> expandedNode = state.getSelectedGoalNode();
KeyData kd = expandedNode.getData(); KeyData kd = expandedNode.getData();
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = createParameters(expandedNode.getAssignments());
params.asMap().forEach((k, v) -> map.put(k.getIdentifier(), v.getData())); params.asMap().forEach((k, v) -> map.put(k.getIdentifier(), v.getData()));
LOGGER.info("Execute {} with {}", call, map); LOGGER.info("Execute {} with {}", call, map);
try { try {
...@@ -147,4 +151,12 @@ public class RuleCommandHandler implements CommandHandler<KeyData> { ...@@ -147,4 +151,12 @@ public class RuleCommandHandler implements CommandHandler<KeyData> {
} }
} }
private Map<String, Object> createParameters(VariableAssignment assignments) {
Map<String, Object> params = new HashMap<>();
for (String s : MAGIC_PARAMETER_NAMES) {
params.put(s, assignments.getValue(new Variable(Variable.MAGIC_PREFIX + s)));
}
return params;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment