JACK——BOM Exercise4

来源:http://aosgrp.com/

 

Exercise 4

Use a beliefset callback method.

 

Introduction

In this exercise you add an order beliefset to your Planner agent and use a beliefset callback method to post a FindSubcomponent event to the agent. This will result in a list of subcomponents being printed for the required new component.

 

Instructions

1. Use the JDE to create a new beliefset type called Orders that belongs to the bom package. It will have one key field (String orderId) and three non-key fields (String component, int numberRequired, String date). Add at least one query to the beliefset.

2. Open the Planner_AD design diagram and

  • Drag a new named data component from the design palette onto the design diagram canvas. Call the named data orders and set its type to be bom.Orders.
  • Create a private link from the Planner agent to the orders named data.

3. Modify the Planner agent so that it contains a new method addOrder(String orderId, String component, int numberRequired, String dueDate) that will add a new order into the orders beliefset. Note that the add() method may throw a BeliefSetException, so add must be invoked from inside a try/catch block. You will also need to import aos.jack.jak.beliefset.BeliefSetException. Your addOrder() method should be similar to the following:

public void addOrder(String id, String comp, int n, String dd)

{

try {

orders.add(id,comp,n,dd);

}

catch (BeliefSetException e) {

System.err.println("error entering data in orders db");

System.exit(1);

}

}

4. Edit the Orders beliefset type and

  • add a #posts event FindSubcomponent ev; declaration;
  • Add an indexed query that accepts the order ID (String), component type (logical String), number of components required (logical int) and the order date (logical String).
  • Add a newfact callback to the orders beliefset. Each time a new fact is added to the beliefset, the callback will print a trace message to indicate that a new order has been added to the beliefset and then post a FindSubcomponent event. An example of a beliefset that includes a newfact callback can be found in the Politician example in the introductory notes.

5. Modify the main program so that it:

  • includes the following import statement:

import java.util.*;

  •  no longer invokes the Planner agent's findSubcomponent method;
  •  no longer exits; and
  • reads in the order data from a datafile and invokes the Planner agent's addOrder method to add the details to the beliefset. Sample code for this follows:

StringTokenizer tokens;

String record;

String t1,t2,t3,t4;

BufferedReader datafile = null;

Planner planner = new Planner("planner1","bom.dat");

try {

datafile = new BufferedReader(new FileReader("orders.dat"));

}

catch (FileNotFoundException e) {

System.err.println("unable to open file "+e.toString());

System.exit(1);

}

try {

while ( (record = datafile.readLine()) != null ) {

tokens = new StringTokenizer(record);

t1 = tokens.nextToken();

t2 = tokens.nextToken();

t3 = tokens.nextToken();

t4 = tokens.nextToken();

planner.addOrder(t1,t2,Integer.parseInt(t3),t4);

}

}

catch (Exception e) {

System.err.println("error reading orders data ");

System.exit(1);

}

6. Create a data file that contains several orders. At least one order should be for a component that has four or more subcomponents.

7. Compile and run the program.

 

示例程序

运行结果

(1) a1 added to orders db:

(2) chair 10 30/01/05

(3) a2 added to orders db:

(4) table 50 20/02/05

(5) a3 added to orders db:

(6) cupboard 60 20/03/05

(7) seat is a subcomponent of chair

(8) back is a subcomponent of chair

(9) arm is a subcomponent of chair

(10) leg is a subcomponent of chair

(11) leg is a subcomponent of table

(12) top is a subcomponent of table

(13) cabinet is a subcomponent of cupboard

(14) door is a subcomponent of cupboard

(15) leg is a subcomponent of cupboard

(16) drawer is a subcomponent of cupboard

 

运行结果分析

在main方法中调用Planner Agent的初始化方法,读取bom.dat的数据文件,形成BOM信念集。读取文件orders.dat的数据文件,通过add()方法,添加形成Orders的信念集,add()方法会自动触发Orders信念集的newFact()方法,获取元组信息,打印输出(1)、(2)、(3)、(4)、(5)、(6),并触发FindSubcomponent事件(InferenceGoalEvent-执行所有可应用的规划,以及一个规划的所有可能的绑定实例),比如第一个是chair,那么查找chair的所有子部件,输出(7)、(8)、(9)。后面的类似。

你可能感兴趣的:(bom)