What is a KBL Script?

A Brief Introduction

 

 

 

KBL is a command-driven language and a script is a series of logical command lines that are interpreted and executed by the KBL script processor. Scripts may vary from just a few lines - to get a specific task completed - to many 100s of lines for a full 'line-of-business' application.

Script files are .kbl.txt (text files) so can be read by most text editors including Notepad but are normally written using KBL Studio (KSE or KSP). Scripts will only run in Studio (KSE, KSP ) and the Keg Browser (for end users). All scripts you create and save in your Studio environment are located in the /KBL (script) folder (See System > Folders > KBL)

KBL scripts do not have line numbers and there can be blank lines between command lines. We recommend liberal use of comment lines (starting REM or // ) and short in-line notes after commands. We also recommend that the Intellisense option is set to ON to ensure getting the syntax correct.

In effect, KBL scripts are written to mirror your business process or a series of business processes where each step taken depends on the success, or other outcome of the previous step.

A Simple Script Example

The following is a simple script to import a dataset, in this case, an Excel spreadsheet and eliminate records where the value of the 9th column (order status) equals "Cancelled", remove unwanted columns and export to a new spreadsheet.


REM ---------------------------------------------------------------------------------------------------------------
REM Example_Script  Import a spreadsheet into MAIN and export to a processed version
REM ---------------------------------------------------------------------------------------------------------------
 
Import /XLSX Customer_Orders worksheet=Sheet1
 
TAB.SHOW MAIN "Data as imported" // Display the data just as it was read
 
SORT ASC c.2    // Sort on column 2 (date ascending order)
 
MARK C.9 = "Cancelled"    // Mark any records with an order status of Cancelled
Remove /Clean // Remove marked order records marker column
 
Delete /COL c.5 // Delete the Columns we do not need need
Delete /COL c.6
Delete /COL c.7

// Write out the cleaned spreadsheet to a new file

Export /XLSX  Cleaned_Customer_Orders worksheet=WEEK21
 
MSG "New file successfully exported" 

EXIT

The above script carries out a simple "linear" task (note the comments lines starting with REM an // followed by text). The example uses one of KBL is key features: working at whole dataset level, for this task, no parameters are used and no conditional logic is needed as operations are performed at whole row and whole record level. The script is processed from start to finish without interruption BUT we could have easily displayed the processed data and asked if the user wanted to export to a new file.

Types of Script

KBL scripts are incredibly versatile and vary enormously in size and purpose and are, of course, determined by their intended function from the simple whole dataset operation, as illustrated, to full line of business applications. They may be as small as 5 command lines to many hundreds. Here are some examples of what scripts are used for:

  • Data manipulation: Importing, merging, cleansing, transforming exporting (e.g. an ETL task to map data from one system to another)
  • Data Analysis: Finding patterns, creating summaries, performing statistics, organising data for visualisation. KBL is good at processing large datasets even up to millions of rows
  • Semantic Data Processing: Analysing "rich" data, finding patterns, exceptions etc.
  • Data input, validation and storage. KBL has easily programmed user interfaces (UI) for input and display.
  • Modular "reusable components" scripts that are functions or instructions that are used within other scripts (e.g. Stylesheets, FUNCtions, UI components).
  • Executing business flow: Users can easily link together functional components in a script E.g. for dealing with mainstream and exception processing paths (Happy and unhappy paths).
  • Applications that require a lot of human interaction (input of parameters, control information). For example Computer-based testing and training .

Scripts that model Business Flow

In real life, business (as well as scientific and technical) activities are rarely as simple as the example given and you will need to plan for both mainstrean and exception scenarios. Typically, the "Happy Path" scenario prevails for the majority of cases you deal with (all the right data have been captured, customers are happy and contented) and you can usually write your script quickly and easily for these as a series of logic steps.

However, you will often have to make provision and design for "unhappy paths".  For example, data might be missing or indeterminate, sales orders have not been confirmed, customers need to be refunded and transactions reversed. These processes or functions are often more complex and take a lot longer to write.

So, a KBL script (or program, if you wish) needs to be constructed as a series of logical steps, often with discrete functions connected together, to complete the required task. Sometimes a script is very linear - a series of actions preformed consecutively to read, process, output and display information. Some requirements, however, will result in more intricate scripts where far more control of the execution path is needed. These will contain conditional logic where the functional components to be executed depend on specified business logic, the state of data and settings of control parameters. Conditional logic will be essential, (here, meaning IF, THEN, ELSE, LOOP, GOTO etc. commands).

Planning to write a script?

Before you rush into writing a script for anything but the simplest tasks, we strongly urge drafting out your business process flow at least on a piece of paper (process mapping tools are available if your business needs the formality, but pen and paper are usually adequate). A script can soon develop into several hundred lines long so to avoid simple errors or massive rewrites PLAN YOUR SCRIPT STRUCTURE WITH CARE, this WILL save you a lot of time. Additionally, If the data you are going to use comprises different entities and / or comes from different sources, we also recommend sketching an ERD (Entity Relationship Diagram) so that you are clear on how to use your data again proper understanding of your data will save you hours of frustration in the long run. So consider the following basics BEFORE you embark on a new script:

  • Understand the requirements identify all the inputs and outputs
  • Categorise requirements by Musts, Should haves, Could haves and Would likes (MoSCoW)
  • List the individual, discrete functions you need to perform within your script solution
  • Map out the logical process flow within and between your functions
  • Sketch the structure of your data and relationships between "entities"
  • Plan how you will create your Physical solution from your Logical design

So the simple answer to "What is a script" is that it is a series of consecutive programmed actions, BUT a better answer is, as the name suggests, a script is a choreographed series of logical steps that deal with known and predictable events. The more you know about your subject and the logic you need to deal with your task, the more elegant, effective and timely your script solution will be.

KBL scripts are easy and fast to write, to document, test, run and enhance. They can be created to perform intricate business process flow or just execute simple logic. They are written using easy to follow English-like vocabulary.

Scripts are created using the Script > New action and are best organised by Project.

Example Scripts

There are plenty of example scripts to look at and you may find that, rather than creating a script completely from scratch, you can adapt and existing example (Script > Save As).

Scripts as Applications

You can create business applications with KBL scripts. Although KSE has only limited commands for user interfacing and formatting and you cannot create run-time version scripts to run in the Keg Browser for end-users, there is no limit to the number of lines in a scripts and you do have the ability to create INPUT and DISPLAY screens as well as full conditional logic capability. Note KSP, which is designed for application building will run any scripts created in KSP.

Run-Time Scripts? Can KBL Scripts be Compiled?

Scripts can be packaged up as compact, encrypted run-time applications for "end-users" to run in the Keg Browser but this requires the KSP version and above. Scripts, and the resources they need (data, images, etc.) are turned into .krt PACKs.

Documentation Standards

If you intend to write a lot of scripts, we recommend creating a script standard template that suits your needs particularly if you intend to share or distribute your work with others. Copies and versions of scripts soon proliferate so do put version control comments in your scripts and always keep the latest working / production copy in a separate project to avoid having to retrace your steps.

Good script writing is both an art and a science and does require practice and experience. This article is just a brief introduction what scripts are and what they can do.

 

Back to Top