diff --git a/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java b/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java index 0dcab51..37aeefe 100644 --- a/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java +++ b/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java @@ -146,12 +146,12 @@ int h = 30; ResourcePath res = resNode.getResourceIdentifier(); Object resource = graph.insertVertex(parent, null, - res.getLeafResourceName(), x, y, w, h, - "shape=ellipse;perimeter=ellipsePerimeter"); // insert a resource as a vertex + res.toString(), x, y, w, h, + "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); // insert a resource as a vertex resources.put(resNode, resource); createChildSimulateResourceVerticies(resource, resNode, resources, w, h); x+=80; - } + } } finally { graph.getModel().endUpdate(); @@ -168,12 +168,16 @@ for (Resource childNode: resNode.getChildren()) { ResourcePath childRes = childNode.getResourceIdentifier(); Object childResource = graph.insertVertex(resource, null, - childRes.getName(), 0, 0, w/2, h/2, - "shape=ellipse;perimeter=ellipsePerimeter"); // insert a resource as a vertex + childRes.toString(), 0, 0, w/2, h/2, + "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); // insert a resource as a vertex resources.put(childNode, childResource); createChildSimulateResourceVerticies(childResource, childNode, resources, w, h); } } + if (resNode.getChildren() == null || resNode.getChildren().size() == 0) { + Object state = graph.insertVertex(resource, null, + resNode.getState().getValue().toString(), 0.5, 0.5, 0.25, 0.25, "opacity=0;verticalAlign=down;", true); // insert a state label as a vertex + } } public mxGraphComponent getGraphComponent() { diff --git a/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java b/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java index b5c491f..91f1dae 100644 --- a/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java +++ b/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java @@ -107,12 +107,14 @@ ArrayList eventChs = new ArrayList<>();//iOchannelList ArrayList messages = new ArrayList<>();//ADLmessage ArrayList eventMessages = new ArrayList<>();//messageList + ResourcePath eventResPath = null; for(Channel ch : simulator.getModel().getIOChannels()) {//all channel if(((DataTransferChannel)ch).getInputResources().size()== 0) {//ioch Or normalch for(ChannelMember out: ((DataTransferChannel)ch).getOutputChannelMembers()) { ResourcePath resPath = out.getResource(); if(resId.isInstanceOf(resPath)) {//account.uid == acounts.123 + eventResPath = resPath; eventChs.add(((DataTransferChannel)ch)); String message = null; Expression mesExp = out.getStateTransition().getMessageExpression();//sync(a,b) @@ -150,8 +152,8 @@ Parser parser = new Parser(stream); stream.addLine(textArea.getText()); Expression eventMessage = parser.parseTerm(stream,simulator.getModel()); - - Event newEvent = new Event(eventChs.get(0), eventMessage, resId, simulator.getCurState().getResource(resId)); + + Event newEvent = new Event(eventChs.get(0), eventMessage, eventResPath, simulator.getCurState().getResource(resId)); simulator.transition(newEvent); constructNextSimulateGraph(simulator.getCurState().getRootResources(), simulator.getModel(),simulator.getModel().getDataFlowGraph(), graph); @@ -202,13 +204,13 @@ int h = 150; ResourcePath res = resNode.getResourceIdentifier(); Object resource = nextGraph.insertVertex(parent, null, - res.getLeafResourceName(), x, y, w, h, - "shape=ellipse;perimeter=ellipsePerimeter;"); // insert a resource as a vertex + res.toString(), x, y, w, h, + "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); // insert a resource as a vertex resources.put(resNode, resource); createNextChildSimulateResourceVerticies(resource, resNode, resources, w, h); x+=400; - } + } } finally { nextGraph.getModel().endUpdate(); @@ -225,13 +227,17 @@ int i = 1; ResourcePath childRes = childNode.getResourceIdentifier(); Object childResource = graph.insertVertex(resource, null, - childRes.getName(), x/i, y, w/(i+1), h/(i+2), - "shape=ellipse;perimeter=ellipsePerimeter;"); // insert a resource as a vertex + childRes.toString(), x/i, y, w/(i+1), h/(i+2), + "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); // insert a resource as a vertex resources.put(childNode, childResource); i++; createNextChildSimulateResourceVerticies(childResource, childNode, resources, w, h); } } + if (resNode.getChildren() == null || resNode.getChildren().size() == 0) { + Object state = graph.insertVertex(resource, null, + resNode.getState().getValue().toString(), 0.5, 0.5, 0.25, 0.25, "opacity=0;verticalAlign=down;", true); // insert a state label as a vertex + } } @Override diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java index dcdba9f..047f906 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java @@ -1,5 +1,7 @@ package models.algebra; +import java.util.List; + public class Symbol { protected String name; protected String implName; @@ -10,6 +12,7 @@ protected models.algebra.Type[] signature = null; protected int[] implParamOrder = null; protected IImplGenerator generator = null; + protected ICalculator calculator = null; public Symbol(String name) { this.name = name; @@ -66,6 +69,37 @@ } } + public Symbol(String name, int arity, ICalculator calculator) { + this.name = name; + this.arity = arity; + this.calculator = calculator; + } + + public Symbol(String name, int arity, Type operatorType, ICalculator calculator) { + this.name = name; + this.arity = arity; + this.operatorType = operatorType; + this.calculator = calculator; + } + + public Symbol(String name, int arity, Type operatorType, IImplGenerator generator, ICalculator calculator) { + this.name = name; + this.arity = arity; + this.operatorType = operatorType; + this.generator = generator; + this.implOperatorType = Type.GENERATIVE; + this.calculator = calculator; + } + + public Symbol(String name, int arity, Type operatorType, String implName, Type implOperatorType, ICalculator calculator) { + this.name = name; + this.implName = implName; + this.arity = arity; + this.operatorType = operatorType; + this.implOperatorType = implOperatorType; + this.calculator = calculator; + } + public void setArity(int arity) { this.arity = arity; } @@ -171,6 +205,17 @@ } return null; } + + public boolean isCalculatable() { + return (calculator != null); + } + + public Constant calculate(List args) { + if (calculator != null) { + return calculator.calculate(args); + } + return null; + } public boolean equals(Object another) { if (!(another instanceof Symbol)) return false; @@ -232,4 +277,8 @@ */ public String generate(models.algebra.Type type, models.algebra.Type[] childrenTypes, String children[], String[] childrenSideEffects, String[] sideEffect); } + + public interface ICalculator { + public Constant calculate(List args); + } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java index 52d000e..b326caa 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java @@ -149,6 +149,18 @@ } else { return new Term(newSymbol, newChildren); } + } else if (symbol.isCalculatable()) { + List newChildren = new ArrayList<>(); + for (Expression child: children) { + if (child instanceof Term) { + child = ((Term) child).reduce(); + } + if (!(child instanceof Constant)) { + return this; + } + newChildren.add((Constant) child); + } + return symbol.calculate(newChildren); } else { // Calculate inverse map List newChildren = new ArrayList<>(); diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java index f72c44a..3f8a50e 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.List; +import models.algebra.Constant; import models.algebra.Expression; import models.algebra.LambdaAbstraction; import models.algebra.Symbol; @@ -36,11 +37,90 @@ public static final Type typePairDouble = new Type("Pair", "Pair", "Pair", typePair); public static final Type typeMap = new Type("Map", "HashMap<>", "Map"); public static final JsonType typeJson = new JsonType("Json", "HashMap<>", "Map"); - public static final Symbol add = new Symbol(Parser.ADD, 2, Symbol.Type.INFIX); - public static final Symbol mul = new Symbol(Parser.MUL, 2, Symbol.Type.INFIX); - public static final Symbol sub = new Symbol(Parser.SUB, 2, Symbol.Type.INFIX); - public static final Symbol div = new Symbol(Parser.DIV, 2, Symbol.Type.INFIX); - public static final Symbol minus = new Symbol(Parser.MINUS, 1); + public static final Symbol add = new Symbol(Parser.ADD, 2, Symbol.Type.INFIX, new Symbol.ICalculator() { + @Override + public Constant calculate(List args) { + String arg0 = args.get(0).getSymbol().toString(); + String arg1 = args.get(1).getSymbol().toString(); + if (args.get(0).getType().equals(typeDouble) || args.get(1).getType().equals(typeDouble)) { + return new Constant(Double.toString(Double.parseDouble(arg0) + Double.parseDouble(arg1)), typeDouble); + } else if (args.get(0).getType().equals(typeFloat) || args.get(1).getType().equals(typeFloat)) { + return new Constant(Float.toString(Float.parseFloat(arg0) + Float.parseFloat(arg1)), typeFloat); + } else if (args.get(0).getType().equals(typeLong) || args.get(1).getType().equals(typeLong)) { + return new Constant(Long.toString(Long.parseLong(arg0) + Long.parseLong(arg1)), typeLong); + } else if (args.get(0).getType().equals(typeInt) || args.get(1).getType().equals(typeInt)) { + return new Constant(Integer.toString(Integer.parseInt(arg0) + Integer.parseInt(arg1)), typeInt); + } + return new Constant(Integer.toString(Integer.parseInt(arg0) + Integer.parseInt(arg1))); + } + }); + public static final Symbol mul = new Symbol(Parser.MUL, 2, Symbol.Type.INFIX, new Symbol.ICalculator() { + @Override + public Constant calculate(List args) { + String arg0 = args.get(0).getSymbol().toString(); + String arg1 = args.get(1).getSymbol().toString(); + if (args.get(0).getType().equals(typeDouble) || args.get(1).getType().equals(typeDouble)) { + return new Constant(Double.toString(Double.parseDouble(arg0) * Double.parseDouble(arg1)), typeDouble); + } else if (args.get(0).getType().equals(typeFloat) || args.get(1).getType().equals(typeFloat)) { + return new Constant(Float.toString(Float.parseFloat(arg0) * Float.parseFloat(arg1)), typeFloat); + } else if (args.get(0).getType().equals(typeLong) || args.get(1).getType().equals(typeLong)) { + return new Constant(Long.toString(Long.parseLong(arg0) * Long.parseLong(arg1)), typeLong); + } else if (args.get(0).getType().equals(typeInt) || args.get(1).getType().equals(typeInt)) { + return new Constant(Integer.toString(Integer.parseInt(arg0) * Integer.parseInt(arg1)), typeInt); + } + return new Constant(Integer.toString(Integer.parseInt(arg0) * Integer.parseInt(arg1))); + } + }); + public static final Symbol sub = new Symbol(Parser.SUB, 2, Symbol.Type.INFIX, new Symbol.ICalculator() { + @Override + public Constant calculate(List args) { + String arg0 = args.get(0).getSymbol().toString(); + String arg1 = args.get(1).getSymbol().toString(); + if (args.get(0).getType().equals(typeDouble) || args.get(1).getType().equals(typeDouble)) { + return new Constant(Double.toString(Double.parseDouble(arg0) - Double.parseDouble(arg1)), typeDouble); + } else if (args.get(0).getType().equals(typeFloat) || args.get(1).getType().equals(typeFloat)) { + return new Constant(Float.toString(Float.parseFloat(arg0) - Float.parseFloat(arg1)), typeFloat); + } else if (args.get(0).getType().equals(typeLong) || args.get(1).getType().equals(typeLong)) { + return new Constant(Long.toString(Long.parseLong(arg0) - Long.parseLong(arg1)), typeLong); + } else if (args.get(0).getType().equals(typeInt) || args.get(1).getType().equals(typeInt)) { + return new Constant(Integer.toString(Integer.parseInt(arg0) - Integer.parseInt(arg1)), typeInt); + } + return new Constant(Integer.toString(Integer.parseInt(arg0) - Integer.parseInt(arg1))); + } + }); + public static final Symbol div = new Symbol(Parser.DIV, 2, Symbol.Type.INFIX, new Symbol.ICalculator() { + @Override + public Constant calculate(List args) { + String arg0 = args.get(0).getSymbol().toString(); + String arg1 = args.get(1).getSymbol().toString(); + if (args.get(0).getType().equals(typeDouble) || args.get(1).getType().equals(typeDouble)) { + return new Constant(Double.toString(Double.parseDouble(arg0) / Double.parseDouble(arg1)), typeDouble); + } else if (args.get(0).getType().equals(typeFloat) || args.get(1).getType().equals(typeFloat)) { + return new Constant(Float.toString(Float.parseFloat(arg0) / Float.parseFloat(arg1)), typeFloat); + } else if (args.get(0).getType().equals(typeLong) || args.get(1).getType().equals(typeLong)) { + return new Constant(Long.toString(Long.parseLong(arg0) / Long.parseLong(arg1)), typeLong); + } else if (args.get(0).getType().equals(typeInt) || args.get(1).getType().equals(typeInt)) { + return new Constant(Integer.toString(Integer.parseInt(arg0) / Integer.parseInt(arg1)), typeInt); + } + return new Constant(Integer.toString(Integer.parseInt(arg0) / Integer.parseInt(arg1))); + } + }); + public static final Symbol minus = new Symbol(Parser.MINUS, 1, new Symbol.ICalculator() { + @Override + public Constant calculate(List args) { + String arg = args.get(0).getSymbol().toString(); + if (args.get(0).getType().equals(typeDouble)) { + return new Constant(Double.toString(-Double.parseDouble(arg)), typeDouble); + } else if (args.get(0).getType().equals(typeFloat)) { + return new Constant(Float.toString(-Float.parseFloat(arg)), typeFloat); + } else if (args.get(0).getType().equals(typeLong)) { + return new Constant(Long.toString(-Long.parseLong(arg)), typeLong); + } else if (args.get(0).getType().equals(typeInt)) { + return new Constant(Integer.toString(-Integer.parseInt(arg)), typeInt); + } + return new Constant(Integer.toString(-Integer.parseInt(arg))); + } + }); public static final Symbol cons = new Symbol("cons", 2, Symbol.Type.PREFIX, "($x,$y)->$x.add(0, $y)", Symbol.Type.LAMBDA_WITH_SIDE_EFFECT, new int[] {1, 0}); public static final Symbol append = new Symbol("append", 2, Symbol.Type.PREFIX, "add", Symbol.Type.METHOD_WITH_SIDE_EFFECT); public static final Symbol remove = new Symbol("remove", 2, Symbol.Type.PREFIX, "remove", Symbol.Type.METHOD_WITH_SIDE_EFFECT); @@ -108,10 +188,32 @@ count[0]++; return temp; } + }, new Symbol.ICalculator() { + @Override + public Constant calculate(List args) { + if (args.get(0).getSymbol().equals(true_)) { + return args.get(1); + } else if (args.get(0).getSymbol().equals(false_)) { + return args.get(2); + } + return null; + } }); - public static final Symbol mod = new Symbol("mod", 2, Symbol.Type.PREFIX, "%", Symbol.Type.INFIX); + public static final Symbol mod = new Symbol("mod", 2, Symbol.Type.PREFIX, "%", Symbol.Type.INFIX, new Symbol.ICalculator() { + @Override + public Constant calculate(List args) { + String arg0 = args.get(0).getSymbol().toString(); + String arg1 = args.get(1).getSymbol().toString(); + if (args.get(0).getType().equals(typeLong) || args.get(1).getType().equals(typeLong)) { + return new Constant(Long.toString(Long.parseLong(arg0) % Long.parseLong(arg1)), typeLong); + } else if (args.get(0).getType().equals(typeInt) || args.get(1).getType().equals(typeInt)) { + return new Constant(Integer.toString(Integer.parseInt(arg0) % Integer.parseInt(arg1)), typeInt); + } + return new Constant(Integer.toString(Integer.parseInt(arg0) % Integer.parseInt(arg1))); + } + }); public static final Symbol eq = new Symbol("eq", 2, Symbol.Type.PREFIX, "==", Symbol.Type.INFIX); public static final Symbol neq = new Symbol("neq", 2, Symbol.Type.PREFIX, "!=", Symbol.Type.INFIX); public static final Symbol gt = new Symbol("gt", 2, Symbol.Type.PREFIX, ">", Symbol.Type.INFIX); diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/Resource.java b/AlgebraicDataflowArchitectureModel/src/simulator/Resource.java index 5f7e67d..665d4a9 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/Resource.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/Resource.java @@ -89,6 +89,10 @@ public ResourceState getState() { return state; } + + public void changeState(ResourceState state) { + this.state = state; + } public Constant getParameter() { return parameter; @@ -99,22 +103,26 @@ if (resourceHierarchy.getChildren().size() > 0) { children = new ArrayList<>(); ResourceHierarchy childRes = resourceHierarchy.getChildren().iterator().next(); - Map childStates = ((CompositeResourceState) state).getChildStates(); if (childRes.getNumParameters() > 0) { // List or Map type. - for (Map.Entry childEnt: childStates.entrySet()) { - String childParam = childEnt.getKey(); - ResourceState childState = childEnt.getValue(); - Type thisType = resourceHierarchy.getResourceStateType(); - if (DataConstraintModel.typeList.isAncestorOf(thisType)) { - children.add(new Resource(childRes, this, new Constant(childParam, DataConstraintModel.typeInt), childState)); - } else if (DataConstraintModel.typeMap.isAncestorOf(thisType)) { - children.add(new Resource(childRes, this, new Constant(childParam, DataConstraintModel.typeString), childState)); + if (state instanceof CompositeResourceState) { + // If the list or map is not nil. + Map childStates = ((CompositeResourceState) state).getChildStates(); + for (Map.Entry childEnt: childStates.entrySet()) { + String childParam = childEnt.getKey(); + ResourceState childState = childEnt.getValue(); + Type thisType = resourceHierarchy.getResourceStateType(); + if (DataConstraintModel.typeList.isAncestorOf(thisType)) { + children.add(new Resource(childRes, this, new Constant(childParam, DataConstraintModel.typeInt), childState)); + } else if (DataConstraintModel.typeMap.isAncestorOf(thisType)) { + children.add(new Resource(childRes, this, new Constant(childParam, DataConstraintModel.typeString), childState)); + } } } } else { // Json type. if (this.children == null) { + Map childStates = ((CompositeResourceState) state).getChildStates(); for (Map.Entry childEnt: childStates.entrySet()) { String childParam = childEnt.getKey(); ResourceState childState = childEnt.getValue(); diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java b/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java index 9e331e5..494ac79 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java @@ -13,7 +13,9 @@ import models.algebra.Term; import models.algebra.Type; import models.dataConstraintModel.DataConstraintModel; +import models.dataConstraintModel.ResourceHierarchy; import models.dataFlowModel.DataTransferChannel; +import simulator.states.CompositeResourceState; import simulator.states.JsonResourceState; import simulator.states.ListResourceState; import simulator.states.MapResourceState; @@ -66,10 +68,27 @@ if (listValue.getSymbol().equals(DataConstraintModel.append)) { Resource res = getResource(resourceIdentifier); ResourceState state = res.getState(); - Expression childExp = new Constant(Integer.toString(((ListResourceState) state).getChildStates().size())); - ResourceIdentifier childResourceIdentifier = new ResourceIdentifier(resourceIdentifier, - childExp, - resourceIdentifier.getResourceHierarchy().getChildren().iterator().next()); + Expression childExp = null; + if (state instanceof ListResourceState) { + childExp = new Constant(Integer.toString(((ListResourceState) state).getChildStates().size())); + } else if (state instanceof PrimitiveResourceState && ((PrimitiveResourceState) state).getValue().getSymbol().equals(DataConstraintModel.nil)) { + // If the value of state is nil. + childExp = new Constant("0"); + ResourceState parentState = res.getParent().getState(); + ResourceState newState = new ListResourceState(); + if (parentState instanceof CompositeResourceState) { + ((CompositeResourceState) parentState).replaceChildState(state, newState); + } + state = newState; + res.changeState(state); + } + ResourceHierarchy childResourceHierarchy = null; + if (resourceIdentifier.getResourceHierarchy().getChildren() != null && resourceIdentifier.getResourceHierarchy().getChildren().size() > 0) { + childResourceHierarchy = resourceIdentifier.getResourceHierarchy().getChildren().iterator().next(); + } else { + childResourceHierarchy = new ResourceHierarchy(resourceIdentifier.getResourceHierarchy(), 1); + } + ResourceIdentifier childResourceIdentifier = new ResourceIdentifier(resourceIdentifier, childExp, childResourceHierarchy); Map.Entry childInfo = createResourceState(childResourceIdentifier, listValue.getChild(1)); ((ListResourceState) state).addChildState(childInfo.getKey()); return childInfo.getValue(); @@ -84,9 +103,23 @@ if (childExp instanceof Constant) { Resource res = getResource(resourceIdentifier); ResourceState state = res.getState(); - ResourceIdentifier childResourceIdentifier = new ResourceIdentifier(resourceIdentifier, - childExp, - resourceIdentifier.getResourceHierarchy().getChildren().iterator().next()); + if (state instanceof PrimitiveResourceState && ((PrimitiveResourceState) state).getValue().getSymbol().equals(DataConstraintModel.nil)) { + // If the value of state is nil. + ResourceState parentState = res.getParent().getState(); + ResourceState newState = new MapResourceState(); + if (parentState instanceof CompositeResourceState) { + ((CompositeResourceState) parentState).replaceChildState(state, newState); + } + state = newState; + res.changeState(state); + } + ResourceHierarchy childResourceHierarchy = null; + if (resourceIdentifier.getResourceHierarchy().getChildren() != null && resourceIdentifier.getResourceHierarchy().getChildren().size() > 0) { + childResourceHierarchy = resourceIdentifier.getResourceHierarchy().getChildren().iterator().next(); + } else { + childResourceHierarchy = new ResourceHierarchy(resourceIdentifier.getResourceHierarchy(), 1); + } + ResourceIdentifier childResourceIdentifier = new ResourceIdentifier(resourceIdentifier, childExp, childResourceHierarchy); String childId = ((Constant) childExp).toString(); Map.Entry childInfo = createResourceState(childResourceIdentifier, mapValue.getChild(2)); ((MapResourceState) state).addChildState(childId, childInfo.getKey()); @@ -114,6 +147,9 @@ } } } else { + if (resStateValue instanceof Term) { + resStateValue = ((Term) resStateValue).reduce(); + } if (resStateValue instanceof Constant) { Resource res = getResource(resourceIdentifier); ResourceState state = null; diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/states/CompositeResourceState.java b/AlgebraicDataflowArchitectureModel/src/simulator/states/CompositeResourceState.java index 3cc29ca..5e6311f 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/states/CompositeResourceState.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/states/CompositeResourceState.java @@ -2,11 +2,13 @@ import java.util.Map; -abstract public class CompositeResourceState extends ResourceState { +public abstract class CompositeResourceState extends ResourceState { @Override boolean hasChildren() { return true; } - abstract public Map getChildStates(); + public abstract Map getChildStates(); + + public abstract void replaceChildState(ResourceState state, ResourceState newState); } diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/states/JsonResourceState.java b/AlgebraicDataflowArchitectureModel/src/simulator/states/JsonResourceState.java index 327da4f..afd1c20 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/states/JsonResourceState.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/states/JsonResourceState.java @@ -33,4 +33,13 @@ public void addChildState(String param, ResourceState childState) { children.put(param, childState); } + + @Override + public void replaceChildState(ResourceState state, ResourceState newState) { + for (Map.Entry childEnt: children.entrySet()) { + if (childEnt.getValue().equals(state)) { + children.put(childEnt.getKey(), newState); + } + } + } } diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/states/ListResourceState.java b/AlgebraicDataflowArchitectureModel/src/simulator/states/ListResourceState.java index e49bd72..83367e0 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/states/ListResourceState.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/states/ListResourceState.java @@ -29,13 +29,23 @@ @Override public Map getChildStates() { Map childParams = new HashMap<>(); - for (int i = 1; i <= children.size(); i++) { + for (int i = 0; i < children.size(); i++) { childParams.put(Integer.toString(i), children.get(i)); } - return null; + return childParams; } public void addChildState(ResourceState childState) { children.add(childState); } + + + @Override + public void replaceChildState(ResourceState state, ResourceState newState) { + for (int i = 0; i < children.size(); i++) { + if (children.get(i).equals(state)) { + children.set(i, newState); + } + } + } } diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/states/MapResourceState.java b/AlgebraicDataflowArchitectureModel/src/simulator/states/MapResourceState.java index 1f36406..5bd2fc7 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/states/MapResourceState.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/states/MapResourceState.java @@ -33,4 +33,13 @@ public void addChildState(String param, ResourceState childState) { children.put(param, childState); } + + @Override + public void replaceChildState(ResourceState state, ResourceState newState) { + for (Map.Entry childEnt: children.entrySet()) { + if (childEnt.getValue().equals(state)) { + children.put(childEnt.getKey(), newState); + } + } + } }