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  package zeus.rete.action;
24  import zeus.rete.*;
25  import zeus.concepts.fn.*; 
26  import zeus.concepts.*;
27  import zeus.util.*;
28  import java.util.*;
29  /*** 
30      
31      @author Simon Thompson
32      @since 1.1
33      */
34  public class  ModifyAction extends ReteAction{
35      
36      /***
37          An action that gets a fact and then modifies it before putting it back
38          @see ReteAction
39          */
40      public void executeAction (Action a, Info info) { 
41                ValueFunction type_var = ((VarFn)a.head).resolve(info.getBindings());
42                Assert.notFalse(type_var.getID() == ValueFunction.TYPE);
43                boolean found = false;
44                for(int k = 0; !found && k < info.getInput().size(); k++ ) {
45                   Fact f1 = (Fact)info.getInput().elementAt(k);
46                   found = (f1.functor()).equals(type_var);
47                   if ( found ) {
48                      Core.DEBUG(2," <== " + f1);
49                      retract(f1);
50                      Enumeration enum = a.table.keys();
51                      while( enum.hasMoreElements() ) {
52                         String attribute = (String)enum.nextElement();
53                         ValueFunction value = (ValueFunction)a.table.get(attribute);
54                         value = value.resolve(info.getBindings());
55                         f1.setValue(attribute,value);
56                      }
57                      Core.DEBUG(2," ==> " + f1);
58                      assertFact(f1);
59                   }
60                }
61                Assert.notFalse(found);
62      }
63      
64      
65    private void retract(Fact f1) {
66        if ( context == null )
67           engine.update(Node.REMOVE,f1);
68        else
69           context.ResourceDb().del(f1);
70     }
71      
72      
73     public void assertFact(Fact f1) {
74        if ( context == null ) // i.e. a local engine
75           engine.update(Node.ADD,f1);
76        else
77           context.ResourceDb().add(f1);
78     }
79  }