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

📄 ontology.java

📁 toocom源代码,主要应用在本体匹配方面!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package toocom.ocgl;

import java.util.*;

/**
 * This class represents the ontologies. An ontology contains a concept type hierarchy, a 
 * relation type hierarchy, a set of axioms and the graph built using this ontology. To create 
 * a new concept type, a new relation type, a new axiom, a new concept, a new relation, or a 
 * new graph, one have to use the new(Object) method where Object is the name of the classe
 * which represents the conceptual object to build. Some methods permit to build axioms which
 * represent conceptual primitive properties. Two particular conceptual primitives are allowed :
 * the universal concept type and the difference relation type.
 *
 * @author Fr閐閞ic F黵st
 */
public class Ontology extends NamedObject{

	private Hashtable conceptTypes;
	private Hashtable relationTypes;
	private Hashtable individuals;
	private Hashtable axioms;
	private RelationType difference;
	private RelationType identity;
	private ConceptType universal;


	/** Creates a new ontology and add the universal concept type, the difference relation
	 *  type and the identity relation type if full is true, only creates an empty ontology 
	 *  if full is false. */
	public Ontology(Terms t,boolean full){
		super(t);
		this.conceptTypes = new Hashtable();
		this.relationTypes = new Hashtable();
		this.individuals = new Hashtable();
		this.axioms = new Hashtable();
		if(full){
			this.universal = this.newConceptType(CGConstants.getUniversalTerms(),CGConstants.DEFAULT_CONCEPTUAL_OBJECT_X,CGConstants.DEFAULT_CONCEPTUAL_OBJECT_Y);
			new ConceptAbstraction(this.universal);
			this.difference = this.newRelationType(CGConstants.getDifferentTerms(),2,CGConstants.DEFAULT_CONCEPTUAL_OBJECT_X,CGConstants.DEFAULT_CONCEPTUAL_OBJECT_Y);
			this.difference.getSignature().setConceptType(this.universal,1);
			this.difference.getSignature().setConceptType(this.universal,2);
			new RelationIrreflexivity(this.difference);
			new RelationSymmetry(this.difference);
			this.identity = this.newRelationType(CGConstants.getIdenticalTerms(),2,CGConstants.DEFAULT_CONCEPTUAL_OBJECT_X+20,CGConstants.DEFAULT_CONCEPTUAL_OBJECT_Y+20);
			this.identity.getSignature().setConceptType(this.universal,1);
			this.identity.getSignature().setConceptType(this.universal,2);
			new RelationReflexivity(this.identity);
			new RelationSymmetry(this.identity);
			new RelationTransitivity(this.identity);
			new RelationExclusivity(this.difference,this.identity);
		}
	}

	public String toString(Language l){
		return this.getTerm(l);
	}
	
	public RelationType getDifferenceRelationType(){
		return this.difference;
	}
	
	public RelationType getIdentityRelationType(){
		return this.identity;
	}
	
	public ConceptType getUniversalConceptType(){
		return this.universal;
	}
	
	public void setDifferenceRelationType(RelationType rt){
		this.difference = rt;
	}
	
	public void setUniversalConceptType(ConceptType ct){
		this.universal = ct;
	}
	
	public void setIdentityRelationType(RelationType rt){
		this.identity = rt;
	}
	
	/** Returns the object of the ontology specified by the given id, null if any such object
	 *  is present in the ontology. */
	public NamedObject getObject(int id){
		NamedObject result = (NamedObject) conceptTypes.get(new Integer(id));
		if(result == null){
			result = (NamedObject) relationTypes.get(new Integer(id));
			if(result == null){
				result = (NamedObject) individuals.get(new Integer(id));
				if(result == null){
					result = (NamedObject) axioms.get(new Integer(id));
				}
			}
		}
		return result;
	}
	
	/** Returns the list of concept types. */
	public Collection getConceptTypes(){
		return conceptTypes.values();
	}
	
