Snippets

Sebastian Sardina FAQ for using SARL + SWI/JPL Prolog + Maven

Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 Yes, use something like `TimeUnit.SECONDS.sleep(5)`
 
 
-## Intialize behavior in agents do not terminate! 
+## Emitting events and spawning agents in "`on Intialize`" behaviors: be careful!
 
 The `on Initialize` behavior handler in agents is a bit special, as it is the code ran when an agent is born. As such, its execution is more "synchronous" than other on-behavior rules. In particular:
 
 1. Any event emitted within an `on Initialize`, will not be processed until that `on Initialize` code finishes. So, your agent initialization should _not_ depend (and wait) on any emited event being processed, as they won't!
-2. When spawning an agent in `on Initialize`, the spawn instructions will not return until all the `Initialize` behaviors of the spawned agent have been executed fully. 
+2. When spawning an agent in `on Initialize`, the spawn instructions will return only after the agent has been created. However, creation of the agent (i.e., of the corresponding object) doe snot include initialization of the agent via its `on Initialize` behaviors. Said so, the Java thread manager may process those initialization processes of the new agent before continuing with the execution of the spawnning agent (and this seems to be the case in many Linux boxes where the executor service of Java tends to have the same behavior during all the runs). If you change computer, it may be different (I have encountered this fact with my students). In the following example, the thread executor service of Java seems to give the priority to the "`on Initialize`" of agent 2 instead of continuing the run of the spawn function.
+
 
 ```
 import io.sarl.core.Initialize
 }
 ```
 
-The result will be:
+The output has been:
 
 ```
 Launching the agent: Agent1
 agent1 end
 ```
 
-### More details on how events are processed by the SARL execution engine
+Here it appears as the "`on Initialize`" behaviors have been run all before the execution resumes after the `spawn()` statement, but this is just one way and one should not rely on that behavior being guaranteed: once the spawned agent is created, the `spawn()` commands returns.
+
+
+### More details on how events and spawnning are processed by the SARL execution engine
 
 When the event `e` is received by an agent the following algorithm is applied:
 
 
 The function `fire(e)` retrieves all the "`on E`" and runs them in parallel, and there is a synchronization point after the running of all the "`on E`" if `E` is `Initialize` or `Destroy` (for forcing synchronous execution of "`on Initialize`" and "`on Destroy`"). At the end of the "`on Initialize`" (after synchronization point), all the buffered events are fired.
 
-Regarding `spawn()`, the function runs the initialization process within a _separated thread_. In other works, the caller of `spawn()` is never blocked by the call. Then, the created thread runs all the initialization process, including the synchronous execution of "`on Initialize`".
+Observe that if the event is fired from within the "`on Initialize`", the same algorithm is applied whatever the receiving agent.
+
+Regarding `spawn()`, the function runs in two parts:
+
+1. First, the spawn agent is created. This part is run _in the same thread_ as the caller of spawn, so the spawn call _blocks_.
+2. Once the spawn agent has been created, the initialization process runs within a _separated thread_ from the spawner agent. So, the call `spawn()` is now not locked anymore. Then, the created thread runs all the initialization process, including the synchronous execution of "`on Initialize`". Consequently, the "`on Initialize`" of the spawn agent will not block the spawn caller.
+
 
 
 
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 
 If you think a question is worth putting it here, please let me know, I am happy to add it! THANKS!
 
+
 [TOC]
 
 -----------------------
 -----------------------
 # SARL
 
+Many of the answers here are almost verbatim explanations from SARL developer Stéphane Galland to my email enquires :-) 
+
 ### SARL is similar to Java but has different syntax in many places, how come?
 
 Because it uses and builts on [XTEND](https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html) framework.
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 agent1 end
 ```
 
+### More details on how events are processed by the SARL execution engine
+
+When the event `e` is received by an agent the following algorithm is applied:
+
+    if "on Initialize" is currently running then
+       add e to a buffer of events.
+    else if "on Destroy" is currently running then
+       ignore the event.
+    else
+       fire(e)
+    fi
+
+The function `fire(e)` retrieves all the "`on E`" and runs them in parallel, and there is a synchronization point after the running of all the "`on E`" if `E` is `Initialize` or `Destroy` (for forcing synchronous execution of "`on Initialize`" and "`on Destroy`"). At the end of the "`on Initialize`" (after synchronization point), all the buffered events are fired.
+
+Regarding `spawn()`, the function runs the initialization process within a _separated thread_. In other works, the caller of `spawn()` is never blocked by the call. Then, the created thread runs all the initialization process, including the synchronous execution of "`on Initialize`".
 
 
 
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 -----------------------
 # ECLIPSE
 
-### How do I make ECLIPSE know about environment variables (e.g., `SARL_VERSION`)?
+## How do I make ECLIPSE know about environment variables (e.g., `SARL_VERSION`)?
 
 I can imagine there are other ways, but the way I made it work is by starting ECLIPSE from CLI with the variable exported already:
 
 If you find another way that ECLIPSE can gather the environment variables (without re-defining them one by one), let me know! 
 
 
+## What plugins are useful to develop on SARL in ECLIPSE?
+
+On the SARL ECLIPSE distribution I install:
+
+* The Markdown Text Editor (to edit `README.md` files) - via Marketplace..
+* The Prolog Development Tool (ProDT) - via Marketplace.
+    * This will require first to install [Eclipse Dynamic Language Toolkit](https://projects.eclipse.org/projects/technology.dltk) (DLTK) 
+        * You can use this update link: http://download.eclipse.org/technology/dltk/updates-dev/latest/
+
+
 
 
 -----------------------
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 
 ## Intialize behavior in agents do not terminate! 
 
-The `Initialize` behavior handler in agents is a bit special, because it is the code ran when an agent is born. As such, its execution is more synchronous than other behavior rules.
-
-
-1. Any event emitted within an `Initialize`, will not be processed until `Initialize` finishes. So your agent initialization should not depend and wait on any other event being processed, as they won't!
-2. When spawning an agent in `Initialize`, the spawn instructions will not return until all the `Initialize` behaviors of the spawned agent have been executed fully. 
+The `on Initialize` behavior handler in agents is a bit special, as it is the code ran when an agent is born. As such, its execution is more "synchronous" than other on-behavior rules. In particular:
 
+1. Any event emitted within an `on Initialize`, will not be processed until that `on Initialize` code finishes. So, your agent initialization should _not_ depend (and wait) on any emited event being processed, as they won't!
+2. When spawning an agent in `on Initialize`, the spawn instructions will not return until all the `Initialize` behaviors of the spawned agent have been executed fully. 
 
 ```
 import io.sarl.core.Initialize
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.