Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / models / dataFlowModel / ResourceNode.java
package models.dataFlowModel;

import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import models.Node;
import models.algebra.Expression;
import models.algebra.Type;
import models.dataConstraintModel.ResourceHierarchy;
import models.dataConstraintModel.ResourcePath;
import models.dataConstraintModel.Selector;

public class ResourceNode extends Node {
	protected ResourceNode parent = null;
	protected Set<ResourceNode> children = null;
	protected ResourceHierarchy resourceHierarchy = null;
	protected Map<DataTransferChannel, ResourcePath> inSide = null;
	protected Map.Entry<DataTransferChannel, ResourcePath> outSide = null;
	protected List<Selector> selectors = null;

	public ResourceNode(ResourceNode parent, 
						DataTransferChannel outSideChannel, 
						ResourcePath outSideResource) {
		this.parent = parent;
		this.children = new HashSet<>();
		this.inSide = new HashMap<>();
		this.outSide = new AbstractMap.SimpleEntry<>(outSideChannel, outSideResource);
		this.resourceHierarchy = outSideResource.getResourceHierarchy();
		this.selectors = new ArrayList<>();
		if (resourceHierarchy.getNumParameters() > 0) {
			if (outSideChannel != null) {
				selectors.addAll(outSideChannel.getSelectors());
			} else {
				List<Expression> pathParams = outSideResource.getPathParams();
				selectors.add(new Selector(pathParams.get(pathParams.size() - 1)));
			}
		}
	}
	
	public ResourceNode(ResourceNode parent, 
						Map<DataTransferChannel, ResourcePath> inSide, 
						Map.Entry<DataTransferChannel, ResourcePath> outSide) {
		this.parent = parent;
		this.children = new HashSet<>();
		this.inSide = inSide;
		this.outSide = outSide;
		this.resourceHierarchy = outSide.getValue().getResourceHierarchy();
		this.selectors = new ArrayList<>();
		DataTransferChannel outSideChannel = outSide.getKey();
		ResourcePath outSideResource = outSide.getValue();
		if (resourceHierarchy.getNumParameters() > 0) {
			if (outSideChannel != null) {
				selectors.addAll(outSideChannel.getSelectors());
			} else {
				List<Expression> pathParams = outSideResource.getPathParams();
				selectors.add(new Selector(pathParams.get(pathParams.size() - 1)));
			}
		}
	}
	
	public ResourceHierarchy getResourceHierarchy() {
		return resourceHierarchy;
	}
	
	public String getResourceName() {
		return resourceHierarchy.getResourceName();
	}
	
	public Type getResourceStateType() {
		return resourceHierarchy.getResourceStateType();
	}
	
	public int getNumberOfParameters() {
		return resourceHierarchy.getTotalNumParameters();
	}
	
	public ResourceNode getParent() {
		return parent;
	}
	
	public Set<ResourceNode> getChildren() {
		return children;
	}

	public void addChild(ResourceNode child) {
		children.add(child);
		child.parent = this;
	}

	public Collection<ResourcePath> getInSideResources() {
		return inSide.values();
	}
	
	public ResourcePath getInSideResource(DataTransferChannel channel) {
		return inSide.get(channel);
	}
	
	public ResourcePath getOutSideResource() {
		return outSide.getValue();
	}
	
	public DataTransferChannel getOutSideChannel() {
		return outSide.getKey();
	}
	
	public void addInSideResource(DataTransferChannel channel, ResourcePath inResource) {
		inSide.put(channel, inResource);
	}
	
	public void setOutSideResource(DataTransferChannel channel, ResourcePath outResource) {
		outSide = new AbstractMap.SimpleEntry<>(channel, outResource);
	}

	public List<Selector> getSelectors() {
		return selectors;
	}

	public List<Selector> getAllSelectors() {
		List<Selector> selectors = new ArrayList<>();
		if (parent != null) {
			selectors.addAll(parent.getAllSelectors());
		}
		selectors.addAll(this.selectors);
		return selectors;
	}
	
//	public boolean equals(Object another) {
//		if (this == another) return true;
//		if (!(another instanceof ResourceNode)) return false;
//		return resourcePath.equals(((ResourceNode)another).resourcePath);
//	}
//	
//	public int hashCode() {
//		return resourcePath.hashCode();
//	}
//	
//	public String toString() {
//		return resourcePath.toString();
//	}
}