34
35:- module(pce_util,
36 [ get_object/3, 37 get_object/4,
38 get_object/5,
39 get_object/6,
40 get_object/7,
41 get_object/8,
42
43 send_list/2, 44 send_list/3, 45
46 get_chain/3, 47 chain_list/2, 48
49 default/3 50 ]). 51
52
53:- meta_predicate
54 get_object(+, :, -),
55 get_object(+, :, +, -),
56 get_object(+, :, +, +, -),
57 get_object(+, :, +, +, +, -),
58 get_object(+, :, +, +, +, +, -),
59 get_object(+, :, +, +, +, +, +, -),
60
61 send_list(:, +),
62 send_list(:, +, +),
63 send_list1(:, +),
64 send_list1(:, +, +),
65
66 get_chain(+, :, -). 67
68
69:- use_module(library(pce)). 70
77
78get_object(Obj, Sel, Out) :-
79 get(Obj, Sel, R),
80 get_to_object(R, Out).
81get_object(Obj, Sel, A1, Out) :-
82 get(Obj, Sel, A1, R),
83 get_to_object(R, Out).
84get_object(Obj, Sel, A1, A2, Out) :-
85 get(Obj, Sel, A1, A2, R),
86 get_to_object(R, Out).
87get_object(Obj, Sel, A1, A2, A3, Out) :-
88 get(Obj, Sel, A1, A2, A3, R),
89 get_to_object(R, Out).
90get_object(Obj, Sel, A1, A2, A3, A4, Out) :-
91 get(Obj, Sel, A1, A2, A3, A4, R),
92 get_to_object(R, Out).
93get_object(Obj, Sel, A1, A2, A3, A4, A5, Out) :-
94 get(Obj, Sel, A1, A2, A3, A4, A5, R),
95 get_to_object(R, Out).
96
97get_to_object(Ref, Object) :-
98 ( atomic(Ref)
99 -> Object = Ref
100 ; object(Ref, Object)
101 ).
102
103
108
109send_list(X, _) :-
110 var(X),
111 throw(error(instantiation_error, _)).
112send_list(_, X) :-
113 var(X),
114 throw(error(instantiation_error, _)).
115pce_ifhostproperty(prolog(quintus), [],
116( send_list([], _) :- !)).
117send_list(_, []) :- !.
118pce_ifhostproperty(prolog(quintus), [],
119( send_list([Object|Objects], Selectors) :- !,
120 send_list(Object, Selectors),
121 send_list(Objects, Selectors))).
122send_list(Object, [Selector|Selectors]) :-
123 !,
124 send_list(Object, Selector),
125 send_list(Object, Selectors).
126send_list(Object, Selector) :-
127 send_list1(Object, Selector).
128
129send_list1(Module:Obj, Selector) :-
130 atom(Module),
131 !,
132 send_list_module(Obj, Selector, Module).
133send_list1(Object, Selector) :-
134 send(Object, Selector).
135
136send_list_module([], _, _) :- !.
137send_list_module(_, [], _) :- !.
138send_list_module([Object|Objects], Selectors, Module) :-
139 !,
140 send_list_module(Object, Selectors, Module),
141 send_list_module(Objects, Selectors, Module).
142send_list_module(Object, [Selector|Selectors], Module) :-
143 !,
144 send_list_module(Object, Selector, Module),
145 send_list_module(Object, Selectors, Module).
146send_list_module(Object, Selector, Module) :-
147 send(Object, Module:Selector).
148
149
154
155send_list(X, _, _) :-
156 var(X),
157 throw(error(instantiation_error, _)).
158send_list(_, X, _) :-
159 var(X),
160 throw(error(instantiation_error, _)).
161send_list(_, _, X) :-
162 var(X),
163 throw(error(instantiation_error, _)).
164pce_ifhostproperty(prolog(quintus), [],
165( send_list([], _, _) :- !)).
166send_list(_, [], _) :- !.
167send_list(_, _, []) :- !.
168pce_ifhostproperty(prolog(quintus), [],
169( send_list([Object|Objects], Selectors, Arguments) :- !,
170 send_list(Object, Selectors, Arguments),
171 send_list(Objects, Selectors, Arguments))).
172send_list(Objects, [Selector|Selectors], Arguments) :-
173 !,
174 send_list(Objects, Selector, Arguments),
175 send_list(Objects, Selectors, Arguments).
176send_list(Object, Selector, [Argument|Arguments]) :-
177 !,
178 send_list(Object, Selector, Argument),
179 send_list(Object, Selector, Arguments).
180send_list(Object, Selector, Argument) :-
181 send_list1(Object, Selector, Argument).
182
183send_list1(Module:Obj, Selector, Arg) :-
184 atom(Module),
185 !,
186 send_list_module(Obj, Selector, Arg, Module).
187send_list1(Obj, Selector, Arg) :-
188 send(Obj, Selector, Arg).
189
190send_list_module([], _, _, _) :- !.
191send_list_module(_, [], _, _) :- !.
192send_list_module(_, _, [], _) :- !.
193send_list_module([Object|Objects], Selectors, Arguments, Module) :-
194 !,
195 send_list_module(Object, Selectors, Arguments, Module),
196 send_list_module(Objects, Selectors, Arguments, Module).
197send_list_module(Objects, [Selector|Selectors], Arguments, Module) :-
198 !,
199 send_list_module(Objects, Selector, Arguments, Module),
200 send_list_module(Objects, Selectors, Arguments, Module).
201send_list_module(Object, Selector, [Argument|Arguments], Module) :-
202 !,
203 send_list_module(Object, Selector, Argument, Module),
204 send_list_module(Object, Selector, Arguments, Module).
205send_list_module(Object, Selector, Argument, Module) :-
206 send(Object, Module:Selector, Argument).
207
208
213
214get_chain(Object, Selector, List) :-
215 get(Object, Selector, Chain),
216 chain_list(Chain, List).
217
218
223
224chain_list(Chain, List) :-
225 nonvar(Chain),
226 !,
227 ( Chain == @nil
228 -> List = []
229 ; to_object(Chain, ChainObject),
230 send(ChainObject, instance_of, chain),
231 ( send(ChainObject, current_no, 1)
232 -> chain_to_list_(ChainObject, List)
233 ; List = []
234 )
235 ).
236chain_list(Chain, List) :-
237 new(Chain, chain),
238 send_list(Chain, append, List).
239
240chain_to_list_(Chain, [El|Rest]) :-
241 get(Chain, next, El),
242 !,
243 chain_to_list_(Chain, Rest).
244chain_to_list_(Chain, []) :-
245 \+ get(Chain, current, _).
246
247to_object(Ref, Ref) :-
248 object(Ref),
249 !.
250to_object(Term, Obj) :-
251 new(Obj, Term).
252
253
254 257
262
263default(@default, Default, Value) :-
264 !,
265 ( var(Default)
266 -> Value = Default
267 ; ( Default = class_variable(Obj, Name)
268 ; Default = resource(Obj, Name)
269 )
270 -> ( get(Obj, class_variable_value, Name, Value)
271 -> true
272 ; pce_error(get_class_variable_failed(Name, Obj))
273 )
274 ; Value = Default
275 ).
276default(Value, _Default, Value)