Author: rahul
Date: Sat Jul 30 09:00:36 2005
New Revision: 226539

URL: http://svn.apache.org/viewcvs?rev=226539&view=rev
Log:
Completing yesterday's commit of the SCXML alphas.

Added:
    
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/SCXMLDialog.java 
  (with props)
Modified:
    
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/RuleBasedDirectedDialog.java

Modified: 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/RuleBasedDirectedDialog.java
URL: 
http://svn.apache.org/viewcvs/jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/RuleBasedDirectedDialog.java?rev=226539&r1=226538&r2=226539&view=diff
==============================================================================
--- 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/RuleBasedDirectedDialog.java
 (original)
+++ 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/RuleBasedDirectedDialog.java
 Sat Jul 30 09:00:36 2005
@@ -60,8 +60,8 @@
  * navigation rules</li>
  * <li><b>Directed:</b> Only one child is active at any given time</li>
  * </ul>
- * <br>Navigation rules specified in an XML document whose URI is the custom
- * attribute of the &lt;group&gt; tag.</p>
+ * <br>Navigation rules specified in an XML document whose URI is the 
+ * config attribute of the &lt;group&gt; tag.</p>
  * 
  * @author Rahul Akolkar
  */

Added: 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/SCXMLDialog.java
URL: 
http://svn.apache.org/viewcvs/jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/SCXMLDialog.java?rev=226539&view=auto
==============================================================================
--- 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/SCXMLDialog.java 
(added)
+++ 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/SCXMLDialog.java 
Sat Jul 30 09:00:36 2005
@@ -0,0 +1,304 @@
+/*
+ *    
+ *   Copyright 2004 The Apache Software Foundation.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *
+ */
+package org.apache.taglibs.rdc.dm;
+
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.jsp.JspContext;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.JspFragment;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.taglibs.rdc.core.BaseModel;
+import org.apache.taglibs.rdc.core.Constants;
+import org.apache.taglibs.rdc.core.GroupModel;
+import org.apache.taglibs.rdc.core.GroupTag;
+import org.apache.taglibs.rdc.scxml.Context;
+import org.apache.taglibs.rdc.scxml.Evaluator;
+import org.apache.taglibs.rdc.scxml.EventDispatcher;
+import org.apache.taglibs.rdc.scxml.SCXMLDigester;
+import org.apache.taglibs.rdc.scxml.SCXMLExecutor;
+import org.apache.taglibs.rdc.scxml.Status;
+import org.apache.taglibs.rdc.scxml.TriggerEvent;
+import org.apache.taglibs.rdc.scxml.env.ELEvaluator;
+import org.apache.taglibs.rdc.scxml.env.RootContext;
+import org.apache.taglibs.rdc.scxml.env.SimpleDispatcher;
+import org.apache.taglibs.rdc.scxml.env.Tracer;
+import org.apache.taglibs.rdc.scxml.model.ModelException;
+import org.apache.taglibs.rdc.scxml.model.SCXML;
+import org.apache.taglibs.rdc.scxml.model.State;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXParseException;
+
+/**
+ * A dialog management strategy for the RDC group container which uses
+ * a SCXML configuration file to define the dialog for each &lt;group&gt;
+ * instance.
+ * 
+ * SCXML, or the "State Chart eXtensible Markup Language", provides a 
+ * generic state-machine based execution environment based on CCXML and 
+ * Harel State Tables.
+ * 
+ * The URI of the SCXML document is the config attribute of the
+ * &lt;group&gt; tag.
+ * 
+ * @author Rahul Akolkar
+ * @author Jaroslav Gergic
+ */
+public class SCXMLDialog extends DialogManagerImpl {
+       
+       private static final String ERR_DIGESTER_FAIL = "<!-- Error parsing " +
+               "SCXML document for group: \"{0}\", with message: \"{1}\" 
-->\n";
+       
+       // Logging
+       private static Log log = LogFactory.getLog(SCXMLDialog.class);
+       
+       /**
+        * The SCXML engine that will execute the SCXML document specified
+        * in the group configuration.
+        */
+       private SCXMLExecutor exec;
+       
+       public SCXMLDialog() {
+               super();
+       }
+       
+       public boolean initialize(JspContext ctx, JspFragment bodyFragment) 
+       throws JspException, IOException {
+               
+               boolean retVal = super.initialize(ctx, bodyFragment);
+               GroupModel groupModel = (GroupModel) 
stateMap.get(groupTag.getId());
+               
+               if (groupModel.getInstanceData() != null) {
+                       exec = (SCXMLExecutor) groupModel.getInstanceData();
+                       return retVal;
+               }
+               
+               SCXML scxml = null;
+               Evaluator engine = new ELEvaluator();
+               Context rootCtx = new RootContext(ctx);
+               try {
+                       scxml = SCXMLDigester.digest(((PageContext) ctx).
+                               getServletContext(), groupTag.getConfig(), 
+                               new SCXMLErrorHandler(), rootCtx, engine);
+               } catch (Exception e) {
+                       MessageFormat msgFormat = new 
MessageFormat(ERR_DIGESTER_FAIL);
+                       String errMsg = msgFormat.format(new Object[] {groupTag.
+                               getConfig(), e.getMessage()});
+               log.error(errMsg, e);
+                       ((PageContext) ctx).getOut().write(errMsg);             
        
+               }
+
+               if (scxml == null) {
+                       retVal = false;
+                       ((PageContext) ctx).getOut().write("<!-- SCXMLDialog" +
+                               ": Error parsing SCXML:" + groupTag.getConfig()+
+                               "-->\n");
+               } else {
+                       // TODO - Remove debugging statement and else above
+                       
//System.out.println(SCXMLDigester.serializeSCXML(scxml));
+               }
+               
+               EventDispatcher ed = new SimpleDispatcher();
+               Tracer trc = new Tracer();
+               try {
+                       exec = new SCXMLExecutor(engine, ed, trc);
+                       scxml.addListener(trc);
+                       exec.setSuperStep(true);
+                       exec.setStateMachine(scxml);
+               } catch (ModelException me) {
+                       retVal = false;
+                       ((PageContext) ctx).getOut().write("<!-- SCXMLDialog" +
+                               ": Model Exception in:" + groupTag.getConfig()+
+                               ", root cause: " + me.getMessage() + "-->\n");
+               }
+               groupModel.setInstanceData(exec);
+               
+               return retVal;
+       }
+       
+       /**
+        * Collect the required information based on the SCXML config
+        */     
+       public void collect(JspContext ctx, JspFragment bodyFragment) 
+       throws JspException, IOException {
+               
+               GroupModel groupModel = (GroupModel) 
stateMap.get(groupTag.getId());
+               Map modelMap = groupModel.getLocalMap();
+
+               if (modelMap.get(Constants.STR_INIT_ONLY_FLAG) == Boolean.TRUE) 
{
+                       // Start off with all children in dormant state
+                       setStateChildren(modelMap, Constants.FSM_DORMANT);
+                       groupState = Constants.GRP_ALL_CHILDREN_DORMANT;
+                       groupModel.setState(Constants.GRP_STATE_RUNNING);
+                       modelMap.put(Constants.STR_INIT_ONLY_FLAG, 
Boolean.FALSE);
+               }
+
+               if (groupModel.getState() == Constants.GRP_STATE_RUNNING) {
+                       Status s;
+                       do {
+                               s = exec.getCurrentStatus();
+                               dialogManager(groupTag);
+                               if (bodyFragment != null) {
+                                       bodyFragment.invoke(null);
+                               }
+                       } while (renderNext(groupModel, s));
+               }
+       }
+       
+       
+       /** 
+        * This method does the rule based dialog management based on the
+        * navigational rules supplied
+        */
+       private void dialogManager(GroupTag groupTag) {
+
+               GroupModel groupModel = (GroupModel) 
stateMap.get(groupTag.getId());
+               Map children = groupModel.getLocalMap();
+               if (children == null) {
+                       return;
+               }
+
+               List activeChildren = groupModel.getActiveChildren();
+               List eventList = new ArrayList();
+               Set currentStates = exec.getCurrentStatus().getStates();
+               if (currentStates != null && currentStates.size() > 0) {
+                       Iterator iter = currentStates.iterator();
+                       while (iter.hasNext()) {
+                               String id = ((State) iter.next()).getId();
+                               BaseModel model = (BaseModel) children.get(id);
+                               groupState = 
DMUtils.invokeDormantChild(children, 
+                                       activeChildren, id);
+                               if (!DMUtils.isChildDone(model)) {
+                                       continue;
+                               } else {
+                                       activeChildren.remove(model.getId());
+                                       eventList.add(model.getId() + ".done");
+                               }
+                       }
+               } else {
+                       if (activeChildren.size() > 0) {
+                               activeChildren.clear();
+                       }
+                       groupState = Constants.GRP_ALL_CHILDREN_DONE;
+                       return;
+               }
+               
+               if (eventList.size() == 0) {
+                       return;
+               } else {
+                       //fire events one at a time
+                       for (int i = 0; i < eventList.size(); i++) {
+                               TriggerEvent evts[] = new TriggerEvent[] { 
+                                       new TriggerEvent((String) 
eventList.get(i), 
+                                       TriggerEvent.SIGNAL_EVENT, null) };
+                               log.trace("Triggering event " + 
eventList.get(i));
+                               try {
+                                       exec.triggerEvents(evts);
+                               } catch (ModelException me) {
+                                       log.error(me.getMessage(), me);
+                               }
+                       }
+               }
+
+       } // end method dialogManager()
+       
+       /** 
+        * Render new states due to events triggered in last dialogManager()
+        * invocation if:
+        * a) All currently active child are done
+        * and
+        * b) The current executor status reached is not final
+        */
+       private boolean renderNext(GroupModel groupModel, Status s) {
+               Map children = groupModel.getLocalMap();
+               List activeChildren = groupModel.getActiveChildren();
+               int sz = activeChildren.size();
+               if (sz > 0) {
+                       for (int i = 0; i < sz; i++) {
+                               BaseModel model = (BaseModel)children.
+                                       get(activeChildren.get(i));
+                               if (!DMUtils.isChildDone(model)) {
+                                       return false;
+                               }
+                       }
+               } else {
+                       if (s.isFinal()) {
+                               if (activeChildren.size() > 0) {
+                                       activeChildren.clear();
+                               }
+                               groupState = Constants.GRP_ALL_CHILDREN_DONE;
+                               log.info("A final configuration reached");
+                               return false;
+                       }
+               }
+               return true;
+       }
+       
+       /** 
+        * Custom error handler to communicate parsing errors in the
+        * XML navigation rules to the client.
+        */     
+       private class SCXMLErrorHandler implements ErrorHandler {
+               
+               private static final String MSG_PREFIX = "<!-- " +
+                       "SCXMLDialog :";
+               private static final String MSG_POSTFIX = " Correct the SCXML. 
" +
+                       "-->\n";
+               
+               public void error(SAXParseException s) {
+                       try {
+                               ((PageContext) 
groupTag.getJspContext()).getOut().
+                               write(MSG_PREFIX + groupTag.getConfig() + " 
Error - " +
+                               s.getMessage() + MSG_POSTFIX);
+                       } catch (IOException ioe) {
+                               ioe.printStackTrace();
+                       }
+               }
+               
+               public void fatalError(SAXParseException s) {
+                       try {
+                               ((PageContext) 
groupTag.getJspContext()).getOut().
+                               write(MSG_PREFIX + groupTag.getConfig() + " 
Fatal Error - " +
+                               s.getMessage() + MSG_POSTFIX);
+                       } catch (IOException ioe) {
+                               ioe.printStackTrace();
+                       }                       
+               }
+               
+               public void warning(SAXParseException s) {
+                       try {
+                               ((PageContext) 
groupTag.getJspContext()).getOut().
+                               write(MSG_PREFIX + groupTag.getConfig() + " 
Warning - " +
+                               s.getMessage() + MSG_POSTFIX);
+                       } catch (IOException ioe) {
+                               ioe.printStackTrace();
+                       }                       
+               }
+       }
+       
+}

Propchange: 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/SCXMLDialog.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/SCXMLDialog.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to