Quickstart
In the following the example jadex.rules.examples.OAVHelloWorld from the directory jadex-rules-applications/src/main/java/jadex/rules/examples/helloworld will be shortly explained explained.Object Type Model
Object type are the foundation for a rule application. These types can be defined in Java as Java Beans or in form of OAV (object attribute value) triples. The latter variant is the preferred way to specify object types, because it avoids several pitfalls that can be encountered when using Java types (concerning changes of objects). In order to define types a type model (container) need to be created (type models can be nested). The types are then created on the model.// Create simple OAV type model. OAVTypeModel helloworld_type_model = new OAVTypeModel("helloworld_type_model"); helloworld_type_model.addTypeModel(OAVJavaType.java_type_model); OAVObjectType message_type = helloworld_type_model.createType("message_type"); final OAVAttributeType message_has_text = message_type.createAttributeType("message_has_text", OAVJavaType.java_string_type);
Rule System Creation
Next, the rules need to be created. For this purpose first a rule system is instantiated.// Create rete system. IOAVState state = new OAVState(helloworld_type_model); // Create the production memory. Rulebase rb = new Rulebase(); RuleSystem rete = new RuleSystem(state, rb, new RetePatternMatcherFunctionality(rb), new LIFOAgenda());
Rule Definition
Then, the rules can be specified using typed variables, (object) conditions and constraints on objects. The action of a rule is defined in plain Java.// Add rule to rulebase. Variable message = new Variable("?message", message_type); ObjectCondition msgcon = new ObjectCondition(message_type); msgcon.addConstraint(new BoundConstraint(null, message)); rete.getRulebase().addRule(new Rule("new_message", msgcon, new IAction() { public void execute(IOAVState state, IVariableAssignments assignments) { Object message = assignments.getVariableValue("?message"); System.out.println("New message found: "+state.getAttributeValue(message, message_has_text)); } }));
Inserting Facts
Now facts can be added to the state.// Add fact. Object m = state.createRootObject(message_type); state.setAttributeValue(m, message_has_text, "Hello OAV (object, attribute, value) World!");
Initialization
Finally the system can be inited and started. The init makes the facts visible to the rete engine. To start the system it is possible to fire rules directly on the engine on the caller's thread. If the system should be run decoupled the RuleSystemExecutor can be used as below. The gui can be used to visualize the state and rete rules.// Initialize rule system. rete.init(); RuleSystemExecutor exe = new RuleSystemExecutor(rete, true); RuleEnginePanel.createRuleEngineFrame(exe, "HelloWorld");
on 02/02/2009 at 21:45