TrustGrapher
r52
A playabale simulator for modelling trust between agents
|
00001 00002 package cu.trustGrapher.graphs; 00003 00004 import cu.trustGrapher.graphs.edges.SimReputationEdge; 00005 import cu.trustGrapher.eventplayer.TrustLogEvent; 00006 import cu.repsystestbed.algorithms.EigenTrust; 00007 import cu.repsystestbed.algorithms.ReputationAlgorithm; 00008 import cu.repsystestbed.entities.Agent; 00009 import cu.repsystestbed.graphs.ReputationEdgeFactory; 00010 import cu.repsystestbed.graphs.ReputationGraph; 00011 import cu.repsystestbed.graphs.TestbedEdge; 00012 import cu.trustGrapher.loading.GraphConfig; 00013 00014 import org.jgrapht.graph.SimpleDirectedGraph; 00015 00016 import java.util.ArrayList; 00017 import java.util.Collection; 00018 00023 public class SimReputationGraph extends SimAbstractGraph { 00024 protected SimFeedbackGraph feedbackGraph; 00025 00027 00032 public SimReputationGraph(GraphConfig graphConfig, SimFeedbackGraph feedbackGraph) { 00033 super(graphConfig, (SimpleDirectedGraph) new ReputationGraph(new ReputationEdgeFactory())); 00034 this.feedbackGraph = feedbackGraph; 00035 } 00036 00038 00045 @Override 00046 public void graphEvent(TrustLogEvent event, boolean forward) { 00047 ReputationAlgorithm alg = (ReputationAlgorithm) getAlgorithm(); 00048 ((EigenTrust) alg).setMatrixFilled(false); 00049 ((EigenTrust) alg).setIterations(((EigenTrust) alg).getIterations() + (forward ? 1 : -1)); 00050 Collection<Agent> fbVertices = feedbackGraph.getVertices(); 00051 ArrayList<Agent> toRemove = new ArrayList<Agent>(); 00052 for (Agent src : fbVertices) { 00053 for (Agent sink : fbVertices) { 00054 if (!src.equals(sink)) { 00055 double trustScore = -1; 00056 try { 00057 trustScore = alg.calculateTrustScore(src, sink); 00058 } catch (Exception ex) { 00059 continue; 00060 } 00061 ensureAgentExists(src.id, this); 00062 ensureAgentExists(sink.id, this); 00063 ((SimReputationEdge) referenceGraph.findEdge(src, sink)).setReputation(trustScore); 00064 ((SimReputationEdge) ensureEdgeExists(src, sink, this)).setReputation(trustScore); 00065 } 00066 } 00067 } 00068 if (getVertexCount() > fbVertices.size()){ 00069 toRemove.clear(); 00070 toRemove.addAll(getVertices()); 00071 toRemove.removeAll(fbVertices); 00072 for (Agent a : toRemove) { 00073 for (TestbedEdge e : getIncidentEdges(a)) { 00074 removeEdge(e); 00075 } 00076 removeVertex(a); 00077 } 00078 } 00079 } 00080 } 00082