added code for keeping track of timestamps

mp2079 [2003-05-02 22:10:08]
added code for keeping track of timestamps
Filename
littlejil/PluginUtil.java
littlejil/TaskExpanderPlugin.java
littlejil/log4j.properties
diff --git a/littlejil/PluginUtil.java b/littlejil/PluginUtil.java
index 7d2fe88..52dc79a 100644
--- a/littlejil/PluginUtil.java
+++ b/littlejil/PluginUtil.java
@@ -2,10 +2,9 @@ package psl.workflakes.littlejil;

 import org.cougaar.planning.ldm.plan.AspectType;
 import org.cougaar.planning.ldm.plan.Constraint;
+import org.apache.log4j.Logger;

-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.Vector;
+import java.util.*;

 /**
  * General utility functions
@@ -31,4 +30,68 @@ public class PluginUtil {

         return v;
     }
+
+    /**
+     * Used to keep track of timestamps and log them in an analyzable format
+     */
+    public static class Timing {
+
+        private static final Logger logger = Logger.getLogger("Timing");
+
+        private static ArrayList columns = new ArrayList();
+        private static StringBuffer row = new StringBuffer();
+        private static int currentCol = 0;
+        private static boolean printColumns = false;
+
+        public static void addTimestamp(String name) {
+
+            addTimestamp(name, System.currentTimeMillis());
+        }
+
+        /**
+         * Add a timestamp that will go in the "current" row of values.
+         * @param value
+         */
+        public static void addTimestamp(String name, long value) {
+
+            if (columns.size() == currentCol) {
+                columns.add(name);
+                printColumns = true;
+            }
+            else if (!columns.get(currentCol).equals(name)) {
+                columns.add(currentCol, name);
+                printColumns = true;
+            }
+
+            if (row.length() > 0) {
+                row.append(",");
+            }
+
+            row.append(value);
+
+            currentCol++;
+        }
+
+        public static void newRow() {
+
+            if (printColumns) {
+                StringBuffer buf = new StringBuffer();
+                for (int i = 0; i < columns.size(); i++) {
+                    String s = (String) columns.get(i);
+                    if (i > 0) {
+                        buf.append(",");
+                    }
+                    buf.append(s);
+                }
+                logger.debug(buf.toString());
+                printColumns = false;
+            }
+
+            logger.debug(row.toString());
+
+            row = new StringBuffer();
+            currentCol = 0;
+        }
+
+    }
 }
diff --git a/littlejil/TaskExpanderPlugin.java b/littlejil/TaskExpanderPlugin.java
index 77cb206..a61edeb 100644
--- a/littlejil/TaskExpanderPlugin.java
+++ b/littlejil/TaskExpanderPlugin.java
@@ -1,8 +1,6 @@
 package psl.workflakes.littlejil;

-import java.util.Enumeration;
-import java.util.Collection;
-import java.util.Iterator;
+import java.util.*;

 import org.apache.log4j.Logger;
 import org.cougaar.core.blackboard.IncrementalSubscription;
@@ -16,13 +14,19 @@ import laser.littlejil.ParameterBinding;
 import laser.littlejil.ParameterDeclaration;

 /**
- * This class gets tasks posted on a blackboard and expand their workflows as necessary
+ * This class gets tasks posted on a blackboard and expand their workflows as necessary.
+ *
+ *
+ *
  * @author matias
  */

 public class TaskExpanderPlugin extends ComponentPlugin {

     private static final Logger logger = Logger.getLogger(TaskExpanderPlugin.class);
+
+    private static Vector listeners = new Vector();
+
     private DomainService domainService;

     private IncrementalSubscription expansionsSubscription;
@@ -72,6 +76,14 @@ public class TaskExpanderPlugin extends ComponentPlugin {

     }

+    public static void addListener(Listener listener) {
+        listeners.add(listener);
+    }
+
+    public static void removeListener(Listener listener) {
+        listeners.remove(listener);
+    }
+
     public void execute() {

         // assumming that the LittleJILExpanderPlugin has already published a stepsTable
@@ -113,6 +125,12 @@ public class TaskExpanderPlugin extends ComponentPlugin {
                 // but would lose history? (would also require implementing PrivilegedClaimant)
                 //blackboard.publishRemove(expansion);

+                // notify listeners of task ending
+                for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
+                    Listener listener = (Listener) iterator.next();
+                    listener.taskFinished(expansion.getTask().getVerb().toString());
+                }
+
             }

         }
@@ -147,12 +165,11 @@ public class TaskExpanderPlugin extends ComponentPlugin {
                     else if (constrainedTask.getAnnotation() != null && constrainedTask.getAnnotation() instanceof ChoiceAnnotation) {

                         logger.debug("task " + constrainedTask.getVerb() + " is a CHOICE task");
-                        Step step = ((ChoiceAnnotation)constrainedTask.getAnnotation()).chooseSubstep();
+                        Step step = ((ChoiceAnnotation) constrainedTask.getAnnotation()).chooseSubstep();

                         MakeTaskRequest request = new MakeTaskRequest(constrainedTask, step);
                         blackboard.publishAdd(request);
-                    }
-                    else {
+                    } else {
                         logger.info("publishing task " + constrainedTask.getVerb());
                         setInParams(constrainedTask);
                         blackboard.publishAdd(constrainedTask);
@@ -161,6 +178,17 @@ public class TaskExpanderPlugin extends ComponentPlugin {
                         if (planElement != null && planElement instanceof Expansion) {
                             //logger.debug("publishing tasks's expansion:" + planElement);
                             blackboard.publishChange(planElement);
+
+                            // notify listeners of task being published
+                            for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
+                                Listener listener = (Listener) iterator.next();
+                                listener.taskPublished(constrainedTask.getVerb().toString());
+                            }
+                        } else {
+                            for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
+                                Listener listener = (Listener) iterator.next();
+                                listener.leafTaskPublished(constrainedTask.getVerb().toString());
+                            }
                         }
                     }
                 }
