Terminal Main Class

This class provides a terminal user-interface to the query interpreter. The Jekejeke Prolog runtime must be present when executing the class. The terminal class is the intermediary be-tween the end-user and the query interpreter. How the query interpreter implements its re-sponsibility has already been described in the previous section. The interaction now works in that the terminal class first sets up the knowledgebase as follows:

    /**
     * <p>Main method.</p>
     *
     * @param args The command line arguments, not used.
     */
    public static void main(String[] args) {
/* setup the Prolog runtime */
Knowledgebase know = new Knowledgebase(ToolkitLibrary.DEFAULT);
Interpreter inter = know.iterable();
Knowledgebase.initKnowledgebase(inter);
/* load the Prolog code */
Object consultGoal = inter.parseTerm("consult(library("+
"example01/table))");
inter.iterator(consultGoal).next().close();

The above shows how we fulfil the initialization responsibility of the main class. We need to create a knowledge base and consult the file table.p. For this purpose we dynamically build a goal of the form consult(library(example01/table)) and submit this goal to the interpreter. We can use the method chaining next().close() method, to executes the goal once and to then abort any backtracking.

After these first steps the query object is ready and can be populated with the search criteria. Our forms based interface on top of the standard input and standard output will be very primitive. We simply write out the names of the search criteria and then read the value of the search criteria. This method has the advantage that we can pass the blank to the query interpreter. But it has the disadvantage that for example numbers are not validated. As an example the acquisition of the search criteria firstname can be seen:
        /* read the search criteria */
Query query = new Query(inter);
Writer ttyout = (Writer) inter.getProperty(
ToolkitLibrary.PROP_SYS_DISP_OUTPUT);
Reader ttyin = (Reader) inter.getProperty(
ToolkitLibrary.PROP_SYS_DISP_INPUT);
ttyout.write("Firstname: ");
ttyout.flush();
query.setFirstname(ForeignConsole.readLine(ttyin));

When all search criteria have been collected, we can let the query interpreter build the query term. The query term will be a normal Prolog term that also contains variables. The normal write operation will generate synthetic names of the form “_<letter><number>” for the variables. To make the query term more readable we will use a write operation that makes use of a variable map. The names of the variables will be provided by the query interpreter itself. The corresponding code reads as follows:

        String[] colids = query.listColumnIdentifiers();
        TermVar[] vars = query.makeVars();
        Object queryTerm = query.makeQuery(vars);
        inter.unparseTerm(ttyout, query.makeVariableNames(colids,
vars), queryTerm);

The names of the variables can be used as column names for the result table. To format the result table on the standard output we use the horizontal tab control character (‘\t’). Iterating over the rows and then over the row values does the job for displaying the table The corresponding code looks as follows:

        Object[][] rows = query.listRows(vars, queryTerm);
        for (int j = 0; j < rows.length; j++) {
Object[] row = rows[j];
for (int i = 0; i < row.length; i++) {
if (i != 0)
ttyout.write('\t');
ttyout.write(row[i].toString());
}
ttyout.write('\n');
ttyout.flush();
}

Comments