1?-use_module(library(lists)).    2%:-prolog_flag(compiling,_,profiledcode).
    3:-ensure_loaded('temporalOrderCleaner.pl').    4
    5
    6mapToTotalOrderPlan( Plan, Ordering ,NewOrdering) :-
    7
    8
    9	fetchTemporalReferences(Plan, OrderingMinusAbstractTimepoints),!,
   10
   11	navigatePlanTree(Plan, Ordering, OrderingMinusAbstractTimepoints, t, NewOrdering),
   12	
   13	%We only every want one solution
   14	!.
   15
   16
   17navigatePlanTree(Plan, Ordering, OrderingMinusAbstractTimepoints, Timepoint, NewOrdering) :- 
   18
   19	%Find the timepoint that is constrained to occur before Timepoint
   20	member( before(X, Timepoint), Ordering),
   21	
   22	%Check that this Timepoint occurs in the plan
   23	member( X,  OrderingMinusAbstractTimepoints),
   24	
   25	navigatePlanTree(Plan, Ordering, OrderingMinusAbstractTimepoints, X, NewOrdering).
   26
   27		
   28%Abstract Timepoint has been identified
   29navigatePlanTree(Plan, Ordering, OrderingMinusAbstractTimepoints, Timepoint, NewOrdering) :- 
   30
   31	%Find the timepoint AbstractTimepoint
   32	member( before(AbstractTimepoint, Timepoint), Ordering),
   33	
   34	
   35	\+ member( AbstractTimepoint,  OrderingMinusAbstractTimepoints),
   36	
   37	
   38	%find the left branch and the right branch connected to this abstract timepoint
   39	
   40	%currently only holds for two branches
   41	findall(BranchTimepoint, member( before(BranchTimepoint, AbstractTimepoint), Ordering), [Head, Tail]),
   42	
   43	mergeBranches(Plan, Head, Tail, Timepoint,AbstractTimepoint, Ordering, NewMergedOrdering ),
   44		
   45	navigatePlanTree(Plan, NewMergedOrdering, OrderingMinusAbstractTimepoints, Timepoint, NewOrdering ). 	
   46	
   47navigatePlanTree(Plan, NewOrdering, OrderingMinusAbstractTimepoints, Timepoint, NewOrdering).
   48	
   49
   50	
   51		
   52mergeBranches(Plan, LeftBranch, RightBranch, AbstractTimepointsChild,AbstractTimepoint, Ordering, NewOrdering):-
   53
   54	findEndTimepoint(LeftBranch, Ordering, LeftEndTimepoint),
   55	
   56	removeAbstractTemporalOrderings(Ordering, AbstractTimepoint, OrderingWithoutTimepoint),
   57	
   58	append(OrderingWithoutTimepoint, [before(RightBranch, LeftEndTimepoint)], BuildingNewOrdering ),
   59	
   60	append(BuildingNewOrdering, [before(LeftBranch, AbstractTimepointsChild)], NewOrdering).
   61	
   62
   63	
   64
   65% Search for the last timepoint connected to Branch
   66findEndTimepoint(Branch,Ordering, EndTimepoint):-
   67
   68	member(before(ParentNode,Branch), Ordering),
   69	
   70	findEndTimepoint(ParentNode, Ordering, EndTimepoint).
   71
   72%Last Timepoint has been found	
   73findEndTimepoint(Branch, Ordering, Branch).
   74	
   75	
   76removeAbstractTemporalOrderings(Ordering, AbstractTimepoint, OrderingWithoutTimepoint) :-
   77	findall( before(X,AbstractTimepoint), member(before(X,AbstractTimepoint), Ordering), ChildrenOrderingToDelete ),	
   78	findall( before(AbstractTimepoint, Y), member(before(AbstractTimepoint,Y), Ordering), ParentOrderingToDelete  ),	
   79
   80	deleteList(ChildrenOrderingToDelete, Ordering, OrderingMinusAbstractChildrenTimepoints ),
   81	deleteList(ParentOrderingToDelete,   OrderingMinusAbstractChildrenTimepoints, OrderingWithoutTimepoint ).
   82
   83		
   84deleteList([], Result, Result).	
   85deleteList([Head | Tail], WorkingList, Accum):-
   86	delete(WorkingList, Head, ResultList),
   87	deleteList(Tail, ResultList, Accum).
   88	
   89
   90	
   91% --------------Tests ----------------------
   92/*
   93test1_OrderingWithoutTimepoint(OrderingWithoutTimepoint):- removeAbstractTemporalOrderings([before(t5,t6),before(t1,t2),before(t3,t2), before(t2,t5)],t2, OrderingWithoutTimepoint).
   94
   95test1_FindStartTimepoint :- findStartTimepoint([before(y,x)], Timepoint).	
   96
   97test1_findEndTimepoint(Result):- findEndTimepoint(t1, [before(t2,t1), before(t3,t2),before(t4,t3) ], Result).
   98
   99
  100test1_mapToTotalOrderPlan(Result):- mapToTotalOrderPlan(
  101																					[happens(b,t70,t70),happens(a,t67,t67),happens(c,t66,t66),happens(d,t25,t25)],
  102																					[before(t25,tabstract), before(t66,tabstract), before(t66, t67), before(t70,t66), before(tabstract,t)], 
  103																					Result).
  104*/