@@ -226,16 +254,32 @@ public class TaskExpanderPlugin extends ComponentPlugin {
                     if (task.getAnnotation() != null && task.getAnnotation() instanceof ChoiceAnnotation) {

                         logger.debug("task " + task.getVerb() + " is a CHOICE task");
-                        Step step = ((ChoiceAnnotation)task.getAnnotation()).chooseSubstep();
+                        Step step = ((ChoiceAnnotation) task.getAnnotation()).chooseSubstep();

                         MakeTaskRequest request = new MakeTaskRequest(task, step);
                         blackboard.publishAdd(request);
-                    }
-                    else {
+                    } else {
                         logger.debug("task " + task.getVerb() + " is not constrained, publishing it");

                         setInParams(task);
                         blackboard.publishAdd(task);
+
+                        PlanElement planElement = task.getPlanElement();
+                        if (planElement != null && planElement instanceof Expansion) {
+                            //logger.debug("publishing tasks's expansion:" + planElement);
+                            blackboard.publishChange(planElement);
+
+                            // notify listeners of task being published
+                            for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
+                                Listener listener = (Listener) iterator.next();
+                                listener.taskPublished(task.getVerb().toString());
+                            }
+                        } else {
+                            for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
+                                Listener listener = (Listener) iterator.next();
+                                listener.leafTaskPublished(task.getVerb().toString());
+                            }
+                        }
                     }
                 }

@@ -258,7 +302,7 @@ public class TaskExpanderPlugin extends ComponentPlugin {
         for (Iterator i = paramBindings.iterator(); i.hasNext();) {
             ParameterBinding binding = (ParameterBinding) i.next();
             if (binding.getBindingMode() == ParameterBinding.COPY_IN ||
-                binding.getBindingMode() == ParameterBinding.COPY_IN_AND_OUT) {
+                    binding.getBindingMode() == ParameterBinding.COPY_IN_AND_OUT) {

                 ParameterDeclaration childDeclaration = binding.getDeclarationInChild();
                 ParameterDeclaration parentDeclaration = binding.getDeclarationInParent();
@@ -269,7 +313,7 @@ public class TaskExpanderPlugin extends ComponentPlugin {
                     childDeclaration.setParameterValue(parentDeclaration.getParameterValue());

                     logger.debug("in param " + childDeclaration.getName() +
-                                 " set to " + childDeclaration.getParameterValue());
+                            " set to " + childDeclaration.getParameterValue());
                 }
             }
         }
@@ -315,6 +359,21 @@ public class TaskExpanderPlugin extends ComponentPlugin {

     }

+
+    /**
+     * This class can be subclassed and used to subscribe to events from the TaskExpanderPlugin.
+     *
+     */
+    public static abstract class Listener {
+
+        public abstract void taskPublished(String taskName);
+
+        public abstract void leafTaskPublished(String taskName);
+
+        public abstract void taskFinished(String taskName);
+
+    }
+
 }


diff --git a/littlejil/log4j.properties b/littlejil/log4j.properties
index bb1adf6..71beb02 100644
--- a/littlejil/log4j.properties
+++ b/littlejil/log4j.properties
@@ -1,11 +1,17 @@
+log4j.rootLogger=DEBUG, Console

-log4j.logger.org.cougaar=WARN, WorkflakesConsole
+log4j.logger.org.cougaar=WARN
+log4j.logger.psl.workflakes.littlejil=INFO
+log4j.logger.psl.ai2tv=DEBUG

-# choose debug level
-#log4j.logger.psl.workflakes=DEBUG, WorkflakesConsole
-log4j.logger.psl.workflakes=INFO, WorkflakesConsole
+log4j.logger.Timing=DEBUG,Timing

-log4j.appender.WorkflakesConsole=org.apache.log4j.ConsoleAppender
-log4j.appender.WorkflakesConsole.layout=org.apache.log4j.PatternLayout
-log4j.appender.WorkflakesConsole.layout.ConversionPattern=%C{1}: %m%n
+log4j.appender.Console=org.apache.log4j.ConsoleAppender
+log4j.appender.Console.layout=org.apache.log4j.PatternLayout
+log4j.appender.Console.layout.ConversionPattern=%d{mm:ss} %C{1}: %m%n
+
+log4j.appender.Timing=org.apache.log4j.FileAppender
+log4j.appender.Timing.file=ai2tv-timing.log
+log4j.appender.Timing.layout=org.apache.log4j.PatternLayout
+log4j.appender.Timing.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS},%m%n