owlontbuilder.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 733 行 · 第 1/3 页

JAVA
733
字号
//The MIT License
//
// Copyright (c) 2004 Mindswap Research Group, University of Maryland, College Park
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

/*
 * Created on Oct 29, 2005
 * Author: Aditya Kalyanpur
 */
package org.mindswap.swoop.utils.owlapi;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.mindswap.swoop.renderer.SwoopRenderingVisitor;
import org.semanticweb.owl.impl.model.OWLInversePropertyAxiomImpl;
import org.semanticweb.owl.model.OWLAnd;
import org.semanticweb.owl.model.OWLAnnotationProperty;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLClassAxiom;
import org.semanticweb.owl.model.OWLDataAllRestriction;
import org.semanticweb.owl.model.OWLDataCardinalityRestriction;
import org.semanticweb.owl.model.OWLDataEnumeration;
import org.semanticweb.owl.model.OWLDataFactory;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDataPropertyInstance;
import org.semanticweb.owl.model.OWLDataPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLDataSomeRestriction;
import org.semanticweb.owl.model.OWLDataType;
import org.semanticweb.owl.model.OWLDataValue;
import org.semanticweb.owl.model.OWLDataValueRestriction;
import org.semanticweb.owl.model.OWLDescription;
import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owl.model.OWLDisjointClassesAxiom;
import org.semanticweb.owl.model.OWLEnumeration;
import org.semanticweb.owl.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owl.model.OWLEquivalentPropertiesAxiom;
import org.semanticweb.owl.model.OWLException;
import org.semanticweb.owl.model.OWLFunctionalPropertyAxiom;
import org.semanticweb.owl.model.OWLIndividual;
import org.semanticweb.owl.model.OWLIndividualAxiom;
import org.semanticweb.owl.model.OWLIndividualTypeAssertion;
import org.semanticweb.owl.model.OWLInverseFunctionalPropertyAxiom;
import org.semanticweb.owl.model.OWLInversePropertyAxiom;
import org.semanticweb.owl.model.OWLNot;
import org.semanticweb.owl.model.OWLObject;
import org.semanticweb.owl.model.OWLObjectAllRestriction;
import org.semanticweb.owl.model.OWLObjectCardinalityRestriction;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLObjectPropertyInstance;
import org.semanticweb.owl.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLObjectSomeRestriction;
import org.semanticweb.owl.model.OWLObjectValueRestriction;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOr;
import org.semanticweb.owl.model.OWLProperty;
import org.semanticweb.owl.model.OWLPropertyAxiom;
import org.semanticweb.owl.model.OWLPropertyDomainAxiom;
import org.semanticweb.owl.model.OWLSameIndividualsAxiom;
import org.semanticweb.owl.model.OWLSubClassAxiom;
import org.semanticweb.owl.model.OWLSubPropertyAxiom;
import org.semanticweb.owl.model.OWLSymmetricPropertyAxiom;
import org.semanticweb.owl.model.OWLTransitivePropertyAxiom;
import org.semanticweb.owl.model.change.AddClassAxiom;
import org.semanticweb.owl.model.change.AddDataPropertyInstance;
import org.semanticweb.owl.model.change.AddDataPropertyRange;
import org.semanticweb.owl.model.change.AddDomain;
import org.semanticweb.owl.model.change.AddEntity;
import org.semanticweb.owl.model.change.AddIndividualAxiom;
import org.semanticweb.owl.model.change.AddIndividualClass;
import org.semanticweb.owl.model.change.AddInverse;
import org.semanticweb.owl.model.change.AddObjectPropertyInstance;
import org.semanticweb.owl.model.change.AddObjectPropertyRange;
import org.semanticweb.owl.model.change.AddPropertyAxiom;
import org.semanticweb.owl.model.change.ChangeVisitor;
import org.semanticweb.owl.model.change.OntologyChange;
import org.semanticweb.owl.model.change.RemoveClassAxiom;
import org.semanticweb.owl.model.change.RemoveDataPropertyInstance;
import org.semanticweb.owl.model.change.RemoveDataPropertyRange;
import org.semanticweb.owl.model.change.RemoveDomain;
import org.semanticweb.owl.model.change.RemoveEquivalentClass;
import org.semanticweb.owl.model.change.RemoveIndividualAxiom;
import org.semanticweb.owl.model.change.RemoveIndividualClass;
import org.semanticweb.owl.model.change.RemoveInverse;
import org.semanticweb.owl.model.change.RemoveObjectPropertyInstance;
import org.semanticweb.owl.model.change.RemoveObjectPropertyRange;
import org.semanticweb.owl.model.change.RemovePropertyAxiom;
import org.semanticweb.owl.model.change.RemoveSuperClass;
import org.semanticweb.owl.model.change.SetFunctional;
import org.semanticweb.owl.model.change.SetInverseFunctional;
import org.semanticweb.owl.model.change.SetSymmetric;
import org.semanticweb.owl.model.change.SetTransitive;
import org.semanticweb.owl.model.helper.OWLBuilder;
import org.semanticweb.owl.model.helper.OWLObjectVisitorAdapter;

