Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax for SARL language. #3772

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,6 @@
[submodule "vendor/grammars/language-reason"]
path = vendor/grammars/language-reason
url = https://github.com/reasonml-editor/language-reason
[submodule "vendor/grammars/sarl-textmate"]
path = vendor/grammars/sarl-textmate
url = https://github.com/sarl/sarl-textmate
3 changes: 3 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,6 @@ vendor/grammars/xml.tmbundle:
- text.xml.xsl
vendor/grammars/zephir-sublime:
- source.php.zephir
vendor/grammars/sarl-textmate:
- source.sarl

9 changes: 9 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5034,6 +5034,7 @@ Xtend:
- ".xtend"
ace_mode: text
language_id: 406

YAML:
type: data
tm_scope: source.yaml
Expand Down Expand Up @@ -5183,3 +5184,11 @@ xBase:
tm_scope: source.harbour
ace_mode: text
language_id: 421
sarl:
type: programming
ace_mode: text
codemirror_mime_type: text/x-sarl
extensions:
- ".sarl"
language_id: 422

72 changes: 72 additions & 0 deletions samples/sarl/behaviors.sarl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.sarl.demos.basic.behaviors

import io.sarl.core.Behaviors
import io.sarl.core.Destroy
import io.sarl.core.Initialize
import io.sarl.core.Lifecycle
import io.sarl.core.Logging

/*
* Declaration of a simple Factorial event with two internal variables
*/
event Factorial {
var number : Long
var value : Long
}

/*
* Declaration of a simple BehaviorInitialize event without any data
*/
event BehaviorInitialize {}

/*
* Declaration of a new behavior
*/
behavior FactorialBehavior {
uses Lifecycle, Behaviors, Logging /* the list of capacities used by this behavior */

var upto : Long = 5l /* A behavior attribute */

on BehaviorInitialize {
this.upto = new Long(16)
info("Received Calculate for " + this.upto)
wake(new Factorial => [ number = 0l ; value = 1l ]) /* Wake function send a new factorial event within the internal context of an agent (could be listen by the agent itself, its collections of behaviors and its sub-members) */
}

on Factorial [ occurrence.number < upto ] {
wake(new Factorial => [ number = increment(occurrence.number) ; value = occurrence.value * (increment(occurrence.number)) ])
}

on Factorial [occurrence.number == upto] {
info("Factorial of " + upto + " is " + occurrence.value)

killMe
}

def increment(nb: Long) : Long {
return nb+1
}
}

/*
* A simple agent executing/adopting a externally defined behavior.
*/
agent BehaviorPlayer {

uses Behaviors

var myFactorialBehavior : FactorialBehavior


on Initialize {
myFactorialBehavior = new FactorialBehavior(this) /* instantiation of the previously declared behavior */
registerBehavior(myFactorialBehavior); /* registration of this behavior as one of the agent's behaviors */
wake(new BehaviorInitialize) /* send the BehaviorInitialize event in the agent internal context */
}

on Destroy {
unregisterBehavior(myFactorialBehavior); /* WARNING: never forget to unregister a behavior before dying */
}

}

36 changes: 36 additions & 0 deletions samples/sarl/countdown.sarl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.sarl.demos.basic.countdown

import io.sarl.core.Destroy
import io.sarl.core.Initialize
import io.sarl.core.Lifecycle
import io.sarl.core.Logging
import io.sarl.core.Schedules
import java.util.concurrent.atomic.AtomicInteger

event Element

agent CountDownAgent {
uses Lifecycle, Schedules, Logging

val counter = new AtomicInteger(5)

on Initialize {
info("Initialize")

every(2000) [ /* Decrement counter every 2 sec until v = 0, then kill itself*/
var v = counter.decrementAndGet;
info("Counter=" + v)
if (v <= 0) {
killMe
}
]


}

on Destroy {
info("Goodbye World!")
}

}

112 changes: 112 additions & 0 deletions samples/sarl/distributedfactorial.sarl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.sarl.demos.factorial.distributed

import io.sarl.core.Behaviors
import io.sarl.core.DefaultContextInteractions
import io.sarl.core.Initialize
import io.sarl.core.Lifecycle
import io.sarl.core.Logging
import io.sarl.core.Schedules

