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 CardinalityFacet implements Facet {
38  	private int cardMin;
39  	private int cardMax;
40  	
41  	/***
42  	   Construct a <code>CardinalityFacet</code> that forces
43  	   the number of elements in an AbsAggregate to be within
44  	   a given range
45  	   @param cardMin The range lower bound
46  	   @param cardMax The range upper bound
47  	 */
48  	public CardinalityFacet(int cardMin, int cardMax) {
49  		this.cardMin = cardMin;
50  		this.cardMax = cardMax;
51  	}
52  	
53  	/***
54  	   Check that
55  	   the number of elements in an AbsAggregate is within
56  	   a given range
57  	   @param value The value to be checked
58  	   @throws OntologyException If the value is not valid
59  	 */
60  	public void validate(AbsObject value, Ontology onto) throws OntologyException {
61    	if (!(value instanceof AbsAggregate)) {
62    		throw new OntologyException(value+" is not an AbsAggregate");
63    	}
64    	
65    	int size = ((AbsAggregate) value).size();
66    	if (size < cardMin) {
67    		throw new OntologyException(value+" includes less elements than required ("+cardMin+")");
68    	}
69    	if (cardMax != ObjectSchema.UNLIMITED && size > cardMax) {
70    		throw new OntologyException(value+" includes more elements than allowed ("+cardMax+")");
71    	}
72  	}
73  }