	/** Returns the concept type with the given id, null if any such concept type exists in
	 *  the ontology. */
	public ConceptType getConceptType(int id){
		Collection collec = conceptTypes.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			ConceptType ct = (ConceptType) i.next();
			if(ct.getId() == id) return ct;
		}
		return null;
	}
	
	/** Returns the first encountered concept type with the given label for the given language, 
	 *  null if any such concept type exists in the ontology. */
	public ConceptType getConceptType(String label,Language l){
		Collection collec = conceptTypes.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			ConceptType ct = (ConceptType) i.next();
			if(ct.getTerm(l).equals(label)) return ct;
		}
		return null;
	}
	
	/** Returns the list of labels (String) of existing concept types for the given language. */
	public LinkedList getConceptTypeLabels(Language l){
		LinkedList result = new LinkedList();
		Collection collec = conceptTypes.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			result.add(((ConceptType) i.next()).getTerm(l));
		}
		return result;
	}
	
	/** Returns the list of relation types. */
	public Collection getRelationTypes(){
		return relationTypes.values();
	}
	
	/** Returns the relation type with the given id, null if any such relation type exists in
	 *  the ontology. */
	public RelationType getRelationType(int id){
		Collection collec = relationTypes.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			RelationType rt = (RelationType) i.next();
			if(rt.getId() == id) return rt;
		}
		return null;
	}
	
	/** Returns the first encountered relation type with the given label for the given language,
	 *  null if any such relation type exists in the ontology. */
	public RelationType getRelationType(String label,Language l){
		Collection collec = relationTypes.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			RelationType rt = (RelationType) i.next();
			if(rt.getTerm(l).equals(label)) return rt;
		}
		return null;
	}
	
	/** Returns the list of relation types homonymous with rt for the given language. */
	public LinkedList getHomonymousRT(RelationType rt,Language l){
		LinkedList result = new LinkedList();
		for(Iterator i = this.getRelationTypes().iterator();i.hasNext();){
			RelationType reltype = (RelationType) i.next();
			if(!rt.equals(reltype) && rt.getTerm(l).equals(reltype.getTerm(l))) result.add(reltype);
		}
		return result;
	}
	
	/** Returns the first encountered relation type which the terms is t, returns 
	 *  null if no such relation type exists in the ontology. */
	public RelationType getRelationType(Terms t){
		for(Iterator i = relationTypes.values().iterator();i.hasNext();){
			RelationType rt = (RelationType) i.next();
			if(rt.getTerms().equals(t)) return rt;
		}
		return null;
	}
	
	/** Returns the list of labels (String) of existing concept types for the given language. */
	public LinkedList getRelationTypeLabels(Language l){
		LinkedList result = new LinkedList();
		Collection collec = relationTypes.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			result.add(((RelationType) i.next()).getTerm(l));
		}
		return result;
	}
	
	/** Returns the list of individuals. */
	public Collection getIndividuals(){
		return individuals.values();
	}
	
	/** Returns the list of individuals of the specified concept type. */
	public Collection getIndividuals(ConceptType ct){
		LinkedList result = new LinkedList();
		for(Enumeration i = individuals.elements();i.hasMoreElements();){
			Individual ind = (Individual) i.nextElement();
			if(ind.getType().equals(ct)) result.add(ind);
		}
		return result;
	}
	
	/** Returns the individual with the given id, null if any such individual exists in
	 *  the ontology. */
	public Individual getIndividual(int id){
		Collection collec = individuals.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			Individual ind = (Individual) i.next();
			if(ind.getId() == id) return ind;
		}
		return null;
	}
	
	/** Returns the first encountered individual with the given label for the given language,
	 *  null if any such individual exists in the ontology. */
	public Individual getIndividual(String label, Language l){
		Collection collec = individuals.values();
		for(Iterator i = collec.iterator();i.hasNext();){
			Individual ind = (Individual) i.next();
			if(ind.getTerm(l).equals(label)) return ind;
		}
		return null;
	}
	
	/** Returns the list of individual homonymous with ind for the given language. */
	public LinkedList getHomonymousIndividual(Individual ind,Language l){
		LinkedList result = new LinkedList();
		for(Iterator i = this.getIndividuals().iterator();i.hasNext();){
			Individual indBis = (Individual) i.next();
			if(!ind.equals(indBis) && ind.getTerm(l).equals(indBis.getTerm(l))) result.add(indBis);
		}
		return result;
	}
	
	/** Returns the list of concepts contained in axioms of the ontology. */
	public LinkedList getConcepts(){
		LinkedList result = new LinkedList();
		for(Iterator i = this.axioms.values().iterator();i.hasNext();){
			result.addAll(((Axiom) i.next()).getAllConcepts());
		}
		return result;
	}
	
	/** Returns the list of relations contained in axioms of the ontology. */
	public Collection getRelations(){
		LinkedList result = new LinkedList();
		for(Iterator i = this.axioms.values().iterator();i.hasNext();){
			result.addAll(((Axiom) i.next()).getAllRelations());
		}
		return result;
	}
	
	/** Returns the list of axiom schemata. */
	public Collection getAxiomSchemata(){
		LinkedList result = new LinkedList();
		for(Iterator i = this.conceptTypes.values().iterator();i.hasNext();){
			ConceptType ct = (ConceptType) i.next();
			for(Iterator j = ct.getAxiomSchemata().iterator();j.hasNext();){
				result.add(j.next());
			}
		}
		for(Iterator i = this.relationTypes.values().iterator();i.hasNext();){
			RelationType rt = (RelationType) i.next();
			for(Iterator j = rt.getAxiomSchemata().iterator();j.hasNext();){
				result.add(j.next());
			}
		}
		return result;
	}
	
	/** Returns the list of axioms. */
	public Collection getAxioms(){
		return axioms.values();
	}
	
	/** Returns the list of not finalized axioms. */
	public LinkedList getNotFinalizedAxioms(){
		LinkedList result = new LinkedList();
		for(Iterator i = this.axioms.values().iterator();i.hasNext();){
			Axiom a = (Axiom) i.next();
			if(!a.isFinalized()) result.add(a);
		}
		return result;
	}
	
	/** Returns the axiom in the given position in the list, null if i < 0 ou i >= number of axioms. */
	public Axiom getAxiom(int i){
		Object[] tab = axioms.values().toArray();
		if((i >= 0) && (i < tab.length)) return ((Axiom) (tab[i]));
		else return null;
	}
	
	/** Returns the index of the given axiom in the ontology, between 0 and (number of axioms
	 *  -1). Returns -1 if the axiom is not in the ontology. */
	public int getAxiomIndex(Axiom a){
		int cpt = 0;
		for(Iterator i = axioms.values().iterator();i.hasNext();cpt ++){
			if(((Axiom) i.next()).equals(a)) return cpt;
		}
		return -1;
	}
	
	/** Returns the first encountered axiom with the given label in the specified language, 
	 *  null if any. */
	public Axiom getAxiom(String label, Language l){
		for(Iterator i = axioms.values().iterator();i.hasNext();){
			Axiom a = (Axiom) i.next();

⌨️ 快捷键说明

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