You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Anna Klisiewicz <an...@googlemail.com> on 2008/07/14 02:10:17 UTC

SCXML - getting started

Hello,
I am not only SCXML but also Java beginner and there is a reason that I have
to use both. Unfortunately I do not really know how to use Commons SCXML
even after reading documentation, because there is to many new things. Can
you give me some more SIMPLE examples how to parse xml files (how to use
urls to the xml files in SCXML Parser) and how to make some simple code
working. Sorry for boring you with not really chellenging questions.
Anna

Re: SCXML - getting started

Posted by Rahul Akolkar <ra...@gmail.com>.
My response below ...

On Sat, Aug 2, 2008 at 11:49 AM, Anna Klisiewicz
<an...@googlemail.com> wrote:
> Thanks Rahul for your answer. I have another problem now.
>
> Whatever SCXML file I load I get NullPointerException. When I try the simple
> hello world for example:
>
> <?xml version="1.0"?>
> <scxml xmlns="http://www.w3.org/2005/07/scxml"
>       version="1.0"
>       initialstate="hello">
>   <state id="hello" final="true">
>       <onentry>
>           <log expr="'hello world'" />
>       </onentry>
>   </state>
> </scxml>
>
>
> I get:
>
> Exception in thread "main" java.lang.NullPointerException
>
>        at
> org.apache.commons.scxml.SCInstance.getContext(SCInstance.java:181)
>        at org.apache.commons.scxml.model.Log.execute(Log.java:102)
>        at
> org.apache.commons.scxml.semantics.SCXMLSemanticsImpl.executeActions(SCXMLSemanticsImpl.java:232)
>        at
> org.apache.commons.scxml.SCXMLExecutor.reset(SCXMLExecutor.java:252)
>        at org.apache.commons.scxml.SCXMLExecutor.go(SCXMLExecutor.java:351)
>        at dialoguesystem.Parser.Parser(Main.java:63)
>        at dialoguesystem.Main.main(Main.java:24)
> Java Result: 1
>
> What am I doing wrong? This is the code I use:
>
>
> -------------------------------------Code------------------------------------
> package dialoguesystem;
>
> import java.io.IOException;
> import java.net.URL;
> import org.apache.commons.scxml.io.SCXMLParser;
> import org.apache.commons.scxml.model.ModelException;
> import org.apache.commons.scxml.model.SCXML;
> import org.xml.sax.ErrorHandler;
> import org.xml.sax.SAXException;
> import org.apache.commons.scxml.SCXMLExecutor;
> import org.apache.commons.scxml.PathResolver;
> import org.apache.commons.scxml.Context;
> import org.apache.commons.scxml.ErrorReporter;
> import org.apache.commons.scxml.Evaluator;
> import org.apache.commons.scxml.EventDispatcher;
> import org.apache.commons.scxml.SCXMLListener;
>
>
>
>
> public class Main{
>   public static void main(String[] args) {
>      Parser parse = new Parser();
>      parse.Parser();
>     }
>  }
>
>  class Parser {
>
>     private URL xml;
>     private ErrorHandler errHandler;
>     private PathResolver pathResolver;
>     private SCXML scxml = null;
>     private SCXMLExecutor exec;
>     private Evaluator expEvaluator;
>     private EventDispatcher evtDisp;
>     private ErrorReporter errRep;
>     private SCXMLListener scxmlListener;
>     private Context rootContext;
>
>
>
>
>  public void Parser(){
>     Thread.currentThread().setContextClassLoader
> (this.getClass().getClassLoader());
>     xml =
> this.getClass().getClassLoader().getResource("resources/hello.xml");
>     try {
>        scxml = SCXMLParser.parse(xml, errHandler);
>        } catch (IOException io) {
>        } catch (SAXException se) {
>        } catch (ModelException me) {
>        }
>
>
>        exec = null;
>        try {
>            exec = new SCXMLExecutor(expEvaluator, evtDisp, errRep);
>                    if (exec != null) {
>                   System.out.println("Co!!!!\n");}
>            exec.setStateMachine(scxml);
>            exec.addListener(scxml, scxmlListener);
>            exec.setRootContext(rootContext);
>            exec.go();
>            } catch (ModelException me) {
>        }
>     }
>    }
>
<snip/>

Most of the references in the above try block start off as being null,
and unless there is additional code, that doesn't seem to change.

Place the following lines just above the try block (you can import the
classes to avoid having to use the fully qualified names):

  expEvaluator = new org.apache.commons.scxml.env.jexl.JexlEvaluator();
  evtDisp = new org.apache.commons.scxml.env.SimpleDispatcher();
  errRep = new org.apache.commons.scxml.env.SimpleErrorReporter();
  scxmlListener = new org.apache.commons.scxml.env.SimpleSCXMLListener();
  rootContext = new org.apache.commons.scxml.env.jexl.JexlContext();

I have some other comments about the code, but lets wait a bit on that
and get the hello world example going for you first.

-Rahul


> --------------------------------------------
>
> Thank you in advance,
> Anna
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: SCXML - getting started

Posted by Anna Klisiewicz <an...@googlemail.com>.
Thanks Rahul for your answer. I have another problem now.

