incubator-s4-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mmo...@apache.org
Subject [13/50] [abbrv] git commit: Added data structures to save ap graph and added toString() to print the app graph.
Date Tue, 03 Jan 2012 14:03:28 GMT
Added data structures to save ap graph and added toString() to print the app graph.


Project: http://git-wip-us.apache.org/repos/asf/incubator-s4/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-s4/commit/d6e3684c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-s4/tree/d6e3684c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-s4/diff/d6e3684c

Branch: refs/heads/piper
Commit: d6e3684ccc0f11c8cd06264096d0820ab87faada
Parents: b8cea4a
Author: Leo Neumeyer <leo@s4.io>
Authored: Sat Dec 10 18:15:30 2011 -0800
Committer: Leo Neumeyer <leo@s4.io>
Committed: Sat Dec 10 18:15:30 2011 -0800

----------------------------------------------------------------------
 .../java/org/apache/s4/appbuilder/AppMaker.java    |   77 +++++++++++----
 .../main/java/org/apache/s4/appbuilder/MyApp.java  |   33 ++++++
 .../java/org/apache/s4/appbuilder/PEMaker.java     |    5 +-
 .../java/org/apache/s4/appbuilder/StreamMaker.java |    9 +-
 4 files changed, 99 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/d6e3684c/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/AppMaker.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/AppMaker.java b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/AppMaker.java
index bd0917d..f688809 100644
--- a/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/AppMaker.java
+++ b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/AppMaker.java
@@ -1,48 +1,61 @@
 package org.apache.s4.appbuilder;
 
+import java.util.Collection;
+import java.util.Map;
+
 import org.apache.s4.base.Event;
 import org.apache.s4.core.App;
 import org.apache.s4.core.ProcessingElement;
 
 import com.google.common.collect.LinkedListMultimap;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 
