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 models.dataConstraintModel.ResourcePath; 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 ResourceIdentifier getResourceIdentifier() { ResourceIdentifier resId = null; if (parent != null) { ResourceIdentifier parentResId = parent.getResourceIdentifier(); if (resourceHierarchy.getNumParameters() == 0) { resId = new ResourceIdentifier(parentResId, resourceHierarchy.getResourceName(), resourceHierarchy); } else { resId = new ResourceIdentifier(parentResId, parameter, resourceHierarchy); } } else { resId = new ResourceIdentifier(resourceHierarchy.getResourceName(), resourceHierarchy); } return resId; } public ResourceState getState() { return state; } public Constant getParameter() { return parameter; } 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 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) { for (Map.Entry<String, ResourceState> childEnt: childStates.entrySet()) { String childParam = childEnt.getKey(); ResourceState childState = childEnt.getValue(); Type thisType = resourceHierarchy.getResourceStateType(); if (DataConstraintModel.typeJson.isAncestorOf(thisType)) { children.add(new Resource(childRes, this, new Constant(childParam, DataConstraintModel.typeString), childState)); } } this.children = children; } return this.children; } } return children; } public Resource getDescendant(ResourceIdentifier resId) { if (this.getResourceIdentifier().equals(resId)) return this; if (!resId.startsWith(this.getResourceIdentifier())) return null; for (Resource child: getChildren()) { Resource res = child.getDescendant(resId); if (res != null) return res; } return null; } public Resource getDescendant(String resId) { if (this.getResourceIdentifier().toString().equals(resId)) return this; if (!resId.startsWith(this.getResourceIdentifier().toString())) return null; for (Resource child: getChildren()) { Resource res = child.getDescendant(resId); if (res != null) return res; } return null; } }