From: Jean-Marc V. <jea...@gm...> - 2012-03-13 18:26:18
|
Hi all I experimented a way to leverage on Drools to automatically retract GUI components and content when some domain fact triple is retracted from the Knowledge Base. We leverage on Drools thruth maintenance (alias logicial insert) . How to test ( use snapshot<http://eulergui.svn.sourceforge.net/viewvc/eulergui/trunk/eulergui/html/documentation.html#Install1>) : 1. Open test/n3JavaMapping-GUI.n3p<http://eulergui.svn.sourceforge.net/viewvc/eulergui/trunk/eulergui/test/n3JavaMapping-GUI.n3p> 2. run Drools engine ==> a new Window with "data" appears 3. Open N3 shell 4. paste this in N3 shell: @prefix kb: <http://deductions.sf.net/ontology/knowledge_base.owl#> . _:d kb:retract ( <http://test#x> <http://test#p> "data" ) . ==> in new Window , "data" disappears How it works: There is a Swing DefaultListModel associated to the URI :listModel . Then the rule says: whenever there is a triple :x :p ?V , add ?V to the ListModel . :listModel a java:javax-swing-DefaultListModel. { :x :p ?V . } => { :listModel javam:addElement ( ?V ). } . The DefaultListModel is added in a JList object itself added to a JFrame ( standard Swing technology, just wrapped in N3 ) : :JFRAME a java:javax-swing-JFrame . :JLIST a java:javax-swing-JList . :JLIST javam:setModel ( :listModel ) . :JFRAME javam:add :JLIST . :JFRAME javam:setSize ( 200 200 ) . :JFRAME java:visible true . So the scene is set at step 2. As a by-product, this has added to the KB an object DeferredMethodCall that represents the addElement() method call in the consequent of the rule. This is N3 + Drools engine machinery ( BTW it allows to make calls deferred until both subject and arguments are bound ) . Now, when the triple :x :p "data" is retracted, Drools automatically retracts the DeferredMethodCall object representing the addElement() method call: - because the antecedent of the rule matches the retraction, - and because of thruth maintenance ( logicial insert ). We added a class JavaObjectsRetractionListener implementing WorkingMemoryEventListener which is called when the DeferredMethodCall object is retracted. The listener calls removeElement when the method was addElement (also another common pattern : remove and add methods names). So the element "data" is removed from DefaultListModel, which removes the list item in the window by standard Swing Model - View design pattern. So, this amounts to "monitor" in real time in the GUI additions and removals of N3 pattern: :x :p ?V . Of course the monitored pattern can be as complex as needed, or :x :p ?V. can be a consequence by other rules. -- Jean-Marc Vanel Déductions SARL - Consulting, services, training, Rule-based programming, Semantic Web http://jmvanel.free.fr/ - EulerGUI, a turntable GUI for Semantic Web + rules, XML, UML, eCore, Java bytecode +33 (0)6 89 16 29 52 chat : irc://irc.freenode.net#eulergui |