Load data (facts and rules) dynamically from external source

I’m using: SWI-Prolog version 8.2.1

Is there a way to load dynamically new facts from an external file/source while my main program is running?

If the facts are in different files then I don’t see why using consult/1 would not work even after the code is running. If the facts are added to the same file, consult will not work as it will replace the facts. If the facts have the same functor then you will need dynamic/1.

Did not test any of this but the that is the idea.

Thank you, it works.
And what if I want to keep all facts from all files (The main program and the external source)?

Can you post what you did?

I have 2 files:
main.pl
external.pl

main.pl:

:- dynamic color/1.

color(blue).

external.pl:

color(red).

So I ran main.pl and then loaded external.pl with consult.
When i ask about red(blue) I got false instead of true, as it was before consult.

This is what I have.

When I first tired it failed because multifile/1 was also needed. After adding multifile/1 it all worked.

main.pl

:- initialization(main).

:- dynamic facts/1.
:- multifile(facts/1).

main(_) :-
    format('In main.~n',[]),
    task_1,
    length(_,10000),
    task_2.

task_1 :-
    format('In task_1.~n',[]),
    consult(facts_1),
    listing(facts).

task_2 :-
    format('In task_2.~n',[]),
    consult(facts_2),
    listing(facts).

facts_1.pl

facts(a).
facts(b).
facts(c).

facts_2.pl

facts(1).
facts(2).
facts(3).

Example run:

?- working_directory(_,'C:/Users/Groot/Documents').
true.

?- ['main'].
In main.
In task_1.
:- dynamic facts/1.
:- multifile facts/1.

facts(a).
facts(b).
facts(c).

In task_2.
:- dynamic facts/1.
:- multifile facts/1.

facts(a).
facts(b).
facts(c).
facts(1).
facts(2).
facts(3).

true.
1 Like

was that suppose to be color(blue) ?

Yes of course, It just a mistake.
Thank you, I will try to understand your example.

1 Like