@@ -28,6 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
2828 *
2929 */
3030import java .net .URL ;
31+ import java .util .Iterator ;
3132
3233import javax .json .Json ;
3334import javax .json .JsonBuilderFactory ;
@@ -53,6 +54,11 @@ public final class Pengine {
5354 private final PengineBuilder po ;
5455 @ SuppressWarnings ("unused" )
5556 private final String pengineID ;
57+ private JsonObject availableAnswer = null ; // might have several
58+ // set to true when the pengine is destroyed
59+ // note that we might still have answers held
60+ private boolean destroyed = false ;
61+ private Query currentQuery = null ;
5662
5763 /**
5864 * Create a new pengine object from a set of {@link PengineBuilder}.
@@ -96,6 +102,9 @@ private String create(PengineBuilder po) throws CouldNotCreateException {
96102 con .setRequestProperty ("Content-type" , "application/json" );
97103
98104 String urlParameters = po .getRequestBodyCreate ();
105+
106+ // TODO think this out away from computer
107+ this .currentQuery = po .getAskQuery ();
99108
100109 // Send post request
101110 con .setDoOutput (true );
@@ -116,7 +125,7 @@ private String create(PengineBuilder po) throws CouldNotCreateException {
116125
117126 int responseCode = con .getResponseCode ();
118127 if (responseCode < 200 || responseCode > 299 ) {
119- throw new CouldNotCreateException ("bad response code" + Integer .toString (responseCode ));
128+ throw new CouldNotCreateException ("bad response code (if 500, query was invalid? query threw Prolog exception?) " + Integer .toString (responseCode ));
120129 }
121130
122131 BufferedReader in = new BufferedReader (
@@ -142,72 +151,91 @@ private String create(PengineBuilder po) throws CouldNotCreateException {
142151 JsonReader jr = jrf .createReader (new StringReader (response .toString ()));
143152 JsonObject respObject = jr .readObject ();
144153
154+ // TODO handle answer here
155+ if (respObject .containsKey ("answer" )) {
156+ handleAnswer (respObject .getJsonObject ("answer" ));
157+ }
158+
145159 JsonString eventjson = (JsonString )respObject .get ("event" );
146160 String evtstr = eventjson .getString ();
147161
148- if (!evtstr .equals ("create" )) {
149- throw new CouldNotCreateException ("create request returned an event other than create" );
162+ // TODO store slave_limit and have accessor
163+ if (evtstr .equals ("destroy" )) {
164+ this .destroyed = true ;
165+ } else if (evtstr .equals ("create" )) {
166+ ;
167+ } else {
168+ throw new CouldNotCreateException ("create request event was" + evtstr + " must be create or destroy" );
150169 }
151170
152171 String id = ((JsonString )respObject .get ("id" )).getString ();
153172 return id ;
154173
155174 } catch (IOException e ) {
156175 throw new CouldNotCreateException (e .toString ());
176+ } catch (SyntaxErrorException e ) {
177+ throw new CouldNotCreateException (e .getMessage ());
157178 }
158179 }
159180
160181 /**
161- *
182+ * @param jsonObject
183+ * @throws SyntaxErrorException
162184 */
163- public void dumpStateDebug () {
164- System .err .println (this .pengineID );
185+ private void handleAnswer (JsonObject answer ) throws SyntaxErrorException {
186+ if (answer .containsKey ("event" )) {
187+ switch ( ((JsonString )answer .get ("event" )).getString ()) {
188+ case "success" :
189+ handleData (answer , true );
190+ break ;
191+ case "destroy" :
192+ this .destroyed = true ;
193+ handleData (answer , true );
194+ break ;
195+ case "failure" :
196+ handleData (answer , false );
197+ default :
198+ throw new SyntaxErrorException ("Bad event in answer" + ((JsonString )answer .get ("event" )).getString ());
199+ }
200+ }
201+ /*
202+ *
203+ * "answer":{
204+ "data":{
205+ "event":"failure",
206+ "id":"c7e9e0c5-84b6-4faa-bbcb-1e1139df1206",
207+ "time":0.000019857999999999985
208+ },
209+ "event":"destroy",
210+ "id":"c7e9e0c5-84b6-4faa-bbcb-1e1139df1206"
211+ },
212+ */
213+
165214 }
166- }
167- /*
168- * This project has involved a lot of experimenting, as hte protocol is poorly documented. As such,
169- * I'm retaining my notes here
170- */
171- // <domain>/<path>/pengine/create
172- // application/json
173- // see server_url/4, line 1529 of pengines.pl
174- /*
175- * HTTP/1.1 200 OK
176- Server: nginx/1.4.6 (Ubuntu)
177- Date: Wed, 20 Apr 2016 20:53:39 GMT
178- Content-Type: text/x-prolog; charset=UTF-8
179- Content-Length: 65
180- Connection: close
181-
182- http://www.oracle.com/technetwork/articles/java/json-1973242.html
183-
184- Asks response looks like
185- success('8eb2ec31-fd63-43a2-a80b-4552b6d505d7',
186- [q(b)],
187- 1.1307000000002065e-5,
188- true)
189-
190- the last item is whether there are more
191-
192- then when you get the last one, and it does a destroy (I guess?)
193215
194- destroy('8eb2ec31-fd63-43a2-a80b-4552b6d505d7',
195- success('8eb2ec31-fd63-43a2-a80b-4552b6d505d7',
196- [q(c)],
197- 2.1917999999999244e-5,
198- false))
199-
216+ /**
217+ * @param answer
218+ * @param b
219+ */
220+ private void handleData ( JsonObject answer , boolean b ) {
221+ // TODO Auto-generated method stub
200222
201- from pengines.pl http_pengine_create/1
202-
203- % HTTP POST handler for =/pengine/create=. This API accepts the
204- % pengine creation parameters both as =application/json= and as
205- % =www-form-encoded=.
206-
207- Looks like event_to_json we want the lang to be json-s maybe? certainly need to look into it.
208- For the moment, just json
209-
210-
223+ }
211224
225+ /**
226+ *
227+ */
228+ public void dumpStateDebug () {
229+ System .err .println (this .pengineID );
230+ }
212231
213- */
232+ /**
233+ * @param string
234+ * @param string2
235+ * @return
236+ */
237+ public Iterator <Proof > ask (String string , String string2 ) {
238+ // TODO Auto-generated method stub
239+ return null ;
240+ }
241+ }
0 commit comments