TrustGrapher  r52
A playabale simulator for modelling trust between agents
D:/src/cu/repsystestbed/algorithms/TrustAlgorithm.java
Go to the documentation of this file.
00001 package cu.repsystestbed.algorithms;
00002 
00003 import java.util.Set;
00004 
00005 import org.apache.log4j.Logger;
00006 
00007 import cu.repsystestbed.entities.Agent;
00008 import cu.repsystestbed.exceptions.GenericTestbedException;
00009 import cu.repsystestbed.graphs.FeedbackHistoryGraphEdge;
00010 import cu.repsystestbed.graphs.ReputationEdge;
00011 import cu.repsystestbed.graphs.ReputationEdgeFactory;
00012 import cu.repsystestbed.graphs.ReputationGraph;
00013 import cu.repsystestbed.graphs.TrustEdge;
00014 import cu.repsystestbed.graphs.TrustEdgeFactory;
00015 import cu.repsystestbed.graphs.TrustGraph;
00016 
00017 public abstract class TrustAlgorithm
00018 {
00019         private static TrustAlgorithm algorithm;
00020         static Logger logger = Logger.getLogger(TrustAlgorithm.class.getName());
00021         private ReputationGraph reputationGraph;
00022         private TrustGraph trustGraph; 
00023         
00024         @SuppressWarnings("unchecked")
00025         public static TrustAlgorithm getInstance(String className) throws GenericTestbedException
00026         {
00027                 try
00028                 {
00029                         Class<?>cls = (Class<TrustAlgorithm>) Class.forName(className);
00030                         algorithm = (TrustAlgorithm) cls.newInstance();
00031                 }catch(Exception e)
00032                 {
00033                         String msg = "Error loading trust algorithm with name " + className;
00034                         logger.error(msg);
00035                         throw new GenericTestbedException(msg, e);
00036 
00037                 }
00038                 return algorithm;
00039                 
00040         }
00041 
00042         public void update() throws Exception
00043         {
00044                 //go through each agent and find out the agents it trusts
00045                 //create an edge if an agent trusts another
00046                 Set<Agent> agents = this.trustGraph.vertexSet();
00047                 for(Agent src : agents)
00048                 {
00049                         Set<Agent> agents2 = this.trustGraph.vertexSet();
00050                         for(Agent sink : agents)
00051                         {
00052                                 if(trusts(src, sink)) this.trustGraph.addEdge(src, sink);
00053                         }
00054                         
00055                 }
00056                 
00057                 
00058         }
00059 
00060         public void setReputationGraph(ReputationGraph reputationGraph)
00061         {
00062                 this.reputationGraph = reputationGraph;
00063                 //instantiate the trust graph
00064                 trustGraph = new TrustGraph(new TrustEdgeFactory());
00065                 //an edge in the trust graph means src trusts sink. So don't copy the edges from the rep graph. just copy the agents
00066                 for(Agent agent : this.reputationGraph.vertexSet())
00067                 {
00068                         trustGraph.addVertex(agent);
00069                 }
00070                 
00071         }
00072 
00073         public ReputationGraph getReputationGraph()
00074         {
00075                 return reputationGraph;
00076         }
00077 
00078 
00079         public TrustGraph getTrustGraph()
00080         {
00081                 return trustGraph;
00082         }
00083         
00084         public abstract boolean trusts(Agent src, Agent sink) throws Exception;
00085         
00086         
00087 
00088 }
00089 
00090