⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectschemaimpl.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * ***************************************************************
 * JADE - Java Agent DEvelopment Framework is a framework to develop
 * multi-agent systems in compliance with the FIPA specifications.
 * Copyright (C) 2000 CSELT S.p.A.
 *
 * GNU Lesser General Public License
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation,
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * **************************************************************
 */
package jade.content.schema;

import jade.content.onto.*;
import jade.content.abs.*;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import jade.content.schema.facets.*;
import jade.core.CaseInsensitiveString;
import jade.util.leap.Serializable;
import jade.util.Logger;

/**
 * @author Giovanni Caire - TILAB
 */
class ObjectSchemaImpl extends ObjectSchema {
	private Logger logger = Logger.getMyLogger(this.getClass().getName());

    static final String RESULT_SLOT_NAME = "__Result_SLOT_123"; 
    
	private class SlotDescriptor implements Serializable {
		private String       name = null;
		private ObjectSchema schema = null;
		private int          optionality = 0;
		/**
		 Construct a SlotDescriptor
		 */
		private SlotDescriptor(String name, ObjectSchema schema, int optionality) {
			this.name = name;
			this.schema = schema;
			this.optionality = optionality;
		}

	}


	private String          typeName = null;
	private Hashtable       slots;
	private Vector       	slotNames;
	private Vector          superSchemas;
	
	// Note that the list of facets for a given slot cannot be included in the slot descriptor.
	// In fact facets are associated to the a slot in a given schema, not to the slot itself.
	// For instance if we have 
	// - schema A that extends schema B (B is a super-schema of A) 
	// - B defines slot S and associates facet f1 to it
	// - A associates facet f2 to slot S
	// When validating an instance of B only facet f1 must be applied, while when validating
	// an instance of A both f1 and f2 must be applied. 
	
	private Hashtable       facets;

	static {
		baseSchema = new ObjectSchemaImpl();
	}

	/**
	 * Construct a schema that vinculates an entity to be a generic
	 * object (i.e. no constraints at all)
	 */
	private ObjectSchemaImpl() {
		this(BASE_NAME);
	}

	/**
	 * Creates an <code>ObjectSchema</code> with a given type-name.
	 * @param typeName The name of this <code>ObjectSchema</code>.
	 */
	protected ObjectSchemaImpl(String typeName) {
		this.typeName = typeName;
	}

	/**
	 * Add a slot to the schema.
	 * @param name The name of the slot.
	 * @param slotSchema The schema defining the type of the slot.
	 * @param optionality The optionality, i.e., <code>OPTIONAL</code>
	 * or <code>MANDATORY</code>
	 */
	protected void add(String name, ObjectSchema slotSchema, int optionality) {
		CaseInsensitiveString ciName = new CaseInsensitiveString(name);
		if (slots == null) {
			slots = new Hashtable();
			slotNames = new Vector();
		}
		if (slots.put(ciName, new SlotDescriptor(name, slotSchema, optionality)) == null) {
			// We treat Action results as if they were slots. However we don't want the special
			// RESULT_SLOT_NAME to be included among slot names
			if (!name.equals(RESULT_SLOT_NAME)) {
				slotNames.addElement(ciName);
			}
		}
	}

	/**
	 * Add a mandatory slot to the schema.
	 * @param name name of the slot.
	 * @param slotSchema schema of the slot.
	 */
	protected void add(String name, ObjectSchema slotSchema) {
		add(name, slotSchema, MANDATORY);
	}

	/**
	 * Add a slot with cardinality between <code>cardMin</code>
	 * and <code>cardMax</code> to this schema.
	 * Adding such a slot corresponds to add a slot
	 * of type Aggregate and then to add proper facets (constraints)
	 * to check that the type of the elements in the aggregate are
	 * compatible with <code>elementsSchema</code> and that the
	 * aggregate contains at least <code>cardMin</code> elements and
	 * at most <code>cardMax</code> elements. By default the Aggregate
	 * is of type <code>BasicOntology.SEQUENCE</code>.
	 * @param name The name of the slot.
	 * @param elementsSchema The schema for the elements of this slot.
	 * @param cardMin This slot must get at least <code>cardMin</code>
	 * values
	 * @param cardMax This slot can get at most <code>cardMax</code>
	 * values
	 */
	protected void add(String name, ObjectSchema elementsSchema, int cardMin, int cardMax) {
		add(name, elementsSchema, cardMin, cardMax, BasicOntology.SEQUENCE);
	}

