Merlin: Let There Be Code!
This is part six of a series on Merlin. I recommend you read from the beginning.
So after all this thinking, planning, and testing, I was finally going to get my hands dirty! Recalling the pseudo-code that I'd written just a couple of nights earlier, I decided to flush out some of the remaining objects. That said, I did choose to skip the Context object for now since I wasn't too sure about how it would play out. Here's what I was left with:
public abstract class Condition
{
public abstract bool Evaluate();
}
public abstract class Action
{
public abstract void Execute();
}
public class Rule
{
...
public void Run()
{
if (condition.Evaluate())
action.Execute();
}
}
Since I was going to start with the "engine" in the middle, saving the UI for last, I decided to "fake it" for the time being. And since I wanted to keep it simple, I only created a couple of conditions and a single action, none of which were overly complex. My communityserver.config file ended up looking something like this:
...
<CSModules>
<add name="Merlin - My First Rule" type="JoseLema.Merlin.MerlinModule, JoseLema.Merlin">
<rule name="My First Rule">
<condition type="JoseLema.Merlin.TrueCondition, JoseLema.Merlin" />
<action type="JoseLema.Merlin.NullAction, JoseLema.Merlin" />
</rule>
</add>
<add name="Merlin - My Second Rule" type="JoseLema.Merlin.MerlinModule, JoseLema.Merlin">
<rule name="My Second Rule">
<condition type="JoseLema.Merlin.FalseCondition, JoseLema.Merlin" />
<action type="JoseLema.Merlin.NullAction, JoseLema.Merlin" />
</rule>
</add>
</CSModules>
...
As you might imagine, the implementation for these new rule parts was trivial:
public class TrueCondition : Condition
{
public override bool Evaluate()
{
return true;
}
}
public class FalseCondition : Condition
{
public override bool Evaluate()
{
return false;
}
}
public class NullAction : Action
{
public override void Execute()
{
// Do Nothing
}
}
With a note to myself about hiding these simple rule parts from the UI, I was ready to begin the task of parsing... (continued)