📜 ⬆️ ⬇️

Prologue: Fact Base from CSV File

Import facts into the prologue database from a CSV file



In order to work with database facts in the prologue, their (facts) must be imported from an external source.


The diagram conventionally shows three areas of activities:
Yellow - preparation of the intermediate file. For a simple import, it might just be saving the document in CSV format. To work on the regulations, it is possible to configure database components (for example, MS SQL Server Integration Services) for periodic unloading. This activity is not considered in the article.
Red - import data from a CSV file to the fact base.
Green - working with the fact base in Prolog.
Note. Arrows indicate data streams.

The SWI-Prolog implementation incorporates predicates that simplify working with a CSV file. Here are the main points to create a program:
1. Entering data from a CSV file

csv_read_file(File, RowList, [ separator(0';)])
File - the name of the file in a special format.
RowList - list of rows after import
The input of the function is the name of the file from which the import will be performed, as well as the parameter that defines the separator between the fields. At the output we get (variable RowList) a list of the form:
 [row(…, …, …), row(…, …, …), …] 

')
2. The definition of the file name facts

This is done so that later it was convenient to refer to it.
 file(base1, csv, 'c:/pl/prj1/file.csv'). file(base1, base, 'c:/pl/prj1/file.pl'). 


3. Determining the base of facts

 :- dynamic(base1/3). 

In this database we will add the facts.

4. Zero base

 abolish(base1/3). 


5. Generating a database from the list

Here you can also make preliminary data processing. For example, select only those fields that are needed. The assert predicate adds fact to the base.
 perform_cl([]). perform_cl([row(N1, N2, _, N3,_)|T]):- assert(base1(N1,N2,N3)), !, perform_cl(T). 

Or
 forall(member(row(N1,N2,N3),RowList), assert(base1(N1,N2,N3)). 

The forall predicate is very powerful. For all alternatives of the search for a solution (1 parameter) it performs the action indicated by the second parameter.

6. Saving the database to an external file

  file(base1, base, F), tell(F), forall( base1(N1,N2,N3), (writeq( base1(N1,N2,N3)), write('.'), nl) ), told. 


7. Loading Fact Base From File

 file(base1, base, F), Consult(F). 

or
 file(base1, base, F), [F]. 


Full program:


 %    % :- dynamic(base1/3). %   % %     CSV file(base1, csv, 'c:/pl/prj1/file1.csv'). %   file(base1, base, 'c:/pl/prj1/file1.pl'). %   csv    % import_base:-    %       file(base1, csv, File),    %    c  ";"   [row(..., ..., ...), ...]    csv_read_file(File, RowList, [ separator(0';)]),    %      abolish(base1/3),    %        perform_row1(RowList),!. %      % %   -  . perform_row([]). %    perform_row([row(N1, N2, _, N3,_)|T]):-    %  ,       assert(base1(N1,N2,N3)),    !,    %       perform_row(T). %     % perform_row1(RowList):-    %     :        forall(member(row(N1, N2, _, N3,_),RowList),          %            assert(base1(N1,N2,N3))). %     % save_base:-    %       file(base1, base, F),    %  ,          tell(F),    %     :       forall( base1(N1,N2,N3),        %          (writeq( base1(N1,N2,N3)), write('.'), nl)        ),    told. %    % load_base:-    %      file(base1, base, File),    %     consult(File). 


Initial data

file1.csv file contents
; 12; /; ;
; 13; /; ;
; 14; /; ;


Work protocol:

4 ?- import_base.
true.

5 ?- forall(base1(N1,N2,N3),writeln(base1(N1,N2,N3))).
base1( , 12, )
base1( , 13, )
base1( , 14, )
true.

6 ?- save_base.
true.

7 ?- abolish(base1/3).
true.

8 ?- forall(base1(N1,N2,N3),writeln(base1(N1,N2,N3))).
ERROR: toplevel: Undefined procedure: base1/3 (DWIM could not correct goal)

9 ?- load_base.
% c:/pl/prj1/file1.pl compiled 0.00 sec, 492 bytes
true.

10 ?- forall(base1(N1,N2,N3),writeln(base1(N1,N2,N3))).
base1( , 12, )
base1( , 13, )
base1( , 14, )
true.



Fact1.bl file file1.pl

base1(' ',' 12',' ').
base1(' ',' 13',' ').
base1(' ',' 14',' ').

Conclusion

This article provides a program that demonstrates some of the preparatory work for creating a fact base that does not show the true power of Prolog. However, after importing into the fact base, elegant data processing methods can be applied. For example, control and visualization of hierarchical structures (organizational - staff structure), preparation of data for billing, cleaning (transformation) of data in tables and others.

Source: https://habr.com/ru/post/112137/


All Articles