View Javadoc

1   /***
2    * ***************************************************************
3    * JADE - Java Agent DEvelopment Framework is a framework to develop
4    * multi-agent systems in compliance with the FIPA specifications.
5    * Copyright (C) 2000 CSELT S.p.A.
6    * 
7    * GNU Lesser General Public License
8    * 
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation,
12   * version 2.1 of the License.
13   * 
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the
21   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22   * Boston, MA  02111-1307, USA.
23   * **************************************************************
24   */
25  package JADE_SL.schema.facets;
26  
27  import JADE_SL.onto.*;
28  import JADE_SL.schema.*;
29  import JADE_SL.abs.*;
30  import java.util.*;
31  
32  /***
33   * This facet forces the elements in an AbsAggregate
34   * to be instances of a given schema.
35   * @author Giovanni Caire - TILAB
36   */
37  public class TypedAggregateFacet implements Facet {
38  	private ObjectSchema type;
39  	
40  	/***
41  	   Construct a <code>TypedAggregateFacet</code> that forces
42  	   the elements in an AbsAggregate to be instances of a given 
43  	   schema
44  	 */
45  	public TypedAggregateFacet(ObjectSchema s) {
46  		type = s;
47  	}
48  	
49  	/***
50  	   Check whether a given value for the slot this Facet applies
51  	   to is valid.
52  	   @param value The value to be checked
53  	   @throws OntologyException If the value is not valid
54  	 */
55  	public void validate(AbsObject value, Ontology onto) throws OntologyException {
56    	if (!(value instanceof AbsAggregate)) {
57    		throw new OntologyException(value+" is not an AbsAggregate");
58    	}
59    	
60    	AbsAggregate agg = (AbsAggregate) value;
61  		Iterator it = agg.iterator();
62  		while (it.hasNext()) {
63  			AbsTerm el = (AbsTerm) it.next();
64  			ObjectSchema s = onto.getSchema(el.getTypeName());
65  			if (!s.isCompatibleWith(type)) {
66  				throw new OntologyException("Schema "+s+" for element "+el+" is not compatible with type "+type);
67  			}
68  		}
69  	}
70  }