• SYEDHUSSIM.COM
  • Node.js
  • HANA DB
  • SAP Business One
  • Miscellaneous
  • Stock Transfer Part 2 07 August 2020 - 1669 views
    Batch managed items can be transferred between warehouses and bins by including the BatchNumbers property in the StockTransfers API post request. The BatchNumbers property is an array of batch numbers. Listing 1 below shows an example JSON object that contains an item with a single batch. Notice that the quantity specified for the batch number object is equal to the quantity at the item level.

    Listing 1

    {
        FromWarehouse: "From Warehouse code",
        StockTransferLines: [
            {
                WarehouseCode: "Target Warehouse Code",
                ItemCode: "v100001",
                Quantity: 5,
                BatchNumbers: [
                    {
                        BaseLineNumber: 0,
                        Quantity: 5,
                        BatchNumberProperty: "BN0001"
    
                    }
                ]
            }
        ]
    }
    

    The BaseLineNumber property of the batch object refers to the index position of the current item contained within the StockTransferLines array. In the example above, the StockTransferLines array contains a single item with the index position 0. This position is used as the BaseLineNumber value for batch objects.

    As mentioned, the BatchNumbers property allows you to specify an array of batch number objects. The quantities for each batch object must sum up to the total quantity at the item level as shown in listing 2 below.

    Listing 2

    {
        FromWarehouse: "From Warehouse code",
        StockTransferLines: [
            {
                WarehouseCode: "Target Warehouse Code",
                ItemCode: "v100001",
                Quantity: 10,
                BatchNumbers: [
                    {
                        BaseLineNumber: 0,
                        Quantity: 5,
                        BatchNumberProperty: "BN0001"
    
                    },
                    {
                        BaseLineNumber: 0,
                        Quantity: 5,
                        BatchNumberProperty: "BN0002"
    
                    }
                ]
            }
        ]
    }
    

    The final code sample below shows how to transfer multiple items with multiple batch numbers. Notice how the BaseLineNumber value for each item relates to the items index position.

    Listing 3

    {
        FromWarehouse: "From Warehouse code",
        StockTransferLines: [
            {
                WarehouseCode: "Target Warehouse Code",
                ItemCode: "v100001",
                Quantity: 10,
                BatchNumbers: [
                    {
                        BaseLineNumber: 0,
                        Quantity: 5,
                        BatchNumberProperty: "BN0001"
    
                    },
                    {
                        BaseLineNumber: 0,
                        Quantity: 5,
                        BatchNumberProperty: "BN0002"
    
                    }
                ]
            },
            {
                WarehouseCode: "Target Warehouse Code",
                ItemCode: "v100002",
                Quantity: 5,
                BatchNumbers: [
                    {
                        BaseLineNumber: 1,
                        Quantity: 5,
                        BatchNumberProperty: "BN0003"
    
                    }
                ]
            }
        ]
    }
    
  • Logo Programming Sample Code 29 July 2020 - 815 views
    This article details the usage of the CSharpLogo program, which is a basic port of the iconic Logo programming language. The program compiles logo source code into an HTML file and uses SVG's to display the output.

    Installation


    Download or clone the CSharpLogo repository from https://github.com/syedhussim/CSharpLogo.git.

    git clone https://github.com/syedhussim/CSharpLogo.git

    Usage


    The program accepts two command line arguments. The first argument is the full file path to the source code. The second argument is the path to the HTML output file.

    dotnet run /path/to/source/code /path/to/output.html

    Standard Commands


    CommandDescription
    homeReturn to the centre of the canvas.
    penupInk on the canvas is turned off.
    pendownInk on the canvas is turned on.
    setpencolorSets the pen colour.
    forwardMoves the cursor forward.
    rightTurns right.
    leftTurns left.
    repeatControl flow function. Repeats code withing code block.

    Extended Commands


    CommandDescription
    setcanvasSets the canvas width, height and background colour.
    randpencolorSets a random pen color.
    funcCreates a user defined function.

    Examples



    To draw a square use the FORWARD and LEFT commands as shown below.

    Listing 1

    FORWARD 100
    LEFT 90
    FORWARD 100
    LEFT 90
    FORWARD 100
    LEFT 90
    FORWARD 100
    

    The REPEAT command is used to repeat a block of code. The code sample below shows how to draw a square by repeating the FORWARD and LEFT commands.

    Listing 2

    REPEAT 4 [
        FORWARD 100
        LEFT 90
    ]
    
  • A JavaScript Implementation Of The Logo Programming Language - Part 2 10 June 2020 - 1482 views
    In this article we continue developing the Logo Programming language further by writing a simple TokenCollection class to help traverse the array of tokens returned from the lexer we created in part 1. This class will be used in Part 3, when we develop the parser.

    Part 1 - The Lexer
    Part 2 - TokenCollection Class

    In part 1, we developed a simply lexer function that returns an array of tokens given an input string. We now need to create a class that will allow us to traverse the tokens with the ability to move back and forth. To understand why we need this class, let's take a look at how we can use a loop to iterate through the tokens.

    Listing 1

    let tokens = tokenize("FORWARD 100");
    
    for(let i=0; i < tokens.length; i++){
        let token = token[i];
    }
    

    On first iteration the variable token is FORWARD and on the second iteration the token variable is 100. The token 100 is an argument of the FORWARD command and as a result, we would need to look ahead at the next token while currently viewing the FORWARD command. The loop also needs to account for white space tokens and in some cases we would need to look ahead on more than one token.

    A more efficient way to iterate over the tokens, is to encapsulate the tokens in a class and call methods such as getNextToken() which would return a token and advance an internal array pointer. Let's take a look at some code for a better understanding.

    Listing 2

    class TokenCollection{
    
        constructor(tokens){
            this._tokens = tokens;
            this._index = -1;
        }
    
        getNextToken(){
            this._index ++;
    
            if(this._index < this._tokens.length){
                return this._tokens[this._index];
            }
            return false;
        }
    }
    
    let str = "FORWARD 100";
    
    let tokens = tokenize(str);
    
    let tokenCollection = new TokenCollection(tokens);
    
    console.log(tokenCollection.getNextToken());
    console.log(tokenCollection.getNextToken());
    console.log(tokenCollection.getNextToken());
    console.log(tokenCollection.getNextToken());
    

    The TokenCollection class above is initialized with an array of tokens that is returned from the tokenize() function. Notice the _index variable is initialized with the value -1. This value is incremented each time the getNextToken() method is called. The final call to getNextToken() will return false as there are only three tokens.

    The second call to getNextToken() returns a white space token. White space tokens are problematic because it's not known in advance how many white space tokens exists between commands and command arguments. We can eliminate this problem by writing a function that consumes all white space tokens until a non white space token is encountered. This function needs to inspect the next token without advancing the internal index. If the next token is of type T_SPACE then we advance to the next token and repeat the process again.

    Let's first create a function that can inspect the next token without advancing the internal pointer.

    Listing 3

    class TokenCollection{
    ...
        inspectNextToken(){
            if((this._index + 1) < this._tokens.length){
                return this._tokens[this._index + 1];
            }
            return false;
        }
    ...
    }
    

    Using the function above we can now write a function that will consume all white space tokens as shown below.

    Listing 4

    ...
        consumeSpaces(){
            let next = this.inspectNextToken();
            if(next && next.type == 'T_SPACE'){
                this._index++;
                this.consumeSpaces();
            }
        }
    ...
    
    

    Notice that the function is recursive. We only advance to the next token if the current inspecting token is of type T_SPACE.

    We now have a class that we can use to traverse an array of tokens, getting and inspecting tokens. In part 3, we take a look at the Recursive Descent Parser, an algorithm used in parsing.
  • A JavaScript Implementation Of The Logo Programming Language - Part 1 06 June 2020 - 2488 views
    The Logo programming language was designed by Wally Feurzeig, Seymour Papert, and Cynthia Solomon in 1967 as an educational tool. The language was used to program a robotic turtle that would draw graphics given a series of instructions. In later years the physical robot was replaced by a virtual on-screen turtle. Over the years, various institutions and organizations have implemented the language with additional features. Notable major implementations are UCBLogo and MSWLogo.

    Part 1 - The Lexer
    Part 2 - TokenCollection Class

    In this four-part article, we will develop a basic implementation of the original Logo programming language in JavaScript by creating a Lexer and a Parser. To render the graphics, we will parse the Logo statements and compile an HTML output file that consists of an SVG element.

    Before proceeding, let's take a moment to look at some Logo programming code.

    Listing 1

    FORWARD 100
    RIGHT 90
    FORWARD 100
    RIGHT 90
    FORWARD 100
    RIGHT 90
    FORWARD 100
    

    The code in Listing 1 above shows a complete set of statements required to produce a square shape. The command FORWARD 100 and RIGHT 90 can be thought of as function names that accept a single argument. Logo's syntax allows for more complex statements such as loops. The code above can be rewritten to use the REPEAT statement as shown below.

    Listing 2

    REPEAT 4 [ 
        FORWARD 100 
        RIGHT 90
    ]
    

    Repeat statements can also be nested. The code below shows how to produce a square using nested loops.

    Listing 3

    REPEAT 2 [
    
        REPEAT 2 [ 
            FORWARD 100 
            RIGHT 90
        ]
    ]
    

    The use of loops allows a Logo developer to create complex shapes and patterns without having to write a lot of code. Take a look at the code sample below and the image it produces.

    Listing 4

    REPEAT 10 [
    
        RIGHT 45
    
        REPEAT 120 [
            FORWARD 2
            RIGHT 1
            PENCOLOR RED
        ]
    
        RIGHT 60
    
        REPEAT 120 [
            FORWARD 2
            RIGHT 1
            PENCOLOR GREEN
        ]
    
        RIGHT 60
    ]
    


    Lexer


    The first task in developing the Logo language is to read a given source code and break it down into small units. These units are typically called a lexeme or more commonly a token. In computer science, a lexer is used to perform lexical analysis on a sequence of characters and symbols and give meaning to each lexeme when a match occurs. Let's take a look at an example for a better understanding. Consider the following statement below.

    Listing 5

    print 5 + ( 2 * x )
    

    In order to process this statement, a lexer will need to break it down into tokens. The simplest way to tokenize this statement is to split it on white space characters which will produce the following array of tokens.

    Listing 6

    array[ 
        print, 
        5,
        +, 
        (, 
        2, 
        *, 
        x,
        ) 
    ]
    

    This approach will only work if words and symbols are separated by a space character in the source input. Consider the following two expressions. Both are valid but only the first expression can be tokenized using the space character.

    Listing 7

    //Characters separated by a space character
    5 + 5
    
    // No space character
    5+5
    

    We can solve this problem by splitting on more than one character using regular expressions. If you have used regular expressions before, you will know that the special pipe | character is used to denote an OR operation. Since we need to split on one or more characters, we can use the | character to construct a regular expression. Let's take a look at a JavaScript function that can take an input source and tokenize it using a regular expression pattern.

    Listing 8

    let str = "print 5 + ( 2 * x )";
    
    function tokenize(str){
        let regex = new RegExp(/( |\+|-|\*|\/|\(|\))/);
    
        return str.split(regex);
    }
    
    let tokens = tokenize(str);
    
    console.log(tokens);
    

    The tokenize() function splits an input string using multiple characters space, +, -, *, /, (, ). You may have noticed that the pattern also includes backslashes \. This is because some characters such as the + and * have a special meaning in regular expressions and as a result, we need to escape these characters using a backslash.

    We now have a collection of tokens that we can parse using a parser. However, a lexer not only tokenizes a string but also gives meaning to each token that describes the token type. For example we can say that the token 1 is of type INT and * is of type OPERATOR. Additionally most lexers will also include a line number, which is helpful when generating syntax error messages.

    While we can use regular expressions to capture named groups that we can use for the token type, I personally find this approach complicated and hard to maintain and so we will rewrite the tokenize() function and remove the regular expression. Instead, we will use a loop to read one character at a time. This process can be broken down into the following steps.

    Create a global string variable (token) to hold the current token.
    Create a global array variable (tokens) to hold a collection of tokens.
    Split the source input on line breaks.
    Iterate over each line.
    For each line read one character at a time.
    Append each character to the token variable.
    If the current character is either space, +, -, *, /, (, ), take the current token and determine its type then add it to an object with the line number.
    Add the object to the tokens array.
    Clear the token variable and continue.

    Let's take a look at the updated tokenize() function.

    Listing 9

    function tokenize(str){
    
        // Split the string on new lines.
        let lines = str.split("\n");
    
        let strToken = '';
        let tokens = [];
        let symbols = {
            ' ' : 'T_SPACE',
            '+' : 'T_PLUS', 
            '-' : 'T_MINUS',
            '*' : 'T_MULTIPLY',
            '/' : 'T_DIVIDE',
            '(' : 'T_LEFT_PAREN',
            ')' : 'T_RIGHT_PAREN',
            '[' : 'T_LEFT_BRACKET',
            ']' : 'T_RIGHT_BRACKET'
        };
    
        // Iterate over each line
        for(let i=0; i < lines.length; i++){
    
            let line = lines[i];
    
            // Iterate over each character in the current line
            for(let j=0; j < line.length; j++){
    
                let char = line[j];
    
                // Check if the current character exists in the symbols object
                if(symbols.hasOwnProperty(char)){
    
                    let token = {};
    
                    // Check that the current token is not empty
                    if(strToken.length > 0){
    
                        // Default type
                        let type = 'T_STRING';
    
                        // Is the token a float ?
                        let isNum = parseFloat(strToken);
    
                        if(!isNaN(isNum)){
                            type = 'T_NUMBER'
                        }
    
                        // Store the string token, type and line number into an object
                        token = {
                            value : strToken,
                            type : type,
                            line : i
                        };
    
                        // Add the object to the tokens array
    
                        tokens.push(token);
                    }
    
                    // Store the current charatcer as a seperate token
                    token = {
                        value : char,
                        type : symbols[char],
                        line : i
                    };
    
                    tokens.push(token);
    
                    // Clear the token and continue
                    strToken = '';
                    continue;
                }
                
                // Append the current character to the token
                strToken += char;
            }
    
            // After the loop above completes, the strToken varibale may contain a token.
            // Default type
            let type = 'T_STRING';
    
            // Is the token a float ?
            let isNum = parseFloat(strToken);
    
            if(!isNaN(isNum)){
                type = 'T_NUMBER'
            }
    
            token = {
                value : strToken,
                type : type,
                line : i
            };
    
            // Add the object to the tokens array
    
            tokens.push(token);
        }
    
        return tokens;
    }
    
    let str = `print 5 + 
    ( 2 * x )`;
    
    let tokens = tokenize(str);
    
    console.log(tokens);
    
    // Output
    
    [ { value: "print", type: "T_STRING", line: 0 },
      { value: " ", type: "T_SPACE", line: 0 },
      { value: "5", type: "T_NUMBER", line: 0 },
      { value: " ", type: "T_SPACE", line: 0 },
      { value: "+", type: "T_PLUS", line: 0 },
      { value: " ", type: "T_SPACE", line: 0 },
      { value: "(", type: "T_LEFT_PAREN", line: 1 },
      { value: " ", type: "T_SPACE", line: 1 },
      { value: "2", type: "T_NUMBER", line: 1 },
      { value: " ", type: "T_SPACE", line: 1 },
      { value: "*", type: "T_MULTIPLY", line: 1 },
      { value: " ", type: "T_SPACE", line: 1 },
      { value: "x", type: "T_STRING", line: 1 },
      { value: " ", type: "T_SPACE", line: 1 },
      { value: ")", type: "T_RIGHT_PAREN", line: 1 } ]
    

    The code has increased significantly but if you follow it through and read the comments, you will realize that it's not too difficult to understand. Each character that we need to split on has its own token type. Additionally, there are two other types (T_STRING and T_NUMBER). A comprehensive lexer will be able to detect other types such as keywords and variables.

    The lexer that we have created so far will work for simple expressions, but we want to develop a lexer for the Logo programming language. We need to add two more characters to the list of split characters in the symbols object. Update the symbols object to include [ and ] characters as shown below.

    Listing 10

    let symbols = {
        ' ' : 'T_SPACE',
        '+' : 'T_PLUS', 
        '-' : 'T_MINUS',
        '*' : 'T_MULTIPLY',
        '/' : 'T_DIVIDE',
        '(' : 'T_LEFT_PAREN',
        ')' : 'T_RIGHT_PAREN',
        '[' : 'T_LEFT_BRACKET',
        ']' : 'T_RIGHT_BRACKET'
    };
    

    We now have a lexer function that we can use to tokenize Logo statements. In part two, we will develop a TokenCollection class that will encapsulate the array of tokens returned from the tokenize() function. By creating this class, we will be able to traverse the array of tokens using helper methods such as getNextToken() and getPreviousToken().

    Continue reading Part 2 - TokenCollection Class
  • Generating Web API Keys 29 May 2020 - 4758 views
    If you're building a REST API, chances are you're going to need to generate secure random API keys. You may be tempted to use a third-party library but before you do, consider using the crypto package from Node.js. The crypto module can generate random bytes, which can then be used to create your API keys.

    The randomBytes() function from the crypto module can be used to generate cryptographically strong pseudo-random data given a size. The size is provided as an argument specifying the number of bytes to generate. Take a look at the following example.

    Listing 1

    const crypto = require("crypto");
    
    const rand = crypto.randomBytes(20);
    
    console.log(rand);
    

    The result returned from the function is a Buffer object consisting of the generated bytes. If you log the result to the console, you can see the hex value for each byte.

    Listing 2

    <Buffer 85 c7 b6 96 54 15 75 c8 03 55 9e 2f e6 6f a1 cc b9 24 3e 68>
    

    Joining each hex value together will result in a unique string. This can be done easily by calling the toString('hex') method on the result as shown below.

    Listing 3

    ...
    const unique_id = rand.toString("hex")
    
    console.log(unique_id);
    
    // Output similar to 6bba108d4b2212f2c30c71dfa279e1f77cc5c3b2
    

    So now you have a unique id but it looks more like a UID than an API key. There are examples on the web that suggest you should convert the result into a base64 string rather than a hex string but base64 strings are not url friendly as they can contain non alphabet characters and simply replacing these characters with an empty string will give you an uneven string length.

    An alternative approach is to pick characters from a predefined set of characters using an index. There are 256 characters that can be represented by a single byte. If you create a string that holds 256 characters, you can use the decimal value for a given byte as the index to lookup a character from the predefined character set. The example code below will hopefully make this clearer.

    Listing 4

    const crypto = require("crypto");
    
    const rand = crypto.randomBytes(20);
    
    let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".repeat(5);
    
    let str = '';
    
    for(let i=0; i < rand.length; i++){
        let decimal = rand[i];
        str += chars[decimal];
    }
    
    console.log(str);
    
    // Output similar to uUAAw0VabC6YJtbDVyTv
    

    As you can see from the code above, I've created a chars variable that holds more than 256 characters by repeating the string 5 times. The string contains only the characters I want to generate my API keys. The for loop iterates over each byte and the decimal value for the byte is used as an index to select a character from the chars variable. The output now looks more like an API key rather than a UID.

    The code above can be made slightly more efficient by not having to create a string with 256 characters. Instead we create a string with only the characters we need and then use the modulo operator to determine the index.

    Listing 5

    ...
    let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
    let str = "";
    
    for(let i=0; i < rand.length; i++){
        let index = rand[i] % chars.length;
        str += chars[index];
    }
    
    console.log(str);
    

    As you can see it only takes a few lines of code to generate a secure random API key.