View Javadoc

1   /******************************************************************
2   JADE - Java Agent DEvelopment Framework is a framework to develop 
3   multi-agent systems in compliance with the FIPA specifications.
4   Copyright (C) 2000 CSELT S.p.A. 
5   
6   GNU Lesser General Public License
7   
8   This library is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation, 
11  version 2.1 of the License. 
12  
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  Lesser General Public License for more details.
17  
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the
20  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  Boston, MA  02111-1307, USA.
22  *****************************************************************/
23  
24  
25  package sl;
26  
27  import java.util.Map;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  
33  
34  /***
35    Representation of an ontological entity as a set of untyped slots.
36    This class can hold different slots, keeping track both their unique name and
37    their position.
38  
39    @author Giovanni Rimassa - Universita` di Parma
40    @version $Date: 2003/10/09 13:00:37 $ $Revision: 1.1.1.1 $
41  */
42  public class Frame {
43  
44    static class NoSuchSlotException extends OntologyException {
45      public NoSuchSlotException(String frameName, String slotName) {
46        super("No slot named " + slotName + " in frame " + frameName);
47      }
48    }
49  
50    private String myName;
51    private List slotNames;
52    private List slotValues;
53  
54    /*** This string is the prefix of all the unnamed slots of a Frame **/
55    public static String UNNAMEDPREFIX = "_JADE.UNNAMED"; 
56    /***
57      Creates a new frame with the given name.
58      @param name The name of this frame.
59    */
60    public Frame(String name) {
61      myName = name;
62      slotNames = new ArrayList();
63      slotValues = new ArrayList();
64    }
65  
66    /***
67      Reads the name of this frame.
68      @return The name of this frame, as was set by the constructor.
69    */
70    public String getName() {
71      return myName;
72    }
73  
74    /***
75      Adds a named slot to this frame.
76      @param name The unique name of the new slot
77      @param value A Java object that will be associated with the given name.
78    */
79    public void putSlot(String name, Object value) {
80      slotNames.add(new CaseInsensitiveString(name));
81      slotValues.add(value);
82    }
83  
84    /***
85      Adds an unnamed slot to this frame. Its
86      position is determined by the number of slots of this frame at the time of
87      the call. The given Java object is put at the end of the slot sequence.  
88      A dummy name is set for this slot with the prefix <code>UNNAMEDPREFIX</code>.
89      @param value A Java object that will be associated with the given position.
90    */
91    public void putSlot(Object value) {
92      // generate a name with an underscore followed by the position number
93      String dummyName = UNNAMEDPREFIX + Integer.toString(slotValues.size());
94  
95      // Add more underscores as needed
96      while(slotNames.contains(dummyName))
97        dummyName = dummyName+"_";
98  
99      putSlot(dummyName, value);
100 
101   }
102 
103   /***
104     Retrieves a named slot from this frame, by name.
105     @param name The name of the desired slot.
106     @return The value of that slot 
107     @exception OntologyException If no suitable slot exists.
108   */
109   public Object getSlot(String name) throws OntologyException {
110     int i = slotNames.indexOf(new CaseInsensitiveString(name));
111     if (i<0)
112       throw new NoSuchSlotException(myName, name);
113     else
114       return getSlot(i);
115   }
116 
117   /***
118     Retrieves an unnamed slot from this frame, by position.
119     @param position The position of the desired slot.
120     @return The value of that slot 
121     @exception OntologyException If no suitable slot exists.
122   */
123   public Object getSlot(int position) throws OntologyException { 
124     try {
125       return slotValues.get(position);
126     }
127     catch(IndexOutOfBoundsException ioobe) {
128       throw new NoSuchSlotException(myName, "@" + position);
129     }
130   }
131 
132 
133   /***
134    @return the number of slots in this Frame.
135    **/
136    public int size() {
137      return slotNames.size();
138    }
139 
140 	/***
141     @return The name of the slot at the indicated position 
142     @exception OntologyException If no suitable slot exists.
143   */
144   public String getSlotName(int position) throws OntologyException { 
145     try {
146       return ((CaseInsensitiveString)slotNames.get(position)).toString();
147     }
148     catch(Exception ioobe) {
149       throw new NoSuchSlotException(myName, "at position" + position);
150     }
151   }
152 
153   /*** it is here just for debugging purposes **/
154   public String toString() {
155     String s = "(" + getName() + " ";
156     try {
157      for (int i=0; i<size(); i++ ) 
158       s = s + ':' + getSlotName(i) + " " + getSlot(i).toString() + " ";
159     } catch (OntologyException oe) {
160      oe.printStackTrace();
161     }
162     return s+ ") ";
163   }
164     
165 
166 }
167