TrustGrapher  r52
A playabale simulator for modelling trust between agents
D:/src/cu/trustGrapher/graphs/SimAbstractGraph.java
Go to the documentation of this file.
00001 
00002 package cu.trustGrapher.graphs;
00003 
00004 import cu.repsystestbed.entities.Agent;
00005 import cu.repsystestbed.graphs.TestbedEdge;
00006 import cu.trustGrapher.graphs.edges.*;
00007 import cu.trustGrapher.eventplayer.TrustLogEvent;
00008 
00009 import org.jgrapht.graph.SimpleDirectedGraph;
00010 
00011 import aohara.utilities.ChatterBox;
00012 import cu.trustGrapher.loading.GraphConfig;
00013 import edu.uci.ics.jung.graph.Graph;
00014 import edu.uci.ics.jung.graph.util.Context;
00015 import org.apache.commons.collections15.Predicate;
00016 
00032 public abstract class SimAbstractGraph extends JungAdapterGraph<Agent, TestbedEdge> implements Predicate<Context<Graph<Agent, TestbedEdge>, Object>> {
00033 
00034     protected JungAdapterGraph<Agent, TestbedEdge> referenceGraph;
00035     protected GraphConfig graphConfig;
00036 
00038 
00043     public SimAbstractGraph(GraphConfig graphConfig, SimpleDirectedGraph innerGraph) {
00044         super(innerGraph);
00045         referenceGraph = new JungAdapterGraph<Agent, TestbedEdge>((SimpleDirectedGraph) innerGraph.clone());
00046         this.graphConfig = graphConfig;
00047     }
00048 
00050 
00053     public JungAdapterGraph getReferenceGraph() {
00054         return referenceGraph;
00055     }
00056 
00062     public String getDisplayName() {
00063         return graphConfig.getDisplayName();
00064     }
00065 
00071     public Object getAlgorithm() {
00072         return graphConfig.getAlgorithm();
00073     }
00074 
00079     public int getID() {
00080         return graphConfig.getIndex();
00081     }
00082 
00087     public boolean isDisplayed() {
00088         return graphConfig.isDisplayed();
00089     }
00090 
00098     public boolean evaluate(Context<Graph<Agent, TestbedEdge>, Object> context) {
00099         if (context.element instanceof TestbedEdge) {
00100             return containsEdge((TestbedEdge) context.element);
00101         } else if (context.element instanceof Agent) {
00102             return containsVertex((Agent) context.element);
00103         } else {
00104             ChatterBox.criticalError(this, "evaluate()", "Uncaught predicate");
00105             return false;
00106         }
00107     }
00108 
00110 
00117     protected Agent ensureAgentExists(int id, JungAdapterGraph<Agent, TestbedEdge> caller) {
00118         Agent tempAgent = new Agent(id);
00119         for (Agent agent : caller.getVertices()) {
00120             if (agent.equals(tempAgent)) {
00121                 return agent;
00122             }
00123         }
00124         caller.addVertex(tempAgent);
00125         return tempAgent;
00126     }
00127 
00138     protected TestbedEdge ensureEdgeExists(Agent src, Agent sink, JungAdapterGraph<Agent, TestbedEdge> caller) {
00139         TestbedEdge edge = caller.findEdge(src, sink);
00140         if (edge == null) {
00141             edge = newEdge(src, sink, caller);
00142             caller.addEdge(edge, src, sink);
00143         }
00144         return edge;
00145     }
00146 
00157     private TestbedEdge newEdge(Agent src, Agent sink, JungAdapterGraph caller) {
00158         if (caller.getInnerGraph() instanceof cu.repsystestbed.graphs.FeedbackHistoryGraph) {
00159             try {
00160                 return new SimFeedbackEdge(src, sink);
00161             } catch (Exception ex) {
00162                 ChatterBox.debug("MyFeedbackEdge", "MyFeedbackEdge()", ex.getMessage());
00163                 return null;
00164             }
00165         } else if (caller.getInnerGraph() instanceof cu.repsystestbed.graphs.ReputationGraph) {
00166             return new SimReputationEdge(src, sink);
00167         } else if (caller.getInnerGraph() instanceof cu.repsystestbed.graphs.TrustGraph) {
00168             return new SimTrustEdge(src, sink);
00169         } else {
00170             ChatterBox.debug(this, "newEdge()", "Unsupported caller");
00171             return null;
00172         }
00173     }
00174 
00180     protected void removeEdgeAndVertices(TestbedEdge edge) {
00181         java.util.Collection<Agent> agents = getIncidentVertices(edge);
00182         removeEdge(edge);
00183         for (Agent v : agents) {
00184             if (getIncidentEdges(v).isEmpty()) {
00185                 removeVertex(v);
00186             }
00187         }
00188     }
00189 
00197     public void graphConstructionEvent(TrustLogEvent event) {
00198         if (event == null) {  //If a null event is passed, add all possible edges to the graph
00199             for (Agent src : referenceGraph.getVertices()) {
00200                 for (Agent sink : referenceGraph.getVertices()) {
00201                     if (!src.equals(sink)) {
00202                         ensureEdgeExists(src, sink, referenceGraph);
00203                     }
00204                 }
00205             }
00206         } else { //Otherwise, just add any new Agents to the graph
00207             ensureAgentExists(event.getAssessor(), referenceGraph);
00208             ensureAgentExists(event.getAssessee(), referenceGraph);
00209         }
00210     }
00211 
00219     public abstract void graphEvent(TrustLogEvent event, boolean forward);
00220 }
00222