Code Samples
Upcoming SlideShare
Loading in...5
×
 

Code Samples

on

  • 6,506 views

API examples

API examples

Statistics

Views

Total Views
6,506
Views on SlideShare
982
Embed Views
5,524

Actions

Likes
0
Downloads
16
Comments
0

4 Embeds 5,524

http://jactorconsulting.com 5402
http://66.147.242.164 118
http://translate.googleusercontent.com 3
https://translate.googleusercontent.com 1

Accessibility

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Code Samples Code Samples Presentation Transcript

  • 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
  • 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-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.)
  • 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.
  • Basic Code Samples● Hello world!● Actor <==> Actor● RP Games● Parallel Programming● Dependency Injection● Exception Handling● Loops
  • Hello world!● GettingStarted.java (main)● Test.java (actor)● Start.java (request)
  • 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(); }}
  • 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); }}
  • 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; }}
  • 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 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); } }); }}
  • 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!"); }}
  • 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; }}
  • 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 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); }}
  • 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!"); }}
  • 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; }}
  • 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 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); }}
  • 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); }}
  • 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; }}
  • 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 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); } }); }}
  • 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); }}
  • 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); }}
  • 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; }}
  • 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 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); } }); }}
  • 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!"); }}
  • 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; }}
  • OutputBoo!GettingStarted>
  • Loops● Test.java (actor)
  • 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); }}
  • 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 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); }}
  • OutputabcGettingStarted>
  • Looping● Test.java (actor)
  • 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); }}
  • Output12345GettingStarted>