my attempt at using one (state machine)

81 views
Skip to first unread message

DrydenBoss

unread,
Aug 4, 2015, 5:10:09 AM8/4/15
to squirrel state machine
Hi,

I think your state machine is wonderful. However, much too complex for me at this point (much too much features ;-)).

But I was writing my own state machine and I keep looking to yours for guidance ;-D.

My State machine usage has the following properties:

  • Input is a stream of characters that fall into specific classes
  • Currently I am using the characters as input to a Conditional (your .when()) and devoid the use of events altogether
  • My Conditional is simply called "CharTest" and is instantiated with a class (enum type value) to match against.
When I look at your state machine (Squirrel) I would have two options for using it:

  1. Turn the characters into classes (enum values) BEFORE I feed them into the machine, which renders them as Events.
  2. Do not use events at all and only use when, create Conditions instantiated with matched classes and use isSatisfied() to test for their equivalence. Obtain the input from the Context which would be a custom object. Obtain any flags I have from the context as well.
Currently I have a cascade of 'tests' (descendents of my Conditional) that I can combine using e.g. new AndTest(new CharTest(CharType.MODE), new NegTest(new FlagTest(ALT))) which is my little way of not having to write any additional code. It reminds me of the Hamcrest unit test matchers for JUnit. (((.In Hamcrest you can write this as assertThat(CharType.from(input), both(equalTo(CharType.MODE)).and(not(ALT.value())) or something of the kind..))).

In the form of Squirrel's Conditions this would be nearly identical to what I have now.

class CharCondition extends Condition {
  CharType matchedClass;

  public CharCondition(CharType matchedClass) {
    this.matchedClass = matchedClass;
  }
  
  public boolean isSatisfied(Context c) {
    return c != null && CharType.from(c.getInput()) == matchedClass;
  }
}

Then I could even have a EnumMap that links the CharTypes to the instantiated matcher classes ;-).

Time to write some code! :D.

He Henry

unread,
Aug 6, 2015, 6:13:48 AM8/6/15
to squirrel state machine
I think you can take a look at SimpleCssParser

在 2015年8月4日星期二 UTC+8下午5:10:09,DrydenBoss写道:

DrydenBoss

unread,
Aug 6, 2015, 11:24:22 AM8/6/15
to squirrel state machine
I now see you're Chinese (or something close to it) which would explain your style of English ;-). Your English is very good except for regular missing small words that a Chinese might or may not regularly use in his language.

Your tutorial/documentation is really EXCELLENT. I deeply wish more regular open source projects would follow that quality, but I guess I should be happy that the smaller ones of us do it right.

I will look at your test code later on in a better way.

My own state machine currently approaches something that is at least usable. I look to your state machine for a lot of guidance. Consider me a bastard :D :P.

It would require a vast amount of development of course to get to your quality. I don't really know why I don't use yours, except that I want to do and learn it on my own. State and pattern matching is really at the heart of many things and a good understanding of it is essential and could even be called most vital ;-D.

Regards,

Bart.

He Henry

unread,
Aug 13, 2015, 4:33:16 AM8/13/15
to squirrel state machine
You are right. I am from China. I am glad you like my project. :-)

在 2015年8月6日星期四 UTC+8下午11:24:22,DrydenBoss写道:
Reply all
Reply to author
Forward
0 new messages