package simulator.states; import java.util.HashMap; import java.util.Map; import models.algebra.Constant; import models.algebra.Expression; import models.algebra.Term; import models.dataConstraintModel.DataConstraintModel; public class MapResourceState extends CompositeResourceState { private Map<String, ResourceState> children = new HashMap<>(); @Override public Expression getValue() { Term value = new Constant(DataConstraintModel.nil); value.setType(DataConstraintModel.typeMap); for (Map.Entry<String, ResourceState> childEnt: children.entrySet()) { Term newTerm = new Term(DataConstraintModel.insert); newTerm.addChild(value); newTerm.addChild(new Constant(childEnt.getKey(), DataConstraintModel.typeString)); newTerm.addChild(childEnt.getValue().getValue()); value = newTerm; } return value; } @Override public Map<String, ResourceState> getChildStates() { return children; } public void addChildState(String param, ResourceState childState) { children.put(param, childState); } @Override public void replaceChildState(ResourceState state, ResourceState newState) { for (Map.Entry<String, ResourceState> childEnt: children.entrySet()) { if (childEnt.getValue().equals(state)) { children.put(childEnt.getKey(), newState); } } } }