View Javadoc

1   package zeus.generator.agent;
2   
3   import java.util.*;
4   import java.awt.*;
5   import javax.swing.*;
6   import javax.swing.event.*;
7   import javax.swing.border.*;
8   import javax.swing.table.*;
9   import java.awt.event.*;
10  
11  import zeus.util.*;
12  import zeus.concepts.*;
13  import zeus.gui.help.*;
14  import zeus.gui.fields.*;
15  import zeus.generator.*;
16  import zeus.generator.util.*;
17  import zeus.gui.editors.*;
18  
19  /***
20   * Handle all events arising from the TaskAttributePanel, pricipally
21   * selecting a different task or fact row.
22   */
23  public class TaskAttributeListener implements ListSelectionListener {
24  
25    private JTable taskTable;
26    private JTable factTable;
27    private JTable attTable;
28    private TaskModel taskModel;
29    private FactModel factModel;
30    private AttributeModel attModel;
31    private GeneratorModel genModel;
32    private java.util.List restrictions;
33  
34    private String currentTask;
35  
36    public TaskAttributeListener(JTable taskTable, TaskModel taskModel,
37  			       JTable factTable, FactModel factModel,
38  			       JTable attTable, AttributeModel attModel,
39  			       GeneratorModel genModel,AgentDescription agent){
40  
41      this.taskTable = taskTable;
42      this.taskModel = taskModel;
43      this.factTable = factTable;
44      this.factModel = factModel;
45      this.attTable = attTable;
46      this.attModel = attModel;
47  
48      this.genModel = genModel;
49  
50      restrictions = agent.getRestrictions();
51      if(restrictions == null) {
52        restrictions = new Vector();
53      }
54    }
55  
56    /***
57     * Event handling interface.
58     */
59    public void valueChanged(ListSelectionEvent e) {
60  
61      Object source = e.getSource();
62      if(source == taskTable.getSelectionModel()) {
63        taskSelected();
64      }
65      else if(source == factTable.getSelectionModel()) {
66        factSelected();
67      }
68      else if(source == attTable.getSelectionModel()) {
69      }
70    }
71  
72    /***
73     * Fired when a task row is selected. Saves the attribute
74     * restrictions currently active, then loads the new fact table
75     * associated with this task.
76     */
77    private void taskSelected() {
78  
79      if(attTable.getSelectedRow() >= 0) {
80        saveAttributeRestrictions();
81      }
82  
83      int row = taskTable.getSelectedRow();
84  
85      if(row < 0) {
86        return;
87      }
88  
89      String taskName = (String)taskModel.getValueAt(row, TaskModel.TASK);
90      currentTask = taskName;
91      String taskID = genModel.reverseTaskNameLookup(taskName);
92  
93      AbstractTask aTask = genModel.getTask(taskID);
94  
95      if(!Task.class.isAssignableFrom(aTask.getClass())) {
96        //Rulebase tasks cannot be interpreted;
97        factModel.reset(new Fact[0]);
98        return;
99      }
100 
101     Task task = (Task)aTask;
102     
103     Fact[] input = task.getPreconditions();
104     Fact[] output = task.getPostconditions();
105 
106     Fact[] newFacts = new Fact[input.length + output.length];
107 
108     for(int index = 0 ; index < input.length ; index++) {
109       newFacts[index] = input[index];
110     }
111     for(int index = input.length ; index < newFacts.length ; index++) {
112       newFacts[index] = output[index - input.length];
113     }
114 
115     factModel.reset(newFacts);
116     attModel.reset(null);
117   }
118 
119   /***
120    * Fired when a fact row is selected. Saves current attribute
121    * restrictions, loads the attribute table associated with the new
122    * fact, and loads the restrictions into it.
123    */
124   private void factSelected() {
125 
126     if(attTable.getSelectedRow() >= 0) {
127       saveAttributeRestrictions();
128     }
129 
130     int row = factTable.getSelectedRow();
131     if(row < 0) {
132       return;
133     }
134 
135     Fact fact = factModel.getData()[row];
136 
137     attModel.reset(fact);
138     loadAttributeRestrictions();
139   }
140 
141   /***
142    * Save any attribute restrictions in the current attribute
143    * table. Existing design of AttributeModel made it a serious
144    * problem to save the restriction information in there, so it is
145    * extracted and saved here.
146    */
147   private void saveAttributeRestrictions() {
148 
149     for(int row = 0 ; row < attModel.getRowCount() ; row++) {
150       String value = (String)attModel.getValueAt(row,
151 						 AttributeModel.RESTRICTION);
152 
153       if(value != null) {
154 	String factInstance = attModel.getData().getId();
155 
156 	String attName = (String)attModel.getValueAt(row,
157 						     AttributeModel.ATTRIBUTE);
158 
159 	Restriction item = new Restriction(currentTask, factInstance,
160 					   attName, value);
161 
162 	for(int index = 0 ; index < restrictions.size() ; index++) {
163 	  Restriction element = (Restriction)restrictions.get(index);
164 	  if(element.sameTarget(item)) {
165 	    item = (Restriction)restrictions.remove(index);
166 	    item.setRestriction(value);
167 	    break;
168 	  }
169 	}
170 
171 	//If there is no restriction, don't insert restriction item
172 	if(item.getRestriction() == null ||
173 	   item.getRestriction().length() == 0) {
174 	  return;
175 	}
176 
177 	restrictions.add(item);
178       }
179     }
180   }
181 
182   /***
183    * Restore and restrictions that are associated with the current
184    * attribute table.
185    */
186   private void loadAttributeRestrictions() {
187     int factRow = factTable.getSelectedRow();
188     String factInstance = (String)factModel.getValueAt(factRow,
189 						       FactModel.INSTANCE);
190 
191     for(int row = 0 ; row < attModel.getRowCount() ; row++) {
192       String attName = (String)attModel.getValueAt(row,
193 						   AttributeModel.ATTRIBUTE);
194 
195       Restriction item = new Restriction(currentTask, factInstance,
196 						 attName, "");
197 
198       for(Iterator i = restrictions.iterator() ; i.hasNext() ; ) {
199 	Restriction element = (Restriction)i.next();
200 
201 	if(element.sameTarget(item)) {
202 	  attModel.setRestriction(element.getRestriction(), row);
203 	  break;
204 	}
205       }
206     }
207   }
208 
209   /***
210    * Retrieve the list of restrictions. Pricipally used for saving them.
211    */
212   public java.util.List getRestrictions() {
213     return restrictions;
214   }
215 
216 }