Discussions related to Visual Prolog
dcgreenwood
Posts: 14
Joined: 16 Jan 2021 1:33

Beginner question - declare class facts across packages

Unread post by dcgreenwood »

I am a beginner and am building main with a large number of fact databases that are used by many clauses. For organization purposes I would like to put all the assert clauses that build the databases into a different package, but I can't figure out how to make them visible to main.

Is there a way to declare class facts so that a database of class facts built in one package are available for use in a different package?
Harrison Pratt
VIP Member
Posts: 335
Joined: 5 Nov 2000 0:01

Re: Beginner question - declare class facts across packages

Unread post by Harrison Pratt »

Facts can be declared only in a class's implementation (.pro file) so you cannot make the facts themselves visible outside that class. However, you can easily put predicate definitions to manipulate those facts in the class's .cl file or .i file(s) to make them accessible from other classes as in the example below:

Code: Select all

class classA     open core   predicates     putFact : (integer).     getFact_nd : () -> integer nondeterm. end class classA

Code: Select all

implement classA     open core   class facts     factInA : (integer).   clauses     putFact(I) :-         assert(factInA(I)).             getFact_nd() = I :-         factInA(I).   end implement classA
For non-trivial applications, it is a good practice to put all the basic database manipulations into a class (e.g., myDataDB) that handles operations such as consulting, saving, exporting, ,backups, "tidying", etc. Then you create other classes that read or modify that database (e.g., myDataDB_Reporter, myDataDB_Loader ). This is a way of controlling the complexity of your code so it will be easier for you or others to maintain.
dcgreenwood
Posts: 14
Joined: 16 Jan 2021 1:33

Re: Beginner question - declare class facts across packages

Unread post by dcgreenwood »

Thanks, that answers my question. Now to move some code...
Post Reply