Author: batosai
Date: 2008-09-05 14:13:20 +0000 (Fri, 05 Sep 2008)
New Revision: 22453

Modified:
   trunk/apps/WoT/src/plugins/WoT/Identity.java
   trunk/apps/WoT/src/plugins/WoT/IdentityParser.java
   trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
   trunk/apps/WoT/src/plugins/WoT/Trust.java
   trunk/apps/WoT/src/plugins/WoT/Trustlist.java
   trunk/apps/WoT/src/plugins/WoT/WoT.java
   trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
Log:
Big refactor. The score calculation is now done by identities themselves and, 
hopefully, much more readable.

Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-09-05 14:06:22 UTC 
(rev 22452)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-09-05 14:13:20 UTC 
(rev 22453)
@@ -18,6 +18,7 @@

 import freenet.keys.FreenetURI;
 import freenet.support.ByteArrayWrapper;
+import freenet.support.Logger;

 /**
  * An identity as handled by the WoT (a USK)
@@ -26,6 +27,16 @@
  *
  */
 public class Identity {
+       
+       // 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

        @SuppressWarnings("unused")
        private ByteArrayWrapper id;
@@ -130,8 +141,112 @@
                return query.execute();
        }

+       public void setTrust(ObjectContainer db, Identity trustee, int value, 
String comment) throws DuplicateTrustException, InvalidParameterException, 
DuplicateScoreException {
+               // Check if we are updating an existing trust value
+               Trust trust;
+               try {
+                       trust = getGivenTrust(trustee, db);
+                       
+                       if(!trust.getComment().equals(comment)) {
+                               trust.setComment(comment);
+                               db.store(trust);
+                       }
+                       
+                       if(trust.getValue() != value) {
+                               trust.setValue(value);
+                               db.store(trust);
+                               Logger.debug(this, "Updated trust value, now 
updating Score.");
+                               trustee.updateScore(db);
+                       }
+               } catch (NotTrustedException e) {
+                       trust = new Trust(this, trustee, value, comment);
+                       db.store(trust);
+                       Logger.debug(this, "New trust value, now updating 
Score.");
+                       trustee.updateScore(db);
+               } 
+       }

+       public void updateScore (ObjectContainer db) throws 
DuplicateScoreException, DuplicateTrustException {
+               ObjectSet<OwnIdentity> treeOwners = OwnIdentity.getAll(db);
+               while(treeOwners.hasNext())
+                       updateScore (db, treeOwners.next());
+       }

+       public void updateScore (ObjectContainer db, OwnIdentity treeOwner) 
throws DuplicateScoreException, DuplicateTrustException {
+               
+               if(this == treeOwner) return;
+               
+               boolean hasNegativeTrust = false;
+               boolean changedCapacity = false;
+               Score score;
+               
+               int value = computeScoreValue(db, treeOwner);
+               int rank = computeRank(db, treeOwner);
+               
+               if(rank == -1) { // -1 value means the identity is not in the 
trust tree
+                       try { // If he had a score, we delete it
+                               score = getScore(treeOwner, db);
+                               db.delete(score); // He had a score, we delete 
it
+                               changedCapacity = true;
+                       } catch (NotInTrustTreeException e) {} 
+               }
+               else { // The identity is in the trust tree
+                       
+                       try { // Get existing score or create one if needed
+                               score = getScore(treeOwner, db);
+                       } catch (NotInTrustTreeException e) {
+                               score = new Score(treeOwner, this, 0, -1, 0);
+                       }
+                       
+                       score.setScore(value);
+                       score.setRank(rank + 1);
+                       int oldCapacity = score.getCapacity();
+                       
+                       // Does the treeOwner personally distrust this identity 
?
+                       try {
+                               if(getReceivedTrust(treeOwner, db).getValue() < 
0) hasNegativeTrust = true;
+                       } catch (NotTrustedException e) {}
+                       
+                       if(hasNegativeTrust) score.setCapacity(0);
+                       else score.setCapacity(capacities[score.getRank()]);
+                       
+                       if(score.getCapacity() != oldCapacity) changedCapacity 
= true;
+                       db.store(score);
+               }
+               
+               if(changedCapacity) { // We have to update trustees' score
+                       ObjectSet<Trust> trustees = getGivenTrusts(db);
+                       while(trustees.hasNext()) 
trustees.next().getTrustee().updateScore(db, treeOwner);
+               }
+       }
+       
+       public int computeScoreValue(ObjectContainer db, OwnIdentity treeOwner) 
throws DuplicateScoreException {
+               int value = 0;
+               
+               ObjectSet<Trust> receivedTrusts = getReceivedTrusts(db);
+               while(receivedTrusts.hasNext()) {
+                       Trust trust = receivedTrusts.next();
+                       try {
+                               value += trust.getValue() * 
trust.getTruster().getScore(treeOwner, db).getCapacity();
+                       } catch (NotInTrustTreeException e) {}
+               }
+               return value;
+       }
+       
+       public int computeRank(ObjectContainer db, OwnIdentity treeOwner) 
throws DuplicateScoreException {
+               int rank = -1;
+               
+               ObjectSet<Trust> receivedTrusts = getReceivedTrusts(db);
+               while(receivedTrusts.hasNext()) {
+                       Trust trust = receivedTrusts.next();
+                       try {
+                               int trusterRank = 
trust.getTruster().getScore(treeOwner, db).getRank();
+                               if(rank == -1 || trusterRank < rank) rank = 
trusterRank;
+                       } catch (NotInTrustTreeException e) {}
+               }
+               return rank;
+       }
+       
        public void setRequestURI(FreenetURI requestURI) throws 
InvalidParameterException {
                if(requestURI.getKeyType().equals("SSK")) requestURI = 
requestURI.setKeyType("USK");
                if(!requestURI.getKeyType().equals("USK")) throw new 
InvalidParameterException("Key type not supported");

Modified: trunk/apps/WoT/src/plugins/WoT/IdentityParser.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityParser.java  2008-09-05 14:06:22 UTC 
(rev 22452)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityParser.java  2008-09-05 14:13:20 UTC 
(rev 22453)
@@ -90,7 +90,7 @@
                                                int value = 
Integer.parseInt(attrs.getValue("value"));
                                                String comment = 
attrs.getValue("comment");

-                                               wot.setTrust(new 
Trust(identity, trustee, value, comment));                                     
+                                               identity.setTrust(db, trustee, 
value, comment);
                                        }
                                        catch (UnknownIdentityException e) {


Modified: trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java     2008-09-05 14:06:22 UTC 
(rev 22452)
+++ trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java     2008-09-05 14:13:20 UTC 
(rev 22453)
@@ -80,7 +80,9 @@
                return getById(db, new ByteArrayWrapper(uri.getRoutingKey()));
        }

-               
+       public static ObjectSet<OwnIdentity> getAll (ObjectContainer db) {
+               return db.queryByExample(OwnIdentity.class);
+       }

        public void exportToXML(ObjectContainer db, OutputStream os) throws 
ParserConfigurationException, TransformerConfigurationException, 
TransformerException, FileNotFoundException, IOException, Db4oIOException, 
DatabaseClosedException, InvalidParameterException {


Modified: trunk/apps/WoT/src/plugins/WoT/Trust.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Trust.java   2008-09-05 14:06:22 UTC (rev 
22452)
+++ trunk/apps/WoT/src/plugins/WoT/Trust.java   2008-09-05 14:13:20 UTC (rev 
22453)
@@ -28,26 +28,14 @@
        private int value;
        private String comment;

-       
-       /**
-        * Creates a trust relationship between two identities
-        * 
-        * @param truster Identity that gives the trust
-        * @param trustee Identity that receives the trust
-        * @param value Numeric value of the trust [-100;+100] 
-        * @throws InvalidParameterException 
-        */
-       public Trust(Identity truster, Identity trustee, int value) throws 
InvalidParameterException {
+               
+       public Trust(Identity truster, Identity trustee, int value, String 
comment) throws InvalidParameterException {
                this.truster = truster;
                this.trustee = trustee;
                setValue(value);
+               setComment(comment);
        }

-       public Trust(Identity truster, Identity trustee, int value, String 
comment) throws InvalidParameterException {
-               this(truster, trustee, value);
-               this.comment = comment;
-       }
-       
        public static int getNb(ObjectContainer db) {
                ObjectSet<Trust> trusts = db.queryByExample(Trust.class);
                return trusts.size();

Modified: trunk/apps/WoT/src/plugins/WoT/Trustlist.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Trustlist.java       2008-09-05 14:06:22 UTC 
(rev 22452)
+++ trunk/apps/WoT/src/plugins/WoT/Trustlist.java       2008-09-05 14:13:20 UTC 
(rev 22453)
@@ -32,8 +32,7 @@
         * @throws Db4oIOException 
         */
        public Trustlist(ObjectContainer db, OwnIdentity truster) throws 
Db4oIOException, DatabaseClosedException, InvalidParameterException {
-               
-               list = db.queryByExample(new Trust(truster,null,0));
+               list = truster.getGivenTrusts(db);
        }       

        /**

Modified: trunk/apps/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-09-05 14:06:22 UTC (rev 
22452)
+++ trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-09-05 14:13:20 UTC (rev 
22453)
@@ -9,12 +9,8 @@

 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
-import com.db4o.ext.DatabaseClosedException;
-import com.db4o.ext.Db4oIOException;
 import com.db4o.query.Query;

-import freenet.support.Logger;
-
 /**
  * The Web of Trust
  * 
@@ -23,15 +19,7 @@
  */
 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;

@@ -44,160 +32,6 @@
                this.db = db;
        }

-       /**
-        * Handles the creation of a new trust relationship, or the update of 
an existing one.
-        * @param newTrust A Trust object reflecting the trust relationship.
-        * @throws InvalidParameterException 
-        * @throws DatabaseClosedException 
-        * @throws Db4oIOException 
-        * @throws DuplicateScoreException 
-        * @throws DuplicateTrustException 
-        * @throws NotTrustedException 
-        */
-       public void setTrust(Trust newTrust) throws Db4oIOException, 
DatabaseClosedException, InvalidParameterException, DuplicateScoreException, 
NotTrustedException, DuplicateTrustException {
-               
-               boolean changedTrustValue = false;
-               boolean changedComment = false;
-               
-               Logger.debug(this, "Handling : " + newTrust);
-
-               // FIXME For some reason this never works and we keep 
creating/storing new Trust objects !
-               ObjectSet<Trust> trustResult = db.queryByExample(new 
Trust(newTrust.getTruster(), newTrust.getTrustee(), 0, null));
-               if(trustResult.size() != 0) { // We are updating a existing 
trust relationship
-                       Trust oldTrust = trustResult.next();
-                       
-                       Logger.debug(this, "Updating : " + oldTrust);
-                       
-                       if(oldTrust.getValue() != newTrust.getValue()) { // The 
trust value has changed
-                               changedTrustValue = true;
-                               oldTrust.setValue(newTrust.getValue());
-                       }
-                       if(oldTrust.getComment() != newTrust.getComment()) { // 
The comment has changed
-                               changedComment = true;
-                               oldTrust.setComment(newTrust.getComment());
-                       }
-                       if(changedComment || changedTrustValue) 
db.store(oldTrust); // We store the changes, if needed
-               }
-               else { // This is a new trust relationship
-                       
-                       Logger.debug(this, "Creating : " + newTrust);
-                       
-                       db.store(newTrust);
-                       changedTrustValue = true;
-               }
-
-               if(changedTrustValue) { // We have to update the trust tree, 
starting with the trustee
-                       
-                       Logger.debug(this, "The trust is new or changed, 
updating the trust trees.");
-                       
-                       ObjectSet<OwnIdentity> ownIdentites = 
db.queryByExample(OwnIdentity.class);
-                       while(ownIdentites.hasNext()) { // Each OwnIdentity has 
its trust tree, we update all
-                               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
-        * @throws InvalidParameterException 
-        * @throws DatabaseClosedException 
-        * @throws Db4oIOException 
-        * @throws DuplicateScoreException 
-        * @throws DuplicateTrustException 
-        * @throws NotTrustedException 
-        */
-       private void updateScore(OwnIdentity treeOwner, Identity identity) 
throws Db4oIOException, DatabaseClosedException, InvalidParameterException, 
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
-               
-               Score score;
-               
-               int oldCapacity = 0;
-               int newScore = 0;
-               int newRank = -1;
-               int newCapacity = 0;
-               boolean hasNegativeTrust = false;
-               
-               // Don't update in our own trustTree
-               if(identity == treeOwner) return;
-               
-               // We get the trust given by the tree owner (because if it is 
negative, the identity has no capacity)
-               Trust trust = identity.getReceivedTrust(treeOwner, db);
-               if(trust != null && trust.getValue() <= 0) hasNegativeTrust = 
true;
-               
-               // We get get all trust received by the identity
-               ObjectSet<Trust> trusters = identity.getReceivedTrusts(db);
-               while(trusters.hasNext()) {
-                       
-                       Trust trusterTrust = trusters.next();
-                                                               
-                       // We get the score of each identity that gives trust 
to this one
-                       try {
-                               Score trusterScore = 
trusterTrust.getTruster().getScore(treeOwner, db);
-                               if(trusterScore.getCapacity() > 0) { // If the 
truster has capacity                             
-                                       newScore += trusterScore.getCapacity() 
* trusterTrust.getValue() / 100; // We update the trustee's score
-                                       if(newRank == -1 || 
trusterScore.getRank() < newRank) // We store the lowest truster's rank (but 
not -1) 
-                                               newRank = 
trusterScore.getRank();
-                               }
-                       }
-                       catch(NotInTrustTreeException e) {
-                               // The truster is not in the trust tree
-                               continue;
-                       }
-
-               }
-               
-               if(newRank == -1) { // None of the trusters had capacity in 
that trust tree, this identity isn't either
-                        if(!(identity instanceof OwnIdentity)) {
-                               try {
-                                       score = identity.getScore(treeOwner, 
db); 
-                                       db.delete(score); // We get this 
identity out of this trust tree                                
-                               }catch (NotInTrustTreeException e) {
-                                       // Nothing to do
-                               }
-                        }
-                       return;
-               }
-               
-               if(newRank != -1)       newRank++; // We don't want to give the 
trustee the same rank as the truster, right ?
-               
-               if((newScore > 0) && !hasNegativeTrust) {
-                       if(newRank >= capacities.length) {
-                               newCapacity = capacities[capacities.length - 1];
-                       }
-                       else {
-                               newCapacity = capacities[newRank];
-                       }
-               }
-               
-               // We get the score of the identity, or create one if it 
doesn't exist 
-               try {
-                       score = identity.getScore(treeOwner, db);
-               } catch (NotInTrustTreeException e) {
-                       score = new Score(treeOwner, identity, 0, -1, 0);
-               }
-               
-               oldCapacity = score.getCapacity();
-               
-               if(score.getRank() != 0) { // We don't want our own identites' 
rank to be changed either
-                       score.setRank(newRank);
-                       score.setCapacity(newCapacity);
-               }
-               score.setScore(newScore);
-
-               db.store(score);
-               // We don't commit here because this method is going to be 
called *many* times while parsing a trust list.
-       
-               Logger.debug(this, score.toString());
-               
-               if(oldCapacity != newCapacity) { // If the capacity has 
changed, we have to update all identities this one trusts 
-                       ObjectSet<Trust> trustees = identity.getGivenTrusts(db);
-                       while(trustees.hasNext()) {
-                               updateScore(treeOwner, 
trustees.next().getTrustee());
-                       }
-               }
-       }
-
        public int getNbOwnIdentities() {
                ObjectSet<OwnIdentity> ownIdentities = 
db.queryByExample(OwnIdentity.class);
                return ownIdentities.size();

Modified: trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-09-05 14:06:22 UTC 
(rev 22452)
+++ trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-09-05 14:13:20 UTC 
(rev 22453)
@@ -276,8 +276,7 @@

        private void setTrust(OwnIdentity truster, Identity trustee, int value, 
String comment) throws TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException, Db4oIOException, DatabaseClosedException, 
InvalidParameterException, DuplicateScoreException, NotTrustedException, 
DuplicateTrustException {

-               Trust trust = new Trust(truster, trustee, value, comment);
-               wot.setTrust(trust);
+               truster.setTrust(db, trustee, value, comment);
                truster.updated();
                db.store(truster);
                db.commit();    
@@ -334,7 +333,7 @@
                db.store(score);

                // This identity trusts the seed identity
-               wot.setTrust(new Trust(identity, seed, 100, "I trust the WoT 
plugin"));
+               identity.setTrust(db, seed, 100, "I trust the WoT plugin");

                Logger.debug(this, "Successfully created a new OwnIdentity (" + 
identity.getNickName() + ")");



Reply via email to