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.lang.reflect.*;
28  
29  import java.io.Serializable;
30  import java.io.Reader;
31  import java.io.Writer;
32  import java.io.IOException;
33  
34  import java.util.List;
35  import java.util.ArrayList;
36  import java.util.Set;
37  import java.util.Iterator;
38  
39  
40  
41  /***
42  @author Giovanni Rimassa - Universita` di Parma
43  @version $Date: 2003/10/09 13:00:37 $ $Revision: 1.1.1.1 $
44  */
45  class FrameSchema implements Cloneable, Serializable {
46  
47    static class WrongSlotTypeException extends OntologyException {
48      public WrongSlotTypeException(String functorName, String slotName, String slotType) {
49        super("No slot of type " + slotType + " named " + slotName + " in functor " + functorName);
50      }
51    }
52  
53    private Ontology myOntology;
54    private CaseInsensitiveString myName;
55    private List slots;
56  
57  
58    public FrameSchema(Ontology o, String n) {
59      myOntology = o;
60      myName = new CaseInsensitiveString(n);
61      slots = new ArrayList();
62    }
63  
64    public String getName() {
65      return myName.toString();
66    }
67  
68    public void addSlot(SlotDescriptor td) {
69      slots.add(td);
70    }
71  
72  
73  	private boolean isGoodPrimitiveType(String required, Object current) {
74    	try { 
75      	return (Class.forName(required).isInstance(current));
76    	} catch (Exception e) {
77      	e.printStackTrace();
78      	return false;
79    	}
80  	}
81  
82  
83    public void checkAgainst(Frame f) throws OntologyException {
84      for(int i = 0; i < slots.size(); i++) {
85        SlotDescriptor sd = (SlotDescriptor)slots.get(i);
86        String name = sd.getName();
87        if(!sd.isOptional()) {
88  				Object o = f.getSlot(name); // If a slot called name is not present in f, this method throws an exception
89  				if (sd.isPrimitive()) {
90  	  			if (!isGoodPrimitiveType(sd.getType(),o))
91  	    			throw new WrongSlotTypeException(f.getName(), name, sd.getType()); 
92  				} 
93  				else {
94  					// If the slot is not primitive than its value must be an instance of Frame
95  					// In this case recursively check that frame
96  	  			if(!(o instanceof Frame))
97  	    			throw new WrongSlotTypeException(f.getName(), name, "Frame");
98  	  			myOntology.check((Frame)o);
99  				}
100 
101       } // End of 'if slot is not optional'
102 
103     } // End of slot iteration
104 
105   }
106 
107   Iterator subSchemas() {
108     return slots.iterator();
109   }
110 
111   SlotDescriptor[] slotsArray() {
112     Object[] objs = slots.toArray();
113     SlotDescriptor[] result = new SlotDescriptor[objs.length];
114     System.arraycopy(objs, 0, result, 0, objs.length);
115     return result;
116   }
117 
118 }
119