Skip to content

Commit

Permalink
async array search
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuergen committed Oct 25, 2023
1 parent 9a37d3e commit cb5815c
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 31 deletions.
8 changes: 4 additions & 4 deletions apax.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# General information
name: '@simatic-ax/json'
version: 0.0.0-placeholder
version: 1.0.0
type: lib
# Description will be displayed in the apax extension
description: This is supporting the serializing of a JSON object model and
Expand All @@ -17,11 +17,11 @@ targets:
- 'axunit-llvm'
# Dependencies
devDependencies:
"@ax/sdk": 4.0.2
"@simatic-ax/snippetscollection": 0.1.3
dependencies:
"@ax/system-strings": 5.0.12
"@simatic-ax/conversion": 4.0.1
"@ax/system-strings": 5.0.60
"@simatic-ax/conversion": 4.1.1
"@ax/sdk": 4.0.3
registries:
'@simatic-ax': 'https://npm.pkg.github.com/'
# Files which 'apax pack' will include
Expand Down
47 changes: 21 additions & 26 deletions src/Document/Deserializer.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ NAMESPACE Simatic.Ax.Json
VAR PUBLIC
buffer : REF_TO ARRAY[*] OF CHAR;
END_VAR
VAR PRIVATE
_iBufferIndices : IGetBufferIndizes;
_BufferIndices : GetBufferIndizesSyncron;
END_VAR
METHOD PRIVATE KeyIsInIndexSpan : BOOL
VAR_INPUT
key : STRING;
Expand Down Expand Up @@ -51,31 +55,7 @@ NAMESPACE Simatic.Ax.Json
END_FOR;
GetValueFromIndex := Conversion.Arrays.ToString(buffer^, realStartIndex, realEndIndex);
END_METHOD
METHOD PRIVATE GetBufferIndizes
VAR_OUTPUT
bufferStart : INT;
bufferEnd : INT;
END_VAR
VAR_TEMP
index : INT;
incrementBackwards : INT := -1;
_lb, _ub : INT;
END_VAR
_lb := TO_INT(LOWER_BOUND(buffer^, 1));
_ub := TO_INT(UPPER_BOUND(buffer^, 1));
FOR index := _lb TO _ub DO //TODO: When possible change to Array* upper und lowerbound
IF (buffer^[index] = '{') THEN
bufferStart := index;
EXIT;
END_IF;
END_FOR;
FOR index := _ub TO _lb BY incrementBackwards DO
IF (buffer^[index] = '}') THEN
bufferEnd := index;
EXIT;
END_IF;
END_FOR;
END_METHOD

METHOD PRIVATE GetValue_WithKeyArray : BOOL
VAR_INPUT
keyArray : ARRAY[*] OF STRING;
Expand All @@ -97,12 +77,27 @@ NAMESPACE Simatic.Ax.Json
documentEndIndex : INT;
keyArrayLower : DINT;
keyArrayUpper : DINT;
cmd : IPlcOpen;
END_VAR
VAR_OUTPUT
areaStartIndex : INT;
areaEndIndex : INT;
END_VAR
THIS.GetBufferIndizes(documentStartIndex, documentEndIndex);
_iBufferIndices := _BufferIndices;
cmd := _iBufferIndices.Find(buffer);
IF NOT cmd.Busy() THEN
IF cmd.Done() THEN // found '{' and '}'
documentStartIndex := _iBufferIndices.StartIndex();
documentEndIndex := _iBufferIndices.EndIndex();
ELSIF cmd.Error() THEN // havn't found '{' and '}'
GetValue_WithKeyArray := FALSE;
areaStartIndex := 0;
areaEndIndex := 0;
RETURN;
END_IF;
ELSE
; // wait until it is not busy any more
END_IF;
keyArrayLower := LOWER_BOUND(keyArray, 1);
keyArrayUpper := UPPER_BOUND(keyArray, 1);
//iterate through keys
Expand Down
82 changes: 82 additions & 0 deletions src/Document/internal/Command.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
NAMESPACE Simatic.Ax.Json

