Monday, July 9, 2012

Movimentum - The current rewriting rules

Before we continue with a next test, I'd like to sum up all the rules from our rule-action code:

static SolverNode() {
    {
        // 1. 0 = C, 0 <= C
        var z = new TypeMatchTemplate<Constant>();
        new RuleAction<ScalarConstraintMatcher>("0=C",
            new EqualsZeroConstraintTemplate(z).GetMatchDelegate(),
            matcher => matcher != null,
            (currNode, matcher, matchedConstraint) =>
                matcher.Match(z).Value.Near(0)
                    ? new SolverNode(
                        currNode.Constraints
                                .Except(matchedConstraint),
                        currNode)
                    : null);
        new RuleAction<ScalarConstraintMatcher>("0<=C",
            new AtLeastZeroConstraintTemplate(z).GetMatchDelegate(),
            matcher => matcher != null,
            (currNode, matcher, matchedConstraint) => {
                double value = matcher.Match(z).Value;
                return value.Near(0) | value >= 0
                            ? new SolverNode(
                                    currNode.Constraints
                                        .Except(matchedConstraint),
                                    currNode)
                            : null;
            });
    }
    {
        // 2. 0 = V
        var v = new TypeMatchTemplate<Variable>();
        new RuleAction<ScalarConstraintMatcher>("0=V",
            new EqualsZeroConstraintTemplate(v).GetMatchDelegate(),
            matcher => matcher != null,
            (currNode, matcher, matchedConstraint) =>
                currNode.RememberAndSubstituteVariable(
                    matcher.Match(v), 0));
    }
    {
        // 3. 0 = V + C
        var v = new TypeMatchTemplate<Variable>();
        var e = new TypeMatchTemplate<Constant>();
        new RuleAction<ScalarConstraintMatcher>("0=V+C",
            new EqualsZeroConstraintTemplate(v + e).GetMatchDelegate(),
            matcher => matcher != null,
            (currNode, matcher, matchedConstraint) =>
                currNode.RememberAndSubstituteVariable(
                    matcher.Match(v),
                    -matcher.Match(e).Value));
    }
    {
        // 4. Match constraints with formal square roots
        new RuleAction<FindFormalSquarerootVisitor>("root",
            constraint => constraint.Expr
                    .Accept(new FindFormalSquarerootVisitor(), Ig.nore),
            formalRootFinder =>
                formalRootFinder.SomeFormalSquareroot != null,
            (node, formalRootFinder, constraint) =>
                RewriteFormalSquareroot(node,
                    formalRootFinder.SomeFormalSquareroot)
            );
    }
}

The code is not straightforward reading, but with a little bit of knowledge, it can be deciphered. Let us hope that the coming rules are not much more difficult.

No comments:

Post a Comment