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
 Note that because registration happens after the creation of the behavior module, the `initParam` will be available at time of `Initialize` execution.
 
 
+## Cannot access agent owner of a skill in Initialize behavior, why?
+
+Summary of See [this thread](https://groups.google.com/forum/#!topic/sarl/tEGCk90anNk)
+
+The owner of the skill is set when the skill is _attached_ to the agent. It means that the owner cannot be known within the constructor of the skill. The function `install` is invoked just after the skill is installed into the agent.
+So:
+
+	new {
+	   super()
+	   assert this.owner === null
+	}
+
+	def install {
+		assert this.owner !== null
+	}
+
+Importantly, the initialization (via `Initialize` event) is not at creation time (constructor) but when the entity is attached to the agent. 
+
+Within SARL developers' perspective, constructor statements should be redefined only if you want to set the value of a field that is final (`val` keyword), because it is mandatory regarding the semantic of the val keyword. 
+Consequently, the best practice is that all initializations should be within the `on Initialize`. They should avoid constructor definition.
+
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 The `Initialize` event is fired by the `registerBehavior` function. As you could see, this function does not provide a mean for passing initialization parameters.
 I think the ability to set the Initialize parameters when registering the behaviors is a missed feature from the SARL API.
 
-Until it is solved, you could use the following work around:
+Until it is solved (see [open issue here](https://github.com/sarl/sarl/issues/859)), you could use the following work around:
 
 
 	behavior MyBehavior {
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 SARL's developer states that the mapping of the operator from SARL to Java are:
 
 * `a === b` becomes `a == b`
-* `a !== b" becomes `a != b`
+* `a !== b` becomes `a != b`
 * `a == b` becomes `a == null ? (b == null) : a.equals(b)`
 * `a != b` becomes `!Objects.equals(a,b)`. This is null-safe (part of Google API) and the code of the function is `a == b || (a != null && a.equals(b))`
 
 
 Check SARL documentation on that [here](http://www.sarl.io/docs/official/reference/general/Operators.html#8-collection-operators).
 
+
+## I cannot pass data to the Initialize of a Behaivor, occurrence.paramters is always empty!
+
+From SARL developer in this post](https://groups.google.com/forum/#!topic/sarl/E0TuX-V123U):
+
+Currently, there is no way to set the `occurrence.parameters` values with the current API.
+
+The `Initialize` event is fired by the `registerBehavior` function. As you could see, this function does not provide a mean for passing initialization parameters.
+I think the ability to set the Initialize parameters when registering the behaviors is a missed feature from the SARL API.
+
+Until it is solved, you could use the following work around:
+
+
+	behavior MyBehavior {
+
+	   var initParam : Object[]
+
+	   new (owner : Agent, initParam : Object*) {
+		  super(owner)
+		  this.initParam = initParam
+	   }
+
+	   on Initialize {
+		  // Do something with this.initParam
+	   }
+
+	}
+
+	agent MyAgent {
+
+	   uses Behaviors
+
+	   on Initialize {
+		  var beh = new MyBehavior(this, "p1")
+		  beh.registerBehavior
+	   }
+	}
+
+
+Note that because registration happens after the creation of the behavior module, the `initParam` will be available at time of `Initialize` execution.
+
+
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 	}
 
 
-### Equality and identity comparison in SARL and checking for null: same as Java ?
+### Equality and identity comparison (`==`, `===`, `!=`, `!==`) in SARL and checking for null: same as Java ?
 
 SARL's developer states that the mapping of the operator from SARL to Java are:
 
Updated by Sebastian Sardina

File SARL-FAQ.markdown Modified

  • Ignore whitespace
  • Hide word diff
 
 ### Equality and identity comparison in SARL and checking for null: same as Java ?
 
-SARL' s developer states that the mapping of the operator from SARL to Java are:
+SARL's developer states that the mapping of the operator from SARL to Java are:
 
 * `a === b` becomes `a == b`
 * `a !== b" becomes `a != b`
 * `a == b` becomes `a == null ? (b == null) : a.equals(b)`
 * `a != b` becomes `!Objects.equals(a,b)`. This is null-safe (part of Google API) and the code of the function is `a == b || (a != null && a.equals(b))`
 
-It is always better to test valid against null with the `===` or `!==` operators. These operators are not replaced neither `operator_equals` nor `operator_notEquals` within the Java code.
+It is always better to test valid against null with the `===` or `!==` operators. 
 
-I believe this is because SARL comparison operators follow the  which means that:
+Because the SARL `==` operator is mapped to the Java `equals()` function, and the `===` and `!==`  operators to the Java `==` and `!=` operators, it is better/safer, and a best practice, to use `===` and `!==` when one of the operands is of primitive type, e.g. `null`, number constants, primitive type variables. These operators are not replaced neither `operator_equals` nor `operator_notEquals` within the Java code.
+
+Ususally, the SARL compiler generates a warning to push you to use `===` in place of `==`. But with `null == value`, an ambiguous call error occurs before the warning is generated.
+
+I believe SARL mapping this is because SARL comparison operators follow the  which means that:
 
 * In Xtend the equals operators (`==`, `!=`) are bound to `Object.equals`. 
 * Java’s identity equals semantic is mapped to the tripple-equals operators `===` and `!==` in Xtend.
 
 Check xtend doc [here](https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#operators) for more details and SARL doc [here](http://www.sarl.io/docs/official/reference/general/Operators.html#3-comparison-operators).
 
-_**Note from SARL developer**_: SARL extends a part of [Xtend dialect](https://www.eclipse.org/xtend/index.html) in order to have benefits of several background features, such as the validation tests for type inheritance. Since Xtend and SARL uses [Xbase](https://www.eclipse.org/Xtext/documentation/305_xbase.html#xbase-expressions), a large part of the syntax is the same, especially within the blocks of code. (The rest of the syntax is defined in parallel. SARL was inspired by languages such as Scala, Python and Ruby. Several bugs or incoherencies of Xtend are fixed in SARL.
+_**Note directly from SARL developer (Sept 2018)**_: SARL extends a part of [Xtend dialect](https://www.eclipse.org/xtend/index.html) in order to have benefits of several background features, such as the validation tests for type inheritance. Since Xtend and SARL uses [Xbase](https://www.eclipse.org/Xtext/documentation/305_xbase.html#xbase-expressions), a large part of the syntax is the same, especially within the blocks of code. (The rest of the syntax is defined in parallel. SARL was inspired by languages such as Scala, Python and Ruby. Several bugs or incoherencies of Xtend are fixed in SARL.
 
 There is still a bit of an issue inside the Xbase library; check this <https://github.com/sarl/sarl/issues/852#issuecomment-420842088>
 
+I have created an issue regarding null == value: Issue [#854](https://github.com/sarl/sarl/issues/854). 
+According to the associated discussion within the Xbase group [#300](https://github.com/eclipse/xtext-extras/issues/300), the error message will be updated to push the developers to use `===`.
+
+
 
 ### How to return two values? Pairs in SARL
 
  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.