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  
30  /***
31   * @author Federico Bergenti - Universita` di Parma
32   */
33  public class PredicateSchema extends ContentElementSchema {
34      public static final String         BASE_NAME = "Predicate";
35      private static PredicateSchema baseSchema = new PredicateSchema();
36  
37      /***
38       * Construct a schema that vinculates an entity to be a generic
39       * predicate
40       */
41      private PredicateSchema() {
42          super(BASE_NAME);
43      }
44  
45      /***
46       * Creates a <code>PredicateSchema</code> with a given type-name,
47       * e.g. FATHER_OF, WORKS_FOR...
48       * @param typeName The name of this <code>PredicateSchema</code>.
49       */
50      public PredicateSchema(String typeName) {
51          super(typeName);
52      }
53  
54      /***
55       * Retrieve the generic base schema for all predicates.
56       * @return the generic base schema for all predicates.
57       */
58      public static ObjectSchema getBaseSchema() {
59          return baseSchema;
60      } 
61  
62      /***
63       * Add a mandatory slot to this schema. 
64       * @param name The name of the slot.
65       * @param slotSchema The schema of the slot.
66       */
67      public void add(String name, ObjectSchema slotSchema) {
68          super.add(name, slotSchema);
69      } 
70  
71      /***
72       * Add a slot to this schema. 
73       *
74       * @param name The name of the slot.
75       * @param slotSchema The schema of the slot.
76       * @param optionality The optionality, i.e. <code>OPTIONAL</code> 
77       * or <code>MANDATORY</code>
78       */
79      public void add(String name, ObjectSchema slotSchema, int optionality) {
80          super.add(name, slotSchema, optionality);
81      } 
82  
83      /***
84       * Add a slot with cardinality between <code>cardMin</code>
85       * and <code>cardMax</code> to this schema. 
86       * Adding such a slot is equivalent to add a slot
87       * of type Aggregate and then to add proper facets (constraints)
88       * to check that the type of the elements in the aggregate are
89       * compatible with <code>elementsSchema</code> and that the 
90       * aggregate contains at least <code>cardMin</code> elements and
91       * at most <code>cardMax</code> elements.
92       * @param name The name of the slot.
93       * @param elementsSchema The schema for the elements of this slot.
94       * @param cardMin This slot must get at least <code>cardMin</code>
95       * values
96       * @param cardMax This slot can get at most <code>cardMax</code>
97       * values
98       */
99      public void add(String name, TermSchema elementsSchema, int cardMin, int cardMax) {
100       super.add(name, elementsSchema, cardMin, cardMax);
101     } 
102 
103     /*** 
104        Add a <code>Facet</code> on a slot of this schema
105        @param slotName the name of the slot the <code>Facet</code>
106        must be added to.
107        @param f the <code>Facet</code> to be added.
108        @throws OntologyException if slotName does not identify
109        a valid slot in this schema
110      */
111 		public void addFacet(String slotName, Facet f) throws OntologyException {
112 			super.addFacet(slotName, f);
113 		}
114 
115 		/***
116      * Creates an Abstract descriptor to hold a predicate of
117      * the proper type.
118      */
119     public AbsObject newInstance() throws OntologyException {
120         return new AbsPredicate(getTypeName());
121     } 
122 
123 		/***
124 	     Check whether a given abstract descriptor complies with this 
125 	     schema.
126 	     @param abs The abstract descriptor to be checked
127 	     @throws OntologyException If the abstract descriptor does not 
128 	     complies with this schema
129 	   */
130   	public void validate(AbsObject abs, Ontology onto) throws OntologyException {
131 			// Check the type of the abstract descriptor
132   		if (!(abs instanceof AbsPredicate)) {
133 				throw new OntologyException(abs+" is not an AbsPredicate");
134 			}
135 			
136 			// Check the slots
137 			validateSlots(abs, onto);
138   	}
139   	
140   	/***
141   	   Return true if 
142   	   - s is the base schema for the XXXSchema class this schema is
143   	     an instance of (e.g. s is ConceptSchema.getBaseSchema() and this 
144   	     schema is an instance of ConceptSchema)
145   	   - s is the base schema for a super-class of the XXXSchema class
146   	     this schema is an instance of (e.g. s is TermSchema.getBaseSchema()
147   	     and this schema is an instance of ConceptSchema)
148   	 */
149   	protected boolean descendsFrom(ObjectSchema s) {
150   		if (s != null) {
151   			if (s.equals(getBaseSchema())) {
152 	  			return true;
153   			}
154   			return super.descendsFrom(s);
155   		}
156   		else {
157   			return false;
158   		}
159   	}
160 }