incubator-s4-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mmo...@apache.org
Subject [12/50] [abbrv] Experiment with builder pattern.
Date Tue, 03 Jan 2012 14:03:28 GMT
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/fb61d6df/subprojects/s4-example/src/main/java/org/apache/s4/example/model/Controller.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-example/src/main/java/org/apache/s4/example/model/Controller.java
b/subprojects/s4-example/src/main/java/org/apache/s4/example/model/Controller.java
index 8529196..24f6b1f 100644
--- a/subprojects/s4-example/src/main/java/org/apache/s4/example/model/Controller.java
+++ b/subprojects/s4-example/src/main/java/org/apache/s4/example/model/Controller.java
@@ -16,7 +16,6 @@
  */
 package org.apache.s4.example.model;
 
-
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.HashMap;
@@ -26,10 +25,10 @@ import java.util.concurrent.TimeUnit;
 import org.apache.s4.core.Receiver;
 import org.apache.s4.core.Sender;
 import org.apache.s4.model.Model;
+import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import ch.qos.logback.classic.Level;
-import org.slf4j.Logger;
 
 import com.google.inject.Inject;
 import com.google.inject.name.Named;
@@ -39,8 +38,7 @@ import com.google.inject.name.Named;
  */
 public class Controller {
 
-    private static final Logger logger = LoggerFactory
-            .getLogger(Controller.class);
+    private static final Logger logger = LoggerFactory.getLogger(Controller.class);
 
     final private String trainFilename;
     final private String testFilename;
@@ -55,10 +53,8 @@ public class Controller {
     final private Receiver receiver;
 
     @Inject
-    private Controller(@Named("model.train_data") String trainFilename,
-            @Named("model.test_data") String testFilename, Model model,
-            @Named("model.vector_size") int vectorSize,
-            @Named("model.num_iterations") int numIterations,
+    private Controller(@Named("model.train_data") String trainFilename, @Named("model.test_data")
String testFilename,
+            Model model, @Named("model.vector_size") int vectorSize, @Named("model.num_iterations")
int numIterations,
             @Named("model.output_interval_in_seconds") int outputInterval,
             @Named("model.logger.level") String logLevel, Sender sender, Receiver receiver)
{
 
@@ -88,23 +84,21 @@ public class Controller {
             /* Get vector size and number of classes from data set. */
             getDataSetInfo(trainFilename);
 
-            MyApp app = new MyApp(numClasses, numTrainVectors, model,
-                    outputInterval, TimeUnit.SECONDS);
-            
+            MyApp app = new MyApp(numClasses, numTrainVectors, model, outputInterval, TimeUnit.SECONDS);
+
             app.setCommLayer(sender, receiver);
 
             logger.info("Init app.");
-            app.init();
+            app.initApp();
 
             /* Initialize modelPEs by injecting one dummy events per class. */
             for (int i = 0; i < numClasses; i++) {
-                ObsEvent obsEvent = new ObsEvent(-1, new float[vectorSize],
-                        -Float.MAX_VALUE, i, -1, true);
+                ObsEvent obsEvent = new ObsEvent(-1, new float[vectorSize], -Float.MAX_VALUE,
i, -1, true);
                 app.injectByKey(obsEvent);
             }
 
             /* Wait until the app is initialized. */
-            while(!app.isInited()) {
+            while (!app.isInited()) {
                 Thread.sleep(1);
             }
 
@@ -116,7 +110,7 @@ public class Controller {
                 /*
                  * Make sure all the data has been processed.
                  */
-                while(!app.isTrained(i)) {
+                while (!app.isTrained(i)) {
                     Thread.sleep(5);
                 }
             }
@@ -130,22 +124,23 @@ public class Controller {
             stop = System.nanoTime();
             long testTime = stop - start;
 
-            while(!app.isTested(numTestVectors)) {
+            while (!app.isTested(numTestVectors)) {
                 Thread.sleep(5);
             }
-            
+
             /* Print final report. */
             logger.info(app.getReport());
-            
+
             /* Print timing info. */
             logger.info("Total training time was {} seconds.", trainTime / 1000000000);
             logger.info("Training time per observation was {} microseconds.", trainTime /
numTrainVectors / 1000);
-            logger.info("Training time per observation per iteration was {} microseconds.",
trainTime / numTrainVectors / numIterations / 1000);
+            logger.info("Training time per observation per iteration was {} microseconds.",
trainTime / numTrainVectors
+                    / numIterations / 1000);
             logger.info("Total testing time was {} seconds.", testTime / 1000000000);
             logger.info("Testing time per observation was {} microseconds.", testTime / numTrainVectors
/ 1000);
-            
+
             /* Done. */
-            app.remove();
+            app.closeApp();
 
         } catch (FileNotFoundException e) {
             e.printStackTrace();
@@ -159,8 +154,7 @@ public class Controller {
         }
     }
 
-    private void injectData(MyApp app, boolean isTraining, String filename)
-            throws FileNotFoundException, IOException {
+    private void injectData(MyApp app, boolean isTraining, String filename) throws FileNotFoundException,
IOException {
 
         DataFile data = new DataFile(filename);
         int count = 0;
@@ -176,15 +170,13 @@ public class Controller {
 
                 vector[j] = Float.parseFloat(result[j + 1]);
             }
-            ObsEvent obsEvent = new ObsEvent(count++, vector, -Float.MAX_VALUE,
-                    classID, -1, isTraining);
+            ObsEvent obsEvent = new ObsEvent(count++, vector, -Float.MAX_VALUE, classID,
-1, isTraining);
             app.injectToAll(obsEvent);
         }
         data.close();
     }
 
-    private void getDataSetInfo(String filename) throws FileNotFoundException,
-            IOException {
+    private void getDataSetInfo(String filename) throws FileNotFoundException, IOException
{
 
         Map<Integer, Long> countsPerClass = new HashMap<Integer, Long>();
 
@@ -197,8 +189,7 @@ public class Controller {
             /* Format is: label val1 val2 ... valN */
             if (vectorSize != result.length - 1) {
                 throw new IllegalArgumentException("vectorSize: (" + vectorSize
-                        + ") does not match number of columns in data file ("
-                        + (result.length - 1) + ").");
+                        + ") does not match number of columns in data file (" + (result.length
- 1) + ").");
             }
 
             /* Class ID range starts in 1, shift to start in zero. */

http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/fb61d6df/subprojects/s4-example/src/main/java/org/apache/s4/example/model/MyApp.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-example/src/main/java/org/apache/s4/example/model/MyApp.java b/subprojects/s4-example/src/main/java/org/apache/s4/example/model/MyApp.java
index 869a5cc..a069513 100644
--- a/subprojects/s4-example/src/main/java/org/apache/s4/example/model/MyApp.java
+++ b/subprojects/s4-example/src/main/java/org/apache/s4/example/model/MyApp.java
@@ -25,7 +25,6 @@ import org.apache.s4.model.Model;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 public class MyApp extends App {
 
     private static final Logger logger = LoggerFactory.getLogger(MyApp.class);
@@ -42,8 +41,7 @@ public class MyApp extends App {
     Stream<ObsEvent> assignmentStream;
     private String report;
 
-    MyApp(int numClasses, long numVectors, Model model, int outputInterval,
-            TimeUnit timeUnit) {
+    MyApp(int numClasses, long numVectors, Model model, int outputInterval, TimeUnit timeUnit)
{
         super();
         this.numClasses = numClasses;
         this.numVectors = numVectors;
@@ -63,43 +61,44 @@ public class MyApp extends App {
     }
 
     @Override
-    protected void start() {
+    protected void onStart() {
+
+    }
 
+    /* init() visibility is protected. Normally the Server class only should call init. */
+    public void initApp() {
+        init();
     }
 
     @Override
-    protected void init() {
+    protected void onInit() {
 
         metricsPE = createPE(MetricsPE.class);
 
-        Stream<ResultEvent> resultStream = createStream(
-                "Result Stream", new ResultKeyFinder(), metricsPE);
+        Stream<ResultEvent> resultStream = createStream("Result Stream", new ResultKeyFinder(),
metricsPE);
 
         modelPE = createPE(ModelPE.class);
         modelPE.setModel(model);
         modelPE.setNumVectors(numVectors);
-        
-        assignmentStream = createStream( "Assignment Stream",
-                new ClassIDKeyFinder(), modelPE);
+
+        assignmentStream = createStream("Assignment Stream", new ClassIDKeyFinder(), modelPE);
 
         MaximizerPE maximizerPE = createPE(MaximizerPE.class);
         maximizerPE.setNumClasses(numClasses);
         maximizerPE.setAssignmentStream(assignmentStream);
 
-        Stream<ObsEvent> distanceStream = createStream(
-                "Distance Stream", new ObsIndexKeyFinder(), maximizerPE);
+        Stream<ObsEvent> distanceStream = createStream("Distance Stream", new ObsIndexKeyFinder(),
maximizerPE);
 
         /*
-         * There is a loop in this graph so we need to set the stream at the
-         * end. Is there a cleaner way to do this?
+         * There is a loop in this graph so we need to set the stream at the end. Is there
a cleaner way to do this?
          */
         modelPE.setStream(distanceStream, resultStream);
         // modelPE.setOutputIntervalInEvents(10); // output every 10 events
-        metricsPE.setTimerInterval(outputInterval, timeUnit); // output every 5
+        metricsPE.withTimerInterval(outputInterval, timeUnit); // output every 5
                                                                // seconds
         // obsStream = new Stream<ObsEvent>(this, "Observation Stream", new
         // ClassIDKeyFinder(), modelPE);
-        obsStream = createStream( "Observation Stream", modelPE);
+        obsStream = createStream("Observation Stream", modelPE);
     }
 
     /** @return true if modelPE is initialized. */
@@ -159,10 +158,13 @@ public class MyApp extends App {
         return report;
     }
 
+    /* close() visibility is protected. Normally the Server class only should call init.
*/
+    public void closeApp() {
+        close();
+    }
+
     @Override
     protected void close() {
-        // TODO Auto-generated method stub
-
     }
 
     public long getObsCount() {
@@ -170,7 +172,7 @@ public class MyApp extends App {
         return modelPE.getObsCount();
     }
 
-    public void remove() {
-        removeAll();
+    @Override
+    protected void onClose() {
     }
 }


Mime
View raw message