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;
26  
27  import JADE_SL.abs.*;
28  import JADE_SL.onto.*;
29  import java.util.Iterator;
30  
31  /***
32   * @author Federico Bergenti - Universita` di Parma
33   */
34  public class AggregateSchema extends TermSchema {
35      public static final String         BASE_NAME = "Aggregate";
36      private static AggregateSchema baseSchema = new AggregateSchema();
37  
38      /***
39       * Construct a schema that vinculates an entity to be a generic
40       * aggregate
41       */
42      private AggregateSchema() {
43          super(BASE_NAME);
44      }
45  
46      /***
47       * Creates an <code>AggregateSchema</code> with a given type-name.
48       *
49       * @param typeName The name of this <code>AggregateSchema</code>.
50       */
51      public AggregateSchema(String typeName) {
52          super(typeName);
53      }
54  
55      /***
56       * Retrieve the generic base schema for all aggregates.
57       *
58       * @return the generic base schema for all aggregates.
59       */
60      public static ObjectSchema getBaseSchema() {
61          return baseSchema;
62      } 
63      
64      /***
65       * Creates an Abstract descriptor to hold an aggregate of
66       * the proper type.
67       */
68      public AbsObject newInstance() throws OntologyException {
69          return new AbsAggregate(getTypeName());
70      } 
71  
72  		/***
73  	     Check whether a given abstract descriptor complies with this 
74  	     schema.
75  	     @param abs The abstract descriptor to be checked
76  	     @throws OntologyException If the abstract descriptor does not 
77  	     complies with this schema
78  	   */
79    	public void validate(AbsObject abs, Ontology onto) throws OntologyException {
80  			// Check the type of the abstract descriptor
81    		if (!(abs instanceof AbsAggregate)) {
82    			throw new OntologyException(abs+" is not an AbsAggregate");
83    		}
84    		
85    		// Validate the elements in the aggregate against their schemas.
86    		// Note that there is no need to check that these schemas are
87    		// compliant with TermSchema.getBaseSchema() because the
88    		// AbsAggregate class already forces that.
89    		AbsAggregate agg = (AbsAggregate) abs;
90    		Iterator it = agg.iterator();
91    		while (it.hasNext()) {
92    			AbsTerm el = (AbsTerm) it.next();
93    			ObjectSchema s = onto.getSchema(el.getTypeName());
94    			s.validate(el, onto);
95    		}
96    	}
97    	
98    	/***
99    	   Return true if 
100   	   - s is the base schema for the XXXSchema class this schema is
101   	     an instance of (e.g. s is ConceptSchema.getBaseSchema() and this 
102   	     schema is an instance of ConceptSchema)
103   	   - s is the base schema for a super-class of the XXXSchema class
104   	     this schema is an instance of (e.g. s is TermSchema.getBaseSchema()
105   	     and this schema is an instance of ConceptSchema)
106   	 */
107   	protected boolean descendsFrom(ObjectSchema s) {
108   		if (s != null) {
109   			if (s.equals(getBaseSchema())) {
110 	  			return true;
111   			}
112   			return super.descendsFrom(s);
113   		}
114   		else {
115   			return false;
116   		}
117   	}
118   	
119   	// FIXME: This is redefined to make different types of aggregate
120   	// (e.g. SET and SEQUENCE) compatible as the framework is currently
121   	// only able to deal with SEQUENCE.
122     public boolean equals(Object o) {
123     	if (o != null) {
124 	    	return getClass().getName().equals(o.getClass().getName());
125     	}
126     	else {
127     		return false;
128     	}
129     }
130 }