package ls.graph.script;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import ls.script.Scriptable;
import ls.util.Utils;
import edu.uci.ics.jung.graph.ArchetypeGraph;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.EdgeWeightLabeller;
import edu.uci.ics.jung.graph.event.GraphEventListener;
import edu.uci.ics.jung.graph.event.GraphEventType;
import edu.uci.ics.jung.utils.UserData;
import edu.uci.ics.jung.utils.UserDataContainer;
public class ScriptableGraph implements Graph, Scriptable
{
@Override
public Object clone() throws CloneNotSupportedException
{
ScriptableGraph sg = new ScriptableGraph((Graph)(graph.clone()));
return sg;
}
private Graph graph = null;
/**
* C'tor.
* Constructs a new instance with the given graph providing the inner implementation
* @param g
* @throws NullPointerException
*/
public ScriptableGraph(Graph g)
throws NullPointerException
{
if (g == null)
throw new NullPointerException("Graph delegate is null");
this.graph = g;
}
public String getBindingName()
{
return "Graph"; //assumption - 1 instance per engine
}
public EdgeWeightLabeller getEdgeWeightLabeller()
{
return EdgeWeightLabeller.getLabeller(this.graph);
}
public boolean isWeighted()
{
return EdgeWeightLabeller.hasWeightLabeller(this.graph);
}
public void setEdgeWeight(Edge e, int w)
{
EdgeWeightLabeller ewl = EdgeWeightLabeller.getLabeller(this.graph);
if (ewl != null)
{
ewl.setWeight(e, w);
}
else
{
Utils.err("ScriptableGraph.setEdgeWeight: Could not get edge weight labeller");
}
}
public int getEdgeWeight(Edge e)
{
if (!isWeighted())
Utils.err("ScriptableGraph.getEdgeWeight: Graph is not weighted");
else
{
try
{
EdgeWeightLabeller ewl = EdgeWeightLabeller.getLabeller(this.graph);
if (ewl != null)
{
int n = ewl.getWeight(e);
return n;
}
else
{
Utils.err("ScriptableGraph.getEdgeWeight: Could not get edge weight labeller");
return 0;
}
}
catch (Exception exn)
{
Utils.exn(exn);
}
}
return 0;
}
public void setVertexData(Vertex v,Object k, Object val)
{
if (v != null && k != null && val != null)
{
v.setUserDatum(k, val, UserData.CLONE);
}
}
public Edge addEdge(Edge e)
{
return this.graph.addEdge(e);
}
public Vertex addVertex(Vertex v)
{
return this.addVertex(v);
}
public boolean isDirected()
{
return (graph instanceof DirectedGraph);
}
public void removeEdge(Edge e)
{
graph.removeEdge(e);
}
public void removeVertex(Vertex v)
{
graph.removeVertex(v);
}
public void addListener(GraphEventListener gel, GraphEventType get)
{
graph.addListener(gel, get);
}
public ArchetypeGraph copy()
{
return graph.copy(); //todo: should also copy other members (decorated)?
}
public Collection getEdgeConstraints()
{
return graph.getEdgeConstraints();
}
public Set getEdges()
{
return graph.getEdges();
}
public Collection getVertexConstraints()
{
return graph.getVertexConstraints();
}
public Set getVertices()
{
return graph.getVertices();
}
public ArchetypeGraph newInstance()
{
return graph.newInstance();
}
public int numEdges()
{
return graph.numEdges();
}
public int numVertices()
{
return graph.numVertices();
}
public void removeAllEdges()
{
graph.removeAllEdges();
}
public void removeAllVertices()
{
graph.removeAllVertices();
}
public void removeEdges(Set edges)
{
graph.removeEdges(edges);
}
public void removeListener(GraphEventListener gel, GraphEventType get)
{
graph.removeListener(gel, get);
}
public void removeVertices(Set vertices)
{
graph.removeVertices(vertices);
}
public void addUserDatum(Object key, Object datum, CopyAction copyAct)
{
graph.addUserDatum(key, datum, copyAct);
}
public boolean containsUserDatumKey(Object key)
{
return graph.containsUserDatumKey(key);
}
public Object getUserDatum(Object key)
{
return graph.getUserDatum(key);
}
public CopyAction getUserDatumCopyAction(Object key)
{
return graph.getUserDatumCopyAction(key);
}
public Iterator getUserDatumKeyIterator()
{
return graph.getUserDatumKeyIterator();
}
public void importUserData(UserDataContainer udc)
{
graph.importUserData(udc);
}
public Object removeUserDatum(Object key)
{
return graph.removeUserDatum(key);
}
public void setUserDatum(Object key, Object datum, CopyAction copyAct)
{
graph.setUserDatum(key, datum, copyAct);
}
}