- Using the Prolog IDE
In addition to the message based interaction of Java and Prolog supported by the queryOne() and queryAll() methods, the PDT connector also supports an event / observer / notification mechanism.
The Java side does not need to poll the Prolog processes to find out when some event occurred but can declare that it is listening for certain events. The Prolog side triggers these events if something relevant happens (e.g. the end of a long running computation). Then the Java side can send a normal queryOnce() / queryAll() to retrieve the results.
First you need to create a PrologEventDispatcher for a PrologInterface. Then add PrologInterfaceListeners to this dispatcher. Every listener can be listening to one or more subjects.
<code java | Excerpt from CurrentPifListener.> currentDispatcher = new PrologEventDispatcher(currentPif,PrologRuntimeUIPlugin.getDefault().getLibraryManager()); currentDispatcher.addPrologInterfaceListener(“pdt_edit_hook”, this); </Code>
The above code creates a listener for the “pdt_edit_hook” event / notification.
When the related notification is sent by the Prolog side, the update() method of each registered PrologInterfaceListener will be called with a parameter of type PrologInterfaceEvent that contains the event identifier (“pdt_edit_hook” in our example) and some arbitrary additional data from the second parameter of the pif_notify/2 call (see below).
To send a notification from a Prolog process, call the pif_notify/2 predicate. Its first argument is an even identifier (which must be an atom) and the second argument an arbitrary value that will be passed to the Java side along with the event.
pif_observe:pif_notify(pdt_edit_hook, 'l:\test.pl 1')
The above tells the Java side that the pdt_edit_hook event occurred for the file 'l:\test.pl'. For an example see the module pdt_editor_edit_hook.