3 ms·
Would something like this work for you? (Edit: it parse_time/2 is a Swi-Prolog built-in so this won't run in other Prologs. chielk's solution above is best)
by YeGoblynQueenne 9y ago
Would something like this work for you? (Edit: it parse_time/2 is a Swi-Prolog built-in so this won't run in other Prologs. chielk's solution above is best)
entity('TimeWarner').
ticker(entity('TimeWarner'), 'TWC', date('1999-01-01'), date('2014-04-31')).
ticker(entity('TimeWarner'), 'AOL', date('2014-05-01'), date('9999-01-01')).
current_at(Entity,TLC,Date_start,Date_end,date(T)):-
entity(Entity)
,ticker(entity(Entity),TLC,date(Date_start),date(Date_end))
,parse_time(Date_start,DS)
,parse_time(Date_end,DE)
,parse_time(T,Tp)
,Tp > DS
,DE > Tp.
?- current_at('TimeWarner', TLC, DateStart, DateEnd, date('2017-05-29')).
TLC = 'AOL',
DateStart = '2014-05-01',
DateEnd = '9999-01-01'.
Basically, you need to treat the date as an atom and it's better to avoid wrapping up everything in compounds like you're doing in ticker(entity('TimeWarner') etc. That just makes it harder to read and write your queries. You can just pass the argument values in the head of the rule and match them with the facts in the database like I do above.
If you really require the dates to be in yyyy-mm-dd format, the code above becomes slightly more verbose but it's not the end of the world.