CLASS INTERNAL ABSTRACT Command
IMPLEMENTS IPlcOpen
VAR PRIVATE
_Done : BOOL;
_Aborted : BOOL;
_Error : BOOL;
_ErrorID : WORD;
END_VAR

METHOD PUBLIC Busy : BOOL
THIS.ExecuteIfBusy(); // execute as long it is busy
Busy := THIS.IsBusy(); // check, if it is still busy
END_METHOD

METHOD PUBLIC Done : BOOL
THIS.ExecuteIfBusy();
Done := _Done;
END_METHOD

METHOD PUBLIC Aborted : BOOL
THIS.ExecuteIfBusy();
Aborted := _Aborted;
END_METHOD

METHOD PUBLIC ErrorID : WORD
THIS.ExecuteIfBusy();
ErrorID := _ErrorID;
END_METHOD

METHOD PUBLIC Error : BOOL
THIS.ExecuteIfBusy();
Error := _Error;
END_METHOD

METHOD PROTECTED ABSTRACT Execute
END_METHOD

METHOD PROTECTED InitState
_Done := FALSE;
_Aborted := FALSE;
_Error := FALSE;
_ErrorID := word#0;
END_METHOD

METHOD PROTECTED ExecuteIfBusy
IF THIS.IsBusy() THEN
THIS.Execute();
END_IF;
END_METHOD

METHOD PROTECTED IsBusy : BOOL
IsBusy := NOT (_Done OR _Error OR _Aborted);
END_METHOD

METHOD PROTECTED SetDone
_Done := TRUE;
_Error := FALSE;
_ErrorID := WORD#0000;
_Aborted := FALSE;
END_METHOD

METHOD PROTECTED SetError
VAR_INPUT
ErrorID : WORD := WORD#16#0000;
END_VAR
_Error := TRUE;
_ErrorID := ErrorID;
_Done := FALSE;
_Aborted := FALSE;
END_METHOD

METHOD PROTECTED SetAborted
_Aborted := TRUE;
_Error := FALSE;
_Done := FALSE;
_ErrorID := WORD#0000;
END_METHOD
END_CLASS

END_NAMESPACE
73 changes: 73 additions & 0 deletions src/Document/internal/FindCharacterInArray.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
NAMESPACE Simatic.Ax.Json

CLASS INTERNAL FindCharacterInArray
EXTENDS Command
VAR PUBLIC
END_VAR
VAR PROTECTED
_buffer : REF_TO ARRAY[*] OF CHAR;
_charToFind : CHAR;
_mode : FindeMode;
_index : INT;
_found : BOOL;
END_VAR

METHOD INTERNAL Find : IPlcOpen
VAR_INPUT
Buffer : REF_TO ARRAY[*] OF CHAR;
c : CHAR;
Mode : FindeMode;
END_VAR
VAR_OUTPUT
Index : DINT;
END_VAR
_buffer := Buffer;
_mode := Mode;
_charToFind := c;
THIS.InitState();
THIS.Execute();
Find := THIS;

END_METHOD

METHOD PROTECTED OVERRIDE Execute
VAR_TEMP
index : INT;
_lb, _ub : INT;
END_VAR
_lb := TO_INT(LOWER_BOUND(_buffer^, 1));
_ub := TO_INT(UPPER_BOUND(_buffer^, 1));
CASE _mode OF
FindeMode#MODE_FORWARD:
FOR index := _lb TO _ub DO
IF (_buffer^[index] = _charToFind) THEN
_index := index;
THIS.SetDone();
EXIT;
END_IF;
THIS.SetError();
END_FOR;

FindeMode#MODE_REVERSE:
FOR index := _ub TO _lb BY -1 DO
IF (_buffer^[index] = _charToFind) THEN
_index := index;
THIS.SetDone();
EXIT;
END_IF;
THIS.SetError();
END_FOR;

END_CASE;
END_METHOD

METHOD PUBLIC GetIndex : INT
GetIndex := _index;
END_METHOD
END_CLASS

TYPE
FindeMode : (MODE_FORWARD, MODE_REVERSE);
END_TYPE

