Jactor Code Samples●   Slides: http://sourceforge.net/projects/jactor/files/Code%20Samples%2012-JUN-2012.pdf/download●   C...
JActor●   Getting Started●   Basic Code Samples●   State Machines
Getting Started●   The JActor jar file●   Create a test environment
The JActor jar file●   Download Jactor-3.1.0.zip from    https://sourceforge.net/projects/jactor/files/●   Extract jactor-...
Create a Test Environment●   Create a j.bat file in the GettingStarted    directory:       del *.class       call javac -c...
Basic Code Samples●   Hello world!●   Actor <==> Actor●   RP Games●   Parallel Programming●   Dependency Injection●   Exce...
Hello world!●   GettingStarted.java (main)●   Test.java (actor)●   Start.java (request)
GettingStarted.java                  (main)import org.agilewiki.jactor.*;public class GettingStarted {    public static vo...
Test.java                      (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test ex...
Start.java                     (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Start...
OutputHello world!GettingStarted>
Actor <==> Actor●   Test.java (actor)●   Greeter.java (actor)●   Greet.java (request)
Test.java                      (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test ex...
Greeter.java                    (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greete...
Greet.java                    (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greet ...
OutputHello world!GettingStarted>
RP Games●   Test.java (actor)●   Greeter.java (actor)●   Trigger.java (request)
Test.java                      (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test ex...
Greeter.java                    (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greete...
Trigger.java                    (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Trig...
OutputTrigger...Hello world!GettingStarted>
Parallel Programming●   Test.java (actor)●   Timer.java (actor)●   Delay.java (request)
Test.java                         (actor)public class Test extends JLPCActor {    public void start(final RP rp) throws Ex...
Timer.java                      (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Timer ...
Delay.java                    (request)public class Delay extends Request<Object, Timer> {    public final int ms;    publ...
Output1003GettingStarted>
Dependency Injection●   Test.java (actor)●   Greeter.java (actor)●   Printer.java(actor)●   Print.java (request)
Test.java                      (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test ex...
Greeter.java                    (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greete...
Printer.java                     (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Print...
Print.java                     (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Print...
OutputGreeting:Hello world!GettingStarted>
Exception Handling●   Test.java (actor)●   ThrowsException.java (actor)●   ThrowException.java (request)
Test.java                         (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test...
ThrowsException                  (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Throw...
ThrowException                  (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Thro...
OutputBoo!GettingStarted>
Loops●   Test.java (actor)
Test.java                      (actor)public class Test extends JLPCActor {    public void start(RP rp) throws Exception {...
Output12345GettingStarted>
State Machines●   A Sequence of Requests●   Looping
A Sequence of Requests●   Test.java (actor)
Test.java                      (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test ex...
OutputabcGettingStarted>
Looping●   Test.java (actor)
Test.java                             (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;import org.ag...
Output12345GettingStarted>
Upcoming SlideShare
Loading in...5
×

Code Samples

7,205

Published on

API examples

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
7,205
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Code Samples

  1. 1. Jactor Code Samples● Slides: http://sourceforge.net/projects/jactor/files/Code%20Samples%2012-JUN-2012.pdf/download● Code: https://github.com/anirbanbhattacharjee/jactor-in-action
  2. 2. JActor● Getting Started● Basic Code Samples● State Machines
  3. 3. Getting Started● The JActor jar file● Create a test environment
  4. 4. The JActor jar file● Download Jactor-3.1.0.zip from https://sourceforge.net/projects/jactor/files/● Extract jactor-3.1.0.jar from the zip file and copy it to a new directory, GettingStarted.● (The JActor version will change—current version is 3.1.0.)
  5. 5. Create a Test Environment● Create a j.bat file in the GettingStarted directory: del *.class call javac -classpath jactor-3.1.0.jar *.java call java -classpath .;jactor-3.1.0.jar GettingStarted● The j.bat file can be used to run a test.
  6. 6. Basic Code Samples● Hello world!● Actor <==> Actor● RP Games● Parallel Programming● Dependency Injection● Exception Handling● Loops
  7. 7. Hello world!● GettingStarted.java (main)● Test.java (actor)● Start.java (request)
  8. 8. GettingStarted.java (main)import org.agilewiki.jactor.*;public class GettingStarted { public static void main(String[] args) throws Exception { MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(10); Mailbox mailbox = mailboxFactory.createMailbox(); JAFuture future = new JAFuture(); Test test = new Test(); test.initialize(mailbox); Start.req.send(future, test); mailboxFactory.close(); }}
  9. 9. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(RP rp) throws Exception { System.out.println("Hello world!"); rp.processResponse(null); }}
  10. 10. Start.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Start extends Request<Object, Test> { public static final Start req = new Start(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Test a = (Test) targetActor; a.start(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Test; }}
  11. 11. OutputHello world!GettingStarted>
  12. 12. Actor <==> Actor● Test.java (actor)● Greeter.java (actor)● Greet.java (request)
  13. 13. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Greeter greeter = new Greeter(); greeter.initialize(getMailbox()); Greet.req.send(this, greeter, new RP<String>() { public void processResponse(String greeting) throws Exception { System.out.println(greeting); rp.processResponse(null); } }); }}
  14. 14. Greeter.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greeter extends JLPCActor { public void greet(RP rp) throws Exception { rp.processResponse("Hello world!"); }}
  15. 15. Greet.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greet extends Request<String, Greeter> { public static final Greet req = new Greet(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Greeter a = (Greeter) targetActor; a.greet(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Greeter; }}
  16. 16. OutputHello world!GettingStarted>
  17. 17. RP Games● Test.java (actor)● Greeter.java (actor)● Trigger.java (request)
  18. 18. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Greeter greeter = new Greeter(); greeter.initialize(getMailbox()); Greet.req.send(this, greeter, new RP<String>() { public void processResponse(String greeting) throws Exception { System.out.println(greeting); rp.processResponse(null); } }); System.out.println("trigger..."); Trigger.req.sendEvent(this, greeter); }}
  19. 19. Greeter.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greeter extends JLPCActor { private RP rp; public void greet(RP rp) throws Exception { this.rp = rp; } public void trigger(RP rp) throws Exception { rp.processResponse(null); this.rp.processResponse("Hello world!"); }}
  20. 20. Trigger.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Trigger extends Request<Object, Greeter> { public static final Trigger req = new Trigger(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Greeter a = (Greeter) targetActor; a.trigger(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Greeter; }}
  21. 21. OutputTrigger...Hello world!GettingStarted>
  22. 22. Parallel Programming● Test.java (actor)● Timer.java (actor)● Delay.java (request)
  23. 23. Test.java (actor)public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Timer timer1 = new Timer(); timer1.initialize( getMailboxFactory().createAsynchronousMailbox()); Timer timer2 = new Timer(); timer2.initialize( getMailboxFactory().createAsynchronousMailbox()); final long t0 = System.currentTimeMillis(); RP prp = new RP() { boolean pending = true; public void processResponse(Object obj) throws Exception { if (pending) pending = false; else { System.out.println(System.currentTimeMillis()-t0); rp.processResponse(null); } } }; (new Delay(1000)).send(this, timer1, prp); (new Delay(1000)).send(this, timer2, prp); }}
  24. 24. Timer.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Timer extends JLPCActor { public void delay(int ms, RP rp) throws Exception { Thread.sleep(ms); rp.processResponse(null); }}
  25. 25. Delay.java (request)public class Delay extends Request<Object, Timer> { public final int ms; public Delay(int ms) { this.ms = ms; } public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Timer a = (Timer) targetActor; a.delay(ms, rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Timer; }}
  26. 26. Output1003GettingStarted>
  27. 27. Dependency Injection● Test.java (actor)● Greeter.java (actor)● Printer.java(actor)● Print.java (request)
  28. 28. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); final Greeter greeter = new Greeter(); greeter.initialize(getMailbox(), printer); (new Print("Greeting:")). send(this, greeter, new RP() { public void processResponse(Object rsp) throws Exception { Greet.req.send(Test.this, greeter, rp); } }); }}
  29. 29. Greeter.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greeter extends JLPCActor { public void greet(RP rp) throws Exception { (new Print("Hello world!")).send(this, this, rp); }}
  30. 30. Printer.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Printer extends JLPCActor { public void print(String value, RP rp) throws Exception { System.out.println(value); rp.processResponse(null); }}
  31. 31. Print.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Print extends Request<Object, Printer> { public final String value; public Print(String value) { this.value = value; } public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Printer a = (Printer) targetActor; a.processRequest(value, rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Printer; }}
  32. 32. OutputGreeting:Hello world!GettingStarted>
  33. 33. Exception Handling● Test.java (actor)● ThrowsException.java (actor)● ThrowException.java (request)
  34. 34. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { ThrowsException throwsException = new ThrowsException(); throwsException.initialize(getMailbox()); setExceptionHandler(new ExceptionHandler() { public void process(Exception exception) throws Exception { System.out.println(exception.getMessage()); rp.processResponse(null); } }); ThrowException.req.send(this, throwsException, new RP() { public void processResponse(Object response) throws Exception { System.out.println("No exception"); rp.processResponse(null); } }); }}
  35. 35. ThrowsException (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class ThrowsException extends JLPCActor { public void throwException(RP rp) throws Exception { throw new Exception("Boo!"); }}
  36. 36. ThrowException (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class ThrowException extends Request<Object, ThrowsException> { public static final ThrowException req = new ThrowException(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { ThrowsException a = (ThrowsException) targetActor; a.throwException(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof ThrowsException; }}
  37. 37. OutputBoo!GettingStarted>
  38. 38. Loops● Test.java (actor)
  39. 39. Test.java (actor)public class Test extends JLPCActor { public void start(RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); final int max = 5; (new JAIterator() { int i = 0; protected void process(RP irp) throws Exception { if (i == max) irp.processResponse("done"); else { i += 1; (new Print(""+i)). send(Test.this, printer, irp); } } }).iterate(rp); }}
  40. 40. Output12345GettingStarted>
  41. 41. State Machines● A Sequence of Requests● Looping
  42. 42. A Sequence of Requests● Test.java (actor)
  43. 43. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); SMBuilder smb = new SMBuilder(); smb._send(printer, new Print("a")); smb._send(printer, new Print("b")); smb._send(printer, new Print("c")); smb.call(rp); }}
  44. 44. OutputabcGettingStarted>
  45. 45. Looping● Test.java (actor)
  46. 46. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;import org.agilewiki.jactor.stateMachine.*;public class Test extends JLPCActor { int counter; public void start(RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); SMBuilder smb = new SMBuilder(); counter = 1; smb._label("loop"); smb._send(printer, new ObjectFunc() { public Object get(StateMachine sm) { return new Print(""+counter); } }); smb._if(new BooleanFunc() { public boolean get(StateMachine sm) { counter += 1; return counter < 6; } }, "loop"); smb.call(rp); }}
  47. 47. Output12345GettingStarted>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×