diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/ADD_EXAMPLE.md b/contribs/io.sarl.examples/io.sarl.examples.plugin/ADD_EXAMPLE.md new file mode 100644 index 0000000000..8c6c9bc999 --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/ADD_EXAMPLE.md @@ -0,0 +1,108 @@ +# How to add an example + +Let the example having the following identifiers: +* `io-sarl-demos-myexample` +* `io_sarl_demos_myexample` +* `io.sarl.examples.myexample` + + +## 1. Include the example code + +1. Create the folder `projects/io-sarl-demos-myexample`. +2. Copy the example's code into the created folder. + + +## 2. Create the wizard for importing the example + +Add in the `plugin.xml` file: + + + + + + + %io_sarl_demos_myexample_Description + + + + + + %io_sarl_demos_myexample_Description + + + + + + + + + + + +Replace `path/to/the/file/to/open` by the path to the file to be opened after importing the example. + + +## 3. Update the localized strings. + +The update explained in the previous section uses two strings in the localized properties. +Add in the `OSGI-INF/bundle.properties` file: + + + io_sarl_demos_myexample_Name = THE NAME OF THE EXAMPLE + io_sarl_demos_myexample_Description = THE DESCRIPTION OF THE EXAMPLE + + + +## 4. Add a quick link in the welcome pages. + +In order to add a quick link to the example, you should add into the `plugin.xml` file: + + + + + + + + + + +## 5. Add the example into the welcome page + +Add into the `SARL-Example-Intro.xml` file: + + + + + + Detailed description of the example. + + + + + + +## 6. Update the building script + +Add into the `build.xml` file: + + + + + diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/OSGI-INF/l10n/bundle.properties b/contribs/io.sarl.examples/io.sarl.examples.plugin/OSGI-INF/l10n/bundle.properties index 6e8248516e..8682e5a4a7 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/OSGI-INF/l10n/bundle.properties +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/OSGI-INF/l10n/bundle.properties @@ -4,6 +4,9 @@ Bundle-Name = Examples with SARL Language SARLExamplesCategory= SARL Example SARLExamplesProjectCategory= Examples +SARLTutorialsCategory= SARL Tutorials +SARLTutorialsProjectCategory= Tutorials + HelloworldExample_Name= A Helloworld Agent in SARL HelloworldExample_Description= A simple agent displaying Hello on the console and dying 2sec later. @@ -24,3 +27,12 @@ CounterExample_Description= A first agent creates two other agents, the first on FactorialExample_Name= Two examples of factorial computation FactorialExample_Description= The first demo introduces a single agent computing a factorial, the second one introduces two agents in interaction: the first agent asking the second to compute the factorial value of a specified integer. + +io_sarl_tutorials_holonicauction_Name = Auction between holonic members +io_sarl_tutorials_holonicauction_Description = The auctioneer creates biders in its inner context, and manages the auction. + +io_sarl_tutorials_pingpong_Name = Ping-Pong event exchanges +io_sarl_tutorials_pingpong_Description = Agents are exchanging events with a ping-pong interaction protocol. + +io_sarl_tutorials_pingpongspace_Name = Ping-Pong event exchanges inside a specific space +io_sarl_tutorials_pingpongspace_Description = Agents are exchanging events inside a specific space with a ping-pong interaction protocol. \ No newline at end of file diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/build.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/build.xml index 7e2d1c9f0b..3e80367a89 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/build.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/build.xml @@ -23,6 +23,9 @@ + + + diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/intro/SARL-Example-Intro.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/intro/SARL-Example-Intro.xml index 56e6dd5b05..3e9ae62dc0 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/intro/SARL-Example-Intro.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/intro/SARL-Example-Intro.xml @@ -98,6 +98,44 @@ + + + + This project contains the code of the official tutorial related to holonic auctions. + The auctioneer creates bidding agents into its inner context. + The bidding agents send their offers to the parent agent, i.e. the auctioneer. + The auctioneer selects the best offer. + + + + + + + + This project contains the implementation of the simple interaction protocol named "Ping-Pong". + An agent fire a ping event. The receiving agent replies with a pong event. + Then, the pong event receiver replies with a new ping event, and so on. + + + + + + + + This project contains the implementation of the simple interaction protocol named "Ping-Pong". + An agent fire a ping event. The receiving agent replies with a pong event. + Then, the pong event receiver replies with a new ping event, and so on. + The events are exchanged inside a specific event space. + + + + diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/plugin.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/plugin.xml index 4bddd2ddb2..e8647191d3 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/plugin.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/plugin.xml @@ -14,6 +14,18 @@ parentCategory="io.sarl.eclipse.category.wizards" /> + + + + + + + + @@ -230,7 +242,100 @@ - + + + + + + %io_sarl_tutorials_holonicauction_Description + + + + + + %io_sarl_tutorials_holonicauction_Description + + + + + + + + + + + + + + + %io_sarl_tutorials_pingpong_Description + + + + + + %io_sarl_tutorials_pingpong_Description + + + + + + + + + + + + + + + %io_sarl_tutorials_pingpongspace_Description + + + + + + %io_sarl_tutorials_pingpongspace_Description + + + + + + + + + + maxBid) { + info(" better bid") + maxBid = occurrence.value + winner = occurrence.source + } + } + } + +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-holonicauction/src/main/sarl/io/sarl/examples/holonicauction/bidder.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-holonicauction/src/main/sarl/io/sarl/examples/holonicauction/bidder.sarl new file mode 100644 index 0000000000..9dd6e454f6 --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-holonicauction/src/main/sarl/io/sarl/examples/holonicauction/bidder.sarl @@ -0,0 +1,57 @@ +package io.sarl.examples.holonicauction + +import io.sarl.core.DefaultContextInteractions +import io.sarl.core.Initialize +import io.sarl.core.Lifecycle +import io.sarl.util.Scopes +import io.sarl.core.Logging +import java.util.Random + +agent Bidder { + + uses DefaultContextInteractions, Lifecycle, Logging + + val random = new Random() + var maxPrice : float + var myLastBid : float + + on Initialize { + info("Bidder created") + maxPrice = random.nextFloat() * 1000f + 100f + info(maxPrice) + } + + on Price { + if(occurrence.price == myLastBid) { + info("I do not bet, I am the winner with :" + myLastBid) + } else { + if(occurrence.price < maxPrice) { + info("Receiving the price: " + occurrence.price) + var priceIncrease = random.nextFloat() * 50f + if(priceIncrease > 0) { + var newPrice = occurrence.price + priceIncrease + + info("My new price is: " + newPrice) + if(newPrice <= maxPrice) { + info("Send my bid: " + newPrice) + var superScope = Scopes.addresses(defaultSpace.getAddress(defaultContext.getID)) + emit(new Bid(newPrice), superScope) + myLastBid = newPrice + } else { + info(" I give up, this is beyond my resources : " + myLastBid) + } + } + } + else { + info("I dropped to " + myLastBid) + } + } + } + + on StopAuction { + info("Auctioneer wants to kill me") + killMe + } + +} + diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-holonicauction/src/main/sarl/io/sarl/examples/holonicauction/events.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-holonicauction/src/main/sarl/io/sarl/examples/holonicauction/events.sarl new file mode 100644 index 0000000000..31eb217f8d --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-holonicauction/src/main/sarl/io/sarl/examples/holonicauction/events.sarl @@ -0,0 +1,17 @@ +package io.sarl.examples.holonicauction + +event Price { + val price : float + new(price : float) { + this.price = price + } +} + +event Bid { + val value : float + new(value : float) { + this.value = value + } +} + +event StopAuction diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/boot.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/boot.sarl new file mode 100644 index 0000000000..6fb00394bb --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/boot.sarl @@ -0,0 +1,16 @@ +package io.sarl.examples.pingpong + +import io.sarl.core.Initialize +import io.sarl.core.Lifecycle + +agent BootAgent { + + uses Lifecycle + + on Initialize { + spawn( PongAgent ) + spawn( PingAgent ) + killMe + } + +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/events.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/events.sarl new file mode 100644 index 0000000000..9ca53dc5de --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/events.sarl @@ -0,0 +1,21 @@ +package io.sarl.examples.pingpong + +event Ping { + + val index : int + + new (i : int) { + index = i + } + +} + +event Pong { + + val index : int + + new (i : int) { + index = i + } + +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/pingagent.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/pingagent.sarl new file mode 100644 index 0000000000..4738f730a9 --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/pingagent.sarl @@ -0,0 +1,36 @@ +package io.sarl.examples.pingpong + +import io.sarl.core.DefaultContextInteractions +import io.sarl.core.Initialize +import io.sarl.core.Schedules +import io.sarl.util.Scopes +import io.sarl.core.Logging + +agent PingAgent { + + uses DefaultContextInteractions, Schedules, Logging + + on Pong { + info("Receiving Pong #" + occurrence.index) + info("Sending Ping #" + (occurrence.index + 1)) + emit( + new Ping( occurrence.index + 1 ), + Scopes.addresses( occurrence.source ) + ) + } + + on Initialize { + info("Waiting for Pong Agent") + val taskVar = task("waiting_for_partner") + + taskVar.every(1000) [ + if (defaultSpace.participants.size > 1) { + info("Pong Agent detected.") + info("Sending Ping #0") + emit( new Ping(0) ) + taskVar.cancel + } + ] + } + +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/pongagent.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/pongagent.sarl new file mode 100644 index 0000000000..76d90f7c71 --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpong/src/main/sarl/io/sarl/examples/pingpong/pongagent.sarl @@ -0,0 +1,20 @@ +package io.sarl.examples.pingpong + +import io.sarl.core.DefaultContextInteractions +import io.sarl.util.Scopes +import io.sarl.core.Logging + +agent PongAgent { + + uses DefaultContextInteractions, Logging + + on Ping { + info("Receiving Ping #" + occurrence.index) + info("Sending Pong #" + occurrence.index) + emit( + new Pong( occurrence.index ), + Scopes.addresses( occurrence.source ) + ) + + } +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/boot.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/boot.sarl new file mode 100644 index 0000000000..ef24690974 --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/boot.sarl @@ -0,0 +1,18 @@ +package io.sarl.examples.pingpongspace + +import io.sarl.core.Initialize +import io.sarl.core.Lifecycle +import java.util.UUID + +agent BootAgent { + + uses Lifecycle + + on Initialize { + var spaceId = UUID::randomUUID + spawn( PongAgent, spaceId ) + spawn( PingAgent, spaceId ) + killMe + } + +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/events.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/events.sarl new file mode 100644 index 0000000000..dfd0f69710 --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/events.sarl @@ -0,0 +1,21 @@ +package io.sarl.examples.pingpongspace + +event Ping { + + val index : int + + new (i : int) { + index = i + } + +} + +event Pong { + + val index : int + + new (i : int) { + index = i + } + +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/pingagent.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/pingagent.sarl new file mode 100644 index 0000000000..253375a17b --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/pingagent.sarl @@ -0,0 +1,51 @@ +package io.sarl.examples.pingpongspace + +import io.sarl.core.DefaultContextInteractions +import io.sarl.core.Initialize +import io.sarl.core.Schedules +import io.sarl.util.Scopes +import io.sarl.core.Behaviors +import io.sarl.util.OpenEventSpace +import io.sarl.util.OpenEventSpaceSpecification +import java.util.UUID +import io.sarl.core.Logging + +agent PingAgent { + + uses DefaultContextInteractions, Schedules, Behaviors, Logging + + var comspace : OpenEventSpace + + on Pong { + info("Receiving Pong #" + occurrence.index) + info("Sending Ping #" + (occurrence.index + 1)) + var evt = new Ping(occurrence.index + 1) + evt.source = comspace.getAddress(getID()) + comspace.emit( + evt, + Scopes.addresses(occurrence.source) + ) + } + + on Initialize { + comspace = defaultContext.getOrCreateSpaceWithSpec(typeof(OpenEventSpaceSpecification), + occurrence.parameters.get(0) as UUID) + + comspace.register(asEventListener()) + + info("Waiting for Pong Agent") + val task = task("waiting_for_partner") + task.every(1000) [ + if (comspace.participants.size > 1) { + info("Pong Agent detected.") + info("Sending Ping #0") + var evt = new Ping(0) + evt.source = comspace.getAddress(getID()) + + comspace.emit(evt) + task.cancel + } + ] + } + +} diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/pongagent.sarl b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/pongagent.sarl new file mode 100644 index 0000000000..99885922e4 --- /dev/null +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/pongagent.sarl @@ -0,0 +1,36 @@ +package io.sarl.examples.pingpongspace + +import io.sarl.core.DefaultContextInteractions +import io.sarl.util.Scopes +import io.sarl.util.OpenEventSpace +import io.sarl.core.Initialize +import io.sarl.util.OpenEventSpaceSpecification +import io.sarl.core.Behaviors +import java.util.UUID +import io.sarl.core.Logging + +agent PongAgent { + + uses DefaultContextInteractions, Behaviors, Logging + + var comspace : OpenEventSpace + + on Initialize { + comspace = defaultContext.getOrCreateSpaceWithSpec( + typeof(OpenEventSpaceSpecification), + occurrence.parameters.get(0) as UUID) + comspace.register(asEventListener()) + } + + on Ping { + info("Receiving Ping #" + occurrence.index) + info("Sending Pong #" + occurrence.index) + var evt = new Pong( occurrence.index ) + evt.source = comspace.getAddress(getID) + comspace.emit( + evt, + Scopes.addresses( occurrence.source ) + ) + + } +}