TrustGrapher  r52
A playabale simulator for modelling trust between agents
D:/src/cu/trustGrapher/loading/LogReader.java
Go to the documentation of this file.
00001 
00002 package cu.trustGrapher.loading;
00003 
00004 import cu.trustGrapher.TrustGrapher;
00005 import cu.trustGrapher.eventplayer.TrustLogEvent;
00006 import cu.trustGrapher.graphs.SimAbstractGraph;
00007 
00008 import java.io.BufferedReader;
00009 import java.io.File;
00010 import java.io.FileReader;
00011 import java.io.IOException;
00012 import java.util.ArrayList;
00013 import java.util.List;
00014 
00015 import javax.swing.SwingWorker;
00016 import aohara.utilities.ChatterBox;
00017 import aohara.utilities.AreWeThereYet;
00018 
00024 public class LogReader extends SwingWorker<ArrayList<TrustLogEvent>, String> {
00025 
00026     protected TrustGrapher trustGrapher;
00027     protected AreWeThereYet loadingBar;
00028     private File logFile;
00029 
00031 
00034     private LogReader(TrustGrapher trustGrapher, File logFile, AreWeThereYet loadingBar) {
00035         this.loadingBar = loadingBar;
00036         this.trustGrapher = trustGrapher;
00037         this.logFile = logFile;
00038         //Disable the menu bars to stop user from messing up background thread
00039         trustGrapher.enableMenu(false);
00040     }
00041 
00043 
00050     public static void startReader(TrustGrapher trustGrapher, File logFile, AreWeThereYet loadingBar){
00051         LogReader logReader = new LogReader(trustGrapher, logFile, loadingBar);
00052         logReader.execute();
00053     }
00054 
00056 
00061     private int findTotalLines(File logFile) {
00062         try {
00063             BufferedReader reader = new BufferedReader(new FileReader(logFile));
00064             skipToData(reader);
00065             int totalLines = 0;
00066             while (true) {
00067                 if (reader.readLine() != null) {
00068                     totalLines++;
00069                 } else {
00070                     return totalLines;
00071                 }
00072             }
00073         } catch (IOException ex) {
00074             ChatterBox.error(this, "findTotalLines()", "Problem reading log");
00075         }
00076         return 0;
00077     }
00078 
00080 
00083     @Override
00084     protected void done() {
00085         loadingBar.loadingComplete();
00086         try {
00087             if (isDone()) {
00088                 trustGrapher.startGraph(get());
00089             } else {
00090                 ChatterBox.error(this, "done()", "Attempted to load the trust log events when they were not done loading");
00091             }
00092         } catch (Exception ex) {
00093             ex.printStackTrace();
00094         }
00095         trustGrapher.enableMenu(true);
00096     }
00097 
00103     @Override
00104     protected ArrayList<TrustLogEvent> doInBackground() throws Exception {
00105         TrustLogEvent event = null;
00106         int totalLines = findTotalLines(logFile);
00107         ArrayList<TrustLogEvent> logEvents = new ArrayList<TrustLogEvent>(totalLines);
00108         loadingBar.loadingStarted(totalLines, "Log Events");
00109         BufferedReader logReader = new BufferedReader(new FileReader(logFile));
00110         skipToData(logReader);
00111 
00112         logEvents.add(null); //Adding an empty event the start.  This is to signify that no feedback has been given yet
00113         //reading logFile
00114         for (int i = 0; i < totalLines; i++) {
00115             event = new TrustLogEvent(logReader.readLine()); //Read the next line in the 
00116             logEvents.add(event); //add this log event to the list
00117             for (SimAbstractGraph graph : trustGrapher.getGraphs()) {
00118                 graph.graphConstructionEvent(event); //Build any necessary entities referenced by the event
00119             }
00120             publish("progress");
00121         }
00122 
00123         //An empty event is not added to the event list, this is just to get the non-feedback graphs to do a construction event
00124         //since they will only construct their edges when passed a null event.  During regular events, they only add vertices if needed
00125         //If they were to constuct edges after every event, it would severely slow the loading of long logs
00126         for (SimAbstractGraph graph : trustGrapher.getGraphs()) {
00127             graph.graphConstructionEvent(null);
00128         }
00129         return logEvents;
00130     }
00131 
00136     @Override
00137     protected void process(List<String> list) {
00138         for (int i = 0; i < list.size(); i++) {
00139             loadingBar.loadingProgress();
00140         }
00141     }
00142 
00147     private void skipToData(BufferedReader log) {
00148         String line;
00149         while (true) { //reading lines log file
00150             try {
00151                 line = log.readLine();
00152                 if (line.equals("@data") || line.equals("@data\n")) { //Wait until the data field has started
00153                     break;
00154                 }
00155                 publish("progress");
00156             } catch (IOException ex) {
00157                 ChatterBox.error(this, "skipToData()", "Read a null line while trying to skip to data.");
00158             }
00159         }
00160     }
00161 }
00163