next up previous contents
Next: Indexing Static Program Clauses Up: Handling Large Fact Files Previous: Compiling Fact Files

Dynamically Loaded Fact Files

The above strategy of compiling fact-defined predicates works fine for relations that aren't too large. For predicates defined by thousands of facts, compilation becomes cumbersome (or impossible). Such predicates should be dynamically loaded. This means that the facts defining them are read from a file and asserted into XSB's program space. There are two advantages to dynamically loading a predicate: 1) handling of much larger files, and 2) more flexible indexing. Assume that the file qdata.P contains 10,000 facts defining a predicate q(X,Y), true for 1<=X<=100, 1<=Y<=100. It could be loaded with the following command:

	| ?- load_dyn(qdata).

XSB adds the ``.P'' suffix, and reads the file in, asserting all clauses found there. Asserted clauses are by default indexed on the first argument (just as compiled files are.)

Asserted clauses have more powerful indexing capabilities than do compiled clauses. One can ask for them to be indexed on any argument, just as compiled clauses. For dynamic clauses, one uses the executable predicate index/3. The first argument is the predicate to index; the second is the field argument on which to index, and the third is the size of hash table to use. For example,

	| ?- index(q/2,2,10001).

	yes
	| ?- load_dyn(qdata).
	[./qdata.P dynamically loaded, cpu time used: 22.869 seconds]

	yes
	| ?-
The index command set it so that the predicate q/2 would be indexed on the second argument, and would use a hash table of size 10,001. It's generally a good idea to use a hash table size that is an odd number that is near the expected size of the relation. Then the next command, the loaddyn, loads in the data file of 10,000 facts, and indexes them on the second argument.

It is also possible to put the index command in the file itself, so that it will be used when the file is dynamically loaded. For example, in this case the file woulstart with:

:- index(q/2,2,10001).

q(1,1).
q(1,2).
q(1,3).
...

Unlike compiled cclauses, asserted clauses can be indexed on more than one argument. To index on the second argument if it is bound on call, or on the first argument if the second is not bound and the first is, one can use the index command:

:- index(q/2,[2,1],10001).
This declares that two indexes should be build on q/2, and index on the second argument and an index on the first argument. If the first index listed cannot be used (since that argument in a call is not bound), then the next index will be used. Any (reasonable) number of indexes may be specified. (It should be noted that currently an idex takes 16 bytes per clause.)

Managing large extensional relations load_dyn, load_dync, cvt_canonical. Database interface, heterogeneous databases (defining views to merge DB's)


next up previous contents
Next: Indexing Static Program Clauses Up: Handling Large Fact Files Previous: Compiling Fact Files
David S. Warren
1999-07-31