	/**
	 * Add a slot with cardinality between <code>cardMin</code>
	 * and <code>cardMax</code> to this schema and allow specifying the type
	 * of Aggregate to be used for this slot.
	 * @param name The name of the slot.
	 * @param elementsSchema The schema for the elements of this slot.
	 * @param cardMin This slot must get at least <code>cardMin</code>
	 * values
	 * @param cardMax This slot can get at most <code>cardMax</code>
	 * values
	 * @param aggType The type of Aggregate to be used
	 * @see #add(String, ObjectSchema, int, int)
	 */
	protected void add(String name, ObjectSchema elementsSchema, int cardMin, int cardMax, String aggType) {
		int optionality = (cardMin == 0 ? OPTIONAL : MANDATORY);
		try {
			add(name, BasicOntology.getInstance().getSchema(aggType), optionality);
			// Add proper facets
			addFacet(name, new TypedAggregateFacet(elementsSchema));
			addFacet(name, new CardinalityFacet(cardMin, cardMax));
		}
		catch (OntologyException oe) {
			// Should never happen
			oe.printStackTrace();
		}
	}


	/**
	 * Add a super schema tho this schema, i.e. this schema will
	 * inherit all characteristics from the super schema
	 * @param superSchema the super schema.
	 */
	protected void addSuperSchema(ObjectSchema superSchema) {
		if (superSchemas == null) {
			superSchemas = new Vector();
		}
		superSchemas.addElement(superSchema);
	}

	/**
	 Add a <code>Facet</code> on a slot of this schema
	 @param slotName the name of the slot the <code>Facet</code>
	 must be added to.
	 @param f the <code>Facet</code> to be added.
	 @throws OntologyException if slotName does not identify
	 a valid slot in this schema
	 */
	protected void addFacet(String slotName, Facet f) throws OntologyException {
		if (containsSlot(slotName)) {
			CaseInsensitiveString ciName = new CaseInsensitiveString(slotName);
			if (facets == null) {
				facets = new Hashtable();
			}
			Vector v = (Vector) facets.get(ciName);
			if (v == null) {
				v = new Vector();
				facets.put(ciName, v);
				//DEBUG
				if(logger.isLoggable(Logger.CONFIG))
					logger.log(Logger.CONFIG,"Added facet "+f+" to slot "+slotName);
			}
			v.addElement(f);
		}
		else {
			throw new OntologyException(slotName+" is not a valid slot in this schema");
		}
	}

	/**
	 * Retrieves the name of the type of this schema.
	 * @return the name of the type of this schema.
	 */
	public String getTypeName() {
		return typeName;
	}

	/**
	 * Returns the names of all the slots in this <code>Schema</code>
	 * (including slots defined in super schemas).
	 *
	 * @return the names of all slots.
	 */
	public String[] getNames() {
		Vector allSlotNames = new Vector();

		fillAllSlotNames(allSlotNames);

		String[] names = new String[allSlotNames.size()];
		int      counter = 0;
		for (Enumeration e = allSlotNames.elements(); e.hasMoreElements(); ) {
			names[counter++] = ((CaseInsensitiveString) e.nextElement()).toString();
		}

		return names;
	}

	/**
	 * Retrieves the schema of a slot of this <code>Schema</code>.
	 *
	 * @param name The name of the slot.
	 * @return the <code>Schema</code> of slot <code>name</code>
	 * @throws OntologyException If no slot with this name is present
	 * in this schema.
	 */
	public ObjectSchema getSchema(String name) throws OntologyException {
		SlotDescriptor slot = getSlot(new CaseInsensitiveString(name));
		if (slot == null) {
			throw new OntologyException("No slot named: " + name);
		}
		return slot.schema;
	}

	/**
	 * Indicate whether a given <code>String</code> is the name of a
	 * slot defined in this <code>Schema</code>
	 *
	 * @param name The <code>String</code> to test.
	 * @return <code>true</code> if <code>name</code> is the name of a
	 * slot defined in this <code>Schema</code>.
	 */
	public boolean containsSlot(String name) {
		SlotDescriptor slot = getSlot(new CaseInsensitiveString(name));
		return (slot != null);
	}

	/**
	 * Indicate whether a slot of this schema is mandatory
	 *
	 * @param name The name of the slot.
	 * @return <code>true</code> if the slot is mandatory.
	 * @throws OntologyException If no slot with this name is present
	 * in this schema.
	 */
	public boolean isMandatory(String name) throws OntologyException {
		SlotDescriptor slot = getSlot(new CaseInsensitiveString(name));
		if (slot == null) {
			throw new OntologyException("No slot named: " + name);
		}
		return slot.optionality == MANDATORY;
	}

	/**
	 * Creates an Abstract descriptor to hold an object compliant to
	 * this <code>Schema</code>.
	 */
	public AbsObject newInstance() throws OntologyException {
		throw new OntologyException("AbsObject cannot be instantiated");
	}

	private final void fillAllSlotNames(Vector v) {
		// Get slot names of super schemas (if any) first

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -