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

📄 objectschemaimpl.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		if (superSchemas != null) {
			for (Enumeration e = superSchemas.elements(); e.hasMoreElements(); ) {
				ObjectSchemaImpl superSchema = (ObjectSchemaImpl) e.nextElement();

				superSchema.fillAllSlotNames(v);
			}
		}

		// Then add slot names of this schema
		if (slotNames != null) {
			for (Enumeration e = slotNames.elements(); e.hasMoreElements(); ) {
				v.addElement(e.nextElement());
			}
		}
	}

	/**
	 Check whether a given abstract descriptor complies with this
	 schema.
	 @param abs The abstract descriptor to be checked
	 @throws OntologyException If the abstract descriptor does not
	 complies with this schema
	 */
	public void validate(AbsObject abs, Ontology onto) throws OntologyException {
		validateSlots(abs, onto);
	}

	/**
	 For each slot
	 - get the corresponding attribute value from the abstract descriptor
	 abs
	 - Check that it is not null if the slot is mandatory
	 - Check that its schema is compatible with the schema of the slot
	 - Check that it is a correct abstract descriptor by validating it
	 against its schema.
	 */
	protected void validateSlots(AbsObject abs, Ontology onto) throws OntologyException {
		// Validate all the attributes in the abstract descriptor
		String[] slotNames = getNames();
		for (int i = 0; i < slotNames.length; ++i) {
			AbsObject slotValue = abs.getAbsObject(slotNames[i]);
			CaseInsensitiveString ciName = new CaseInsensitiveString(slotNames[i]);
			validate(ciName, slotValue, onto);
		}
	}

	/**
	 Validate a given abstract descriptor as a value for a slot
	 defined in this schema
	 @param slotName The name of the slot
	 @param value The abstract descriptor to be validated
	 @throws OntologyException If the abstract descriptor is not a
	 valid value
	 @return true if the slot is defined in this schema (or in
	 one of its super schemas). false otherwise
	 */
	private boolean validate(CaseInsensitiveString slotName, AbsObject value, Ontology onto) throws OntologyException {
		// DEBUG
		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"Validating "+(value != null ? value.toString() : "null")+" as a value for slot "+slotName);
		// NOTE: for performance reasons we don't want to scan the schema
		// to check if slotValue is a valid slot and THEN to scan again
		// the schema to validate value. This is the reason for the
		// boolean return value of this method
		boolean slotFound = false;

		// If the slot is defined in this schema --> check the value
		// against the schema of the slot. Otherwise let the super-schema
		// where the slot is defined validate the value
		SlotDescriptor dsc = getOwnSlot(slotName);
		if (dsc != null) {
			// DEBUG
			if(logger.isLoggable(Logger.CONFIG))
				logger.log(Logger.CONFIG,"Slot "+slotName+" is defined in schema "+this);
			if (value == null) {
				// Check optionality
				if (dsc.optionality == MANDATORY) {
					throw new OntologyException("Missing value for mandatory slot "+slotName+". Schema is "+this);
				}
				// Don't need to check facets on a null value for an optional slot
				return true;
			}
			else {
				// - Get from the ontology the schema s that defines the type
				// of the abstract descriptor value.
				// - Check if this schema is compatible with the schema for
				// slot slotName
				// - Finally check value against s
				ObjectSchema s = onto.getSchema(value.getTypeName());
				//DEBUG
				if(logger.isLoggable(Logger.CONFIG))
					logger.log(Logger.CONFIG,"Actual schema for "+value+" is "+s);
				if (s == null) {
					throw new OntologyException("No schema found for type "+value.getTypeName());
				}
				if (!s.isCompatibleWith(dsc.schema)) {
					throw new OntologyException("Schema "+s+" for element "+value+" is not compatible with schema "+dsc.schema+" for slot "+slotName);
				}
				//DEBUG
				if(logger.isLoggable(Logger.CONFIG))
					logger.log(Logger.CONFIG,"Schema "+s+" for type "+value+" is compatible with schema "+dsc.schema+" for slot "+slotName);
				s.validate(value, onto);
			}
			slotFound = true;
		}
		else {
			if (superSchemas != null) {
				Enumeration e = superSchemas.elements();
				while (e.hasMoreElements()) {
					ObjectSchemaImpl s = (ObjectSchemaImpl) e.nextElement();
					if (s.validate(slotName, value, onto)) {
						slotFound = true;
						// Don't need to check other super-schemas
						break;
					}
				}
			}
		}

		if (slotFound && facets != null) {
			// Check value against the facets (if any) defined for the
			// slot in this schema
			Vector ff = (Vector) facets.get(slotName);
			if (ff != null) {
				Enumeration e = ff.elements();
				while (e.hasMoreElements()) {
					Facet f = (Facet) e.nextElement();
					//DEBUG
					if(logger.isLoggable(Logger.CONFIG))
						logger.log(Logger.CONFIG,"Checking facet "+f+" defined on slot "+slotName);
					f.validate(value, onto);
				}
			}
			else {
				//DEBUG
				if(logger.isLoggable(Logger.CONFIG))
					logger.log(Logger.CONFIG,"No facets for slot "+slotName);
			}
		}

		return slotFound;
	}

	/**
	 Check if this schema is compatible with a given schema s.
	 This is the case if
	 1) This schema is equals to s
	 2) s is one of the super-schemas of this schema
	 3) This schema descends from s i.e.
	 - s is the base schema for the XXXSchema class this schema is
	 an instance of (e.g. s is ConceptSchema.getBaseSchema() and this
	 schema is an instance of ConceptSchema)
	 - s is the base schema for a super-class of the XXXSchema class
	 this schema is an instance of (e.g. s is TermSchema.getBaseSchema()
	 and this schema is an instance of ConceptSchema)
	 */
	public boolean isCompatibleWith(ObjectSchema s) {
		if (equals(s)) {
			return true;
		}
		if (isSubSchemaOf(s)) {
			return true;
		}
		if (descendsFrom(s)) {
			return true;
		}
		return false;
	}

	/**
	 Return true if
	 - s is the base schema for the XXXSchema class this schema is
	 an instance of (e.g. s is ConceptSchema.getBaseSchema() and this
	 schema is an instance of ConceptSchema)
	 - s is the base schema for a super-class of the XXXSchema class
	 this schema is an instance of (e.g. s is TermSchema.getBaseSchema()
	 and this schema is an instance of ConceptSchema)
	 */
	protected boolean descendsFrom(ObjectSchema s) {
		// The base schema for the ObjectSchema class descends only
		// from itself
		if (s!= null) {
			return s.equals(getBaseSchema());
		}
		else {
			return false;
		}
	}

	/**
	 Return true if s is a super-schema (directly or indirectly)
	 of this schema
	 */
	private boolean isSubSchemaOf(ObjectSchema s) {
		if (superSchemas != null) {
			Enumeration e = superSchemas.elements();
			while (e.hasMoreElements()) {
				ObjectSchemaImpl s1 = (ObjectSchemaImpl) e.nextElement();
				if (s1.equals(s)) {
					return true;
				}
				if (s1.isSubSchemaOf(s)) {
					return true;
				}
			}
		}
		return false;
	}

	public String toString() {
		return getClass().getName()+"-"+getTypeName();
	}

	public boolean equals(Object o) {
		if (o != null) {
			return toString().equals(o.toString());
		}
		else {
			return false;
		}
	}

	public Facet[] getFacets(String slotName) {
		Vector v = getAllFacets(slotName);
		Facet[] ff = null;
		if (v != null) {
			ff = new Facet[v.size()];
			for (int i = 0; i < v.size(); ++i) {
				ff[i] = (Facet) v.elementAt(i);
			}
		}
		return ff;
	}
	
	/**
	 * Get the facets defined upon a slot. More in details this method returns
	 * all facets defined in this schema plus all facets defined in super-schemas 
	 * up to the schema actually declaring the given slot. 
	 * @return the facets defined upon a slot or null if the specified slot is not found.
	 */
	Vector getAllFacets(String slotName) { // Package-scoped for testing purposes only
		Vector allFacets = new Vector();
		CaseInsensitiveString caseInsensitiveSlotName = new CaseInsensitiveString(slotName);
		if (facets != null) {
			Vector v = (Vector)facets.get(caseInsensitiveSlotName);
			if (v != null) {
				// We don't use Vector.addAll() for MIDP compatibility
				addAll(allFacets, v);
			}
		}
		
		boolean found = false;
		if (getOwnSlot(caseInsensitiveSlotName) == null) {
			// The slot must be defined in one of the super-schema
			if (superSchemas!=null) {
				for (int i = 0; i < superSchemas.size(); i++) {
					ObjectSchemaImpl superSchema = (ObjectSchemaImpl) superSchemas.elementAt(i);
					if (superSchema.containsSlot(slotName)) {
						found = true;
						// We don't use Vector.addAll() for MIDP compatibility
						addAll(allFacets, superSchema.getAllFacets(slotName));
					}
				}
			}
		}
		else {
			found = true;
		}
		
		return (found ? allFacets : null);
	}

	private final void addAll(Vector v1, Vector v2) {
		for (int i = 0; i < v2.size(); ++i) {
			v1.addElement(v2.elementAt(i));
		}
	}
	
	private final SlotDescriptor getOwnSlot(CaseInsensitiveString ciName) {
		return (slots != null ? (SlotDescriptor) slots.get(ciName) : null);
	}

	private final SlotDescriptor getSlot(CaseInsensitiveString ciName) {
		SlotDescriptor dsc = getOwnSlot(ciName);
		if (dsc == null) {
			if (superSchemas != null) {
				for (int i = 0; i < superSchemas.size(); ++i) {
					ObjectSchemaImpl sc = (ObjectSchemaImpl) superSchemas.elementAt(i);
					dsc = sc.getSlot(ciName);
					if (dsc != null) {
						break;
					}
				}
			}
		}
		return dsc;
	}
}

⌨️ 快捷键说明

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