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

📄 ontology.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					if(logger.isLoggable(Logger.FINE))
						logger.log(Logger.FINE,"Base ontology # "+i+" for ontology "+getName()+" is null");
				}
				ret = base[i].getSchema(name);
				if (ret != null) {
					return ret;
				}
			}
		}
		return ret;
	}

	/**
	 * Converts an abstract descriptor to a Java object of the proper class.
	 * @param abs the abstract descriptor.
	 * @return the object
	 * @throws UngroundedException if the abstract descriptor contains a
	 * variable
	 * @throws OntologyException if some mismatch with the schema is found
	 * @see #fromObject(Object)
	 */
	public Object toObject(AbsObject abs) throws OntologyException, UngroundedException {
		if (abs == null) {
			return null;
		}

		try {
			return toObject(abs, abs.getTypeName().toLowerCase(), this);
		}
		catch (UnknownSchemaException use) {
			// If we get this exception here, the schema is globally unknown
			// (i.e. is unknown in the reference ontology and all its base
			// ontologies) --> throw a generic OntologyException
			throw new OntologyException("No schema found for type "+abs.getTypeName());
		}
		catch (OntologyException oe) {
			// This ontology can have been thrown as the Abs descriptor is
			// ungrounded. In this case an UngroundedException must be thrown.
			// Note that we don't check ungrouding before to speed up performances
			if (!abs.isGrounded()) {
				throw new UngroundedException();
			}
			else {
				throw oe;
			}
		}
	}

	/**
	 * Converts a Java object into a proper abstract descriptor.
	 * @param obj the object
	 * @return the abstract descriptor.
	 * @throws OntologyException if some mismatch with the schema is found
	 * @see #toObject(AbsObject)
	 */
	public AbsObject fromObject(Object obj) throws OntologyException {
		if (obj == null) {
			return null;
		}

		try {
			return fromObject(obj, this);
		}
		catch (UnknownSchemaException use) {
			// If we get this exception here, the schema is globally unknown
			// (i.e. is unknown in the reference ontology and all its base
			// ontologies) --> throw a generic OntologyException
			throw new OntologyException("No schema found for class "+obj.getClass().getName());
		}
	}

	/**
	 * Retrieves the concrete class associated with element <code>name</code>
	 * in this ontology. The search is extended to the base ontologies
	 * @param name the name of the schema.
	 * @return the Java class or null if no schema called <code>name</code>
	 * is found or if no class is associated to that schema.
	 * @throws OntologyException if name is null
	 */
	public Class getClassForElement(String name) throws OntologyException {
		if (name == null) {
			throw new OntologyException("Null schema identifier");
		}

		Class ret = (Class) classes.get(name.toLowerCase());

		if (ret == null) {
			for (int i = 0; i < base.length; ++i) {
				ret = base[i].getClassForElement(name);
				if (ret != null) {
					return ret;
				}
			}
		}
		return ret;
	}

	//#APIDOC_EXCLUDE_BEGIN
	/**
	 * Converts an abstract descriptor to a Java object of the proper class.
	 * @param abs the abstract descriptor.
	 * @param lcType the type of the abstract descriptor to be translated
	 * aconverted into lower case. This is passed as parameters to avoid
	 * making the conversion to lower case for each base ontology.
	 * @param globalOnto The ontology this ontology is part of (i.e. the
	 * ontology that extends this ontology).
	 * @return the object
	 * @throws UnknownSchemaException If no schema for the abs descriptor
	 * to be translated is defined in this ontology.
	 * @throws UngroundedException if the abstract descriptor contains a
	 * variable
	 * @throws OntologyException if some mismatch with the schema is found      * ontology. In this case UnknownSchema
	 */
	protected Object toObject(AbsObject abs, String lcType, Ontology globalOnto) throws UnknownSchemaException, UngroundedException, OntologyException {

		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"Ontology "+getName()+". Abs is: "+abs);
		// Retrieve the schema
		ObjectSchema schema = (ObjectSchema) elements.get(lcType);
		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"Ontology "+getName()+". Schema is: "+schema);
		if (schema != null) {

			// Retrieve the java class
			Class javaClass = (Class) classes.get(lcType);
			if (javaClass == null) {
				throw new OntologyException("No java class associated to type "+abs.getTypeName());
			}
			if(logger.isLoggable(Logger.FINE))
				logger.log(Logger.FINE,"Ontology "+getName()+". Class is: "+javaClass.getName());

			// If the Java class is an Abstract descriptor --> just return abs
			if (absObjectClass.isAssignableFrom(javaClass)) {
				return abs;
			}

			if (introspector != null) {
				if(logger.isLoggable(Logger.FINE))
					logger.log(Logger.FINE,"Ontology "+getName()+". Try to internalise "+abs+" through "+introspector);
				return introspector.internalise(abs, schema, javaClass, globalOnto);
			}
		}

		// If we get here --> This ontology is not able to translate abs
		// --> Try to convert it using the base ontologies
		for (int i = 0; i < base.length; ++i) {
			try {
				return base[i].toObject(abs, lcType, globalOnto);
			}
			catch (UnknownSchemaException use) {
				// Try the next one
			}
		}

		throw new UnknownSchemaException();
	}

	/**
	 * Converts a Java object into a proper abstract descriptor.
	 * @param obj the object
	 * @param globalOnto The ontology this ontology is part of (i.e. the
	 * ontology that extends this ontology).
	 * @return the abstract descriptor.
	 * @throws UnknownSchemaException If no schema for the object to be
	 * translated is defined in this ontology.
	 * @throws OntologyException if some mismatch with the schema is found
	 */
	protected AbsObject fromObject(Object obj, Ontology globalOnto) throws UnknownSchemaException, OntologyException {

		// If obj is already an abstract descriptor --> just return it
		if (obj instanceof AbsObject) {
			return (AbsObject) obj;
		}

		// Retrieve the Java class
		Class        javaClass = obj.getClass();
		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"Ontology "+getName()+". Class is: "+javaClass);

		// Retrieve the schema
		ObjectSchema schema = (ObjectSchema) schemas.get(javaClass);
		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"Ontology "+getName()+". Schema is: "+schema);
		if (schema != null) {
			if (introspector != null) {
				if(logger.isLoggable(Logger.FINE))
					logger.log(Logger.FINE,"Ontology "+getName()+". Try to externalise "+obj+" through "+introspector);
				return introspector.externalise(obj, schema, javaClass, globalOnto);
			}
		}

		// If we get here --> This ontology is not able to translate obj
		// --> Try to convert it using the base ontologies
		for (int i = 0; i < base.length; ++i) {
			try {
				return base[i].fromObject(obj, globalOnto);
			}
			catch (UnknownSchemaException use) {
				// Try the next one
			}
		}

		throw new UnknownSchemaException();
	}
	//#APIDOC_EXCLUDE_END


	/////////////////////////
	// Utility static methods
	/////////////////////////

	/**
	 * Check whether a given object is a valid term.
	 * If it is an Aggregate (i.e. a <code>List</code>) it also check
	 * the elements.
	 * @throws OntologyException if the given object is not a valid term
	 */
	public static void checkIsTerm(Object obj) throws OntologyException {
		// FIXME: This method is likely to be removed as it does not add any value and creates problems
		// when using the Serializable Ontology
		/*if (obj instanceof String ||
    		  obj instanceof Boolean ||
    		  obj instanceof Integer ||
    		  obj instanceof Long ||
    		  //#MIDP_EXCLUDE_BEGIN
    		  obj instanceof Float ||
    		  obj instanceof Double ||
    		  //#MIDP_EXCLUDE_END
    		  obj instanceof Date ||
    		  obj instanceof Term) {
    		return;
    	}
    	if (obj instanceof List) {
    		Iterator it = ((List) obj).iterator();
    		while (it.hasNext()) {
    			checkIsTerm(it.next());
    		}
    		return;
    	}

    	// If we reach this point the object is not a term
    	throw new OntologyException("Object "+obj+" of class "+obj.getClass().getName()+" is not a term");
		 */
	}

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


	//#J2ME_EXCLUDE_BEGIN
	/**
	 * Retrieve the names of all concepts defined in this ontology (including extended ontologies).
	 * It should be noticed that an agent-action is itself a concept and therefore the returned list 
	 * also includes names of agent-actions defined in this ontology.
	 * @return the names of all concepts defined in this ontology (including extended ontologies)
	 */
	public List getConceptNames(){
		List names = new ArrayList();
		for (Enumeration e = elements.keys(); e.hasMoreElements();){
			String key  = (String)e.nextElement();
			if ((elements.get(key) instanceof ConceptSchema) ){
			  names.add(key);
			}
		}
		return names;
	}

	/**
	 * Retrieve the names of all agent actions defined in this ontology (including extended ontologies).
	 * @return the names of all agent actions defined in this ontology (including extended ontologies)
	 */
	public List getActionNames(){
		List names = new ArrayList();
		for (Enumeration e = elements.keys(); e.hasMoreElements();){
			String key  = (String)e.nextElement();
			if ((elements.get(key) instanceof AgentActionSchema) ){
				names.add(key);
			}
		}
		return names;
	}

	/**
	 * Retrieve the names of all predicatess defined in this ontology (including extended ontologies).
	 * @return the names of all predicatess defined in this ontology (including extended ontologies)
	 */
	public List getPredicateNames(){
		List names = new ArrayList();
		for (Enumeration e = elements.keys(); e.hasMoreElements();){
			String key  = (String)e.nextElement();
			if ((elements.get(key) instanceof PredicateSchema) ){
			  names.add(key);
			}
		}
		return names;
	}
	//#J2ME_EXCLUDE_END
}

⌨️ 快捷键说明

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