diff options
author | Stefan Radomski <github@mintwerk.de> | 2014-07-01 23:45:30 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2014-07-01 23:45:30 (GMT) |
commit | 984bdd288aac0da9f68fdeaba06cd845456122c9 (patch) | |
tree | 78dbd9ce1aef691670f3827f59a5e23fec46a9fe | |
parent | f9e8883a9ae3574c99a7a830ec5d82e07d0f36f0 (diff) | |
parent | 193a01614dc3290eec51745a91be65c166c3763b (diff) | |
download | uscxml-984bdd288aac0da9f68fdeaba06cd845456122c9.zip uscxml-984bdd288aac0da9f68fdeaba06cd845456122c9.tar.gz uscxml-984bdd288aac0da9f68fdeaba06cd845456122c9.tar.bz2 |
Merge pull request #46 from sradomski/master
More work on C# embedding examples
-rw-r--r-- | embedding/csharp/uSCXMLEmbedding.suo | bin | 29184 -> 30208 bytes | |||
-rw-r--r-- | embedding/csharp/uSCXMLEmbedding/Examples.csproj | 2 | ||||
-rw-r--r-- | embedding/csharp/uSCXMLEmbedding/Extensions/CustomExecutableContent.cs | 4 | ||||
-rw-r--r-- | embedding/csharp/uSCXMLEmbedding/Extensions/CustomInvoker.cs | 32 | ||||
-rw-r--r-- | embedding/csharp/uSCXMLEmbedding/Program.cs | 64 | ||||
-rw-r--r-- | embedding/csharp/uSCXMLEmbedding/RunTests.cs | 274 |
6 files changed, 308 insertions, 68 deletions
diff --git a/embedding/csharp/uSCXMLEmbedding.suo b/embedding/csharp/uSCXMLEmbedding.suo Binary files differindex b08325e..9bacedf 100644 --- a/embedding/csharp/uSCXMLEmbedding.suo +++ b/embedding/csharp/uSCXMLEmbedding.suo diff --git a/embedding/csharp/uSCXMLEmbedding/Examples.csproj b/embedding/csharp/uSCXMLEmbedding/Examples.csproj index b312b5f..1ffc38b 100644 --- a/embedding/csharp/uSCXMLEmbedding/Examples.csproj +++ b/embedding/csharp/uSCXMLEmbedding/Examples.csproj @@ -47,7 +47,7 @@ </Reference>
</ItemGroup>
<ItemGroup>
- <Compile Include="Program.cs" />
+ <Compile Include="RunTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Extensions\CustomDataModel.cs" />
<Compile Include="Extensions\CustomExecutableContent.cs" />
diff --git a/embedding/csharp/uSCXMLEmbedding/Extensions/CustomExecutableContent.cs b/embedding/csharp/uSCXMLEmbedding/Extensions/CustomExecutableContent.cs index 4789020..94cf111 100644 --- a/embedding/csharp/uSCXMLEmbedding/Extensions/CustomExecutableContent.cs +++ b/embedding/csharp/uSCXMLEmbedding/Extensions/CustomExecutableContent.cs @@ -15,12 +15,12 @@ namespace embedding public override void enterElement(string node)
{
-
+ Console.WriteLine("enterElement " + node);
}
public override void exitElement(string node)
{
-
+ Console.WriteLine("exitElement " + node);
}
public override ExecutableContent create(Interpreter interpreter)
diff --git a/embedding/csharp/uSCXMLEmbedding/Extensions/CustomInvoker.cs b/embedding/csharp/uSCXMLEmbedding/Extensions/CustomInvoker.cs index 3fd01ce..da941b0 100644 --- a/embedding/csharp/uSCXMLEmbedding/Extensions/CustomInvoker.cs +++ b/embedding/csharp/uSCXMLEmbedding/Extensions/CustomInvoker.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; using System.Linq;
using System.Text;
using org.uscxml;
+using System.Xml;
+using System.Xml.XPath;
+using System.IO;
namespace embedding
{
@@ -22,7 +25,7 @@ namespace embedding public override StringList getNames()
{
StringList names = new StringList();
- names.add("simple");
+ names.add("custom");
return names;
}
@@ -32,6 +35,33 @@ namespace embedding public override void send(SendRequest req)
{
+ Console.WriteLine(req);
+ // send in s1.onentry
+ if ("This is some content!" == req.getContent())
+ {
+ returnEvent(new Event("received1"));
+ return;
+ }
+ // send in s2.onentry
+ if (req.getParams().ContainsKey("foo")
+ && "bar" == (req.getParams()["foo"][0].getAtom()))
+ {
+ returnEvent(new Event("received2"));
+ return;
+ }
+ // send in s3
+ if (req.getXML().Length > 0)
+ {
+ XmlReaderSettings set = new XmlReaderSettings();
+ set.ConformanceLevel = ConformanceLevel.Fragment;
+ XPathDocument doc = new XPathDocument(XmlReader.Create(new StringReader(req.getXML()), set));
+ XPathNavigator nav = doc.CreateNavigator();
+
+ Console.WriteLine("Root element :" + nav.SelectSingleNode("/").Value);
+ returnEvent(new Event("received3"));
+ return;
+ }
+
}
}
}
diff --git a/embedding/csharp/uSCXMLEmbedding/Program.cs b/embedding/csharp/uSCXMLEmbedding/Program.cs deleted file mode 100644 index 0c76cb4..0000000 --- a/embedding/csharp/uSCXMLEmbedding/Program.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Diagnostics;
-using System.Runtime.InteropServices;
-
-namespace embedding
-{
- using org.uscxml;
-
- class Program
- {
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern void SetDllDirectory(string lpPathName);
-
- static bool testLifecycle() {
- // try to instantiate an interpreter with a parse error
- try
- {
- Interpreter interpreter = Interpreter.fromXML("<invalid");
- Debug.Assert(false);
- }
- catch (InterpreterException e) {
- Console.Write(e.Message);
- }
-
- // try to instantiate an interpreter with invalid XML (no scxml element)
- try
- {
- Interpreter interpreter = Interpreter.fromXML("<invalid />");
- Debug.Assert(interpreter.getState() == InterpreterState.USCXML_INSTANTIATED);
- InterpreterState state = interpreter.step();
-
- Debug.Assert(false);
- }
- catch (InterpreterException e)
- {
- Console.Write(e.Message);
- }
-
- return true;
- }
-
- static void Main(string[] args)
- {
-
- /*
- * Make sure this path contains the uscxmlNativeCSharp.dll!
- */
- if (System.Environment.Is64BitProcess)
- {
- SetDllDirectory("C:\\Users\\sradomski\\Desktop\\build\\uscxml64\\lib\\csharp");
- }
- else
- {
- SetDllDirectory("C:\\Users\\sradomski\\Desktop\\build\\uscxml\\lib\\csharp");
- }
-
- testLifecycle();
- Console.ReadKey();
- }
- }
-}
diff --git a/embedding/csharp/uSCXMLEmbedding/RunTests.cs b/embedding/csharp/uSCXMLEmbedding/RunTests.cs new file mode 100644 index 0000000..d98bca1 --- /dev/null +++ b/embedding/csharp/uSCXMLEmbedding/RunTests.cs @@ -0,0 +1,274 @@ +using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+
+namespace embedding
+{
+ using org.uscxml;
+
+ class RunTests
+ {
+ [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
+ private static extern void SetDllDirectory(string lpPathName);
+
+ static void Main(string[] args)
+ {
+
+ /*
+ * Make sure this path contains the uscxmlNativeCSharp.dll!
+ */
+ if (System.Environment.Is64BitProcess)
+ {
+ SetDllDirectory("C:\\Users\\sradomski\\Desktop\\build\\uscxml64\\lib\\csharp");
+ }
+ else
+ {
+ SetDllDirectory("C:\\Users\\sradomski\\Desktop\\build\\uscxml\\lib\\csharp");
+ }
+
+ testLifeCycle();
+ testExecutableContent();
+ testIOProcessor();
+ testInvoker();
+ Console.ReadKey();
+ }
+
+ public static void testInvoker() {
+ CustomInvoker invoker = new CustomInvoker();
+ // just register prototype at global factory
+ Factory.getInstance().registerInvoker(invoker);
+
+ String xml =
+ "<scxml>" +
+ " <state id=\"s1\">" +
+ " <invoke type=\"custom\" id=\"custominvoker1\">" +
+ " <content>Some string content</content>" +
+ " </invoke>" +
+ " <invoke type=\"java\" id=\"custominvoker2\" />" +
+ " <state id=\"s11\">" +
+ " <transition event=\"received1\" target=\"s12\" />" +
+ " </state>" +
+ " <state id=\"s12\">" +
+ " <onentry>" +
+ " <send target=\"#_custominvoker2\" event=\"foo\" />" +
+ " </onentry>" +
+ " <transition event=\"received2\" target=\"done\" />" +
+ " </state>" +
+ " </state>" +
+ " <final id=\"done\" />" +
+ "</scxml>";
+
+ // parse and interpret
+ Interpreter interpreter = Interpreter.fromXML(xml);
+ interpreter.interpret();
+
+ }
+
+ public static void testIOProcessor()
+ {
+ CustomIOProc ioproc = new CustomIOProc();
+ // just register prototype at global factory
+ Factory.getInstance().registerIOProcessor(ioproc);
+
+ String xml =
+ "<scxml>" +
+ " <state id=\"s1\">" +
+ " <onentry>" +
+ " <send type=\"java\">" +
+ " <content>This is some content!</content>" +
+ " </send>" +
+ " </onentry>" +
+ " <transition event=\"received1\" target=\"s2\" />" +
+ " </state>" +
+ " <state id=\"s2\">" +
+ " <onentry>" +
+ " <send type=\"java\">" +
+ " <param name=\"foo\" expr=\"bar\" />" +
+ " </send>" +
+ " </onentry>" +
+ " <transition event=\"received2\" target=\"s3\" />" +
+ " </state>" +
+ " <state id=\"s3\">" +
+ " <onentry>" +
+ " <send type=\"java\">" +
+ " <content>" +
+ " <this><is><xml/></is></this>" +
+ " </content>" +
+ " </send>" +
+ " </onentry>" +
+ " <transition event=\"received3\" target=\"done\" />" +
+ " </state>" +
+ " <final id=\"done\" />" +
+ "</scxml>";
+
+ // parse and interpret
+ Interpreter interpreter = Interpreter.fromXML(xml);
+ interpreter.interpret();
+
+ }
+
+ public static void testExecutableContent()
+ {
+ CustomExecutableContent execContent = new CustomExecutableContent();
+ Factory.getInstance().registerExecutableContent(execContent);
+
+ Interpreter interpreter = Interpreter.fromXML(
+ "<scxml>\n" +
+ " <state id=\"s0\">\n" +
+ " <onentry>\n" +
+ " <custom foo=\"bar\">\n" +
+ " <something></something>\n" +
+ " </custom>\n" +
+ " <custom foo=\"bar\">\n" +
+ " <something></something>\n" +
+ " </custom>\n" +
+ " </onentry>\n" +
+ " <transition target=\"exit\" />" +
+ " </state>\n" +
+ " <final id=\"exit\" />" +
+ "</scxml>\n"
+ );
+ interpreter.interpret();
+ }
+
+ public static void testLifeCycle()
+ {
+ // syntactic xml parse error -> throws
+ try
+ {
+ String xml = "<invalid";
+ Interpreter interpreter = Interpreter.fromXML(xml);
+ Debug.Assert(false);
+ }
+ catch (InterpreterException e)
+ {
+ Console.WriteLine(e);
+ }
+
+ // semantic xml parse error -> throws
+ try
+ {
+ String xml = "<invalid />";
+ Interpreter interpreter = Interpreter.fromXML(xml);
+ Debug.Assert(interpreter.getState() == InterpreterState.USCXML_INSTANTIATED);
+ interpreter.step();
+ Debug.Assert(false);
+ }
+ catch (InterpreterException e)
+ {
+ Console.WriteLine(e);
+ }
+
+ // request unknown datamodel
+ try
+ {
+ string xml =
+ "<scxml datamodel=\"invalid\">" +
+ " <state id=\"start\">" +
+ " <transition target=\"done\" />" +
+ " </state>" +
+ " <final id=\"done\" />" +
+ "</scxml>";
+ Interpreter interpreter = Interpreter.fromXML(xml);
+ Debug.Assert(interpreter.getState() == InterpreterState.USCXML_INSTANTIATED);
+ interpreter.step();
+ Debug.Assert(false);
+ }
+ catch (InterpreterException e)
+ {
+ Console.WriteLine(e);
+ }
+
+
+ try
+ {
+ // two microsteps
+ string xml =
+ "<scxml>" +
+ " <state id=\"start\">" +
+ " <transition target=\"s2\" />" +
+ " </state>" +
+ " <state id=\"s2\">" +
+ " <transition target=\"done\" />" +
+ " </state>" +
+ " <final id=\"done\" />" +
+ "</scxml>";
+
+ Interpreter interpreter = Interpreter.fromXML(xml);
+
+ Debug.Assert(interpreter.getState() == InterpreterState.USCXML_INSTANTIATED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_MICROSTEPPED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_MICROSTEPPED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_FINISHED);
+
+ }
+ catch (InterpreterException e)
+ {
+ Console.WriteLine(e);
+ }
+
+
+ try
+ {
+ // single macrostep, multiple runs
+ string xml =
+ "<scxml>" +
+ " <state id=\"start\">" +
+ " <transition target=\"done\" />" +
+ " </state>" +
+ " <final id=\"done\" />" +
+ "</scxml>";
+
+ Interpreter interpreter = Interpreter.fromXML(xml);
+ Debug.Assert(interpreter.getState() == InterpreterState.USCXML_INSTANTIATED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_MICROSTEPPED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_FINISHED);
+ interpreter.reset();
+
+ Debug.Assert(interpreter.getState() == InterpreterState.USCXML_INSTANTIATED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_MICROSTEPPED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_FINISHED);
+
+ }
+ catch (InterpreterException e)
+ {
+ Console.WriteLine(e);
+ }
+
+
+ try
+ {
+ // macrostep in between
+ string xml =
+ "<scxml>" +
+ " <state id=\"start\">" +
+ " <onentry>" +
+ " <send event=\"continue\" delay=\"2s\"/>" +
+ " </onentry>" +
+ " <transition target=\"s2\" event=\"continue\" />" +
+ " </state>" +
+ " <state id=\"s2\">" +
+ " <transition target=\"done\" />" +
+ " </state>" +
+ " <final id=\"done\" />" +
+ "</scxml>";
+
+ Interpreter interpreter = Interpreter.fromXML(xml);
+ Debug.Assert(interpreter.getState() == InterpreterState.USCXML_INSTANTIATED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_IDLE);
+ Debug.Assert(interpreter.step(true) == InterpreterState.USCXML_MACROSTEPPED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_MICROSTEPPED);
+ Debug.Assert(interpreter.step() == InterpreterState.USCXML_FINISHED);
+
+ }
+ catch (InterpreterException e)
+ {
+ Console.WriteLine(e);
+ }
+ }
+
+ }
+}
|