31
32:- include(env_gen). 33
34name_dev(javaswing).
35
36:- set_debug_level(3). 37
38initializeInterfaces(L) :-
39 printKbInstructions,
40 ground(L),
41 set_debug_level(3),
42 report_message(system(1), 'Building JAVA SWING Configuration'),
43 report_message(system(1), 'Building JAVA SWING Completed!'),
44 45 member([ipswing,SIP], L),
46 string_to_atom(SIP, IP),
47 member([portswing, SP], L),
48 string_to_number(SP, Port),
49 report_message(system(0), 'INITIALIZING INTERFACES!'),
50 report_message(system(1), ['Connecting to JAVA SWING interface at ',IP,'/',Port]),
51 initializeJavaSwing(IP, Port),
52 report_message(system(0), 'INITIALIZATION COMPLETED!').
53
54
55finalizeInterfaces(_) :-
56 report_message(system(0), 'FINALIZING INTERFACES!'),
57 finalizeJavaSwing(_,_),
58 59 report_message(system(0), 'FINALIZATION COMPLETED!').
60
61
62
63
64
65
66
67initializeJavaSwing(Host, Port):-
68 report_message(system(3), ['Establishing connection to JAVA SWING:',Host,'/',Port]), !,
69 socket(internet, stream, comm_java),
70 connect(comm_java, Host/Port),
71 assert(listen_to(socket, comm_java, comm_java)),
72 report_message(system(1), 'Connection to JAVA SWING port established successfully').
73
74finalizeJavaSwing(_, _) :-
75 report_message(system(3), ['Disconnecting from JAVA SWING']), !,
76 listen_to(socket, comm_java, comm_java), !, 77 send_command_to_swing(end, _),
78 sleep(1),
79 closeJavaSwingCom,
80 report_message(system(1), 'Connection to JAVA SWING port disconnected successfully').
81
82finalizeJavaSwing(_, _). 83
84closeJavaSwingCom :-
85 retract(listen_to(socket, comm_java, comm_java)), 86 close(comm_java).
87
88handle_stream(comm_java) :-
89 read_response_from_swing(Data),
90 string_to_atom(Data, A),
91 (A = end_of_file ->
92 93 closeJavaSwingCom
94 ;
95 report_exog_event(A, _)
96 ).
97
98
99
100
101execute(Action, T, _, Sensing) :-
102 member(T, [sensing, simsensing]), !,
103 report_message(action, ['Executing sensing action: *',Action,'*']),
104 send_command_to_swing(' ------------> Enter Sensing value, terminate with ".": '),
105 read_response_from_swing(Sensing), nl.
106
107execute(Action, _, _, ok) :-
108 report_message(action, ['Executing non-sensing action: *',Action,'*']).
109
110:- type_prolog(ecl) ->
111 set_event_handler(170, my_system_error_handler/2) ; true. 112
113my_system_error_handler(E, Goal) :-
114 (
115 errno_id(`Interrupted system call`),
117 restartable_builtin(Goal)
118 ->
119 call(Goal)
120 ;
121 errno_id(M),
122 report_message(error, M),
123 read(_),
124 error(default(E), Goal)
125 ).
126
128restartable_builtin(accept(_,_,_)).
129restartable_builtin(cd(_)).
130restartable_builtin(close(_)).
131restartable_builtin(connect(_,_)).
132restartable_builtin(select_stream(_,_,_)).
133restartable_builtin(stream_select(_,_,_)).
134restartable_builtin(wait(_,_)).
135
139
140
141
142send_command_to_swing(_, ok) :- \+ javaSwingOn, !.
143send_command_to_swing(Command, Response) :-
144 any_to_string(Command, SCommand),
145 write(comm_java, SCommand),
146 nl(comm_java),
147 flush(comm_java), !, Response=ok.
149send_command_to_swing(_, failed).
150
152read_response_from_swing(_) :- \+ javaSwingOn, !.
153read_response_from_swing(Command) :-
154 read_string(comm_java, end_of_line,_, Command).
155
156
158javaSwingOn :- listen_to(socket, comm_java, comm_java).
159
160
161
162
163
164printKbInstructions :-
165 writeln('*********************************************************'),
166 writeln('* NOTE: This is the JAVA SWING SIMULATOR environment*****'),
167 writeln('*created by STEFANO VALENTINI ***************************'),
168 writeln('*********************************************************'), nl.
169
170
171