Author: batosai
Date: 2008-07-24 16:08:30 +0000 (Thu, 24 Jul 2008)
New Revision: 21374

Modified:
   trunk/apps/WoT/src/plugins/WoT/Identity.java
   trunk/apps/WoT/src/plugins/WoT/Score.java
   trunk/apps/WoT/src/plugins/WoT/Trust.java
   trunk/apps/WoT/src/plugins/WoT/WoT.java
Log:
First working version of the WoT's core !
Also added necessary debugging/testing code.

Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-07-24 16:08:19 UTC 
(rev 21373)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-07-24 16:08:30 UTC 
(rev 21374)
@@ -80,23 +80,11 @@
                saxParser.parse(pis,new IdentityParser());
        }

-       public int getScore(OwnIdentity treeOwner, ObjectContainer db) {
-               
-               Query query = db.query();
-               query.constrain(Score.class);
-               query.descend("treeOwner").constrain(treeOwner);
-               query.descend("target").constrain(this);
-               ObjectSet<Score> score = query.execute();
-               
-               if(score.size() == 1) {
-                       return score.next().getScore();
-               }
-               else { 
-                       return 0;
-               }
+       public String toString() {
+               return getRequestURI();
        }

-       public int getRank(OwnIdentity treeOwner, ObjectContainer db) {
+       public Score getScore(OwnIdentity treeOwner, ObjectContainer db) {

                Query query = db.query();
                query.constrain(Score.class);
@@ -105,14 +93,13 @@
                ObjectSet<Score> score = query.execute();

                if(score.size() == 1) {
-                       return score.next().getRank();
+                       return score.next();
                }
                else { 
-                       return -1;
+                       return new Score(treeOwner, this, 0, -1);
                }
-       }
+       }       

-       
        /**
         * @return requestURI
         */

Modified: trunk/apps/WoT/src/plugins/WoT/Score.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Score.java   2008-07-24 16:08:19 UTC (rev 
21373)
+++ trunk/apps/WoT/src/plugins/WoT/Score.java   2008-07-24 16:08:30 UTC (rev 
21374)
@@ -11,6 +11,16 @@
  */
 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;
@@ -22,6 +32,10 @@
                this.score = score;
                this.rank = rank;
        }
+       
+       public String toString() {
+               return getTarget() + " now has " + getScore() + " points in " + 
getTreeOwner() + "'s trust tree (rank " + getRank() + ")";
+       }

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

Modified: trunk/apps/WoT/src/plugins/WoT/Trust.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Trust.java   2008-07-24 16:08:19 UTC (rev 
21373)
+++ trunk/apps/WoT/src/plugins/WoT/Trust.java   2008-07-24 16:08:30 UTC (rev 
21374)
@@ -60,6 +60,10 @@

                return elem;
        }
+       
+       public String toString() {
+               return getTruster() + " trusts " + getTrustee() + " (" + 
getValue() + " : " + getComment() + ")";
+       }

        /**
         * @return truster

Modified: trunk/apps/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-07-24 16:08:19 UTC (rev 
21373)
+++ trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-07-24 16:08:30 UTC (rev 
21374)
@@ -18,6 +18,7 @@
 import com.db4o.Db4o;
 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
+import com.db4o.query.Query;

 /**
  * The Web of Trust
@@ -26,24 +27,83 @@
  * 
  */
 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;

        /**
-        * Creates a new WoT instance
+        * Implements a variation of the Advogato Trust Metric
+        * The only difference is that we allow negative trust to avoid giving 
+        * capacity to people we dislike...
         */
-       public WoT() {
-       
+       public WoT(ObjectContainer db) {
+               this.db = db;
        }

+       public void setTrust(Trust newTrust) {
+               
+               System.out.println(newTrust);
+               
+               // 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();
+               
+               // 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);
+
+                       trusteeScore.setScore(trusteeScore.getScore() - 
(oldTrust.getValue() * trusterScore.getCapacity() / 100));
+                       trusteeScore.setScore(trusteeScore.getScore() + 
(newTrust.getValue() * trusterScore.getCapacity() / 100));
+                       
+                       db.store(trusteeScore);
+
+                       System.out.println(trusteeScore);
+                       
+                       //TODO Handle the case when the score becomes negative 
(zero capacity)
+                       
+                       updateCapacity(trusteeScore, 
trusterScore.getTrusteesRank());
+                       
+               }
+               
+               oldTrust = newTrust; //TODO Check if we are not creating a new 
object in DB4O !!!
+               db.store(oldTrust);
+       }
+               
+       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.
+                               
+                       }
+               }
+               else {
+                       // Nothing to do
+                       System.out.println(identityScore.getTarget() + " 
already is at rank " + identityScore.getRank());
+               }
+       }

        /**
         * This code will have to move in the plugin methods.
@@ -56,11 +116,11 @@

                try {
                        db = Db4o.openFile("WoT.db4o");
-                       
+                       /*
                        fcp = new FcpConnection("localhost",9481);
                        fcp.connect();
                        fcp.sendMessage(new ClientHello("WoT"));
-                       
+                       */
                        //fcp = new FCPHandler(host,port);

                        //identityInserter = new IdentityInserter(db, fcp);
@@ -70,10 +130,28 @@
                        ObjectSet<Object> result = db.queryByExample(new 
Object());
                        while (result.hasNext()) { db.delete(result.next()); }

-                       /*
-                       OwnIdentity root = new OwnIdentity(fcp);
+                       OwnIdentity root = new OwnIdentity("root", "root", new 
Date(), new Date(), 0, true);
+                       Score initScore = new Score(root,root,100,0);
+                       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);
+
                        db.store(root);
+                       db.store(initScore);
+                       db.store(a);
+                       db.store(b);
+                       db.store(c);

+                       WoT wot = new WoT(db);
+                       wot.setTrust(new Trust(root, a, 80, "Friend of mine"));
+                       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"));
+
+                       /*
+                       
+
                        Identity a = new Identity("a", new Date(), true);
                        Identity b = new Identity("b", new Date(), true);

@@ -84,7 +162,7 @@
                        root.addTrust(db, b, 30);
                        a.addTrust(db, b, 100);
                        */
-                       
+                       /*
                        Identity test = new Identity("USK at 
DCOSLbqlCS-~Ly8ZBxvcI9MkrweRl0t7BLLKs2zGj38,J~GQAEk3m5eXI5KagpSXxHX2~3GPC61eJtQGIWn~5Ns,AQACAAE/",
 new Date(), 3, true);

                        IdentityFetcher identityFetcher = new 
IdentityFetcher(db,fcp);
@@ -92,6 +170,7 @@

                        //identityInserter.stop();
                        fcp.disconnect();
+                       */
                        db.close();
                }
                catch (Exception e) {


Reply via email to