Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / simulator / states / ListResourceState.java
package simulator.states;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import models.algebra.Constant;
import models.algebra.Expression;
import models.algebra.Term;
import models.dataConstraintModel.DataConstraintModel;
import models.dataConstraintModel.ListTerm;

public class ListResourceState extends CompositeResourceState {
	private List<ResourceState> children = new ArrayList<>();
	
	@Override
	public Expression getValue() {
		ListTerm value = new ListTerm();
		for (ResourceState child: children) {
			value.append(child.getValue());
		}
		return value;
	}
	
	@Override
	public Map<String, ResourceState> getChildStates() {
		Map<String, ResourceState> childParams = new TreeMap<>();
		for (int i = 0; i < children.size(); i++) {
			childParams.put(Integer.toString(i), children.get(i));
		}
		return childParams;
	}

	public void addChildState(ResourceState childState) {
		children.add(childState);
	}
	
	public void removeChildState(int idx) {
		children.remove(idx);
	}

	public void clearChildStates() {
		children.clear();
	}

	@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);
			}
		}
	}
	
	public void replaceChildState(int idx, ResourceState newState) {
		children.set(idx, newState);
	}
	
	public Object clone() {
		ListResourceState newListResourceState = new ListResourceState();
		for (ResourceState state: children) {
			newListResourceState.children.add((ResourceState) state.clone());
		}
		return newListResourceState;
	}
}