Whatever SCXML file I load I get NullPointerException. When I try the simple
hello world for example:

<?xml version="1.0"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml"
       version="1.0"
       initialstate="hello">
   <state id="hello" final="true">
       <onentry>
           <log expr="'hello world'" />
       </onentry>
   </state>
</scxml>


I get:

Exception in thread "main" java.lang.NullPointerException

        at
org.apache.commons.scxml.SCInstance.getContext(SCInstance.java:181)
        at org.apache.commons.scxml.model.Log.execute(Log.java:102)
        at
org.apache.commons.scxml.semantics.SCXMLSemanticsImpl.executeActions(SCXMLSemanticsImpl.java:232)
        at
org.apache.commons.scxml.SCXMLExecutor.reset(SCXMLExecutor.java:252)
        at org.apache.commons.scxml.SCXMLExecutor.go(SCXMLExecutor.java:351)
        at dialoguesystem.Parser.Parser(Main.java:63)
        at dialoguesystem.Main.main(Main.java:24)
Java Result: 1

What am I doing wrong? This is the code I use:


-------------------------------------Code------------------------------------
package dialoguesystem;

import java.io.IOException;
import java.net.URL;
import org.apache.commons.scxml.io.SCXMLParser;
import org.apache.commons.scxml.model.ModelException;
import org.apache.commons.scxml.model.SCXML;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.apache.commons.scxml.SCXMLExecutor;
import org.apache.commons.scxml.PathResolver;
import org.apache.commons.scxml.Context;
import org.apache.commons.scxml.ErrorReporter;
import org.apache.commons.scxml.Evaluator;
import org.apache.commons.scxml.EventDispatcher;
import org.apache.commons.scxml.SCXMLListener;




public class Main{
   public static void main(String[] args) {
      Parser parse = new Parser();
      parse.Parser();
     }
 }

 class Parser {

     private URL xml;
     private ErrorHandler errHandler;
     private PathResolver pathResolver;
     private SCXML scxml = null;
     private SCXMLExecutor exec;
     private Evaluator expEvaluator;
     private EventDispatcher evtDisp;
     private ErrorReporter errRep;
     private SCXMLListener scxmlListener;
     private Context rootContext;




 public void Parser(){
     Thread.currentThread().setContextClassLoader
(this.getClass().getClassLoader());
     xml =
this.getClass().getClassLoader().getResource("resources/hello.xml");
     try {
        scxml = SCXMLParser.parse(xml, errHandler);
        } catch (IOException io) {
        } catch (SAXException se) {
        } catch (ModelException me) {
        }


        exec = null;
        try {
            exec = new SCXMLExecutor(expEvaluator, evtDisp, errRep);
                    if (exec != null) {
                   System.out.println("Co!!!!\n");}
            exec.setStateMachine(scxml);
            exec.addListener(scxml, scxmlListener);
            exec.setRootContext(rootContext);
            exec.go();
            } catch (ModelException me) {
        }
     }
    }

--------------------------------------------

Thank you in advance,
Anna

Re: SCXML - getting started

Posted by Rahul Akolkar <ra...@gmail.com>.
On Thu, Jul 17, 2008 at 7:05 PM, Anna Klisiewicz
<an...@googlemail.com> wrote:
> Hi Chris,
> Thank you for your answer.
>
> 2008/7/14 Christian Grobmeier <gr...@gmail.com>:
>
>> Hi Anna,
>> are you sure you want to use SCXML? Its not an API for parsing XML files in
>> general, its:
>> "State Chart XML (SCXML) is a general-purpose event-based state machine
>> language"
>
>
> Yes I am sure, I need to use SCXML. I would like to avoid  Java, as I don't
> have experience with it and to be honest, I am not really good at oo
> programming. Unfortunately it seems that there is not too much choice and
> Commons SCXML is the best way.
>
<snip/>

You will need some familiarity with Java to use Commons SCXML. Basics
are here (amongst many other places):

  http://java.sun.com/docs/books/tutorial/


>>
>> I wonder that you are starting with Java and with SCXML at the same time.
>> If yes, did you read that?
>>
>> http://commons.apache.org/scxml/guide/core-parser.html
>>
>> You can read that you have several ways to give an XML file to the SCXML
>> parser:
>>
>> http://commons.apache.org/scxml/apidocs/org/apache/commons/scxml/io/SCXMLParser.html
>
>
> Yes I have seen it and I haven't created anythig working. I couldn't even
> run the StopWatch example, because I had some errors, and due to luck of
> experience I couldn't fix it. The biggest problem I've got is using classes
> effectively.
>
<snap/>

The stopwatch example may not be the easiest to get working if you
aren't familiar with setting up classpaths etc. After "hello world",
the right tools will make the difference, though I'm aware there is
some cost upfront to being able to use any of them. I'd recommend
getting to know either Ant or Maven for building (and more). IDEs
(such as Eclipse) help some developers.


> I have to  write a dialogue system and the scxml code will look simillar to
> this below. What would you suggest to use to make it working? What should I
> focus on while learning, because I feel discouraged now and It doesn't seem
> that I will be able to use Commons SCXML effectively soon.
<snip/>

