1
votes

I'm working on a case-based reasoning subsystem for academic advising that is able to take a new student profile (transcript) and compare it to database of student_profiles in order to find the best match if not the exact match.

I'm still not very familiar with the language and I trying to open a file and extract an specific predicate let's say

student_profile/4 : student_profile(L1, name, L2, L3)

which is found in student.pl.

Once I have extracted the predicate from the .pl file, I want to be able to assign it to a variable and split it so I can extract the first argument (in this case L1) to make some calculations.

This is what I have so far:
I have opened the file and I'm able to see the entire list of predicates found in the student profile or transcript.

load :- open('h:/AAS/Novel_Profiles/vivian.pl', read, Stream),
        read(Stream, X),
        read_data(Stream, X, StudentP),
        write(StudentP),
        close(Stream).
        
read_data(_Stream, end_of_file, []) :- !.

read_data(Stream, X, [X|StudentP]) :-
    read(Stream, Y),
    read_data(Stream, Y, StudentP).

I have looked for examples on how to extract a term and use it for further processing but I have had no luck (or maybe I'm not using the right terminology).

1

1 Answers

1
votes

Your load predicate should 'return' the list read, or cache it with assertz, for later use...

load(StudentData) :- open('h:/AAS/Novel_Profiles/vivian.pl', read, Stream),
        read(Stream, X),
        read_data(Stream, X, StudentData),
        write(StudentP),
        close(Stream).

then you can use member/2 to match some record in loaded list...

?- load(L),
   member(student_profile(Student, vivian, L2, L3), L),
   writeln(vivian(Student, L2, L3)).

But there is a simpler way: consult the file and directly query the data.

load :- consult('h:/AAS/Novel_Profiles/vivian.pl').

Both methods (read or consult), require your file is syntactically correct. Your Prolog will inform you of any problem the file could have.

After a successful consult, each fact (or rule) is ready to use...

?- student_profile(Student, vivian, L2, L3),
   writeln(vivian(Student, L2, L3)).