Class: IfTask

system.logics. IfTask


new IfTask(rule, thenTask, elseTask, elseTask)

Perform some tasks based on whether a given condition holds true or not.

Parameters:
Name Type Description
rule system.rules.Rule

The initial condition.

thenTask system.process.Action

The action to execute if the main condition if true.

elseTask system.process.Action

The action invoked if all the conditions failed.

elseTask array

The optional collection of system.logics.ElseIf tasks.

Implements:
Examples

Usage :

var task = new IfTask( rule:Rule    , thenTask:Action , elseTask:Action , ...elseIfTasks )
var task = new IfTask( rule:Boolean , thenTask:Action , elseTask:Action , ...elseIfTasks )

Basic example :

// -------- Imports

var IfTask      = system.logics.IfTask ;
var Do          = system.process.Do ;
var ElseIf      = system.logics.ElseIf ;
var EmptyString = system.rules.EmptyString ;
var Equals      = system.rules.Equals ;

// -------- init

var task ;

var do1 = new Do() ;
var do2 = new Do() ;
var do3 = new Do() ;
var do4 = new Do() ;

do1.something = function() { trace("do1 ###") } ;
do2.something = function() { trace("do2 ###") } ;
do3.something = function() { trace("do3 ###") } ;
do4.something = function() { trace("do4 ###") } ;

// -------- behaviors

var error = function( message , action  )
{
    trace( "error:" + action + " message:" + message ) ;
};

var finish = function( action )
{
    trace( "finish: " + action ) ;
};

var start = function( action )
{
    trace( "start: " + action ) ;
};

trace(' -------- test 1');

task = new IfTask( new EmptyString('') , do1 , do2 ) ;

task.finishIt.connect(finish) ;
task.errorIt.connect(error) ;
task.startIt.connect(start) ;

task.run() ;

trace(' -------- test 2');

task.clear() ;

task.rule = new Equals(1,2) ;

task.addThen( do1 )
    .addElse( do2 )
    .run() ;

trace(' -------- test 3 : <elseIf>');

task.clear() ;

task.addRule( new Equals(1,2) )
    .addThen( do1 )
    .addElseIf
    (
        new ElseIf( new Equals(2,1) , do3 ) ,
        new ElseIf( new Equals(2,2) , do4 )
    )
    .addElse( do2 )
    .run() ;

trace(' -------- test 4 : <then> is already register');

task.clear() ;
task.throwError = true ;

try
{
    task.addThen( do1 )
        .addElse( do2 )
        .addThen( do3 )
}
catch (e)
{
    trace( e ) ;
}

trace(' -------- test 5 : <rule> is not defined');

try
{
    task.run() ;
}
catch (e)
{
    trace( e ) ;
}

trace(' -------- test 6 : <rule> is not defined and throwError = false');

task.throwError = false ;

task.run() ;

Extends

Members


<readonly> elseIfTask :array

The collection of condition/action invokable if the main rule is not true.

Type:
  • array

<readonly> elseTask :system.process.Action

The action invoked if all the conditions failed.

Type:

<readonly> errorIt :system.signals.Signal

This signal emit when the action failed.

Type:

<constant> finishIt :system.signals.Signal

This signal emit when the action is finished.

Type:
Inherited From:
Implements:

<readonly> phase :string

Indicates the current phase.

Type:
  • string
Inherited From:
Implements:
See:

rule :system.rules.Rule

The rule reference of this task.

Type:

<readonly> running :boolean

Indicates action is running.

Type:
  • boolean
Inherited From:
Implements:

<constant> startIt :system.signals.Signal

This signal emit when the action is started.

Type:
Inherited From:
Implements:

thenTask :system.process.Action

The action to execute if the main condition if true.

Type:

throwError :boolean

Indicates if the class throws errors or notify a finished event when the task failed.

Type:
  • boolean
Default Value:
  • false

Methods


addElse(action)

Defines the action when the condition block use the else condition.

Parameters:
Name Type Description
action system.process.Action

The action to defines with the else condition in the {system.logics.IfTask} reference.

Throws:

Error if an 'else' action is already register.

Returns:

The current IfTask reference.


addElseIf(condition)

Defines an action when the condition block use the elseif condition.

Parameters:
Name Type Description
condition system.logics.ElseIf | system.rules.Rule | system.process.Action

A {system.logics.ElseIf} instance or a serie of {system.rules.Rule}/{system.process.Action}.

Returns:

The current IfTask reference.

Example
var IfTask      = system.logics.IfTask ;
var Do          = system.process.Do ;
var ElseIf      = system.logics.ElseIf ;
var EmptyString = system.rules.EmptyString ;
var Equals      = system.rules.Equals ;

var do1 = new Do() ;
var do2 = new Do() ;
var do3 = new Do() ;
var do4 = new Do() ;

do1.something = function() { trace("do1 ###") } ;
do2.something = function() { trace("do2 ###") } ;
do3.something = function() { trace("do3 ###") } ;
do4.something = function() { trace("do4 ###") } ;

var task = new IfTask() ;

task.addRule( new Equals(1,2) )
    .addThen( do1 )
    .addElseIf
    (
        new ElseIf( new Equals(2,1) , do3 ) ,
        new Equals(2,2) , do4
    )
    .addElse( do2 )
    .run() ; // do4 ###

addRule(rule)

Defines the main conditional rule of the task.

Parameters:
Name Type Description
rule system.rules.Rule

The main Rule of the task.

Throws:

Error if a 'condition' is already register.

Returns:

The current IfTask reference.


addThen(action)

Defines the action when the condition block success and must run the 'then' action.

Parameters:
Name Type Description
action system.process.Action

Defines the 'then' action in the IfTask reference.

Throws:

Error if the 'then' action is already register.

Returns:

The current IfTask reference.


clear()

Clear all elements conditions and conditional tasks in the process.

Returns:

The current IfTask reference.


clone()

Creates a copy of the object.

Inherited From:
Implements:
Returns:

a shallow copy of this object.


deleteElse()

Removes the 'else' action.

Returns:

The current IfTask reference.


deleteElseIf()

Removes the 'elseIf' action.

Returns:

The current IfTask reference.


deleteRule()

Removes the 'rule' of the task.

Returns:

The current IfTask reference.


deleteThen()

Removes the 'then' action.

Returns:

The current IfTask reference.


isLocked()

Returns true if the object is locked.

Inherited From:
Implements:
Returns:

true if the object is locked.


lock()

Locks the object.

Inherited From:
Implements:

notifyError()

Notify when the process is started.


notifyFinished()

Notify when the process is finished.

Inherited From:
Implements:

notifyStarted()

Notify when the process is started.

Inherited From:
Implements:

run()

Run the process.

Overrides:
Implements:

unlock()

Unlocks the object.

Inherited From:
Implements: