A H Bensiali
A H Bensiali

Reputation: 875

prolog checks passing with findall

Ive been working on something for the last 2 weeks and I seem to hit a wall time and time again. So Im trying to book staff into a system and make sure that there is no overlapping shift bookings.

The time is a unix timestamp and the appointment has 4 things,

Even if there is an existing entry in appointment_logs.pl, with the exact name and times, the prolog script still succeeds and enters the same entry into the appointments which it really should fail. findall gets all entries and returns a list and if the list is empty then the from-to times of the appointment are valid and the doctor is free during that period.

A count is made on the list returned from findall which if Count == 0, then we can assert an appointment into db.

Im kinda new to prolog and its really confusing, so if anyone can shed any light on what Im doing wrong, it will be much appreciated.

appointment_logs.pl file

assert(appointment(3, 1737207000, 1737217800, brian)).
assert(appointment(12,1737379800,1737389800,brian)).
assert(appointment(12,1737379800,1737389800,brian)).

appointments.pl

:- module(appointments,[
    appointment/4,
    assert_appointment/4
]).
:- dynamic(appointment/4).
:- dynamic(assert_appointment/4).

:- initialization(db_attach('appointment_logs.pl', [])).
:- discontiguous appointments:assert_appointment/4.
:- use_module(library(persistency)).
:- persistent(appointment(id:integer, timefrom:integer, timeto:integer, staff:atom)).

doctor(brian).
doctor(abe).
doctor(rufus).

is_between(StartTime, EndTime, SuggestedStart, SuggestedEnd):-
    (between(StartTime, EndTime, SuggestedStart);
    between(StartTime, EndTime, SuggestedEnd)).

find_occurences(FromTime, ToTime, Staff, Result):-
    findall(M, (
        appointment(M, Start, End, Staff),
        is_between(Start, End, FromTime, ToTime), !,
        doctor(Staff)
    ), Result).


assert_appointment(Id, FromTime, ToTime, Staff):-
    find_occurences(FromTime, ToTime, Staff, Result),
    length(Result, Count),
    Count == 0,
    asserta(appointment(Id, FromTime, ToTime, Staff)).

Upvotes: 0

Views: 62

Answers (0)

Related Questions