/* Subject 0440949. AI department, UvA. Assignment 4, knowledge systems: CSI
 * Andreas van Cranenburgh. 
 *
 * Project history:
 * csi-1.pl started: Fri Nov  4 14:39:29 CET 2005.
 * -- enormous procastrination/postponement
 * csi-2.pl started: Fri Nov  17, somewhere 06:00:00 CET 2005.
 * -- brainstorming; mindmaps --
 * csi-3.pl started: Thu Nov 17 16:48:45 CET 2005
 * -- started coding. no alas, mostly debugging!
 * csi-4.pl started: Fri Nov 18 01:42:39 CET 2005
 * -- this is so XP! eXtreme Procastrination!
 * ucsi.pl, rename. Fri Nov 18 04:58:43 CET 2005
 * -- interface works. reasoning + data left to solve.
 *
 *	1. time and place, actors, actions and objects:
 * time is a point. an interval is two points. time is stored in microseconds,
 * or in any case sth very big.
 *
 *	2. make a reasoning engine, show timeline:
 * reasoning engine, see reason.pl. I will use both FC and BC.  see previous
 * versions how the timeline will be, the interface part of it will go in
 * cli.pl
 *
 *	3. add some common sense
 * naturally. I\'ll use if-then rules for that, maybe in reason.pl, or data.pl. eg.:

if event([action(killing), person(X)]) then dead(person(X)).

 * But I haven't pinned down my prolog representation (that's why the code is
 * contradictorary currently. Back to the drawingboard / brainstorming.
 *
 *	4. model the attached case
 * todo, once I have the prolog format fixed, I copy the cause. I'll resist
 * temptations to do NLP on the raw plaintext :)
 *
 *	5. the interface
 * see cli.pl. it does start by typing go. it already offers the needed
 * options, and some of them have been implemented (though without having the
 * representation complete you get mostly ideas.
 *
 * The trace: yes, once it works :)
 *
 */

:- consult(cli). %plain command-line interface. go predicate comes from there.
:- consult(reason). % FC and BC. by Bratko et al.
:- consult(data). %data should go here, however it gets there!?

%Convertors:
% Seconds to BigTime
timeformat( [ _Day,_Hour,_Min, Sec, BigTime ] ) :- var(BigTime),
	BigTime is Sec * 1000000.
	
/* as you may see here, there is a list with Day, Hour, Min and Sec. 
  This is the standard time object in this project. Another standard one is
  BigTime */
  
% BigTime back to Seconds
timeformat( [ _Day, _Hour, _Min, Sec, BigTime ] ) :- var(Sec),
	Sec is BigTime / 1000000.
	
 /* qualitative is mapped onto quantitative data, using conversion: */

% generate an appropriate seconds value:
quantime(Quant, BigTime) :-
	(
		parse_abs_quant(Quant, Sec)
		;
		parse_rel_quant(Quant, Sec)
	),
	timeformat( [ _D, _H, _M, Sec ], BigTime ).
	
% eg. 29 Oktober 00.45 uur
parse_abs_quant( [ DayOfMonth, Month, Time, Unit ], ResultSec) :-
	parse_time( Time, [ _Day, Hour, Minute, _Sec ] ),
	words(Unit, 3600),
	(
		match_month(Month, MontNo)
		% disabled, who uses numerical dates? well YOU convert them :)
		% ; ( Month >= 1, Month =< 12, MonthNo = Month )
	),
	Days is MonthNo * 30.5 + DayOfMonth,
	Hours is Days * 24 + Hour,
	Minutes is Hours * 60 + Minute,
	ResultSec is Minutes * 60.
	% don't get me started about any leap years or gregorian calendars!

parse_abs_quant( [ DayOfMonth, Month, Time], ResultSec ) :-
	parse_abs_quant( [ DayOfMonth, Month, Time, 'uur'], ResultSec).

%the months, in dutch. gosh, why am I typing this??
match_month(januarie, 1).
match_month(februarie, 2).
match_month(maart, 3).
match_month(mei, 5).
match_month(juni, 6).
match_month(juli, 7).
match_month(augustus, 8).

%both
match_month(april, 4).
match_month(september, 9).
match_month(oktober, 10).
match_month(november, 11).
match_month(december, 12).

%english
match_month(january, 1).
match_month(february, 2).
match_month(march, 3).
match_month(may, 5).
match_month(june, 7).
match_month(july, 8).
match_month(august, 9).
match_month(october, 10).

% first argument is word, second is conversion factor to seconds.
words(minute, 60).
words(minuten, 60).
words(minutes, 60).
words(min, 60).
words(m, 60).
words(uur, 3600).
words(dag, 86400). 
words(dagen, 86400). 
words(day, 86400). 
%words(days, 86400). 
%words(hour, 3600).
%words(hr, 3600).
%words(h, 3600).

parse_time(Hr:Min, [_Day, Hr, Min, _Sec ] ).

%and how to do 23.23? better forget.... prolog is sooo 70's!
%[come on, who comes up with a language with forbidden chars.. where's the
%regex rant rant...]
/*
parse_time(TimeStr, TimeObj) :- 
	%convert TimeStr to list??
	(
		%Define seperators:
		Sep = '.' ; Sep = ':'
	)
	( 
		TimeStr = [ Hour, Sep, Minute, _Unit ]
		;
		TimeStr = [ Hour, Sep, Minute ]
	),
	TimeObj = [ _Day, Hour, Sep, Minute, _Sec ].
*/

%parse_rel_quant( [ DayOfMonth, MonthStr, DotTime ], ResultSec ) :-
% ...?	
	

qualtime(Quant, BigTime) :-
	approxquant(Quant, Sec),
	timeformat( [ _D, _H, _M, Sec ] , BigTime).

approxquant(Quant, Sec) :-
	str2list(Quant, List),
	(
		words(Word, Factor),
		member(Word, List),
		%N = [the word before that word]
		N = 0,
		Sec is N * Factor
	).
		


%qualquant(Qual, Quant) :- var(Quant),
% let's forget this for now..


/* absolute gets converted to relative, or the other way around, whatever is better in code. original value is preserved, since consistency check is forced after EVERY addition.

so: which way around? */



