anonymous classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

anonymous classes

Dennis Jarvis
I'd rather not write a DSL for a Java-based agent framework (GORITE) that I am working with - I'm hoping that Scala can remove much of the grubbiness from the goal specifications, which make use of anonymous classes as shown below:

class Cell extends Performer {

<snip>

    public Cell( String name ) {

        super( name );

        addGoal( new SequenceGoal( MAKE_BATCH, new Goal [] {
        new Goal ( PROCESS_PERCEPT ) {
            @Override
            public States execute( Data d ) {
                Order o = (Order) d.getValue( PERCEPT );
                d.setValue( ORDER, o );
                return Goal.States.PASSED;
            }
        },
        new Goal ( MAKE_METERBOXES0 ) {
            @Override
            public States execute( Data d ) {
                int n = ((Order) d.getValue( ORDER )).required;
                if ( TimeTrigger.isPending( d, "deadline", n*1000 ) )
                    return Goal.States.BLOCKED;
                n = ((Order) d.getValue( ORDER )).required;
                System.err.println(
                    "cell: made "+n+" meterboxes at "+timeStamp() );
                return Goal.States.PASSED;
            }
        },
                                   }));

        addGoal( new Plan( ABORT ) {
            // This goal tells the executor to relinquish its
            // thread as soon as possible. The goal also returns
            // STOPPED so as to "force" focus change, which in
            // this case leads to the execution finishing without
            // pursuing any other goal whatsoever.
            public Goal.States execute(Data d) {
                System.err.println("cell: abort");
                Executor.getDefaultExecutor().stop( true );
                return Goal.States.STOPPED;
            }
        });

 
 <snip>
}

Two questions:
1. SequenceGoal expects an array of (anonymous) goal instances as its second construction argument. How do you do this in Scala?
2. Are there any tricks in Scala to reduce the noise factor in the above code? 

Regards, Dennis

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: anonymous classes

Stephen Compall-3
On 2/16/17 5:44 AM, Dennis Jarvis wrote:
I'd rather not write a DSL for a Java-based agent framework (GORITE) that I am working with - I'm hoping that Scala can remove much of the grubbiness from the goal specifications, which make use of anonymous classes as shown below:

<snip>
        addGoal( new SequenceGoal( MAKE_BATCH, new Goal [] {
        new Goal ( PROCESS_PERCEPT ) {
<snip>

        addGoal( new Plan( ABORT ) {
            public Goal.States execute(Data d) {
                System.err.println("cell: abort");
                Executor.getDefaultExecutor().stop( true );
                return Goal.States.STOPPED;
            }
        });

 
 <snip>
}

Two questions:
1. SequenceGoal expects an array of (anonymous) goal instances as its second construction argument. How do you do this in Scala?
Array(elem1, elem2)
2. Are there any tricks in Scala to reduce the noise factor in the above code? 
Whenever you need to implement a functional interface, you can just write a lambda expression.
addGoal(d => {
  // make a State
}

That doesn't quite work in your case, but when you want an easy way to generate instances of an anonymous class implementing some non-generic methods, you can write a function that takes lambda to implement the methods, and call that instead. Think of this as "un-glorifying the lambdas" (as fancy classes like Goal are often glorified lambdas).

def plan(thingy: Thingy, exec: Data => Goal.States): Plan =
  new Plan(thingy) {
    override def execute(d: Data) = exec(d)
  }

addGoal(plan(ABORT, d => {
  System.err.println("cell: abort");
  Executor.getDefaultExecutor().stop( true );
  Goal.States.STOPPED
})

There are probably some better instances of low-hanging fruit, but it's hard to say without knowing more about GORITE and what you're doing. You don't have to go full-bore and build a complete DSL; it's a spectrum.  You add the abstractions piece-by-piece that make stuff nicer. You don't need to have defined a whole DSL to get benefit from something like def plan. But you're not going to get very far if you don't want to define any abstractions yourself at all.


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.