[ 
https://issues.apache.org/jira/browse/DAFFODIL-1437?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17343206#comment-17343206
 ] 

Mike Beckerle commented on DAFFODIL-1437:
-----------------------------------------

I can give you some hints here.

In daffodil-runtime1/src/main/org/apache/daffodil/processors/dfa/Runtime.scala.

In it you will find this object definition:
{code:java}
object Rule {
  def apply(testBody: Registers => Boolean)(actionBody: Registers => Unit) = {
    new Rule {
      override def test(r: Registers) = testBody(r)
      override def act(r: Registers) = actionBody(r)
    }
  }
}{code}
This object takes two functions as arguments, a testBody function, and an 
actionBody function.

We want to remove this object, and just utilize the trait Rule defined above it 
like:

 
{code:java}
trait Rule { 
  def test(r:Registers): Boolean
  def action(r: Registers) : Unit
}
{code}
So that means wherever the Rule object is used, as in this line from 
Rules.scala:
{code:java}
    Rule { (r: Registers) => couldBeFirstChar(r.data0, r.delimitersIter) } { 
(r: Registers) => r.status = StateKind.Paused },{code}
we want instead:
{code:java}
new Rule { 
  override def test(r: Registers) = couldBeFirstChar(r.data0, r.delimitersIter)
  override def action(r: Registers) = r.status = StateKind.Paused
}{code}
The point of this is just simplicity. This eliminates all the usage of 
anonymous functions,  replacing them with named methods on anonymous classes. 

Any anonymous rules that appear more than once, can be hoisted out and made 
named Rules. E.g., suppose the anonymous class I just showed appeard multiple 
times, then we could do this:
{code:java}
object PauseRule extends Rule {
 override def test(r: Registers) = couldBeFirstChar(r.data0, r.delimitersIter)
 override def action(r: Registers) = r.status = StateKind.Paused
}
{code}
Then in the places where this rule is constructued (there are 72 rule 
constructions in all, in the Rules.scala file), one would just use PauseRule 
instead of the anonymous one.

 

Hope that helps. If not write back here.

 

> Clarity: DFA Rules - eliminate anonymous classes and functions
> --------------------------------------------------------------
>
>                 Key: DAFFODIL-1437
>                 URL: https://issues.apache.org/jira/browse/DAFFODIL-1437
>             Project: Daffodil
>          Issue Type: Improvement
>          Components: Back End, Clean Ups
>            Reporter: Mike Beckerle
>            Priority: Major
>
> This is a lisp sort of thing to do as written, but there really is no reason 
> these can't be ordinary objects  with two methods, test and act. The code 
> would be clearer and easier to debug. Particularly if the classes have names 
> like 
> {code}
> final class StartState(states: => ArrayBuffer[State], val stateNum: Int) 
> extends State(states) {
>    type R = Registers // put into State base class, protected
>    object Got_EC_goto_ESCState extends Rule {
>        def test(r: R) = { ... }
>        def act(r: R) = { ... }
>      }
>   object Got_EEC_goto_ESCESCState extends Rule {
>        def test(r: R) = { ... }
>        def act(r: R) = { ... }
>      }
> ...
>   override val rules = ArrayBuffer(
>      Got_EC_goto_ESCState, 
>      Got_EEC_goto_EECState,
>     ...
>   )
> ...
> }
> {code}
> In the above, the nesting makes it clear each "Rule" which is a state-machine 
> transition, originates at a particular state.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to