views:

285

answers:

4

I don't think such support exists in current languages. I think what I want to do could be solved by a "workflow engine". But the problem I have with workflow's is generally they are:

  1. Declarative/verbose and I find a imperative style much more succinct
  2. Heavyweight, I'll have a lot of simple though diverse little state machines

I've investigated serializing iterators in C# but that doesn't get me exactly where I want to be. I'm current looking at putting together a DSL in Boo but not sure if I'll be able to get coroutine-like behaviour into Boo, and be able to serialize it as well.

Example

Here is limited fictional example of what I'd like to do. The main issue is that at any point in a routine you may need to get user input. The time between inputs could be very long so the state of service will need to be serialized to disk.

    def RunMachine(user)
      var lever = user.ChooseLever()
      lever.Pull()
      var device = CreateDevice(user)
      machine.Add(device)
      machine.Run()

   def CreateDevice(user)
      var color = user.ChooseColor()
      var shape = user.ChooseShape()
      return Device(color, shape)

Ideas?

+1  A: 

You might want to take a look at Windows Workflow:

http://msdn.microsoft.com/en-us/netframework/aa663328.aspx

It is meant to be used in a manner like this, with the ability to persist a workflow if there is inactivity on it, as well as the ability to restart it.

While technically it isn't language support, it should get the job done.

casperOne
Yeah, I tried to specifically point out I was trying to avoid a XML heavy, declaritive work flow engine, eg. specifically Windows Workflow.
Joseph Kingry
+1  A: 

Are you looking for continuations?

In any language which supports closures, it is possible to write programs in continuation passing style and manually implement call/cc.

call/cc being call-with-current-continuation.

Svante
I believe coroutines are something you could build on top of continuations. Coroutines allow for a much more natural expression of intent IMHO.
Joseph Kingry
But I return to the issue, even given continuation support in a language, can you serialize them?
Joseph Kingry
Some. For example, http://wiki.apache.org/cocoon/RhinoWithContinuations#head-d6b2435bbd91bef7f61324cd35775998da035f7a
ephemient
In fact, the design of the http://www.seaside.st/ Seaside web framework depends upon serializable continuations.
ephemient
+4  A: 

Funny that you should ask this today, and then later I read about Continuations in Mono. Looks like the kind of thing you're after. In particular there's a reference to Microthreading in Second Life, including this description:

SecondLife required that code be suspended at any point in time and that its entire state be serializable into a format suitable for storage into a database. Serialized state could then be restored at a different point in time or on a different computer (for example while moving from node to node).

Unless I've misunderstood you, this could be a good avenue to explore.

Jon Skeet
This looks great, or at least it has the best potential. Thanks!
Joseph Kingry
+1  A: 

I've found the best support for serializing coroutines appears to be in Lua via the Pluto library.

I triend Mono 2.6, but couldn't get coroutines working. I then experimented with Python/IronPython for awhile, but serialization support was lacking.

Now with Lua I'll have to interface via P/Invoke from .NET to Lua which prove to be a challenge to get working on both Linux and Windows, but ultimately if I'm looking for a scripting language to support a game, probably best to use what is already popularly being chosen.

Update

I ended up abandoning Lua for the time being. I was getting too bogged down in integration issues. I went back to Python. I'm using python yield expressions for my coroutines, and working around the issue that pickle doesn't support generators.

In my case, the program state doesn't always have active coroutines, which means sometimes I can pickle the state. Otherwise I store the last pickle state, and a 'replay history' of the actions since. Then restoring state is just unpickling the last state, and replaying any actions.

Joseph Kingry
+1 pluto is pretty impressive.
caspin