insertCollection
Short summary
This method inserts all elements of a collection into the tree.
If any value of the collection could not be inserted because of duplication, operation isn't aborted, insertion is tried for every element of the collection.
Return: SUCCESS: if insert could be made
ABORTED: execute has a falling edge during insert,
BUSY: Insert is in Progress
ERROR: the passed collection where not valid / contained invalid Elements
IDLE: method was never started
- Return type: CNM_ReturnTypes.SingleExecutionState
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| collection | CNM_CollectionInterfaces.ICollection | The collection that should be inserted | input |
| execute | BOOL | If insert should be processed | input |
Code
Declaration
METHOD INTERNAL insertCollection :CNM_ReturnTypes.SingleExecutionState
VAR_INPUT
(*The collection that should be inserted*)
collection :CNM_CollectionInterfaces.ICollection;
(*If insert should be processed*)
execute :BOOL;
END_VAR
VAR_INST
cycleManager :CNM_CycleManager.SimpleCycleManager;
index :LINT := 0;
insertFailed :BOOL;
END_VAR
VAR
iterateObject :CNM_AbstractObject.IObject;
insertOperationsThisCycle :__XWORD := 0;
END_VAR
VAR CONSTANT
INIT :DINT := CNM_ReturnTypes.DefaultSteps.STEP.INIT;
PROCESSING :DINT := INIT + 1;
ROLL_BACK :DINT := PROCESSING + 1;
CLEAN_UP :DINT := ROLL_BACK + 1;
SUCCESS :DINT := CNM_ReturnTypes.DefaultSteps.STEP.SUCCESS;
ERROR :DINT := CNM_ReturnTypes.DefaultSteps.STEP.ERROR;
END_VAR
Implementation
REPEAT
cycleManager(execute := execute);
CASE cycleManager.step.current OF
INIT:
insertFailed := FALSE;
cycleManager.configuration.sequence.requireSuccessStep := FALSE;
IF THIS^.isObjectNull(collection) OR_ELSE THIS^.isEqual(collection) THEN
cycleManager.proceedWith(ERROR);
CONTINUE;
END_IF
IF collection.isEmpty THEN
cycleManager.proceedWith(SUCCESS);
CONTINUE;
END_IF
IF THIS^.insertBuffer <> 0 THEN
__DELETE(THIS^.insertBuffer);
END_IF
THIS^.insertBuffer := __NEW(__XWORD, collection.size);
IF THIS^.isObjectValid(THIS^.insertIterator) THEN
THIS^.insertIterator.destruct();
END_IF
IF (collection.createNewIterator(iterator => THIS^.insertIterator) <> CNM_ReturnTypes.SingleExecutionResult.SUCCESS)
THEN
cycleManager.proceedWith(ERROR);
CONTINUE;
END_IF
THIS^.insertIterator.iterate(execute := FALSE);
index := 0;
cycleManager.proceed();
PROCESSING:
WHILE (insertOperationsThisCycle < THIS^.maxCycleCompares) DO
CASE THIS^.insertIterator.iterate(execute := TRUE, object => iterateObject) OF
CNM_ReturnTypes.SingleExecutionState.BUSY:
CASE THIS^.insert(object := iterateObject) OF
CNM_ReturnTypes.SingleExecutionResult.ERROR:
cycleManager.proceedWith(ROLL_BACK);
EXIT;
CNM_ReturnTypes.SingleExecutionResult.SUCCESS:
THIS^.insertBuffer[index] := iterateObject;
index := index + 1;
END_CASE
insertOperationsThisCycle := (insertOperationsThisCycle + THIS^.treeHeight);
CNM_ReturnTypes.SingleExecutionState.SUCCESS:
cycleManager.proceedWith(CLEAN_UP);
EXIT;
ELSE
cycleManager.proceedWith(ROLL_BACK);
EXIT;
END_CASE
END_WHILE
ROLL_BACK:
insertFailed := TRUE;
REPEAT
THIS^.remove(THIS^.insertBuffer[index]);
index := index - 1;
UNTIL
(index < 0)
END_REPEAT
cycleManager.proceed();
CLEAN_UP:
__DELETE(THIS^.insertBuffer);
IF THIS^.isObjectValid(THIS^.insertIterator) THEN
THIS^.insertIterator.destruct();
THIS^.insertIterator := 0;
END_IF
cycleManager.proceedWith(
step := SEL(insertFailed, SUCCESS, ERROR)
);
END_CASE
UNTIL
(
(cycleManager.state <> CNM_ReturnTypes.SingleExecutionState.BUSY)
OR_ELSE
(insertOperationsThisCycle >= THIS^.maxCycleCompares)
)
END_REPEAT
insertCollection := cycleManager.state;