View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * ConstraintsModel.java
26  *
27  * The underlying model for the Applicability Constraints Table
28  *****************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.util.*;
33  import javax.swing.*;
34  import javax.swing.table.*;
35  import javax.swing.event.*;
36  
37  import zeus.util.*;
38  import zeus.concepts.*;
39  import zeus.concepts.fn.*;
40  import zeus.generator.*;
41  import zeus.generator.event.*;
42  import zeus.generator.util.*;
43  import zeus.gui.editors.*;
44  
45  public class ConstraintsModel extends AbstractTableModel
46                                implements ChangeListener,
47                                           ValidatingModel,
48                                           RenameListener {
49  
50    static final int CONSTRAINT = 0;
51  
52    protected static final String[] columnNames = { "Constraint" };
53  
54    protected EventListenerList changeListeners = new EventListenerList();
55    protected Vector            data            = new Vector();
56    protected Vector            validityInfo    = new Vector();
57    protected BasicFactModel    preconditionsModel;
58    protected BasicFactModel    postconditionsModel;
59  
60    public ConstraintsModel(BasicFactModel preconditionsModel,
61                            BasicFactModel postconditionsModel,
62                            LogicalFn[] input) {
63  
64       this.preconditionsModel = preconditionsModel;
65       this.postconditionsModel = postconditionsModel;
66       preconditionsModel.addChangeListener(this);
67       postconditionsModel.addChangeListener(this);
68       preconditionsModel.addRenameListener(this);
69       postconditionsModel.addRenameListener(this);
70       reset(input);
71    }
72  
73    public void reset(LogicalFn[] input) {
74       int r = data.size();
75       data.removeAllElements();
76       validityInfo.removeAllElements();
77  
78       if ( r != 0 ) fireTableRowsDeleted(0,r-1);
79  
80       Vector items = getConditions();
81       for(int i = 0; i < input.length; i++ ) {
82          data.addElement(input[i].toString());
83          validityInfo.addElement(isValid(input[i],items));
84       }
85       fireTableRowsInserted(0,input.length-1);
86    }
87    
88    public LogicalFn[] getData() {
89       // Save only valid data
90       Vector valid = new Vector();
91       for(int i = 0; i < data.size(); i++ )
92          if ( validityInfo.elementAt(i).equals(Boolean.TRUE) )
93  	   valid.addElement(data.elementAt(i));
94  
95       LogicalFn[] output = new LogicalFn[valid.size()];
96       for(int i = 0; i < valid.size(); i++ )
97          output[i] = (LogicalFn)ZeusParser.Expression((String)valid.elementAt(i));
98       return output; 
99    }
100 
101   public void removeRows(int[] rows) {
102      for(int i = 0; i < rows.length; i++ ) {
103         data.removeElementAt(rows[i]-i);     
104         validityInfo.removeElementAt(rows[i]-i);     
105         fireTableRowsDeleted(rows[i]-i,rows[i]-i);
106      }
107      fireTableStructureChanged(); // swing bug? force redraw of table
108      fireChanged();
109   }                             
110 
111   public void addNewRow()  {
112      data.addElement("true");
113      Vector items = getConditions();
114      validityInfo.addElement(isValid("true",items));
115      int size = data.size();
116      fireTableRowsInserted(size-1,size-1);
117      fireTableStructureChanged(); // swing bug? force redraw of table
118      fireChanged();
119   }
120 
121   protected Vector getConditions() {
122      Vector items = new Vector();
123 
124      Fact[] facts = preconditionsModel.getData();
125      for(int i = 0; i < facts.length; i++ )
126         items.addElement(facts[i].getId());
127 
128      facts = postconditionsModel.getData();
129      for(int i = 0; i < facts.length; i++ )
130         items.addElement(facts[i].getId());	
131      return items;
132   }
133 
134   protected Boolean isValid(LogicalFn fn, Vector items) {
135      // Note: only a surface check is performed
136      // Thus for expressions of the form "?man.name.firstname"
137      // only the fact that ?man is a valid reference will be checked
138      // type checking of the ".name.firstname" is not performed
139 
140      Vector vars = fn.variables();
141      for(int i = 0; i < vars.size(); i++ )
142         if ( !items.contains(vars.elementAt(i).toString()) )
143 	   return Boolean.FALSE;
144      return Boolean.TRUE;
145   }
146   protected Boolean isValid(String input, Vector items) {
147      ValueFunction fn = ZeusParser.Expression(input);
148      if ( (fn != null) && (fn instanceof LogicalFn) )
149         return isValid((LogicalFn)fn,items);
150      else
151         return new Boolean(false);
152   }
153 
154   
155   // ----------------------------------------------------------------------
156 
157   public int     getColumnCount()                 { return columnNames.length;}
158   public boolean isCellEditable(int row, int col) { return true; }
159   public int     getRowCount()                    { return data.size(); }
160   public String  getColumnName(int col)           { return columnNames[col]; }
161   public boolean isValidEntry(int row, int col)   { return validityInfo.elementAt(row).equals(Boolean.TRUE); }
162 
163   public Object getValueAt (int row, int column)  {
164      switch(column) {
165         case CONSTRAINT:
166              return data.elementAt(row);
167      }
168      return null;
169   }
170 
171   public void setValueAt(Object aValue, int row, int column)  {
172     // prevents the table being accidently loaded with a null value
173     // current table implementation needs this - possibly because of a bug 
174     if (aValue.toString().equals(""))
175       return;
176 
177      String old = (String)data.elementAt(row);
178      String input;
179      Vector items;
180      switch(column) {
181         case CONSTRAINT:
182              input = (String)aValue;          
183              if ( input.equals(old) )
184                 return;
185              else {
186                 data.setElementAt(input,row);
187                 items = getConditions();
188                 validityInfo.setElementAt(isValid(input,items),row);
189                 fireTableCellUpdated(row,column);
190                 fireChanged();
191              }
192              break;
193      }
194   }
195 
196   public void stateChanged(ChangeEvent e) {
197      // Preconditions/Postconditions have changed!
198      // NEED to verify all applicability constraints!!
199      if ( e.getSource() == preconditionsModel ||
200           e.getSource() == postconditionsModel ) {
201         Vector items = getConditions();
202         for(int i = 0; i < data.size(); i++ ) {
203            String s = (String)data.elementAt(i);
204            validityInfo.setElementAt(isValid(s,items),i);
205         }
206         fireTableDataChanged();
207      }
208   }
209   public void nameChanged(RenameEvent e) {
210      // Preconditions/Postconditions ids have changed!
211      // NEED to update all ids in applicability constraints!!
212      String prev = (String)e.getOriginal();
213      String curr = (String)e.getCurrent();
214      for(int i = 0; i < data.size(); i++ ) {
215         String s = (String)data.elementAt(i);
216         data.setElementAt(Misc.substitute(s,prev,curr),i);
217      }
218      fireTableDataChanged();
219   }
220 
221   public void addChangeListener(ChangeListener x) {
222      changeListeners.add(ChangeListener.class, x);
223   }
224   public void removeChangeListener(ChangeListener x) {
225      changeListeners.remove(ChangeListener.class, x);
226   }
227 
228   protected void fireChanged() {
229      ChangeEvent c = new ChangeEvent(this);
230      Object[] listeners = changeListeners.getListenerList();
231      for(int i= listeners.length-2; i >= 0; i -=2) {
232         if (listeners[i] == ChangeListener.class) {
233            ChangeListener cl = (ChangeListener)listeners[i+1];
234            cl.stateChanged(c);
235         }
236      }
237   }
238 }