-public class AppMaker {
+abstract public class AppMaker {
+
+    /**
+     * NOTES: reflection+guice:
+     * <code>http://groups.google.com/group/google-guice/browse_thread/thread/23f4bf986a999e00/73f83a98c288a3e1?lnk=gst&q=binding+api#73f83a98c288a3e1</code>
+     */
 
     /**
      * The app graph is stored as follows:
      * <p>
-     * 
+     * PE to Stream
      * <p>
      * PE[1]: S[1,1], S[1,2], ...
      * <p>
      * PE[2]: S[2,1], S[2,2], ...
+     * <p>
+     * Stream to PE
+     * <p>
+     * S[1]: PE[1]
+     * <p>
+     * S[2] : PE[2]
+     * 
      */
 
-    private Multimap<PEMaker, StreamMaker> graph = LinkedListMultimap.create();
+    private Multimap<PEMaker, StreamMaker> psGraph = LinkedListMultimap.create();
+    private Map<StreamMaker, PEMaker> spGraph = Maps.newHashMap();
 
     public AppMaker() {
 
     }
 
-    /**
-     * Add a processing element.
-     * 
-     * @param streams
-     *            events emitted by this PE will be put into these streams.
-     * 
-     * @return a pe maker.
-     */
-    // public <T extends Event> PEMaker addPE(StreamMaker<T>... streams) {
-    //
-    // PEMaker pem = new PEMaker();
-    // for (int i = 0; i < streams.length; i++)
-    // graph.put(pem, streams[i]);
-    //
-    // return pem;
-    // }
+    abstract protected void define();
+
+    void add(PEMaker pem, StreamMaker stream) {
+
+        psGraph.put(pem, stream);
+    }
+
+    void add(StreamMaker stream, PEMaker pem) {
+
+        spGraph.put(stream, pem);
+    }
+
     public PEMaker addPE(Class<? extends ProcessingElement> type) {
-        return new PEMaker(type);
+        return new PEMaker(this, type);
     }
 
     /**
@@ -55,11 +68,33 @@ public class AppMaker {
      */
     public StreamMaker addStream(Class<? extends Event> type) {
 
-        return new StreamMaker(type);
+        return new StreamMaker(this, type);
 
     }
 
     public App make() {
         return null;
     }
+
+    public String toString() {
+
+        StringBuilder sb = new StringBuilder();
+        Map<PEMaker, Collection<StreamMaker>> psMap = psGraph.asMap();
+
+        for (Map.Entry<PEMaker, Collection<StreamMaker>> entry : psMap.entrySet())
{
+            sb.append(entry.getKey() + ": ");
+            for (StreamMaker sm : entry.getValue()) {
+                sb.append(sm + " ");
+            }
+            sb.append("\n");
+        }
+
+        for (Map.Entry<StreamMaker, PEMaker> entry : spGraph.entrySet()) {
+            sb.append(entry.getKey() + ": " + entry.getValue());
+            sb.append("\n");
+        }
+
+        return sb.toString();
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/d6e3684c/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/MyApp.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/MyApp.java b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/MyApp.java
new file mode 100644
index 0000000..f948cc7
--- /dev/null
+++ b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/MyApp.java
@@ -0,0 +1,33 @@
+package org.apache.s4.appbuilder;
+
+public class MyApp extends AppMaker {
+
+    /**
+     * @param args
+     */
+    public void define() {
+
+        PEMaker pem1, pem2;
+        StreamMaker s1;
+        StreamMaker s2, s3;
+
+        pem1 = addPE(PEZ.class);
+
+        s1 = addStream(EventA.class).withName("My first stream.").withKey("{gender}").to(pem1);
+
+        pem2 = addPE(PEY.class).to(s1);
+
+        s2 = addStream(EventB.class).withName("My second stream.").withKey("{age}").to(pem2);
+
+        s3 = addStream(EventB.class).withName("My third stream.").withKey("{height}").to(pem2);
+
+        addPE(PEX.class).to(s2).to(s3);
+    }
+
+    public static void main(String[] args) {
+
+        MyApp myApp = new MyApp();
+        myApp.define();
+        System.out.println(myApp.toString());
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/d6e3684c/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/PEMaker.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/PEMaker.java b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/PEMaker.java
index f8fc071..58a8c6c 100644
--- a/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/PEMaker.java
+++ b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/PEMaker.java
@@ -8,10 +8,12 @@ import org.apache.s4.core.ProcessingElement;
 public class PEMaker {
 
     private Class<? extends ProcessingElement> type;
+    private AppMaker appMaker;
 
     /* Only package classes can instantiate this class. */
-    PEMaker(Class<? extends ProcessingElement> type) {
+    PEMaker(AppMaker appMaker, Class<? extends ProcessingElement> type) {
         this.type = type;
+        this.appMaker = appMaker;
     }
 
     public PEMaker withTrigger(Class<? extends Event> eventType, int numEvents, long
interval, TimeUnit timeUnit) {
@@ -25,6 +27,7 @@ public class PEMaker {
     }
 
     public <T extends Event> PEMaker to(StreamMaker stream) {
+        appMaker.add(this, stream);
         return this;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/d6e3684c/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/StreamMaker.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/StreamMaker.java b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/StreamMaker.java
index c7fac03..e84c282 100644
--- a/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/StreamMaker.java
+++ b/subprojects/s4-core/src/main/java/org/apache/s4/appbuilder/StreamMaker.java
@@ -10,10 +10,12 @@ public class StreamMaker {
     private String keyFinderString;
     private PEMaker pem;
     private Class<? extends Event> type;
+    private AppMaker appMaker;
 
     /* Only package classes can instantiate this class. */
-    StreamMaker(Class<? extends Event> type) {
+    StreamMaker(AppMaker appMaker, Class<? extends Event> type) {
         this.type = type;
+        this.appMaker = appMaker;
     }
 
     /**
@@ -35,7 +37,7 @@ public class StreamMaker {
      *            a function to lookup the value of the key.
      * @return the stream maker object
      */
-    public StreamMaker withKeyFinder(KeyFinder<?> keyFinder) {
+    public StreamMaker withKey(KeyFinder<?> keyFinder) {
         this.keyFinder = keyFinder;
         this.keyFinderString = null;
         return this;
@@ -48,7 +50,7 @@ public class StreamMaker {
      *            a descriptor to lookup the value of the key.
      * @return the stream maker object
      */
-    public StreamMaker withKeyFinder(String keyFinderString) {
+    public StreamMaker withKey(String keyFinderString) {
         this.keyFinder = null;
         this.keyFinderString = keyFinderString;
         return this;
@@ -62,6 +64,7 @@ public class StreamMaker {
      * @return the stream maker object
      */
     public StreamMaker to(PEMaker pem) {
+        appMaker.add(this, pem);
         this.pem = pem;
         return this;
     }


Mime
View raw message