| | package simulator; |
---|
| | |
---|
| | import java.util.ArrayList; |
---|
| | import java.util.List; |
---|
| | import java.util.Map; |
---|
| | |
---|
| | import models.algebra.Constant; |
---|
| | import models.algebra.Type; |
---|
| | import models.dataConstraintModel.DataConstraintModel; |
---|
| | import models.dataConstraintModel.ResourceHierarchy; |
---|
| | import simulator.states.*; |
---|
| | |
---|
| | public class Resource { |
---|
| | private Resource parent = null; |
---|
| | private List<Resource> children = null; |
---|
| | private ResourceHierarchy resourceHierarchy = null; |
---|
| | private ResourceState state = null; |
---|
| | private Constant parameter = null; |
---|
| | |
---|
| | public Resource(ResourceHierarchy resourceHierarchy) { |
---|
| | this.resourceHierarchy = resourceHierarchy; |
---|
| | if (resourceHierarchy.getChildren().size() > 0) { |
---|
| | ResourceHierarchy childRes = resourceHierarchy.getChildren().iterator().next(); |
---|
| | if (childRes.getNumParameters() > 0) { |
---|
| | Type resType = resourceHierarchy.getResourceStateType(); |
---|
| | if (DataConstraintModel.typeList.isAncestorOf(resType)) { |
---|
| | // List resource |
---|
| | state = new ListResourceState(); |
---|
| | } else if (DataConstraintModel.typeMap.isAncestorOf(resType)) { |
---|
| | // Map resource |
---|
| | state = new MapResourceState(); |
---|
| | } |
---|
| | } else { |
---|
| | // Json resource |
---|
| | state = new JsonResourceState(); |
---|
| | children = new ArrayList<>(); |
---|
| | for (ResourceHierarchy child: resourceHierarchy.getChildren()) { |
---|
| | Resource cRes = new Resource(child); |
---|
| | ((JsonResourceState) state).addChildState(child.getResourceName(), cRes.getState()); |
---|
| | children.add(cRes); |
---|
| | } |
---|
| | } |
---|
| | } else { |
---|
| | state = new PrimitiveResourceState((Constant) resourceHierarchy.getInitialValue()); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | public Resource(ResourceHierarchy resourceHierarchy, Resource parent, ResourceState state) { |
---|
| | this.resourceHierarchy = resourceHierarchy; |
---|
| | this.parent = parent; |
---|
| | this.state = state; |
---|
| | } |
---|
| | |
---|
| | public Resource(ResourceHierarchy resourceHierarchy, Resource parent, Constant parameter, ResourceState state) { |
---|
| | this.resourceHierarchy = resourceHierarchy; |
---|
| | this.parent = parent; |
---|
| | this.parameter = parameter; |
---|
| | this.state = state; |
---|
| | } |
---|
| | |
---|
| | public Resource getParent() { |
---|
| | return parent; |
---|
| | } |
---|
| | |
---|
| | public void setParent(Resource parent) { |
---|
| | this.parent = parent; |
---|
| | } |
---|
| | |
---|
| | public ResourceHierarchy getResourceHierarchy() { |
---|
| | return resourceHierarchy; |
---|
| | } |
---|
| | |
---|
| | public ResourceState getState() { |
---|
| | return state; |
---|
| | } |
---|
| | |
---|
| | public List<Resource> getChildren() { |
---|
| | List<Resource> children = null; |
---|
| | if (resourceHierarchy.getChildren().size() > 0) { |
---|
| | children = new ArrayList<>(); |
---|
| | ResourceHierarchy childRes = resourceHierarchy.getChildren().iterator().next(); |
---|
| | Map<String, ResourceState> childStates = ((CompositeResourceState) state).getChildStates(); |
---|
| | if (childRes.getNumParameters() > 0) { |
---|
| | // List or Map type. |
---|
| | for (Map.Entry<String, ResourceState> childEnt: childStates.entrySet()) { |
---|
| | String childParam = childEnt.getKey(); |
---|
| | ResourceState childState = childEnt.getValue(); |
---|
| | Type childType = childRes.getResourceStateType(); |
---|
| | if (DataConstraintModel.typeList.isAncestorOf(childType)) { |
---|
| | children.add(new Resource(childRes, this, new Constant(childParam, DataConstraintModel.typeInt), childState)); |
---|
| | } else if (DataConstraintModel.typeMap.isAncestorOf(childType)) { |
---|
| | children.add(new Resource(childRes, this, new Constant(childParam, DataConstraintModel.typeString), childState)); |
---|
| | } |
---|
| | } |
---|
| | } else { |
---|
| | // Json type. |
---|
| | return this.children; |
---|
| | } |
---|
| | } |
---|
| | return children; |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |