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  package zeus.concepts;
25  
26  import java.io.*;
27  import java.util.*;
28  import zeus.util.*;
29  
30  
31  public class StrategyInfo {
32     public static final int USE    = 0;
33     public static final int NO_USE = 1;
34  
35     public static final String DEFAULT_INITIATOR_STRATEGY =
36        SystemProps.getProperty("system.strategy.initiator.default",
37                                "NO_DEFAULT_STRATEGY_AVAILABLE");
38     public static final String DEFAULT_RESPONDENT_STRATEGY =
39        SystemProps.getProperty("system.strategy.respondent.default",
40                                "NO_DEFAULT_STRATEGY_AVAILABLE");
41  
42     protected Fact      fact;
43     protected String    strategy = null;
44     protected Vector    agents = new Vector();
45     protected Vector    relations = new Vector();
46     protected int       type = USE;
47     protected Hashtable parameters = new Hashtable();
48  
49     public StrategyInfo () { 
50     }
51  
52  
53     public StrategyInfo(Fact fact) {
54        this.fact = new Fact(fact);
55        this.type = NO_USE;
56     }
57  
58     public StrategyInfo(Fact fact, String strategy) {
59        this.fact = new Fact(fact);
60        this.type = USE;
61        this.strategy = strategy;
62     }
63  
64     public StrategyInfo(Fact fact, String strategy,
65                         String[] agents, String[] relations, Hashtable param) {
66        this.fact = new Fact(fact);
67        this.type = USE;
68        this.strategy = strategy;
69        setAgents(agents);
70        setRelations(relations);
71        setParameters(param);
72     }
73  
74     public StrategyInfo(Fact fact, String[] agents, String[] relations) {
75        this.fact = new Fact(fact);
76        this.type = NO_USE;
77        setAgents(agents);
78        setRelations(relations);
79     }
80  
81     public StrategyInfo(StrategyInfo info) {
82        fact = new Fact(info.getFact());
83        type = info.getType();
84        if ( type == USE ) {
85           strategy = info.getStrategy();
86           setParameters(info.getParameters());
87        }
88        setAgents(info.getAgents());
89        setRelations(info.getRelations());
90     }
91  
92     public StrategyInfo duplicate(String name, GenSym genSym) {
93        DuplicationTable table = new DuplicationTable(name,genSym);
94        return duplicate(table);
95     }
96     public StrategyInfo duplicate(DuplicationTable table) {
97        if ( type == USE )
98           return new StrategyInfo(fact.duplicate(table), strategy,
99  	                         getAgents(), getRelations(), getParameters());
100       else
101          return new StrategyInfo(fact.duplicate(table),
102 	                         getAgents(), getRelations());
103    }
104 
105    public void setType(int mode) {
106       Assert.notFalse(mode == USE || mode == NO_USE);
107       this.type = mode;
108    }
109    public int  getType() { return type; }
110    public Fact getFact() { return fact; }
111 
112    public String getStrategy() {
113       if ( type == NO_USE )
114          Core.USER_ERROR("Use/No_Use restriction error");
115       return strategy;
116    }
117    public Hashtable getParameters() {
118       if ( type == NO_USE )
119          Core.USER_ERROR("Use/No_Use restriction error");
120 
121       Hashtable output = new Hashtable();
122       Enumeration enum = parameters.keys();
123       Object key;
124       while( enum.hasMoreElements() ) {
125          key = enum.nextElement();
126          output.put(key,parameters.get(key));
127       }
128       return output;
129    }
130 
131    public String[] getAgents()    { return Misc.stringArray(agents); }
132    public String[] getRelations() { return Misc.stringArray(relations); }
133 
134    public void setStrategy(String strategy) {
135       if ( type == NO_USE )
136          Core.USER_ERROR("Use/No_Use restriction error");
137       this.strategy = strategy;
138    }
139    public void setParameters(Hashtable input) {
140       if ( type == NO_USE )
141          Core.USER_ERROR("Use/No_Use restriction error");
142       parameters.clear();
143       Enumeration enum = input.keys();
144       Object key;
145       while( enum.hasMoreElements() ) {
146          key = enum.nextElement();
147          parameters.put(key,input.get(key));
148       }
149    }
150 
151    public void clearParameters() {
152       if ( type == NO_USE )
153          Core.USER_ERROR("Use/No_Use restriction error");
154       parameters.clear();
155    }
156 
157    public void setAgents(String[] input) {
158       agents = Misc.stringVector(input);
159    }
160    public void setAgents(Vector input) {
161       agents = Misc.copyVector(input);
162    }
163 
164    public void setRelations(String[] input) {
165       relations = Misc.stringVector(input);
166    }
167    public void setRelations(Vector input) {
168       relations = Misc.copyVector(input);
169    }
170 
171    public boolean resolve(Bindings b) {
172       return fact.resolve(b);
173    }
174 
175    public String toString() {
176       String s = "(:fact " + fact + " " + ":type " + type + " ";
177       if ( type == USE ) {
178          s += ":strategy \"" + strategy + "\" ";
179          if ( !parameters.isEmpty() ) {
180             Enumeration enum = parameters.keys();
181             Object key;
182             s += ":parameters (";
183             while( enum.hasMoreElements() ) {
184                key = enum.nextElement();
185                s += "\"" + key + "\" \"" + parameters.get(key) + "\" ";
186             }
187             s = s.trim() + ")" + " ";
188          }
189       }
190       if ( !agents.isEmpty() )
191          s += ":agents (" + Misc.concat(agents) + ")" + " ";
192       if ( !relations.isEmpty() )
193          s += ":relations (" + Misc.concat(relations) + ")";
194       return s.trim() + ")";
195    }
196 
197    public String pprint() {
198       return pprint(0);
199    }
200    public String pprint(int sp) {
201       String prefix;
202       String tabs = Misc.spaces(sp);
203       String eol  = "\n" + tabs + " ";
204 
205       prefix = "(:fact ";
206       String s = prefix + fact.pprint(sp+prefix.length()) + eol +
207                  ":type " + type + eol;
208       if ( type == USE ) {
209          s += ":strategy \"" + strategy + "\"" + eol;
210          if ( !parameters.isEmpty() ) {
211             Enumeration enum = parameters.keys();
212             Object key;
213             s += ":parameters (";
214             while( enum.hasMoreElements() ) {
215                key = enum.nextElement();
216                s += "\"" + key + "\" \"" + parameters.get(key) + "\" ";
217             }
218             s = s.trim() + ")" + eol;
219          }
220       }
221       if ( !agents.isEmpty() )
222          s += ":agents (" + Misc.concat(agents) + ")" + eol;
223       if ( !relations.isEmpty() )
224          s += ":relations (" + Misc.concat(relations) + ")" + eol;
225       return s.trim() + "\n" + tabs + ")";
226    }
227 }