Skip to content

Commit

Permalink
[core] Change AgentTask guard type for fitting the Xtext generated cl…
Browse files Browse the repository at this point in the history
…osures.

see #246

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 21, 2016
1 parent 1e6cf1c commit 447b685
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Expand Up @@ -40,7 +40,7 @@ public class AgentTask {

private String name;

private Function1<Agent, Boolean> guard;
private Function1<? super Agent, ? extends Boolean> guard;

private Procedures.Procedure1<? super Agent> procedure;

Expand Down Expand Up @@ -70,7 +70,7 @@ public void setProcedure(Procedures.Procedure1<? super Agent> procedure) {
*
* @return the guard.
*/
public Function1<Agent, Boolean> getGuard() {
public Function1<? super Agent, ? extends Boolean> getGuard() {
return this.guard;
}

Expand All @@ -80,7 +80,7 @@ public Function1<Agent, Boolean> getGuard() {
* @see #unless
* @see #ifTrue
*/
public void setGuard(Function1<Agent, Boolean> guard) {
public void setGuard(Function1<? super Agent, ? extends Boolean> guard) {
this.guard = guard;
}

Expand All @@ -106,7 +106,7 @@ public void setName(String name) {
* @return <code>this</code>.
* @see #setGuard
*/
public AgentTask unless(final Function1<Agent, Boolean> predicate) {
public AgentTask unless(final Function1<? super Agent, ? extends Boolean> predicate) {
if (predicate == null) {
this.guard = FALSE_GUARD;
} else {
Expand All @@ -121,7 +121,7 @@ public AgentTask unless(final Function1<Agent, Boolean> predicate) {
* @return <code>this</code>.
* @see #setGuard
*/
public AgentTask ifTrue(Function1<Agent, Boolean> predicate) {
public AgentTask ifTrue(Function1<? super Agent, ? extends Boolean> predicate) {
if (predicate == null) {
this.guard = null;
} else {
Expand Down
Expand Up @@ -266,24 +266,26 @@ public void run() {
if (task == null) {
throw new RuntimeException(Messages.SchedulesSkill_2);
}
boolean hasError = false;
boolean mustBeCanceled = false;
try {
final Agent owner = getOwner();
final Function1<Agent, Boolean> guard = task.getGuard();
final Function1<? super Agent, ? extends Boolean> guard = task.getGuard();
if (guard == null || guard.apply(owner).booleanValue()) {
final Procedure1<? super Agent> procedure = task.getProcedure();
if (procedure != null) {
procedure.apply(owner);
}
} else {
mustBeCanceled = true;
}
} catch (Throwable ex) {
final LogRecord record = new LogRecord(Level.SEVERE,
MessageFormat.format(Messages.SchedulesSkill_3, toString(), ex.getLocalizedMessage()));
record.setThrown(ex);
SchedulesSkill.this.logger.log(record);
hasError = true;
mustBeCanceled = true;
} finally {
if (hasError || !this.isPeriodic) {
if (mustBeCanceled || !this.isPeriodic) {
finishTask(task.getName());
}
}
Expand Down Expand Up @@ -322,7 +324,7 @@ private boolean canRun() {
return false;
}

private Function1<Agent, Boolean> getGuard() {
private Function1<? super Agent, ? extends Boolean> getGuard() {
final AgentTask task = this.agentTaskRef.get();
if (task != null) {
return task.getGuard();
Expand All @@ -343,12 +345,15 @@ public void run() {
try {
final Agent owner = getOwner();
while (canRun()) {
final Function1<Agent, Boolean> guard = getGuard();
final Function1<? super Agent, ? extends Boolean> guard = getGuard();
if (guard == null || guard.apply(owner).booleanValue()) {
final Procedure1<? super Agent> procedure = getProcedure();
if (procedure != null) {
procedure.apply(owner);
}
} else {
// Break the loop without introducing a local boolean variable
break;
}
Thread.yield();
}
Expand Down
Expand Up @@ -111,34 +111,34 @@ public void setName() {

@Test
public void unless_positive() {
Function1<Agent,Boolean> guard = (agent) -> Boolean.TRUE;
Function1<? super Agent, ? extends Boolean> guard = (agent) -> Boolean.TRUE;

assertSame(this.task, this.task.unless(guard));

Function1<Agent,Boolean> negativeGuard = this.task.getGuard();
Function1<? super Agent, ? extends Boolean> negativeGuard = this.task.getGuard();
assertNotSame(guard, negativeGuard);
assertFalse(negativeGuard.apply(null));
}

@Test
public void unless_negative() {
Function1<Agent,Boolean> guard = (agent) -> Boolean.FALSE;
Function1<? super Agent, ? extends Boolean> guard = (agent) -> Boolean.FALSE;

assertSame(this.task, this.task.unless(guard));

Function1<Agent,Boolean> negativeGuard = this.task.getGuard();
Function1<? super Agent, ? extends Boolean> negativeGuard = this.task.getGuard();
assertNotSame(guard, negativeGuard);
assertTrue(negativeGuard.apply(null));
}

@Test
public void ifTrue() {
Function1<Agent,Boolean> guard = mock(Function1.class);
Function1<? super Agent, ? extends Boolean> guard = mock(Function1.class);
doReturn(Boolean.TRUE).when(guard).apply(ArgumentMatchers.any(Agent.class));

assertSame(this.task, this.task.ifTrue(guard));

Function1<Agent,Boolean> newGuard = this.task.getGuard();
Function1<? super Agent, ? extends Boolean> newGuard = this.task.getGuard();
assertSame(guard, newGuard);
}

Expand Down

0 comments on commit 447b685

Please sign in to comment.