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

📄 owlparser.java

📁 toocom源代码,主要应用在本体匹配方面!
💻 JAVA
字号:
package toocom.iotools;

import java.io.*;
import java.util.*;
import toocom.ocgl.*;
import java.net.*;

import org.semanticweb.owl.io.owl_rdf.OWLRDFParser;
import org.semanticweb.owl.io.*;
import org.semanticweb.owl.model.*;
import org.semanticweb.owl.impl.model.*;
import org.semanticweb.owl.util.OWLManager;
import org.semanticweb.owl.util.OWLConnection;


/**
 * This class offers methods for parsing and saving OWL files.
 *
 * @author Fr閐閞ic F黵st
 */ 
public abstract class OWLParser{
	
	/** Returns the fragment of an URI, if it exists. Returns the string after the last
	 *  / otherwise. */
	private static String getIdentifier(URI u){
		String result = u.getFragment();
		if(result != null) return result;
		else{
			return u.getPath().substring(u.getPath().lastIndexOf("/") + 1);
		}
	}
	
	/** Load an ontology from the given URI. */
	public static Ontology loadOntology(URI fileName,Language l,boolean trace) throws ParserException,OWLException,URISyntaxException{
		OWLConnection connection = OWLManager.getOWLConnection();
		OWLRDFParser parser = new OWLRDFParser();
		parser.setConnection(connection);
		OWLOntology owlOnto = parser.parseOntology(fileName);
		Ontology onto = new Ontology(new Terms(fileName.getPath()),true);
		if(trace) System.out.println("Loading OWL ontology from file " + fileName.toString());
		// Creation of concepts. Concepts are OWL classes.
		for(Iterator i = owlOnto.getClasses().iterator();i.hasNext();){
			OWLClass c = (OWLClass) i.next();
			String cName = OWLParser.getIdentifier(c.getURI());
			if(!cName.equals("Thing"))
				if(trace) System.out.println("Creating Class " + OWLParser.getIdentifier(c.getURI()));
				onto.newConceptType(new Terms(OWLParser.getIdentifier(c.getURI())));
			// The Thing Class is merged with the Universal concept
		}
		// Linking to the super concepts. Super concepts are super classes. 
		for(Iterator i = owlOnto.getClasses().iterator();i.hasNext();){
			OWLClass c = (OWLClass) i.next();
			if(!OWLParser.getIdentifier(c.getURI()).equals("Thing")){
				ConceptType concept = onto.getConceptType(OWLParser.getIdentifier(c.getURI()),l);
				for(Iterator j = c.getSuperClasses(owlOnto).iterator();j.hasNext();){
					OWLDescription superCon = (OWLDescription) j.next();
					if(superCon instanceof OWLClassImpl){
						String superName = OWLParser.getIdentifier(((OWLClassImpl) superCon).getURI());
						if(!superName.equals("Thing")){
							if(trace) System.out.println("Setting " + superName + " super class of " + OWLParser.getIdentifier(c.getURI()));
							concept.addParent(onto.getConceptType(superName,l));
						}
						else{
							if(trace) System.out.println("Setting Universal super class of " + OWLParser.getIdentifier(c.getURI()));
							concept.addParent(onto.getUniversalConceptType());
						}
					}
				}
				// If no super Class exists, the concept is sub Class of Universal
				if(concept.getParents().size() == 0){
					if(trace) System.out.println("Setting Universal super class of " + OWLParser.getIdentifier(c.getURI()));
					concept.addParent(onto.getUniversalConceptType());
				}
			}
		}
		// Setting disjunction between concepts. Disjunctions are supposed to link only two classes.
		for(Iterator i = owlOnto.getClassAxioms().iterator();i.hasNext();){
			OWLClassAxiom axiom = (OWLClassAxiom) i.next();
			if(axiom instanceof OWLDisjointClassesAxiomImpl){
				Iterator j = ((OWLDisjointClassesAxiom) axiom).getDisjointClasses().iterator();
				if(j.hasNext()){
					OWLDescription desc1 = (OWLDescription) j.next();
					if(j.hasNext()){
						OWLDescription desc2 = (OWLDescription) j.next();
						if((desc1 instanceof OWLClassImpl) && (desc2 instanceof OWLClassImpl)){
								if(trace) System.out.println("Setting disjunction between " + OWLParser.getIdentifier(((OWLClassImpl) desc1).getURI()) + " and " + OWLParser.getIdentifier(((OWLClassImpl) desc2).getURI()));
								new ConceptDisjunction(onto.getConceptType(OWLParser.getIdentifier(((OWLClassImpl) desc1).getURI()),l),onto.getConceptType(OWLParser.getIdentifier(((OWLClassImpl) desc2).getURI()),l));
						}
					}
				}
			}
		}
		// Creation of relations. 
		// Case 1 : relations are OWL object properties.
		for(Iterator i = owlOnto.getObjectProperties().iterator();i.hasNext();){
			OWLObjectProperty p = (OWLObjectProperty) i.next();
			if(trace) System.out.println("Creating Relation " + OWLParser.getIdentifier(p.getURI()));
			RelationType rt = onto.newRelationType(new Terms(OWLParser.getIdentifier(p.getURI())),2);
			// Setting relation domain (first Class encountered in domains list)
			Set set = p.getDomains(owlOnto);
			boolean test = false;
			for(Iterator j = set.iterator();j.hasNext() && !test;){
				OWLDescription desc = (OWLDescription) j.next();
				if(desc instanceof OWLClassImpl){
					if(trace) System.out.println("Setting domain " + OWLParser.getIdentifier(((OWLClassImpl) desc).getURI()));
					rt.getSignature().setConceptType(onto.getConceptType(OWLParser.getIdentifier(((OWLClassImpl) desc).getURI()),l),1);
					test = true;
				}
			}
			// If domain is not a simple Class, the domain is set to Universal
			if(rt.getSignature().getConceptType(1) == null){
				if(trace) System.out.println("Setting domain Universal");
				rt.getSignature().setConceptType(onto.getUniversalConceptType(),1);
			}
			
			// Traiter au moins le cas des unions de class pour domain
			
			// Setting relation range (first Class encountered in range list)
			set = p.getRanges(owlOnto);
			test = false;
			for(Iterator j = set.iterator();j.hasNext() && !test;){
				OWLDescription desc = (OWLDescription) j.next();
				if(desc instanceof OWLClassImpl){
					if(trace) System.out.println("Setting range " + OWLParser.getIdentifier(((OWLClassImpl) desc).getURI()));
					rt.getSignature().setConceptType(onto.getConceptType(OWLParser.getIdentifier(((OWLClassImpl) desc).getURI()),l),2);
					test = true;
				}
			}
			// If range is not a simple Class, the range is set to Universal
			if(rt.getSignature().getConceptType(2) == null){
				if(trace) System.out.println("Setting range Universal");
				rt.getSignature().setConceptType(onto.getUniversalConceptType(),2);
			}
						
			// Traiter au moins le cas des unions de class pour range
			// Setting symmetry and transitivity
			if(p.isSymmetric(owlOnto)){
				if(trace) System.out.println("Setting symmetry");
				new RelationSymmetry(rt);
			}
			if(p.isTransitive(owlOnto)){
				if(trace) System.out.println("Setting transitivity");
				new RelationTransitivity(rt);
			}
		}
		// Case 2 : relations are OWL datatype properties
		for(Iterator i = owlOnto.getDataProperties().iterator();i.hasNext();){
			OWLDataProperty p = (OWLDataProperty) i.next();
			if(OWLParser.getIdentifier(p.getURI()) != null){
				if(trace) System.out.println("Creating Relation " + OWLParser.getIdentifier(p.getURI()));
				RelationType rt = onto.newRelationType(new Terms(OWLParser.getIdentifier(p.getURI())),2);
				// Setting relation domain (first Class encountered in domains list)
				Set set = p.getDomains(owlOnto);
				boolean test = false;
				for(Iterator j = set.iterator();j.hasNext() && !test;){
					OWLObject domain = (OWLObject) j.next();
					if(domain instanceof OWLClassImpl){
						OWLClassImpl desc = (OWLClassImpl) domain;
						if(trace) System.out.println("Setting domain " + OWLParser.getIdentifier(((OWLClassImpl) desc).getURI()));
						rt.getSignature().setConceptType(onto.getConceptType(OWLParser.getIdentifier(desc.getURI()),l),1);
						test = true;
					}
				}
				// If domain is a datatype, the Universal concept is used instead
				if(rt.getSignature().getConceptType(1) == null){
					if(trace) System.out.println("Setting domain Universal");
					rt.getSignature().setConceptType(onto.getUniversalConceptType(),1);
				}
			
				// Traiter au moins le cas des unions de class pour domain
				
				// Setting relation range (first Class encountered in range list)
				set = p.getRanges(owlOnto);
				test = false;
				for(Iterator j = set.iterator();j.hasNext() && !test;){
					OWLObject range = (OWLObject) j.next();
					if(range instanceof OWLClassImpl){
						OWLClassImpl desc = (OWLClassImpl) range;
						if(trace) System.out.println("Setting range " + OWLParser.getIdentifier(((OWLClassImpl) desc).getURI()));
						rt.getSignature().setConceptType(onto.getConceptType(OWLParser.getIdentifier(desc.getURI()),l),2);
						test = true;
					}
				}
				// If range is a datatype, the Universal concept is used instead
				if(rt.getSignature().getConceptType(2) == null){
					if(trace) System.out.println("Setting range Universal");
					rt.getSignature().setConceptType(onto.getUniversalConceptType(),2);
				}
							
				// Traiter au moins le cas des unions de class pour range
			}
		}
		// Linking to the super relations. Super relations are super properties. 
		for(Iterator i = owlOnto.getPropertyAxioms().iterator();i.hasNext();){
			OWLPropertyAxiom axiom = (OWLPropertyAxiom) i.next();
			if(axiom instanceof OWLSubPropertyAxiomImpl){
				OWLProperty sub = ((OWLSubPropertyAxiomImpl) axiom).getSubProperty();
				OWLProperty sup = ((OWLSubPropertyAxiomImpl) axiom).getSuperProperty();
				RelationType rtSub = onto.getRelationType(OWLParser.getIdentifier(sub.getURI()),l);
				RelationType rtSup = onto.getRelationType(OWLParser.getIdentifier(sup.getURI()),l);
				if(trace) System.out.println("Setting " + OWLParser.getIdentifier(sup.getURI()) + " parent of " + OWLParser.getIdentifier(sub.getURI()));
				rtSub.addParent(rtSup);
			}
		}
		// Descente de la hi閞archie des rel pour propager les signatures
		
		// Settings cardinalities
		for(Iterator i = owlOnto.getClasses().iterator();i.hasNext();){
			OWLClass c = (OWLClass) i.next();
			ConceptType concept = onto.getConceptType(OWLParser.getIdentifier(c.getURI()),l);
			for(Iterator j = c.getSuperClasses(owlOnto).iterator();j.hasNext();){
				OWLDescription superCon = (OWLDescription) j.next();
				if(superCon instanceof OWLCardinalityRestriction){
					OWLCardinalityRestriction card = (OWLCardinalityRestriction) superCon;
					String propertyName = OWLParser.getIdentifier(card.getProperty().getURI());
					if(card.isAtLeast()){
						if(trace) System.out.println("Setting min cardinality on " + propertyName + " with value " + card.getAtLeast() + " at index 2");
						new RelationMinCardinality(onto.getRelationType(propertyName,l),2,card.getAtLeast());
					}
					if(card.isAtMost()){
						if(trace) System.out.println("Setting max cardinality on " + propertyName + " with value " + card.getAtMost() + " at index 2");
						new RelationMaxCardinality(onto.getRelationType(propertyName,l),2,card.getAtMost());
					}
				}
			}
		}
		
		
		
		
		
		
				
		
		return onto;
	}

	
}

⌨️ 快捷键说明

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