TrustGrapher  r52
A playabale simulator for modelling trust between agents
D:/src/cu/repsystestbed/algorithms/RankbasedTrustAlg.java
Go to the documentation of this file.
00001 
00004 package cu.repsystestbed.algorithms;
00005 
00006 import java.util.ArrayList;
00007 import java.util.Collections;
00008 import java.util.Set;
00009 
00010 import org.apache.log4j.Logger;
00011 
00012 import cu.repsystestbed.entities.Agent;
00013 import cu.repsystestbed.graphs.ReputationEdge;
00014 
00020 public class RankbasedTrustAlg extends TrustAlgorithm
00021 {
00022         static Logger logger = Logger.getLogger(RankbasedTrustAlg.class.getName());
00023         
00024         private double ratio;
00025 
00026         public RankbasedTrustAlg()
00027         {
00028                 this.ratio = 0.7;
00029         }
00030         
00034         @Override
00035         public boolean trusts(Agent src, Agent sink) throws Exception
00036         {
00037                 Set<ReputationEdge> outGoingEdgesSet = super.getReputationGraph().outgoingEdgesOf(src);
00038                 
00039                 ArrayList<ReputationEdge> outGoingEdgesList = new ArrayList<ReputationEdge>(); 
00040                 for(ReputationEdge edge : outGoingEdgesSet)
00041                 {
00042                         outGoingEdgesList.add(edge);
00043                 }
00044                 //sort the edges in ascending order based on the reputation
00045                 
00046         
00047 //              logger.info("Before ranking: " + outGoingEdgesList);
00048                 Collections.sort(outGoingEdgesList);
00049 //              logger.info("After ranking: " + outGoingEdgesList);
00050                 
00051                 
00052                 int numberOfAgentsToBeTrusted = (int)(this.ratio * outGoingEdgesList.size());
00053                 if(numberOfAgentsToBeTrusted==0) return false;
00054                 
00055                 ArrayList<Agent> trustedAgents = new ArrayList<Agent>(numberOfAgentsToBeTrusted);
00056                 
00057                 for(int i=0;i<numberOfAgentsToBeTrusted;i++)
00058                 {
00059                         ReputationEdge trustedEdge = outGoingEdgesList.get(outGoingEdgesList.size() - 1 - i);
00060                         trustedAgents.add((Agent) trustedEdge.sink);
00061 //                      logger.info("Added " + (Agent) trustedEdge.sink + "as trusted.");
00062                 }
00063                 
00064                 for(Agent trustedAgent : trustedAgents)
00065                 {
00066                         if(trustedAgent.equals(sink))
00067                         {
00068                                 logger.info(src + " trusts " + sink);
00069                                 return true;
00070                         }
00071                 }
00072                 //logger.info(src + " does not trust " + sink);
00073                 return false;
00074         }
00075 
00076         public void setRatio(double ratio) throws Exception
00077         {
00078                 if(ratio<0 || ratio>1) 
00079                 {
00080                         String errMsg = "Ratio must be in [0,1]"; 
00081                         logger.error(errMsg);
00082                         throw new Exception(errMsg);
00083                 }
00084                 this.ratio = ratio;
00085         }
00086 
00087         public double getRatio()
00088         {
00089                 return ratio;
00090         }
00091 
00092 }