event Factorial {
var number : Long
var value : Long
}

event Calculate {
var number : Long
}

event ComputationDone {
var result : Long
}

event Hello

event Die

agent DistributedFactorialBootAgent {
uses DefaultContextInteractions, Lifecycle

on Initialize {
spawn(FactorialAgent)
spawn(FactorialQueryAgent,10l)
killMe
}
}

agent FactorialQueryAgent {
uses Lifecycle, DefaultContextInteractions, Schedules, Logging

var factValue : Long

var started = false

on Initialize {
loggingName = "QueryAgent"
if (occurrence.parameters.length > 0) {
factValue = new Long(occurrence.parameters.get(0) as Long)

info("Auto-detecting Factorial")
task("discovery-task").every(1000) [
emit(new Hello)
]
} else {
info("Bad parameter settings, you must specify an integer value to compute the corresponding factorial")
emit(new Die)
killMe
}

}

on Hello [!isFromMe && !started] {
started = true
task("discovery-task").cancel
info("Found Factorial")
info("Query sent with number " + factValue)
emit(new Calculate => [number = factValue])
}

on ComputationDone {
info(factValue + "! = " + occurrence.result)
emit(new Die)
info("Killing")
killMe
}
}

agent FactorialAgent {
uses Lifecycle, Behaviors, Logging, DefaultContextInteractions

var upto : Long

on Initialize {
loggingName = "FactorialAgent"
info("Factorial initialized")
}

on Hello [!isFromMe] {
emit(new Hello)
}

on Factorial [occurrence.number < upto] {
wake(new Factorial => [number = occurrence.number + 1; value = occurrence.value * (occurrence.number + 1)])
}

on Factorial [occurrence.number == upto] {
info(this.upto + "! = " + occurrence.value)
emit(new ComputationDone => [result = occurrence.value])
}

on Calculate {
this.upto = new Long(occurrence.number);
info("Query for " + this.upto + "!")
wake(new Factorial => [number = 0l; value = 1l])
}

on Die {
info("Killing")
killMe
}

}

63 changes: 63 additions & 0 deletions samples/sarl/helloworld.sarl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.sarl.demos.basic.helloworld

import io.sarl.core.AgentKilled
import io.sarl.core.AgentSpawned
import io.sarl.core.ContextJoined
import io.sarl.core.ContextLeft
import io.sarl.core.Destroy
import io.sarl.core.Initialize
import io.sarl.core.Lifecycle
import io.sarl.core.Logging
import io.sarl.core.MemberJoined
import io.sarl.core.MemberLeft
import io.sarl.core.Schedules

/*
* A "Hello World" Agent.
*/
agent HelloAgent {
uses Lifecycle, Schedules, Logging

/* Print "Hello World" when spawned and wait 2 seconds to kill itself */
on Initialize {
info("Hello World!")
in(2000) [killMe]
}

/* Event trigger before agent dies, Print "Goodbye World" before dying */
on Destroy {
/* the Destroy event is automatically */
info("Goodbye World!")
}

/* Event trigger when an agent of the level joins a shared context, sent to agent of the same level*/
on ContextJoined {
info("ContextJoined, contextID:" + occurrence.holonContextID + " SpaceID: " + occurrence.defaultSpaceID)
}

/* Event trigger when an agent of the level leaves a shared context, sent to agent of the same level */
on ContextLeft {
info("ContextLeft, contextID:" + occurrence.holonContextID)
}

/* Event trigger when an agent joins one of our shared super-holons, sent to all members of the considered super-holon */
on MemberJoined {
info("MemberJoined, agentID:" + occurrence.agentID)
}

/* Event trigger when an agent leaves one of our shared super-holons, sent to all members of the considered super-holon */
on MemberLeft {
info("MemberLeft, agentID:" + occurrence.agentID)
}

/* A new agent has been spawn in our context */
on AgentSpawned {
info("AgentSpawned, agentID:" + occurrence.agentID + " AgentType: " + occurrence.agentType)
}

/* An agent has been killed in our context */
on AgentKilled {
info("AgentKilled, agentID:" + occurrence.agentID)
}
}

1 change: 1 addition & 0 deletions vendor/grammars/sarl-textmate
Submodule sarl-textmate added at b086c0