/**
 * @author Aditya
 * This class is used to build a new ontology given axioms from another ontology.
 * It passes each axiom to visit(OWLObject) which aligns axiom parameters 
 * relative to the new ontology -> creates a new axiom change and applies the change
 *
 */

public class OWLOntBuilder extends OWLObjectVisitorAdapter implements SwoopRenderingVisitor
//Uncomment for explanation
//,OWLExtendedObjectVisitor
{

	public OWLOntology currentOnt;
	public OWLDataFactory currentDF;
	public boolean addAxiom = true; // toggle whether to add or remove axiom
	public List changes = new ArrayList();
	
	public OWLOntBuilder() {
		try {
			OWLBuilder builder = new OWLBuilder();
			builder.createOntology(new URI("http://www.mindswap.org/test.owl"), new URI("http://www.mindswap.org/test.owl"));
			this.currentOnt = builder.getOntology();
			this.currentDF = currentOnt.getOWLDataFactory();
			this.changes = new ArrayList();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public OWLOntBuilder(URI uri) {
		try {
			OWLBuilder builder = new OWLBuilder();
			builder.createOntology(uri, uri);
			this.currentOnt = builder.getOntology();
			this.currentDF = currentOnt.getOWLDataFactory();
			this.changes = new ArrayList();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public OWLOntology getCurrentOntology(){
		return this.currentOnt;
	}
	
	public OWLDataFactory getCurrentDataFactory(){
		return this.currentDF;
	}
	
	
	
	public OWLOntBuilder(OWLOntology ont) {
		try {
			this.currentOnt = ont;
			this.currentDF = currentOnt.getOWLDataFactory();
			this.changes = new ArrayList();
		}
		catch (OWLException ex) {
			ex.printStackTrace();
		}
	}
	
	public OWLOntology buildOntologyFromAxioms(Set axioms) {
		try {
			for (Iterator iter = axioms.iterator(); iter.hasNext();) {
				OWLObject axiom = (OWLObject) iter.next();
				if(axiom instanceof OWLEquivalentClassesAxiom)
					this.visit((OWLEquivalentClassesAxiom)axiom);
				if(axiom instanceof OWLDisjointClassesAxiom)
					this.visit((OWLDisjointClassesAxiom)axiom);
				if(axiom instanceof OWLSubClassAxiom)
					this.visit((OWLSubClassAxiom)axiom);
				if(axiom instanceof OWLEquivalentPropertiesAxiom)
					this.visit((OWLEquivalentPropertiesAxiom)axiom);
				if(axiom instanceof OWLSubPropertyAxiom)
					this.visit((OWLSubPropertyAxiom)axiom);
				if(axiom instanceof OWLFunctionalPropertyAxiom)
					this.visit((OWLFunctionalPropertyAxiom)axiom);
				if(axiom instanceof OWLInverseFunctionalPropertyAxiom)
					this.visit((OWLInverseFunctionalPropertyAxiom)axiom);
				if(axiom instanceof OWLTransitivePropertyAxiom)
					this.visit((OWLTransitivePropertyAxiom)axiom);
				if(axiom instanceof OWLSymmetricPropertyAxiom)
					this.visit((OWLSymmetricPropertyAxiom)axiom);
				if(axiom instanceof OWLPropertyDomainAxiom)
					this.visit((OWLPropertyDomainAxiom)axiom);
				if(axiom instanceof OWLObjectPropertyRangeAxiom)
					this.visit((OWLObjectPropertyRangeAxiom)axiom);
				if(axiom instanceof OWLDataPropertyRangeAxiom)
					this.visit((OWLDataPropertyRangeAxiom)axiom);
				if(axiom instanceof OWLInversePropertyAxiom)
					this.visit((OWLInversePropertyAxiom)axiom);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.currentOnt;
	}
	
	public String result() {
		return "";
	}

	public OWLClass visitClass( OWLClass clazz ) throws OWLException {
		OWLClass currentDesc = null;
		// special bypass for owl:Thing, or owl:Nothing
		if (clazz.getURI().equals(currentDF.getOWLNothing().getURI())) {
			return currentDF.getOWLNothing();
		}
		else
		if (clazz.getURI().equals(currentDF.getOWLThing().getURI())) {
			return currentDF.getOWLThing();
		}
		else
		if ((currentDesc = currentOnt.getClass(clazz.getURI()))==null) {
			// create a new class
			currentDesc = currentDF.getOWLClass(clazz.getURI());
			AddEntity ae = new AddEntity(currentOnt, (OWLClass) currentDesc, null);
			ae.accept((ChangeVisitor) currentOnt);
			this.changes.add(ae);
		}
		return currentDesc;
	}
	
	public OWLIndividual visitIndividual( OWLIndividual ind ) throws OWLException {

⌨️ 快捷键说明

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