org.ggp.base.util.propnet.architecture.Component Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alloy-ggp-base Show documentation
Show all versions of alloy-ggp-base Show documentation
A modified version of the GGP-Base library for Alloy.
package org.ggp.base.util.propnet.architecture;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
/**
* The root class of the Component hierarchy, which is designed to represent
* nodes in a PropNet. The general contract of derived classes is to override
* all methods.
*/
public abstract class Component implements Serializable
{
private static final long serialVersionUID = 352524175700224447L;
/** The inputs to the component. */
private final Set inputs;
/** The outputs of the component. */
private final Set outputs;
/**
* Creates a new Component with no inputs or outputs.
*/
public Component()
{
this.inputs = new HashSet(1);
this.outputs = new HashSet(1);
}
/**
* Adds a new input.
*
* @param input
* A new input.
*/
public void addInput(Component input)
{
inputs.add(input);
}
public void removeInput(Component input)
{
inputs.remove(input);
}
public void removeOutput(Component output)
{
outputs.remove(output);
}
public void removeAllInputs()
{
inputs.clear();
}
public void removeAllOutputs()
{
outputs.clear();
}
/**
* Adds a new output.
*
* @param output
* A new output.
*/
public void addOutput(Component output)
{
outputs.add(output);
}
/**
* Getter method.
*
* @return The inputs to the component.
*/
public Set getInputs()
{
return inputs;
}
/**
* A convenience method, to get a single input.
* To be used only when the component is known to have
* exactly one input.
*
* @return The single input to the component.
*/
public Component getSingleInput() {
assert inputs.size() == 1;
return inputs.iterator().next();
}
/**
* Getter method.
*
* @return The outputs of the component.
*/
public Set getOutputs()
{
return outputs;
}
/**
* A convenience method, to get a single output.
* To be used only when the component is known to have
* exactly one output.
*
* @return The single output to the component.
*/
public Component getSingleOutput() {
assert outputs.size() == 1;
return outputs.iterator().next();
}
/**
* Returns the value of the Component.
*
* @return The value of the Component.
*/
public abstract boolean getValue();
/**
* Returns a representation of the Component in .dot format.
*
* @see java.lang.Object#toString()
*/
@Override
public abstract String toString();
/**
* Returns a configurable representation of the Component in .dot format.
*
* @param shape
* The value to use as the shape attribute.
* @param fillcolor
* The value to use as the fillcolor attribute.
* @param label
* The value to use as the label attribute.
* @return A representation of the Component in .dot format.
*/
protected String toDot(String shape, String fillcolor, String label)
{
StringBuilder sb = new StringBuilder();
sb.append("\"@" + Integer.toHexString(hashCode()) + "\"[shape=" + shape + ", style= filled, fillcolor=" + fillcolor + ", label=\"" + label + "\"]; ");
for ( Component component : getOutputs() )
{
sb.append("\"@" + Integer.toHexString(hashCode()) + "\"->" + "\"@" + Integer.toHexString(component.hashCode()) + "\"; ");
}
return sb.toString();
}
public abstract String getShortName();
public static Collection toShortNames(Collection components) {
return Collections2.transform(components, new Function() {
@Override
public String apply(Component input) {
return input.getShortName();
}
});
}
}