END_NAMESPACE
99 changes: 99 additions & 0 deletions src/Document/internal/GetBufferIndizesSyncron.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
NAMESPACE Simatic.Ax.Json

CLASS INTERNAL GetBufferIndizesSyncron EXTENDS Command
IMPLEMENTS IGetBufferIndizes
VAR
_startFound : BOOL;
_endFound : BOOL;
_state : INT;
_finder : FindCharacterInArray;
_buffer : REF_TO ARRAY[*] OF CHAR;
_startIndex : INT;
_endIndex : INT;
END_VAR
VAR CONSTANT
IDLE : INT := 0;
FIND_START : INT := 1;
FIND_END : INT := 2;
EVALUATE : INT := 3;
END_VAR

METHOD PUBLIC Find : IPlcOpen
VAR_INPUT
Buffer : REF_TO ARRAY[*] OF CHAR;
END_VAR
_buffer := Buffer;
THIS.InitState();
THIS.Execute();
Find := THIS;

END_METHOD

METHOD PUBLIC StartIndex : INT
StartIndex := _startIndex;
END_METHOD
METHOD PUBLIC EndIndex : INT
EndIndex := _endIndex;
END_METHOD
METHOD PROTECTED OVERRIDE Execute
VAR_TEMP
index : INT;
incrementBackwards : INT := -1;
_lb, _ub : INT;
_cmd : IPlcOpen;
END_VAR
// Idle initialize all variables
IF _state = IDLE THEN
_lb := TO_INT(LOWER_BOUND(_buffer^, 1));
_ub := TO_INT(UPPER_BOUND(_buffer^, 1));
_state := FIND_START;
_startFound := FALSE;
_endFound := FALSE;
END_IF;
// Find start '{'
IF _state = FIND_START THEN
_cmd := _finder.Find(buffer := _buffer, c := '{', mode := FindeMode#MODE_FORWARD);
IF (NOT _cmd.Busy()) THEN
IF (_cmd.Done()) THEN
_state := FIND_END;
_startIndex := _finder.GetIndex();
END_IF;
IF (_cmd.Error()) THEN
_startIndex := 0;
THIS.SetError();
_state := IDLE;
END_IF;
END_IF;
END_IF;
IF _state = FIND_END THEN
// Find end '}'
_cmd := _finder.Find(buffer := _buffer, c := '}', mode := FindeMode#MODE_REVERSE);
IF (NOT _cmd.Busy()) THEN
IF (_cmd.Done()) THEN
_state := EVALUATE;
_endIndex := _finder.GetIndex();
END_IF;
IF (_cmd.Error()) THEN
_endIndex := 0;
THIS.SetError();
_state := IDLE;
END_IF;
END_IF;
END_IF;
IF _state = EVALUATE THEN
IF (_startIndex > _endIndex) THEN
_startIndex := 0;
_endIndex := 0;
_state := IDLE;
THIS.SetError();
RETURN;
ELSE // Done
_state := IDLE;
THIS.SetDone();
RETURN;
END_IF;
END_IF;
END_METHOD
END_CLASS

END_NAMESPACE
11 changes: 11 additions & 0 deletions src/Document/internal/IGetBufferIndizes.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NAMESPACE Simatic.Ax.Json
INTERFACE INTERNAL IGetBufferIndizes
METHOD Find : IPlcOpen
VAR_INPUT
buffer : REF_TO ARRAY[*] OF CHAR;
END_VAR
END_METHOD
METHOD StartIndex : INT END_METHOD
METHOD EndIndex : INT END_METHOD
END_INTERFACE
END_NAMESPACE
16 changes: 16 additions & 0 deletions src/Document/internal/IPlcOpen.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
NAMESPACE Simatic.Ax.Json

INTERFACE IPlcOpen
METHOD Busy : BOOL
END_METHOD
METHOD Done : BOOL
END_METHOD
METHOD Aborted : BOOL
END_METHOD
METHOD Error : BOOL
END_METHOD
METHOD ErrorID : WORD
END_METHOD
END_INTERFACE

END_NAMESPACE
Loading

0 comments on commit cb5815c

Please sign in to comment.