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  package sl;
25  
26  import java.io.StringReader;
27  import java.util.List;
28  
29  /***
30  
31    The codec class for the <b><i>SL0</i></b> language. This class
32    implements the <code>Codec</code> interface and allows converting
33    back and forth between strings and frames, according to the SL0
34    grammar.
35    <p>  
36    <b>WARNING:</b> When creating the Ontology, notice that this SL0Parser 
37    returns a Java object of type <code>Long</code> everytime it parses an 
38    integer, and
39    it returns a Java object of type <code>Double</code> everytime it parses a 
40    float.
41    Therefore, the slots of the frames in the ontology must be of type,
42    respectively, <code>Ontology.LONG_TYPE</code> and 
43    <code>Ontology.DOUBLE_TYPE</code>, otherwise you get an 
44    <code>IllegalArgumentException</code>.
45    <p>
46    Notice also that the following convention is needed for all the frames representing actions:
47    the name of the frame must be <code>NAME_OF_ACTION_FRAME</code>,
48    and the name of the two slots representing actor and the action term
49    must be, respectively, <code>NAME_OF_ACTOR_SLOT</code> and 
50    <code>NAME_OF_ACTION_SLOT</code>.
51  
52    @author Giovanni Rimassa - Universita` di Parma
53    @version $Date: 2003/10/09 13:00:37 $ $Revision: 1.1.1.1 $
54  
55   */
56  public class SL0Codec implements Codec {
57  
58    /***
59     A symbolic constant, containing the name of this language.
60     */
61    public static final String NAME = "FIPA-SL0";
62  
63    /*** Symbolic constant identifying a frame representing an action **/ 
64    public static String NAME_OF_ACTION_FRAME = BasicOntology.ACTION;
65    /*** Symbolic constant identifying a slot representing an actor **/ 
66    public static String NAME_OF_ACTOR_SLOT = Frame.UNNAMEDPREFIX+".ACTION.actor";
67    /*** Symbolic constant identifying a slot representing an action **/ 
68    public static String NAME_OF_ACTION_SLOT = Frame.UNNAMEDPREFIX+".ACTION.action";
69  
70    private SL0Parser parser = new SL0Parser(new StringReader(""));
71    private SL0Encoder encoder = new SL0Encoder();
72  
73    public synchronized String encode(List v, Ontology o) {
74      StringBuffer s = new StringBuffer("(");
75      for (int i=0; i<v.size(); i++)
76        s.append(encoder.encode((Frame)v.get(i))+" ");
77     return s.append(")").toString();
78    }
79    
80    
81    public synchronized List decode(String s, Ontology o) throws CodecException {
82      try {
83       return parser.parse(s);
84      }
85      catch(ParseException pe) {
86        throw new CodecException("Parse exception", pe);
87      }
88      catch(TokenMgrError tme) {
89        throw new CodecException("Token Manager error", tme);
90      }
91    }
92  
93  }