Author: batosai
Date: 2008-07-26 10:48:02 +0000 (Sat, 26 Jul 2008)
New Revision: 21418

Modified:
   trunk/apps/WoT/src/plugins/WoT/Identity.java
   trunk/apps/WoT/src/plugins/WoT/Score.java
   trunk/apps/WoT/src/plugins/WoT/WoT.java
Log:
Complete rewrite of the WoT's calculating system.
It is now able to handle any type of trust creation/modification and capacity 
lost for identities that have negative score or that are marked down by the 
tree owner.

Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-07-26 06:23:42 UTC 
(rev 21417)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-07-26 10:48:02 UTC 
(rev 21418)
@@ -96,7 +96,7 @@
                        return score.next();
                }
                else { 
-                       return new Score(treeOwner, this, 0, -1);
+                       return new Score(treeOwner, this, 0, -1, 0);
                }
        }       


Modified: trunk/apps/WoT/src/plugins/WoT/Score.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Score.java   2008-07-26 06:23:42 UTC (rev 
21417)
+++ trunk/apps/WoT/src/plugins/WoT/Score.java   2008-07-26 10:48:02 UTC (rev 
21418)
@@ -11,30 +11,23 @@
  */
 public class Score {

-       // Capacity is the maximum amount of points an identity can give to an 
other by trusting it.
-       public final static int capacities[] = {
-                       100,// Rank 0 : Own identities
-                       40,     // Rank 1 : Identities directly trusted by 
ownIdenties
-                       16, // Rank 2 : Identities trusted by rank 1 identities
-                       6,      // So on...
-                       2,
-                       1       // Every identity above rank 5 can give 1 point
-       };                      // Identities with negative score have zero 
capacity
-       
        private OwnIdentity treeOwner;
        private Identity target;
        private int score;
        private int rank;
+       private int capacity;

-       public Score (OwnIdentity treeOwner, Identity target, int score, int 
rank) {
+
+       public Score (OwnIdentity treeOwner, Identity target, int score, int 
rank, int capacity) {
                this.treeOwner = treeOwner;
                this.target = target;
                this.score = score;
                this.rank = rank;
+               this.capacity = capacity;
        }

        public String toString() {
-               return getTarget() + " now has " + getScore() + " points in " + 
getTreeOwner() + "'s trust tree (rank " + getRank() + ")";
+               return getTarget() + " has " + getScore() + " points in " + 
getTreeOwner() + "'s trust tree (rank : " + getRank() + ", capacity : " + 
getCapacity() + ")";
        }

        public OwnIdentity getTreeOwner() {
@@ -68,22 +61,12 @@
        public void setRank(int rank) {
                this.rank = rank;
        }
-       
+
        public int getCapacity() {
-               if(getScore() > 0 && getRank() >= 0) 
-                       return capacities[getRank()];
-               else 
-                       return 0;
+               return capacity;
        }
-       
-       public int getCapacity(int rank) {
-               if(rank != -1)
-                       return capacities[rank];
-               else
-                       return 0;
+
+       public void setCapacity(int capacity) {
+               this.capacity = capacity;
        }
-       
-       public int getTrusteesRank() {
-               return (getRank() == -1) ? -1 : getRank() + 1;
-       }
 }

Modified: trunk/apps/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-07-26 06:23:42 UTC (rev 
21417)
+++ trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-07-26 10:48:02 UTC (rev 
21418)
@@ -5,20 +5,13 @@
  */
 package plugins.WoT;

-import java.io.IOException;
-import java.net.UnknownHostException;
 import java.util.Date;

-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
-
-import net.pterodactylus.fcp.ClientHello;
 import net.pterodactylus.fcp.FcpConnection;

 import com.db4o.Db4o;
 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
-import com.db4o.query.Query;

 /**
  * The Web of Trust
@@ -28,6 +21,16 @@
  */
 public class WoT {

+       // Capacity is the maximum amount of points an identity can give to an 
other by trusting it.
+       public final static int capacities[] = {
+                       100,// Rank 0 : Own identities
+                       40,     // Rank 1 : Identities directly trusted by 
ownIdenties
+                       16, // Rank 2 : Identities trusted by rank 1 identities
+                       6,      // So on...
+                       2,
+                       1       // Every identity above rank 5 can give 1 point
+       };                      // Identities with negative score have zero 
capacity
+       
        ObjectContainer db;

        /**
@@ -41,68 +44,105 @@

        public void setTrust(Trust newTrust) {

-               System.out.println(newTrust);
+               boolean changedTrustValue = false;
+               boolean changedComment = false;

-               // We fetch the old trust relationship, or create one if there 
wasn't
-               Trust oldTrust = new Trust(newTrust.getTruster(), 
newTrust.getTrustee(), 0);
-               ObjectSet<Trust> trustResult = db.queryByExample(oldTrust);
-               if(trustResult.size() == 1) oldTrust = trustResult.next();
+               System.out.println("*****" + newTrust);

-               // We have to process every trust tree
-               ObjectSet<OwnIdentity> ownIdentites = 
db.queryByExample(OwnIdentity.class);
-               while(ownIdentites.hasNext()) {
-                       
-                       OwnIdentity ownIdentity = ownIdentites.next();
-                       Score trusterScore = 
newTrust.getTruster().getScore(ownIdentity, db);
-                       Score trusteeScore = 
newTrust.getTrustee().getScore(ownIdentity, db);
+               ObjectSet<Trust> trustResult = db.queryByExample(new 
Trust(newTrust.getTruster(), newTrust.getTrustee(), 0));
+               if(trustResult.size() != 0) {
+                       Trust oldTrust = trustResult.next();
+                       if(oldTrust.getValue() != newTrust.getValue()) {
+                               changedTrustValue = true;
+                               oldTrust.setValue(newTrust.getValue());
+                       }
+                       if(oldTrust.getComment() != newTrust.getComment()) {
+                               changedComment = true;
+                               oldTrust.setComment(newTrust.getComment());
+                       }
+                       if(changedComment || changedTrustValue) 
db.store(oldTrust);
+               }
+               else {
+                       db.store(newTrust);
+                       changedTrustValue = true;
+               }

-                       trusteeScore.setScore(trusteeScore.getScore() - 
(oldTrust.getValue() * trusterScore.getCapacity() / 100));
-                       trusteeScore.setScore(trusteeScore.getScore() + 
(newTrust.getValue() * trusterScore.getCapacity() / 100));
+               if(changedTrustValue) {
+                       ObjectSet<OwnIdentity> ownIdentites = 
db.queryByExample(OwnIdentity.class);
+                       while(ownIdentites.hasNext()) {
+                               updateScore(ownIdentites.next(), 
newTrust.getTrustee());
+                       }
+               }
+       }
+       
+       /**
+        * Computes an id's score in a particular trust tree.
+        * @param treeOwner The owner of the trust tree
+        * @param identity The identity you want to update the score
+        */
+       private void updateScore(OwnIdentity treeOwner, Identity identity) {
+               
+               Score score;
+               
+               int oldCapacity = 0;
+               int newScore = 0;
+               int newRank = -1;
+               int newCapacity = 0;
+               boolean hasNegativeTrust = false;
+               
+               
+               ObjectSet<Score> scoreResult = db.queryByExample(new 
Score(treeOwner, identity, 0, 0, 0));
+               if(scoreResult.hasNext()) {
+                       score = scoreResult.next();
+                       oldCapacity = score.getCapacity();
+               }
+               else score = new Score(treeOwner, identity, 0, -1, 0);
+               
+               ObjectSet<Trust> trustResult = db.queryByExample(new 
Trust(treeOwner, identity, 0));
+               if(trustResult.hasNext()) {
+                       Trust trust = trustResult.next();
+                       if(trust.getValue() <= 0) hasNegativeTrust = true;
+               }
+               
+               ObjectSet<Trust> trusters = db.queryByExample(new Trust(null, 
identity, 0));
+               while(trusters.hasNext()) {

-                       db.store(trusteeScore);
-
-                       System.out.println(trusteeScore);
+                       Trust trusterTrust = trusters.next();

-                       //TODO Handle the case when the score becomes negative 
(zero capacity)
+                       System.out.println("**"+trusterTrust);

-                       updateCapacity(trusteeScore, 
trusterScore.getTrusteesRank());
+                       Score trusterScore; 

+                       ObjectSet<Score> trustersScore = db.queryByExample(new 
Score(treeOwner, trusterTrust.getTruster(), 0, 0, 0));
+                       if(trustersScore.hasNext()) trusterScore = 
trustersScore.next();
+                       else trusterScore = new Score(treeOwner, 
trusterTrust.getTruster(), 0, -1, 0);
+                       
+                       System.out.println("*"+trusterScore);
+                       
+                       if(trusterScore.getCapacity() > 0) {
+                               newScore += trusterScore.getCapacity() * 
trusterTrust.getValue() / 100;
+                               if(newRank == -1 || newRank > 
trusterScore.getRank())
+                                       newRank = trusterScore.getRank();
+                       }
                }

-               db.delete(oldTrust);
-               db.store(newTrust);
-       }
+               if(newRank != -1) newRank++; // We don't want to give the 
trustee the same rank as the truster, right ?
+               if((newScore > 0) && !hasNegativeTrust) newCapacity = 
capacities[newRank];

-       public void updateCapacity(Score identityScore, int newRank) {
-
-               if(newRank < identityScore.getRank() || identityScore.getRank() 
< 0) { 
-                       
-                       System.out.println(identityScore.getTarget() + " has 
been promoted to rank " + newRank + "\n");
-                       
-                       int oldRank = identityScore.getRank();
-                       identityScore.setRank(newRank);
-                       db.store(identityScore);
-                                               
-                       ObjectSet<Trust> trustList = db.queryByExample(new 
Trust(identityScore.getTarget(), null, 0));
-                       while(trustList.hasNext()) {
-                               Trust trust = trustList.next();
-                               Score trusteeScore = 
trust.getTrustee().getScore(identityScore.getTreeOwner(), db);
-                               
-                               trusteeScore.setScore(trusteeScore.getScore() - 
(trust.getValue() * identityScore.getCapacity(oldRank) / 100));
-                               trusteeScore.setScore(trusteeScore.getScore() + 
(trust.getValue() * identityScore.getCapacity() / 100));
-                               
-                               db.store(trusteeScore);
-                               
-                               System.out.println(identityScore.getTarget() + 
"'s trustee " + trusteeScore);
-                               
-                               updateCapacity(trusteeScore, 
identityScore.getTrusteesRank()); // Recurse through the Trust Tree to update 
ranks and recalculate scores.
-                               
+               score.setScore(newScore);
+               score.setRank(newRank);
+               score.setCapacity(newCapacity);
+               db.store(score);
+               
+               System.out.println(score);
+               
+               if(oldCapacity != newCapacity) {
+                       ObjectSet<Trust> trustees = db.queryByExample(new 
Trust(identity, null, 0));
+                       while(trustees.hasNext()) {
+                               Trust trustee = trustees.next();
+                               updateScore(treeOwner,trustee.getTrustee());
                        }
                }
-               else {
-                       // Nothing to do
-                       System.out.println(identityScore.getTarget() + " 
already is at rank " + identityScore.getRank() + "\n");
-               }
        }

        /**
@@ -131,7 +171,7 @@
                        while (result.hasNext()) { db.delete(result.next()); }

                        OwnIdentity root = new OwnIdentity("root", "root", new 
Date(), new Date(), 0, true);
-                       Score initScore = new Score(root,root,100,0);
+                       Score initScore = new Score(root,root,100,0,100);
                        Identity a = new Identity("a", new Date(), 0, true);
                        Identity b = new Identity("b", new Date(), 0, true);
                        Identity c = new Identity("c", new Date(), 0, true);
@@ -147,7 +187,7 @@
                        wot.setTrust(new Trust(b, c, 50, "Like him"));
                        wot.setTrust(new Trust(a, b, 100, "He's my brother"));
                        wot.setTrust(new Trust(root, b, 30, "Looks like a nice 
guy"));
-                       wot.setTrust(new Trust(root, b, 60, "In fact, he is 
!"));
+                       wot.setTrust(new Trust(root, b, -100, "That was a trap 
!"));

                        System.out.println("\n***** Trust objects in the 
database :");
                        ObjectSet<Trust> trustList = 
db.queryByExample(Trust.class);


Reply via email to