Table Data

The queried table is stored as a Prolog text. The Prolog text can comfortably be tested in the Jekejeke Prolog development environment. The Prolog text is stored in a file “table.p” and it basically contains only one predicate employee/4. This predicate lists the first name and name of some imaginary employees together with their age and current salary. The following excerpt shows some of the example facts that define the predicate employee/4:

% employee(Firstname, Name, Age, Salary)
employee('Сергей',    'Иванов',     53,   18500).
employee('Сергей',    'Беляев',     53,   19000).
employee('Hans',      'Fischer',    62,   21500).
...

Since we are in the course of using Jekejeke Prolog 0.8.8 which has the Unicode extension, we used some facts that contain Unicode strings for demonstration and testing purpose. Because our first name and name atoms start with upper case letters we have to place them in single quote strings, otherwise they would be recognized as variables. The file “table.p” needs to be encoded in some encoding that covers all the used Unicode characters, and when loading this encoding needs to be used again.

We have used the default encoding of Jekejeke Prolog 0.8.8 which is UTF-8. The file can easiest be consulted in the development environment when the path of the file is included in the class path. The class path can be set via the menu item File | Settings. The development environment has to be restarted when the class path is changed. More information can be found in the console manual documentation.

After having consulted the Prolog text of the table we can query the employee/4 predicate. The simplest query does not use any restrictions on the predicate. We simply use different variables for each argument position of the predicate. As a result we will be able to retrieve the arguments of the facts that are stored for the predicate employee/4. For each fact one solution is generated which we can explore via backtracking:

?- [example01/table].
Yes
?- employee(Firstname, Name, Age, Salary).
Firstname = 'Сергей',
Name = 'Иванов',
Age = 53,
Salary = 18500 ;
Firstname = 'Сергей',
Name = 'Беляев',
Age = 53,
Salary = 19000 ;
Firstname = 'Hans',
Name = 'Fischer',
Age = 62,
Salary = 21500 ;
...

We can also pose more difficult queries by instantiating the queried predicate. So when we for example set the second argument to ‘Иванов’ [Iwanov] we will only retrieve those facts that exactly match the name ‘Иванов’ [Iwanov]. We will use the unification operator (=)/2 to do it. Again we can explore the solutions via backtracking:

?- Name = 'Иванов', employee(Firstname, Name, Age, Salary).
Firstname = 'Сергей',
Age = 53,
Salary = 18500 ;
...

A further way to enhance our query is by making use of arithmetical predicates. Among the arithmetical predicates we find inequality checks such as less than or equal (‘=<’) and greater than or equal (‘>=’). So when we chain the query predicate with appropriate arithmetic predi-cates we can filter the employee facts. Again we can explore the solutions via backtracking:

?- employee(Firstname, Name, Age, Salary), Age >= 20, Age =< 50.
Firstname = 'Филипп',
Name = 'Иванов',
Age = 32,
Salary = 15000 ;
...

The idea behind the applications in this document will be to provide the end-user with a form based user interface and dynamically build and execute a query on the employee table. The dynamically built queries will cover what we have already seen, namely instantiation and constant arithmetic predicates on predicate arguments.

Comments