diff --git a/eclipse-sarl/plugins/io.sarl.core/src/io/sarl/core/bic.sarl b/eclipse-sarl/plugins/io.sarl.core/src/io/sarl/core/bic.sarl index fbabfe1ad9..07d254dd51 100644 --- a/eclipse-sarl/plugins/io.sarl.core/src/io/sarl/core/bic.sarl +++ b/eclipse-sarl/plugins/io.sarl.core/src/io/sarl/core/bic.sarl @@ -309,28 +309,17 @@ capacity Lifecycle { */ capacity Schedules { - /** - * Creates an anonymous task to execute the procedure that will be triggered after the specified delay. - * - *

The given procedure takes one parameter: the agent associated to the task. It is name it by default. - * - * @param delay time in milliseconds to delay the procedure execution. - * @param procedure the closure to execute. - * @return the generated task. - */ - def in(delay : long, procedure : (Agent) => void ) : AgentTask - /** * Schedule a given task to be executed after the specified delay. * *

The given procedure takes one parameter: the agent associated to the task. It is name it by default. * - * @param task the task that will run the given closure. + * @param task the task that will run the given closure. If null, a new task is created. * @param delay time in milliseconds to delay the procedure execution. * @param procedure the closure to execute. * @return the generated task. */ - def in(task : AgentTask, delay : long, procedure : (Agent) => void) : AgentTask + def in(task : AgentTask = null, delay : long, procedure : (Agent) => void) : AgentTask /** * Create a named task that can be retrieved and schedule later. @@ -341,28 +330,6 @@ capacity Schedules { */ def task(name : String) : AgentTask - /** - * Attempts to cancel execution of this task. This attempt will - * fail if the task has already completed, has already been cancelled, - * or could not be cancelled for some other reason. If successful, - * and this task has not started when cancel is called, - * this task should never run. If the task has already started, - * then the mayInterruptIfRunning parameter determines - * whether the thread executing this task should be interrupted in - * an attempt to stop the task. - * - *

This function interrupts ongoing tasks. So, it is - * equivalent to passing true as the - * value for the parameter mayInterruptIfRunning - * to the function {@link #cancel(AgentTask, boolean)}. - * - * @param task the task to cancel. - * @return false if the task could not be cancelled, - * typically because it has already completed normally; - * true otherwise - */ - def cancel(task : AgentTask) : boolean - /** * Attempts to cancel execution of this task. This attempt will * fail if the task has already completed, has already been cancelled, @@ -381,34 +348,7 @@ capacity Schedules { * typically because it has already completed normally; * true otherwise */ - def cancel(task : AgentTask, mayInterruptIfRunning : boolean) : boolean - - /** - * Create an anonymous task and schedules a periodic execution of its behavior. - *

- * If the duration of the task is greater to the given period length, then - * multiple task's instances will be run in parallel. - * For example, consider the following code: - *


-	 * every(500) [ sleep(2000) ]
-	 * 
- * At a given time, 4 instances (A, B, C, D) of the task may be run in parallel: - *

-	 * t=0   0500   1000   1500   2000   2500   3000   3500   4000   4500
-	 *   |    |      |      |      |      |      |      |      |      |
-	 *   [-A-----------------------]
-	 *        [-B-------------------------]
-	 *               [-C-------------------------]
-	 *                      [-D-------------------------]
-	 *                             [-E-------------------------]
-	 *                                    [-F-------------------------]
-	 * 
- * - * @param period the number of milliseconds between two launches of the given procedure. - * @param procedure the procedure to launch. The parameter of the procedure is the agent. - * @return the created task. - */ - def every(period : long, procedure : (Agent) => void) : AgentTask + def cancel(task : AgentTask, mayInterruptIfRunning : boolean = true) : boolean /** * Schedule a periodic execution of the given task. @@ -433,12 +373,12 @@ capacity Schedules { * *

The given procedure takes one parameter: the agent associated to the task. It is name it by default. * - * @param task the task to associate to the procedure. + * @param task the task to associate to the procedure. If null a new task is created. * @param period the number of milliseconds between two launches of the given procedure. * @param procedure the procedure to launch. The parameter of the procedure is the agent. * @return the given task. */ - def every(task : AgentTask, period : long, procedure : (Agent) => void ) : AgentTask + def every(task : AgentTask = null, period : long, procedure : (Agent) => void ) : AgentTask } diff --git a/sre/io.janusproject/io.janusproject.plugin/src/io/janusproject/kernel/bic/SchedulesSkill.java b/sre/io.janusproject/io.janusproject.plugin/src/io/janusproject/kernel/bic/SchedulesSkill.java index d5f99f2fbc..c6924d67c7 100644 --- a/sre/io.janusproject/io.janusproject.plugin/src/io/janusproject/kernel/bic/SchedulesSkill.java +++ b/sre/io.janusproject/io.janusproject.plugin/src/io/janusproject/kernel/bic/SchedulesSkill.java @@ -135,15 +135,16 @@ protected synchronized void uninstall() { @Override public AgentTask in(long delay, Procedure1 procedure) { - return in(task("task-" + UUID.randomUUID()), delay, procedure); //$NON-NLS-1$ + return in(Schedules.$DEFAULT_VALUE$IN_0, delay, procedure); } @Override public synchronized AgentTask in(AgentTask task, long delay, Procedure1 procedure) { - task.setProcedure(procedure); - final ScheduledFuture sf = this.executorService.schedule(new AgentRunnableTask(task, false), delay, TimeUnit.MILLISECONDS); - this.futures.put(task.getName(), sf); - return task; + final AgentTask rtask = task == null ? task("task-" + UUID.randomUUID()) : task; //$NON-NLS-1$ + rtask.setProcedure(procedure); + final ScheduledFuture sf = this.executorService.schedule(new AgentRunnableTask(rtask, false), delay, TimeUnit.MILLISECONDS); + this.futures.put(rtask.getName(), sf); + return rtask; } @Override @@ -166,7 +167,7 @@ public Boolean apply(Agent arg0) { @Override public final boolean cancel(AgentTask task) { - return cancel(task, true); + return cancel(task, Schedules.$DEFAULT_VALUE$CANCEL_0); } @Override @@ -183,16 +184,17 @@ public synchronized boolean cancel(AgentTask task, boolean mayInterruptIfRunning @Override public AgentTask every(long period, Procedure1 procedure) { - return every(task("task-" + UUID.randomUUID()), period, procedure); //$NON-NLS-1$ + return every(Schedules.$DEFAULT_VALUE$EVERY_0, period, procedure); } @Override public synchronized AgentTask every(AgentTask task, long period, Procedure1 procedure) { - task.setProcedure(procedure); - final ScheduledFuture sf = this.executorService.scheduleAtFixedRate(new AgentRunnableTask(task, true), 0, period, + final AgentTask rtask = task == null ? task("task-" + UUID.randomUUID()) : task; //$NON-NLS-1$ + rtask.setProcedure(procedure); + final ScheduledFuture sf = this.executorService.scheduleAtFixedRate(new AgentRunnableTask(rtask, true), 0, period, TimeUnit.MILLISECONDS); - this.futures.put(task.getName(), sf); - return task; + this.futures.put(rtask.getName(), sf); + return rtask; } /**