Tutorial - Setting swing components SourceForge.net Logo
 Home
 Documentation
 To do
 Download
 Project page
 JSR-94

How to use the rule engine to get/set graphical properties of Swing components

JRuleEngine can be used to set/get graphical properties of java.awt.Component instances. These instances are added to the rule engine.
Objects derived from java.awt.Component have a "name" property that must be setted as the object unique identifier: JRuleEngine internally uses name property to reference a specific Component instance.
"If conditions" and "then actions" can reference Component instances by name property and use these instances to evaluate or set some Component properties.
So you can define graphic components behaviour by means of the rule engine.

Example

The following is an example of XML file which contains a simple rule whose action changes graphic components behaviour:

 <?xml version="1.0" encoding="UTF-8"?>
 <rule-execution-set>
   <name>RuleExecutionSet1</name>
   <description>Rule Execution Set</description>
 
   <!--
   If the credit limit of the customer is greater or equals to the amount of the
   invoice and the status of the invoice is "unpaid" then
   decrement the credit limit with the amount of the invoice and
   set the status of the invoice to "paid".
   -->
   <rule name="Rule1" description="credit limit control rule" >
   <if leftTerm="creditLimitControl.getText" op=">=" rightTerm="amountControl.getText" />
    <if leftTerm="statusControl.getStatus" op="=" rightTerm="unpaid" />
    <then method="creditLimitControl.decrementCreditLimit" arg1="amountControl.getText" />
    <then method="statusControl.setStatus" arg1="paid" />
   </rule>
 
 </rule-execution-set>


This rule can be used inside a JFrame window which contains four components:

  • a credit limit input control, identified by the name property "creditLimitControl"
    This control has a getText and decrementCreditLimit methods, called by the rule engine
  • an amount input control, identified by the name property "amountControl"
    This control has a getText method, called by the rule engine
  • a payment status combo-box control, identified by the name property "statusControl"
    This control has getStatus and setStatus methods, used to change combo items and called by the rule engine
  • a button, used to call the rule engine, passing to it the three input controls
Notice that the three input controls are not simple JTextField components, because they need some business methods: they derive from JTextField/JComboBox components, so they inherit name property and any other Component property.
Notice that you can directly add any number of JTextField component: you only have to respect the rule that each component has a unique name property (a unique identifier).

The following is a sample java code that calls the rule engine by passing to it the three components:

 public class CreditLimitFrame extends JFrame {
 
   CreditLimitControl creditLimitControl = new CreditLimitControl(); // derive from JTextField
   StatusControl statusControl = new StatusControl(); // derive from JComboBox
   JTextField amountControl = new JTextField();
 
   public CreditLimitFrame() {
    super("Customer Credit Limit");
 
    // set name property on each input control...
    creditLimitControl.setName("creditLimitControl");
    amountControl.setName("amountControl");
    statusControl.setName("statusControl");
 
    // ...
   }
 
   // method called the user clicks on the button:
   // rule engine will be called...
   void calcButton_actionPerformed(ActionEvent e) {
    try {
     // add input controls as rule engine input objects
     controller.getStatefulRuleSession().addObject(amountControl);
     controller.getStatefulRuleSession().addObject(creditLimitControl);
     controller.getStatefulRuleSession().addObject(statusControl);
 
     controller.getStatefulRuleSession().executeRules();
 
     // ...


(c) 2006 by Mauro Carniel