TrustGrapher  r52
A playabale simulator for modelling trust between agents
D:/src/cu/trustGrapher/LogGen.java
Go to the documentation of this file.
00001 
00002 package cu.trustGrapher;
00003 
00004 import cu.trustGrapher.eventplayer.TrustLogEvent;
00005 import java.io.File;
00006 import java.util.List;
00007 import java.util.Random;
00008 import aohara.utilities.BitStylus;
00009 import aohara.utilities.ChatterBox;
00010 
00015 public class LogGen {
00016 
00018     private static StringBuffer getHeader(File file) {
00019         return new StringBuffer("@relation " + file.getName() + "\n\n@attribute assessorID string\n@attribute assesseeID string\n"
00020                 + "@attribute feedbackValue string\n\n@data\n");
00021     }
00022 
00023     private static File getSaveLocation() {
00024         File file = BitStylus.chooseSaveLocation("Where would you like to save the log?", new File("/home/zalpha314/Programming/Work/TrustGrapher2/test"), new String[]{"arff"});
00025         if (!file.getAbsolutePath().endsWith(".arff")) {
00026             file = new File(file.getPath() + ".arff");
00027         }
00028         return file;
00029     }
00030 
00031     private static String generateArffLog(int length, int peers, File file) {
00032         Random r = new Random();
00033         StringBuffer s = getHeader(file);
00034         int peer1 = -1, peer2 = -1;
00035         int rating;
00036         String feedback;
00037         for (int i = 0; i < length; i++) {
00038             if (peer2 < peers) {
00039                 peer1 = peer2 + 1;
00040             } else {
00041                 peer1 = r.nextInt(peers);
00042             }
00043 
00044             if (peer1 < peers) {
00045                 peer2 = peer1 + 1;
00046             } else {
00047                 peer2 = peer1;
00048                 while (peer1 == peer2) {
00049                     peer2 = r.nextInt(peers);
00050                 }
00051             }
00052 
00053             rating = r.nextInt(11);
00054             if (rating == 10) {
00055                 feedback = "1.0";
00056             } else {
00057                 feedback = "0." + rating;
00058             }
00059             s.append(peer1).append(",").append(peer2).append(",").append(feedback).append("\n");
00060         }
00061         return s.toString();
00062     }
00063 
00064     public static void saveEventLog(List<TrustLogEvent> events) {
00065         File saveLocation = getSaveLocation();
00066         StringBuffer log = getHeader(saveLocation);
00067         for (int i = 0 ; i < events.size() - 1 ; i++) {
00068             if (events.get(i) != null){
00069                 log.append(events.get(i).toString()).append("\n");
00070             }
00071         }
00072         log.append(events.get(events.size() - 1));
00073         BitStylus.saveTextToFile(saveLocation, log.toString());
00074         if (saveLocation.exists()){
00075             ChatterBox.alert("File succesfully saved to:\n" + saveLocation.getPath());
00076         }else{
00077             ChatterBox.alert("The file was not succesfully created.\n" + saveLocation.getPath());
00078         }
00079     }
00080 
00081     public static void main(String[] args) {
00082         int peers = 1;
00083         int length = 0;
00084         while (peers > length) {
00085             length = Integer.parseInt(ChatterBox.userInput("What length would you like the log to be?"));
00086             peers = Integer.parseInt(ChatterBox.userInput("How many peers would you like?"));
00087             if (peers > length) {
00088                 ChatterBox.alert("Error: The number of peers cannot be greater than the size of the log.");
00089             }
00090         }
00091         File saveLocation = getSaveLocation();
00092         String log = generateArffLog(length, peers, saveLocation);
00093         BitStylus.saveTextToFile(saveLocation, log);
00094     }
00095 }