Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / algorithms / NecessityOfStoringResourceStates.java
package algorithms;

import java.util.Collection;
import java.util.HashSet;

import models.*;
import models.algebra.*;
import models.dataConstraintModel.*;
import models.dataFlowModel.*;

public class NecessityOfStoringResourceStates {
	static public ResourceDependencyGraph doDecide(DataFlowModel model) {
		ResourceDependencyGraph graph = model.getResourceDependencyGraph();
		Collection<ChannelGenerator> channels = new HashSet<>(model.getIOChannelGenerators());
		channels.addAll(model.getChannelGenerators());
		for (ChannelGenerator generator: channels) {
			for (ChannelMember member: ((DataflowChannelGenerator) generator).getOutputChannelMembers()) {
				boolean flag = !member.getStateTransition().isRightUnary();		// The state does not need to be stored if the state transition function is right unary.
				for (Node node : graph.getNodes()) {
					if (((ResourceNode) node).getIdentifierTemplate().equals(member.getIdentifierTemplate())) {
						setStoreAttribute(flag, (ResourceNode) node);
					}
				}
			}
		}
		for (Node node : graph.getNodes()) {
			HashSet<ChannelGenerator> inChannels = new HashSet<>();
			for(Edge pre : ((ResourceNode) node).getInEdges()) {
				inChannels.add(((ResourceDependency) pre).getChannelGenerator());
			}
			if ((inChannels.size() > 1)) {
				setStoreAttribute(true, (ResourceNode) node);
			} else if (((ResourceNode) node).getAttribute() == null) {
				setStoreAttribute(false, (ResourceNode) node);
			}
		}
		return graph;
	}

	static private void setStoreAttribute(boolean flag, ResourceNode node) {
		NodeAttribute attr = node.getAttribute();
		StoreAttribute store;
		if (attr != null && attr instanceof NodeAttribute) {
			store = (StoreAttribute) attr;
			store.setNeeded(store.isNeeded() || flag);
		} else {
			store = new StoreAttribute();
			store.setNeeded(flag);
			node.setAttribute(store);
		}
	}
}