Introduction
Getting Started
This version of the TR implmentation runs on XSB Prolog. To run the Prolog
interpreter, perform the Unix shell command:
$XSB_DIR/emu/xsb -i
At the XSB prompt, consult the main module and initialise the TR implementation
by calling "init".
XSB Version 1.4.1(94/11/21)
[sequential,single word, optimal mode]
|?- [prot3].
[prot3 loaded]
yes
|?- init.
[naming3 loaded]
[basic_funs3 loaded]
[tr_basics3 loaded]
[parser3 loaded]
[query3 loaded]
[upload3 loaded]
[keys_appl3 loaded]
[keys_basics3 loaded]
yes
|?-
Reading Files
A transaction program consists of a set of transaction rules and declaration
statements. To input a transaction base from a file tr_file, perform the
following directive:
|?- loadtrans(tr_file).
The transaction base in tr_file will pass through a parser and
be compiled into a set of Prolog rules. The compiler stores the rules into
a new file with suffix _tran(i.e. tr_file_tran in this
case) and loads them into the Prolog database.
The directive loadtrans/1 loads a transaction base. To load
database, perform the following directive:
|?- loadbase(db_file).
The database in the file db_file contains a set of declaration statements,
database rules and tuples. Note that database rules and transaction rules
are treated differently.
Transaction Rules and Database Rules
Like Prolog rules, a transaction rule consists of two parts: a rule head
and a rule body.
Head <- Body.
or
Head.
where Head is an atomic formulae, and Body is a sequence
of transaction actions (or queries) connected by serial conjunction
connectives (sc).
For example, suppose the transaction base consists of the following
two rules:
add(0).
add(N) <- N>0 sc N1 is N-1 sc create(p(N)) sc add(N1).
Then, the transaction query '|?- trans add(10).' would create 10
tuples (p(1) to p(10)) in the database.
On the other hand, a database rule has the same format as a Prolog rule, i.e.
Head :- Body.
or
Head.
The body of a Prolog clause can be a database predicate or a
conjunction of database predicates.
Note that predicates in the body of a transaction rule can define in
the database, the transaction base or the transition base, but predicates
in the body of a database rule can only be defined in the database.
Transaction Queries
To distinguish a transaction from a Prolog query, the user must put the
transaction query as the argument of the Prolog predicate trans/1.
For simplicity, "|?- " is a Prolog query prompt, and "|?- trans
" is a transaction prompt.
For example, suppose the transaction base and database contain the following:
transaction base database
p(1) p(2)
Then, |?- p(X). outputs X=2 only but |?- trans p(X).
outputs X=1 and X=2. So, rules in transaction base are
invisible to Prolog query.
A transaction query is a sequence of goals (or updates). The serial conjuction
connective is denoted by sc. For example,
|?- trans p(1) sc create(q(2)).
means if p(1) is in the database(or transaction base), create a tuple q(2)
to the database.
Standard TR Predicates
Declaration
Database atoms represent tuples in a relation. Each relation has its
own key. Each key determines a unique value. A declaration statement for
a relation should be made before attempting to access it. The
statement declares the arity of the relation, its key, and indexes. The format
of a declare statement is:
declare Name/NArgs
key NKey size S indexing IList
The statement declares a database relation with name Name and
number of arguments NArgs. The first NKey arguments of the
relation are the key, which uniquely identifies the values
of the remaining arguments. The remaining part of the declaration statement
is indexing specifications. The system would create
a hash table with S entries for the arguments listed in IList
. Note that in most Prologs other than XSB, this part of a declaration
is neglected, because most Prolog interpreters do not allow user-defined indexing on predicates.
In the declaration statement, apart from the relation name and arity,
if any of the remaining declaration is missing, the program uses default
settings:
- If the key field is missing, the program uses
the whole tuple as the key.
- If the size and indexing fields are missing, the program does
not define any special indexing.
- If the indexing field is missing, the program defines
a hash table with S entries on the arguments of
the key.
Updates
After declaration, a relation can be accessed through queries and updates.
There are three types of simple updates: create, destroy and set; and three
types of bulk updates: bulk insert, bulk delete and assignment.
The following examples use declarations of p/1 key 1,
and q/2 key 1.
- create(Tuple)
- Inserts the tuple Tuple into database. The tuple specification
should have been declared in advance. The predicate also fails if
the key of Tuple is already in the database. In other word, the
key of tuple has already assigned a value. Note that all variables
in Tuple should have been unified before being created. For
example, trans create(p(1)) sc create(p(2)) creates two
tuples, p(1) and p(2) to the database, but trans
create(q(1,1)) sc create(q(1,2)) fails.
- destroy(Tuple)
- Removes the entry Tuple from database. The process fails if
Tuple was not in the database. Arguments of Tuple can be
uninstantiated because the execution would unify the variables by
choosing tuples in database. For example, trans destroy(p(1))
deletes p(1) from database; trans destory(q(X,Y)) deletes
one of the q(X,Y) tuple from database.
- set(Tuple)
- Change the value of the key identified by Tuple. The key of
Tuple should not be the tuple itself, otherwise, no value of
the key can be set. For instance, if q(1,1) is in the database,
trans set(q(1,2)) succeeds, but if q(2,2) is not in the
database, trans set(q(2,3)) fails.
- bulk_ins(Cond,ToTuple)
- adds all tuples to ToTuple which satisfy the condition $Cond$.
Also, it never fails except ToTuple is not declared.
- bulk_del(DTuple)
- destroys all tuples satisfying Dtuple. Again, it never fails
unless Dtuple is not declared.
- ToTuple := Cond
- deletes all tuples in the relation of ToTuple and overwrites
them with those satisfies the condition Cond. For instance,
p(X) := q(X,Y) selects the first elements of q(X,Y),
and then put in the predicate p(X). So, if the database contains
q(1,1), q(2,2), then p(1) and p(2) are created. Note
that Cond is a database formula and the assignment searches over
the database only.
Loop and Selection
There are two types of loop defined: while loop and and for loop.
- [with VList] while Cond
do Actions
- iterates Actions repeatedly until Cond fails.
The list VList contains the variable names which have
been unified and cannot be used as free variable inside the
loop.
- [with VList] for Vars
in Cond do Actions
- executes Actions for all sets of distinct variables
denoted by Vars chosen from Cond.
The simplest selection statement is a if-then-else statement. It has the
format of:
- if Cond then Action1 else
Action2.
- exectes Action1 if Cond is true or
executes Action2 otherwise.
Some Prolog interpreters have the directive if/3 which performs
similar function.
You can take a look of a simple tutorial
here.