TrustGrapher  r52
A playabale simulator for modelling trust between agents
D:/src/cu/repsystestbed/algorithms/ReputationAlgorithm.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.FeedbackHistoryGraph;
00010 import cu.repsystestbed.graphs.FeedbackHistoryGraphEdge;
00011 import cu.repsystestbed.graphs.ReputationEdge;
00012 import cu.repsystestbed.graphs.ReputationEdgeFactory;
00013 import cu.repsystestbed.graphs.ReputationGraph;
00014 
00015 public abstract class ReputationAlgorithm
00016 {
00017         static Logger logger = Logger.getLogger(ReputationAlgorithm.class.getName());
00018         
00019         private static ReputationAlgorithm algorithm;
00020         protected FeedbackHistoryGraph feedbackHistoryGraph;
00021         private ReputationGraph reputationGraph;
00022         //global trust algorithms produce a complete reputation graph
00023         private boolean isGlobal;
00024         private static double MINIMUM_TRUST_SCORE = 0.0;
00025         private static double MAXIMUM_TRUST_SCORE = 1.0;
00026         
00033         @SuppressWarnings("unchecked")
00034         public static ReputationAlgorithm getInstance(String className) throws GenericTestbedException
00035         {
00036                 try
00037                 {
00038                         Class<?>cls = (Class<ReputationAlgorithm>) Class.forName(className);
00039                         algorithm = (ReputationAlgorithm) cls.newInstance();
00040                         
00041                 }catch(Exception e)
00042                 {
00043                         String msg = "Error loading reputation algorithm with name " + className;
00044                         logger.error(msg);
00045                         throw new GenericTestbedException(msg, e);
00046                 }
00047                 return algorithm;
00048                 
00049         }
00056         public void setFeedbackHistoryGraph(FeedbackHistoryGraph feedbackHistoryGraph)
00057         {
00058                 this.feedbackHistoryGraph = feedbackHistoryGraph;
00059                 
00060                 //initialize the reputation graph
00061                 reputationGraph = new ReputationGraph(new ReputationEdgeFactory());
00062                 Set<FeedbackHistoryGraphEdge> edges = this.feedbackHistoryGraph.edgeSet();
00063                 for(FeedbackHistoryGraphEdge edge : edges)
00064                 {
00065                         Agent src = (Agent)edge.src;
00066                         Agent sink = (Agent)edge.sink;
00067                         this.reputationGraph.addVertex(src);
00068                         this.reputationGraph.addVertex(sink);
00069                         this.reputationGraph.addEdge(src, sink);
00070                 }
00071                 
00072         }
00073         
00074         
00075         
00076         public ReputationGraph getReputationGraph() throws GenericTestbedException
00077         {
00078                 if(this.reputationGraph.edgeSet().size()<1) throw new GenericTestbedException("No edges in reputation graph."); 
00079                 return this.reputationGraph;
00080         }
00081         
00086         public void update() throws Exception
00087         {
00088                 /*
00089                  * update the reputation graph
00090                  * for every src and sink node in the experience graph, 
00091                  * weight = calculateTrustGraph (src, sink)
00092                  * observer.setEdgeWeight(weight) 
00093                  */
00094         
00095                 
00096                 Set<Agent> agents = feedbackHistoryGraph.vertexSet();
00097                 
00098                 if(isGlobal)
00099                 {
00103                         for(Agent src : agents)
00104                         {
00105                                 for(Agent sink : agents)
00106                                 {
00107                                         if(!src.equals(sink))
00108                                         {
00109                                                 double trustScore = calculateTrustScore(src, sink);
00110                                                 //ensure the trust score is in [0,1].
00111                                                 if(trustScore < this.MINIMUM_TRUST_SCORE || trustScore > this.MAXIMUM_TRUST_SCORE)
00112                                                 {
00113                                                         throw new GenericTestbedException("Algorithm returned a trust score (" 
00114                                                                         + trustScore + ")that is not within [0,1]. ");
00115                                                 }
00116                                                 ReputationEdge repEdge = this.reputationGraph.getEdge(src, sink);
00117                                                 
00118                                                 //repEdge may be null. see setFeedbackHistoryGraph() for why.
00119                                                 if(repEdge==null)
00120                                                 {
00121                                                         this.reputationGraph.addEdge(src, sink);
00122                                                         repEdge = this.reputationGraph.getEdge(src, sink);
00123                                                         repEdge.setReputation(trustScore);
00124                                                 }
00125                                                 this.reputationGraph.setEdgeWeight(repEdge, trustScore);
00126                                         }
00127                                 }
00128                         }
00129                 }else
00130                 {
00131                         //TODO
00136                 }
00137                 
00138                 //need to let the trust algorithms know that something has changed
00139                 this.reputationGraph.notifyObservers();
00140                 
00141         }
00142         
00143         public abstract double calculateTrustScore(Agent src, Agent sink) throws Exception;
00144         public void setGlobal(boolean isGlobal)
00145         {
00146                 this.isGlobal = isGlobal;
00147         }
00148         public boolean isGlobal()
00149         {
00150                 return isGlobal;
00151         }
00152         
00153 
00154         
00155         
00156 
00157 }