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 ProtocolInfo {
32     public static final String INITIATOR  = "Initiator";
33     public static final String RESPONDENT = "Respondent";
34  
35     protected String name;
36     protected String type;
37     protected Vector constraints = new Vector();
38     
39     public ProtocolInfo() { 
40     }
41  
42     public ProtocolInfo(String name, String type, Fact fact) {
43        Assert.notNull(name);
44        Assert.notFalse(type.equals(INITIATOR) || type.equals(RESPONDENT));
45        this.name = name;
46        this.type = type;
47  
48        String strategy = (type.equals(RESPONDENT))
49                          ? StrategyInfo.DEFAULT_RESPONDENT_STRATEGY
50                          : StrategyInfo.DEFAULT_INITIATOR_STRATEGY;
51  
52        StrategyInfo info = new StrategyInfo(fact,strategy);
53        constraints.addElement(info);
54     }
55  
56     public ProtocolInfo(String name, String type, StrategyInfo[] input) {
57        Assert.notNull(name);
58        Assert.notFalse(type.equals(INITIATOR) || type.equals(RESPONDENT));
59        this.name = name;
60        this.type = type;
61        setConstraints(input);
62     }
63     public ProtocolInfo(String name, String type, Vector input) {
64        Assert.notNull(name);
65        Assert.notFalse(type.equals(INITIATOR) || type.equals(RESPONDENT));
66        this.name = name;
67        this.type = type;
68        setConstraints(input);
69     }
70  
71     public ProtocolInfo(ProtocolInfo info) {
72        name = info.getName();
73        type = info.getType();
74        setConstraints(info.getConstraints());
75     }
76  
77     public ProtocolInfo duplicate(String name, GenSym genSym) {
78        DuplicationTable table = new DuplicationTable(name,genSym);
79        return duplicate(table);
80     }
81     public ProtocolInfo duplicate(DuplicationTable table) {
82        Vector data = new Vector();
83        StrategyInfo info;
84        for(int i = 0; i < constraints.size(); i++ ) {
85           info = (StrategyInfo)constraints.elementAt(i);
86           data.addElement(info.duplicate(table));
87        }
88        return new ProtocolInfo(name,type,data);
89     }
90  
91     public String getName() {
92        return name;
93     }
94     public String getType() {
95        return type;
96     }
97  
98     public StrategyInfo[] getConstraints() {
99        StrategyInfo[] info = new StrategyInfo[constraints.size()];
100       for(int i = 0; i < constraints.size(); i++ )
101          info[i] = new StrategyInfo((StrategyInfo)constraints.elementAt(i));
102       return info;
103    }
104 
105    public void setConstraints(StrategyInfo[] input) {
106       constraints.removeAllElements();
107       if ( input == null ) return;
108       for(int i = 0; i < input.length; i++ )
109          constraints.addElement(new StrategyInfo(input[i]));
110    }
111    public void setConstraints(Vector input) {
112       constraints.removeAllElements();
113       if ( input == null ) return;
114       for(int i = 0; i < input.size(); i++ )
115          constraints.addElement(new StrategyInfo((StrategyInfo)input.elementAt(i)));
116    }
117 
118    public boolean resolve(Bindings b) {
119       boolean status = true;
120       StrategyInfo info;
121       for(int i = 0; status && i < constraints.size(); i++ ) {
122          info = (StrategyInfo)constraints.elementAt(i);
123          status &= info.resolve(b);
124       }
125       return status;
126    }
127 
128    public String toString() {
129       String s = "(:name \"" + name + "\" :type " + type;
130       s += " " + ":constraints (" + Misc.concat(constraints) + ")";
131       s +=  ")";
132       return s;
133    }
134 
135    public String pprint() {
136       return pprint(0);
137    }
138    public String pprint(int sp) {
139       String prefix, suffix;
140       String tabs = Misc.spaces(sp);
141       String eol  = "\n" + tabs + " ";
142 
143       String s = tabs + "(:name \"" + name + "\"" + eol + ":type " + type + eol;
144       prefix = ":constraints ";
145       suffix = Misc.spaces(1 + sp + prefix.length());
146       s += prefix + "(";
147       for(int i = 0; i < constraints.size(); i++ )
148          s += ((StrategyInfo)constraints.elementAt(i)).pprint(1+suffix.length()) +
149               "\n" + suffix + " ";
150       s = s.trim() + "\n" + suffix + ")" + eol;
151       return s.trim() + "\n" + tabs + ")";
152    }
153 }