The general pattern is:
 * Obtain a URL to the SCXML document (theres couple of other options,
using InputSource and "real path", we'll skip that for now)
 * Parse the document:
    http://commons.apache.org/scxml/guide/core-parser.html
 * Create an instance of the state machine described by the document:
    http://commons.apache.org/scxml/guide/core-engine.html
 * Trigger one or more events on the state machine (causing various
other interesting things to happen, thats for another day):
    http://commons.apache.org/scxml/guide/core-events.html

If you're having specific difficulties in using Commons SCXML, please
do not hesitate to ask.

Finally, the content below won't validate as SCXML (see W3C Working
Draft for all the details):

 * <state> doesn't have a target attribute, you'll need to use <initial>
 * <transition> doesn't have vars or precond attributes

You will have to get that sorted out before you can use the document.

-Rahul


> Best wishes, Anna
>
>
> <datamodel>
>
>    <data name="is">
>       <private>
>          <plan eval="true"> </plan>
>          <bel eval="true"> </bel>
>       </private>
>       <shared>
>          <com eval="true">  </com>
>          <qud eval="true"> </qud>
>       </shared>
>    </data>
> <datamodel>
>
>
> <parallel id="ds">
>
>    <state id="Moves" target="select">
>       <state id="select" target="selectAction">
>          <state id="Action">
>             <transition vars="Action" precond="" target="selectMove">
>                <script>...</script>
>             </transition>
>             <transition target="selectMove"/>
>          </state>
>          <state id="selectMove">
>             <transition vars="Question Answer"
> precond="..."target="update">
>                <send target="Self" event="generateMove" expr=""/>
>             </transition>
>          <state>
>      <!--some more states...-->
>       </state>
>    </state>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: SCXML - getting started

Posted by Anna Klisiewicz <an...@googlemail.com>.
Hi Chris,
Thank you for your answer.

2008/7/14 Christian Grobmeier <gr...@gmail.com>:

> Hi Anna,
> are you sure you want to use SCXML? Its not an API for parsing XML files in
> general, its:
> "State Chart XML (SCXML) is a general-purpose event-based state machine
> language"



Yes I am sure, I need to use SCXML. I would like to avoid  Java, as I don't
have experience with it and to be honest, I am not really good at oo
programming. Unfortunately it seems that there is not too much choice and
Commons SCXML is the best way.

>
> I wonder that you are starting with Java and with SCXML at the same time.
> If yes, did you read that?
>
> http://commons.apache.org/scxml/guide/core-parser.html
>
> You can read that you have several ways to give an XML file to the SCXML
> parser:
>
> http://commons.apache.org/scxml/apidocs/org/apache/commons/scxml/io/SCXMLParser.html


Yes I have seen it and I haven't created anythig working. I couldn't even
run the StopWatch example, because I had some errors, and due to luck of
experience I couldn't fix it. The biggest problem I've got is using classes
effectively.

I have to  write a dialogue system and the scxml code will look simillar to
this below. What would you suggest to use to make it working? What should I
focus on while learning, because I feel discouraged now and It doesn't seem
that I will be able to use Commons SCXML effectively soon.
Best wishes, Anna


<datamodel>

    <data name="is">
       <private>
          <plan eval="true"> </plan>
          <bel eval="true"> </bel>
       </private>
       <shared>
          <com eval="true">  </com>
          <qud eval="true"> </qud>
       </shared>
    </data>
<datamodel>


<parallel id="ds">

    <state id="Moves" target="select">
       <state id="select" target="selectAction">
          <state id="Action">
             <transition vars="Action" precond="" target="selectMove">
                <script>...</script>
             </transition>
             <transition target="selectMove"/>
          </state>
          <state id="selectMove">
             <transition vars="Question Answer"
precond="..."target="update">
                <send target="Self" event="generateMove" expr=""/>
             </transition>
          <state>
      <!--some more states...-->
       </state>
    </state>

Re: SCXML - getting started

Posted by Christian Grobmeier <gr...@gmail.com>.
Hi Anna,

I am not only SCXML but also Java beginner and there is a reason that I have
> to use both. Unfortunately I do not really know how to use Commons SCXML
> even after reading documentation, because there is to many new things. Can
> you give me some more SIMPLE examples how to parse xml files (how to use
> urls to the xml files in SCXML Parser) and how to make some simple code
> working. Sorry for boring you with not really chellenging questions.
>

are you sure you want to use SCXML? Its not an API for parsing XML files in
general, its:
"State Chart XML (SCXML) is a general-purpose event-based state machine
language"

I wonder that you are starting with Java and with SCXML at the same time.
If yes, did you read that?

http://commons.apache.org/scxml/guide/core-parser.html

You can read that you have several ways to give an XML file to the SCXML
parser:
http://commons.apache.org/scxml/apidocs/org/apache/commons/scxml/io/SCXMLParser.html

If you are not looking for that but for simple XML read/write access, you
better look at:
http://commons.apache.org/betwixt/
http://commons.apache.org/digester/

Chris.