19
21
22:- meta_predicate(memorize_edit(3,*,*,*)). 23memorize_edit(Pred3, Figment, M0, M2) :- assertion(\+ is_list(Figment)),
24 Figment =.. [Name,Value], OldFigment =.. [Name,OldValue],
25 (forget(OldFigment, M0, M1)
26 -> ( call(Pred3, OldValue,Value,NewValue), NewFigment =.. [Name,NewValue])
27 ; (NewFigment=Figment, M0=M1)),
28 memorize(NewFigment, M1, M2).
29
30memorize_appending(Figment, M0, M2) :- memorize_edit(append,Figment, M0, M2).
31
33memorize(Figment, M0, M1) :- assertion(\+ is_list(Figment)), notrace(append([Figment], M0, M1)).
35forget(Figment, M0, M1) :- select(Figment, M0, M1).
36forget_always(Figment, M0, M1) :- select_always(Figment, M0, M1).
39thought(Figment, M) :- declared(Figment, M).
40
41
42in_agent_model(Agent, Fact, State):- in_model(Fact, State)*-> true ; (agent_thought_model(Agent, ModelData, State), in_model(Fact, ModelData)).
43
44in_model(E, L):- quietly(in_model0(E, L)).
45in_model0(E, L):- \+ is_list(L),declared_link(declared, E, L).
46in_model0(E, L):- compound(E),E = holds_at(_,_),!, member(E, L).
47in_model0(E, L):- member(EE, L), same_element(EE,E).
48same_element(E, E) :- !.
49same_element(holds_at(E,T), E):- nonvar(T).
50
51
52
53:- defn_state_getter(agent_thought_model(agent,model)). 54agent_thought_model(Agent, ModelData, M0):- var(M0), get_advstate(State),!, member(memories(Agent,M0),State), agent_thought_model(Agent, ModelData, M0).
55agent_thought_model(Agent, ModelData, M0):- \+ is_list(M0), !, declared_link(agent_thought_model(Agent), ModelData, M0).
56agent_thought_model(Agent, ModelData, M0) :- memberchk(inst(Agent),M0), ModelData = M0, !.
57agent_thought_model(Agent, ModelData, M0):- declared(memories(Agent,M1),M0),!,
58 agent_thought_model(Agent, ModelData, M1).
59
60
61
64
66
68update_relation( NewHow, Item, NewParent, Timestamp, M0, M2) :-
69 remove_old_info( NewHow, Item, NewParent, Timestamp, M0, M1),
70 append([(h(NewHow, Item, NewParent))], M1, M2).
71
72remove_old_info( _NewHow, '<mystery>'(_, _, _), _NewParent, _Timestamp, M0, M0) :- !.
73remove_old_info( _NewHow, Item, _NewParent, _Timestamp, M0, M2) :-
74 select_always((h(_OldHow, Item, _OldWhere)), M0, M1),
75 select_always(h(_OldHow2, Item, _OldWhere2), M1, M2).
76
77
78remove_children(_At, '<mystery>'(_, _, _), _Object, _Timestamp, M0, M0):- !.
79remove_children( At, _, Object, Timestamp, M0, M2):-
80 select((h(At, _, Object)), M0, M1), !,
81 remove_children( At, _, Object, Timestamp, M1, M2).
82remove_children( _At, _, _Object, _Timestamp, M0, M0).
83
85
86update_relations(Prep, '<mystery>'(How,What,Object2), Object, Timestamp, M0, M1):-
87 \+ in_model((h(What, _Child, Object2)), M0),
88 89 update_relation( Prep, '<mystery>'(How,What,Object2), Object, Timestamp, M0, M1).
90
91update_relations(_NewHow, '<mystery>'(_,_,_), _NewParent, _Timestamp, M, M).
92update_relations(_NewHow, [], _NewParent, _Timestamp, M, M).
93update_relations( NewHow, [Item|Tail], NewParent, Timestamp, M0, M2) :-
94 update_relation( NewHow, Item, NewParent, Timestamp, M0, M1),
95 update_relations( NewHow, Tail, NewParent, Timestamp, M1, M2).
96
97
100realize_model_exit(At, From, _Timestamp, M0, M2) :-
101 forget((h(exit(At), From, To)), M0, M1),
102 append([(h(exit(At), From, To))], M1, M2).
103realize_model_exit(At, From, _Timestamp, M0, M1) :-
104 append([(h(exit(At), From, '<mystery>'(exit, At, From)))], M0, M1).
105
106update_model_exit(At, From, To, _Timestamp, M0, M2) :-
107 select_always((h(exit(At), From, _To)), M0, M1),
108 append([(h(exit(At), From, To))], M1, M2).
109
110
112update_model_exits([], _From, _T, M, M).
113update_model_exits([Exit|Tail], From, Timestamp, M0, M2) :-
114 realize_model_exit(Exit, From, Timestamp, M0, M1),
115 update_model_exits(Tail, From, Timestamp, M1, M2).
116
117update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M0, M2) :-
118 \+ in_model(h(exit(ExitNameReversed), Here, _There), M0),
119 realize_model_exit(ExitNameReversed, Here, Timestamp, M0, M1),
120 update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M1, M2).
121
127
128update_model(Knower, arriving(Agent, At, Here, _, ExitNameReversed), Timestamp, Mem, M0, M2) :- Knower == Agent,
129 130 must(in_model(h(_Was, Agent, There), M0)),
131 132 133 134 must(append(RecentMem, [attempting(go_dir(Agent, _, ExitName))|OlderMem], Mem)), 135 \+ member(attempting(go_dir(Agent, _, _)), RecentMem), 136 memberchk(timestamp(_T1,_OldNow), OlderMem), 137 138 139 update_model_exit(ExitName, There, Here, Timestamp, M0, M11), 140 update_model_exit(ExitNameReversed, Here, There, Timestamp, M11, M1),
141 update_relation(At, Agent, Here, Timestamp, M1, M2), !. 142
143update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M0, M2) :-
144 \+ in_model(h(In, Agent, Here), M0),
145 update_relations( In, [Agent], Here, Timestamp, M0, M1),
146 update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M1, M2).
147
148update_model(_Agent, moved(_Doer, _How, Object,_From, At, To), Timestamp, _Mem, M0, M1) :-
149 update_relation(At, Object, To, Timestamp, M0, M1).
150
151update_model(Agent, Event, Timestamp, Memory, M0, M2) :- fail,
152 implications(event , ( Event), Preconds, Postconds),
153 (satisfy_each(preCond(_),Preconds, M0, _) ->
154 satisfy_each(postCond(_),Postconds, M0, M1) -> M0\=@= M1), !,
155 update_model(Agent, Event, Timestamp, Memory, M1, M2).
156
157
158update_model(Agent, carrying(Agent, Objects), Timestamp, _Memory, M0, M1) :-
159 update_relations( held_by, Objects, Agent, Timestamp, M0, M1).
160update_model(Agent, wearing(Agent, Objects), Timestamp, _Memory, M0, M1) :-
161 update_relations( worn_by, Objects, Agent, Timestamp, M0, M1).
162
163update_model(Agent, percept(Agent, _Sense, _Depth, child_list(Object, At, Children)), Timestamp, _Mem, M0, M2) :-
164 must_det((remove_children( At, Children, Object, Timestamp, M0, M1),
165 update_relations( At, Children, Object, Timestamp, M1, M2))).
166update_model(Agent, percept_props(Agent, _Sense, Object, _Depth, PropList), _Stamp, _Mem, M0, M2) :-
167 apply_mapl_rest_state(updateprop(Object), PropList, [], M0, M2).
168update_model(_Agent, props(Object, PropList), _Stamp, _Mem, M0, M2) :-
169 apply_mapl_rest_state(updateprop(Object), PropList, [], M0, M2).
170
171
172update_model(Agent, percept(Agent2,_,_,_Info), _Timestamp, _Mem, M0, M0):- Agent \=@= Agent2, !.
174update_model(Agent, percept(Agent,_,_,exit_list(in, Here, ExitRelations)), Timestamp, _Mem, M0, M4) :-
175 update_model_exits(ExitRelations, Here, Timestamp, M0, M4).
176
178update_model(Agent, percept(Agent, _Sense, child_list(_Depth, Here, Prep, Objects)), Timestamp, _Mem, M0, M3):- !,
179 update_relations(Prep, Objects, Here, Timestamp, M0, M3).
180
181update_model(_Agent, [], _Timestamp, _Memory, M, M).
182update_model(Agent, [Percept|Tail], Timestamp, Memory, M0, M2) :-
183 update_model(Agent, Percept, Timestamp, Memory, M0, M1),
184 update_model( Agent, Tail, Timestamp, Memory, M1, M2),!.
185update_model(_Agent, failure(_,_), _Timestamp, _Mem, M0, M0) :- !.
186update_model(_Agent, success(_,_), _Timestamp, _Mem, M0, M0) :- !.
187update_model(_Agent, failure(_), _Timestamp, _Mem, M0, M0) :- !.
188update_model(_Agent, emoted(_,_,_,_), _Timestamp, _Mem, M0, M0) :- !.
189update_model(_Agent, emote(_,_,_,_), _Timestamp, _Mem, M0, M0) :- !.
190update_model(_Agent, msg(_), _Timestamp, _Mem, M0, M0) :- !.
191
192update_model(Agent, time_passes(Target), Timestamp, _Memory, M, M):-
193 nop(bugout1(unused_update_model(Agent, time_passes(Target), Timestamp, M))).
194
195update_model(Agent, Percept, Timestamp, _Memory, M, M):-
196 nop(bugout1(failed_update_model(Agent, Percept, Timestamp), model)).
197
198
199well_remembered(none).
200
201maybe_remember(Percept, M0, M0):- functor(Percept,F,_),well_remembered(F),!.
203maybe_remember(Percept, M0, M1):- append([Percept], M0, M1).
204
205each_update_model(_Agent, [], _Timestamp, _Memory, M, M).
206each_update_model( Agent, [Percept|Tail], Timestamp, Memory, M0, M3) :-
207 maybe_remember(Percept, M0, M1),
208 update_model(Agent, Percept, Timestamp, Memory, M1, M2),
209 each_update_model( Agent, Tail, Timestamp, Memory, M2, M3)