The ConceptBase.cc Shell (CBShell) is a command line client for ConceptBase.cc. It allows to interact with a CBserver via a text-based command shell. Moreover, it can process commands from a script file without further user interaction. The utility can be employed to automate certain activities such as batch-loading a large number of Telos models into a CBserver, or to extract certain answers from a CBserver.
There are two ways to use the CBShell. The first one processes the commands from a script file (batch mode). The second one prompts the user for commands (interactive mode).
cbshell [-l] [-t] [-i] [-q] [-f] scriptFile [params] cbshell [-l] [-i] [-p]
Command arguments with white space characters have to be enclosed in double quotes (’"’). Command arguments may span multiple lines. Lines starting with ’#’ are comment lines2.
If an argument contains a string of the form $PropName, it will be replaced with the value of the corresponding Java property (which may be defined using the -D option of the Java Virtual Machine), if the property is defined.
The CBShell utility can be used in Unix pipes to extract textual output The CBserver and pass it to subsequent programs as input. To do so, you should start the CBserver with tracemode no and using the showAnswer command of CBShell to specify the elements to be written to standard output. The CBShell script below realizes the extraction of Graphviz [link] specifications from ConceptBase.cc:
# File: myscript startServer -u nonpersistent -t no tellModel ERD-graphviz2 tellModel UniversityModel ask "ShowERD[UniversityModel/erd]" OBJNAMES FRAME Now showAnswer exit
The complete example is available from the CB-Forum at link. Another resource for CBShell scripts is the list of test scripts at link.
A CBShell script can use variables $0 ... $9 inside the script to refer to the positional parameters supplied via the call of cbshell. Assume the following content of the script file:
# File: pascript startServer -u nonpersistent -t low -port $1 tell "$2 in Class end" ask "find_instances[$2/class]" OBJNAMES LABEL Now ask "find_instances[$3/class]" OBJNAMES LABEL Now
and the command line call
cbshell pascript 4321 MyClass Integer
The CBShell interpreter will then replace $1 with 4321, $2 with MyClass, and $3 with Integer. If you supply less parameters than required by the script, it will issue an error message and quit. If the script had started a CBserver, then this CBserver is stopped before quitting. Likewise, if the script had enrolled to an existing CBserver process, it will unenroll before quitting.
The variable $0 is bound to the name of the script file. CBShell uses the Java tokenizer to separate the positional parameters. The tokenizer uses white spaces (blanks, tabs) to separate tokens. Use double quotes if one argument consists of several words, e.g.
cbshell otherscript "MyClass isA Integer end"
An example of a script with positional parameters is provided in the CB-Forum at link. You can also call CBShell scripts within scripts/batch files of the host operating system, see section 7.6.
A CBShell script can be made executable under Unix/Linux and can then be used pretty much like any other shell script. Assume that you have a CBShell script myscript that you want to execute directly from the command line.
As a first step, create a link to the cbshell at a common directory for installed programs:
sudo ln -s /opt/conceptbase/cbshell /usr/bin/cbshell
As a second step include the following comment as the first line of myscript:
#!/usr/bin/cbshell
Then, make the script executable:
chmod u+x myscript
You can then directly call the script by simply typing its name:
myscript
The direct call is equivalent to the call
cbshell ./myscript
CBShell offer the basic commands to interact with a ConceptBase server. However, it lacks control structures such as loops and conditions. It also cannot invoke arbitrary programs. Regular shell scripts such as the Bourne shell of Unix/Linux do provide these capabilities, and thus it is a natural idea to integrate CBShell scripts within regular scripts to accomplish more sophisticated automation tasks.
As a first step, you should make the CBShell script executable and declare in its first line
#!/usr/bin/cbshell -q
This allows to pass parameters that include special characters to the CBShell script. Assume that the CBShell script ascript was declared that way. Then, it can be called in a Bourne shell like:
ascript 'Jet 400' MyClass
The option -q prepares the CBShell to treat parameters with single quotes in a special way. Assume further that ascript has the following content:
#!/usr/bin/cbshell -q # File: ascript ... tell "$1 in $2 end" ...
CBShell will internally expand the quoted parameter ’Jet 400’ to \"Jet 400\" and then execute
tell "\"Jet 400\" in MyClass end"
The resulting frame in ConceptBase shall be
"Jet 400" in MyClass end
Essentially, single quotes of CBShell parameters are converted to double quotes within ConceptBase. An simple example is given in link. A more elaborate example is available in the CB-Forum at link. The main example is in the fileSizeDemo.
Assume you have a program generator that analyzes some input data (e.g from files) and produces output in the form of CBShell commands (tell, ask, etc.). Then, this output can be directly passed to CBShell in a Unix pipe:
generator | cbshell -p
The generator program must take care of generating all required commands, in particular making sure that CBShell is connected to a CBserver. Consider as example generator the script file printfiles4cbshell from the CB-Forum at link:
#!/bin/sh echo "enrollMe localhost 4001" for file in * do if [ -f ${file} ] then fsize=$( stat -c %s ${file}) frame="'${file}' in File with size s: ${fsize} end" echo tell \"\\\"${file}\\\" in File with size s: ${fsize} end\" fi done
It first generates a command to enroll to a CBserver on localhost at port 4001. Then, it forms a frame for each filename in the current directory to tell the file’s size to the CBserver. You can run the script in a terminal to see the output generated by it.
Now, assume that you have started a CBserver on localhost with port number 4001. You should tell at least the following frame to the Cbserver to define the class File to which the above script refers to:
File in Class with attribute size: Integer end
Then execute in a terminal window:
printfiles4cbshell | cbshell -p
It will tell the file size information to the CBserver. At the end, the end of file detection of the CBShell will trigger the CBShell to exit from the CBserver. The use of pipes should also be possible under Windows though the above example printfiles4cbshell is formulated for Unix/Linux.
It is also possible to continue the pipe to a post-processing program. In this case, the commands sent to CBShell should include ask commands in combination with showAnswer. The following pipe shows the main idea:
generator | cbshell -p | postprocessor
An elaborate example of this usage is in the CB-Forum at link. The example shows how to extract module import statements from program source files, store them in ConceptBase and let ConceptBase produce a graph specification, layed out by GraphViz.