segmentation.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,418 行 · 第 1/4 页

JAVA
1,418
字号
package org.mindswap.swoop.refactoring;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.mindswap.swoop.SwoopModel;
import org.mindswap.swoop.reasoner.PelletReasoner;
import org.mindswap.swoop.utils.owlapi.AxiomCollector;
import org.mindswap.swoop.utils.owlapi.CorrectedRDFRenderer;
import org.mindswap.swoop.utils.owlapi.DefaultShortFormProvider;
import org.mindswap.swoop.utils.owlapi.OWLDescriptionFinder;
import org.mindswap.swoop.utils.owlapi.OWLOntBuilder;
import org.mindswap.swoop.utils.owlapi.SignatureCollector;
import org.semanticweb.owl.impl.model.OWLDataFactoryImpl;
import org.semanticweb.owl.impl.model.OWLDataPropertyInstanceImpl;
import org.semanticweb.owl.impl.model.OWLDataPropertyRangeAxiomImpl;
import org.semanticweb.owl.impl.model.OWLFunctionalPropertyAxiomImpl;
import org.semanticweb.owl.impl.model.OWLIndividualTypeAssertionImpl;
import org.semanticweb.owl.impl.model.OWLInverseFunctionalPropertyAxiomImpl;
import org.semanticweb.owl.impl.model.OWLInversePropertyAxiomImpl;
import org.semanticweb.owl.impl.model.OWLObjectPropertyInstanceImpl;
import org.semanticweb.owl.impl.model.OWLObjectPropertyRangeAxiomImpl;
import org.semanticweb.owl.impl.model.OWLPropertyDomainAxiomImpl;
import org.semanticweb.owl.impl.model.OWLSubPropertyAxiomImpl;
import org.semanticweb.owl.impl.model.OWLSymmetricPropertyAxiomImpl;
import org.semanticweb.owl.impl.model.OWLTransitivePropertyAxiomImpl;
import org.semanticweb.owl.io.ShortFormProvider;
import org.semanticweb.owl.model.OWLAnd;
import org.semanticweb.owl.model.OWLCardinalityRestriction;
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.OWLDataFactory;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDataPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLDataRange;
import org.semanticweb.owl.model.OWLDataSomeRestriction;
import org.semanticweb.owl.model.OWLDataValue;
import org.semanticweb.owl.model.OWLDescription;
import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owl.model.OWLDisjointClassesAxiom;
import org.semanticweb.owl.model.OWLEntity;
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.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.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLObjectQuantifiedRestriction;
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.OWLRestriction;
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.AddEntity;
import org.semanticweb.owl.model.change.AddEquivalentClass;
import org.semanticweb.owl.model.change.AddPropertyAxiom;
import org.semanticweb.owl.model.change.ChangeVisitor;
import org.semanticweb.owl.model.change.RemoveClassAxiom;
import org.semanticweb.owl.model.change.RemovePropertyAxiom;
import org.semanticweb.owl.model.change.SetFunctional;
import org.semanticweb.owl.model.change.SetSymmetric;
import org.semanticweb.owl.model.change.SetTransitive;
import org.semanticweb.owl.model.helper.OWLBuilder;

public class Segmentation {
		protected SwoopModel swoopModel;
		protected boolean DEBUG;
		private OWLClass nothing;
		private OWLClass thing;
		private OWLOntology source;
		private int nlocalityChecks;
		private boolean dualConcepts;
		private boolean dualRoles;
		private Map classToModule; //Map from each class to the set of axioms in its module
		
		//Indices we need
		public Set allAxioms; //all the axioms in the ontology;
		public Map axSignature;
		public Map sigToAxioms;
		//
		
		public Segmentation(OWLOntology source) throws OWLException{
			this.source = source;
			OWLDataFactory df = source.getOWLDataFactory();
			this.DEBUG = false;
			this.dualConcepts = false;
			this.dualRoles = false;
			this.thing = df.getOWLThing();
			this.nothing = df.getOWLNothing();
		}
		
		public Segmentation(OWLOntology source, boolean dualConcepts, boolean dualRoles) throws URISyntaxException, OWLException {
			this.source = source;
			this.DEBUG = false;
			OWLDataFactory df = source.getOWLDataFactory();
			this.thing = df.getOWLThing();
			this.nothing = df.getOWLNothing();
			this.dualConcepts = dualConcepts;
			this.dualRoles = dualRoles;
			
			//
			System.out.println("Getting axioms in ontology");
			this.allAxioms = this.getAxiomsInOntology(source);
			System.out.println("Getting signature of axioms");
			this.axSignature = this.axiomsToSignature(allAxioms);
			System.out.println("Getting map from signature to axioms");
			this.sigToAxioms = this.signatureToAxioms(allAxioms, axSignature);
			//
		}
		
		public void setDualConcepts(boolean b){
			dualConcepts = b;
		}
		
		public void setDualRoles(boolean b){
			dualRoles = b;
		}
		
		
		public Set getAllAxioms(){
			return allAxioms;
		}
		
		public Map getAxiomsToSignature(){
			return axSignature;
		}
		
		public Map getSignatureToAxioms(){
			return sigToAxioms;
		}
		
		public OWLDescription makePropositional(OWLDescription desc) throws OWLException, URISyntaxException{
			OWLDataFactory df = source.getOWLDataFactory();
			
			if(desc instanceof OWLClass)
				return desc;
			
			if (desc instanceof OWLNot){
				OWLDescription not = df.getOWLNot(makePropositional(
						((OWLNot)desc).getOperand()));
				return not;
				//return(builder.complementOf(replaceBottom(((OWLNot)desc).getOperand(), sig)));
			}
			
			if (desc instanceof OWLAnd){
				Set operands = new HashSet();
				operands = ((OWLAnd)desc).getOperands();
				OWLDescription conjunction = df.getOWLAnd(makePropositional(operands));
                return conjunction;  			
			}
			
			if (desc instanceof OWLOr){
				Set operands = new HashSet();
				operands = ((OWLOr)desc).getOperands();
				OWLDescription disjunction = df.getOWLOr(makePropositional(operands));
                return disjunction;  			
			}
			
			if(desc instanceof OWLObjectSomeRestriction){
				OWLDescription res = makePropositional(((OWLObjectSomeRestriction)desc).getDescription());
				OWLDescription prop = df.getOWLClass(((OWLObjectSomeRestriction)desc).getProperty().getURI());
				Set conjuncts = new HashSet();
				conjuncts.add(res);
				conjuncts.add(prop);
				return df.getOWLAnd(conjuncts); 
			}
			if(desc instanceof OWLDataSomeRestriction){
				OWLDescription prop = df.getOWLClass(((OWLObjectSomeRestriction)desc).getProperty().getURI());
				return prop;
			}
			if(desc instanceof OWLObjectAllRestriction){
				OWLDescription res = (OWLDescription)makePropositional(((OWLObjectAllRestriction)desc).getDescription());
				OWLDescription prop = df.getOWLNot(df.getOWLClass(((OWLObjectAllRestriction)desc).getProperty().getURI()));
				Set disjuncts = new HashSet();
				disjuncts.add(res);
				disjuncts.add(prop);
				return df.getOWLOr(disjuncts); 
			}
			
			if(desc instanceof OWLCardinalityRestriction){
				if(((OWLCardinalityRestriction)desc).isAtLeast()){
					OWLDescription prop = df.getOWLClass(((OWLCardinalityRestriction)desc).getProperty().getURI());
					return prop; 
				}
				if(((OWLCardinalityRestriction)desc).isAtMost()){
					OWLDescription prop = df.getOWLNot(df.getOWLClass(((OWLCardinalityRestriction)desc).getProperty().getURI()));
					return prop; 
				}
				if(((OWLCardinalityRestriction)desc).isExactly()){
					OWLDescription prop = df.getOWLNothing();
					return prop; 
				}
			}	
			
			return null;
			
			
		}
		
		//Takes an OWLDescription and a signature replaces by bottom the entities not in the signature
		public OWLDescription replaceBottom(OWLDescription desc, Set sig) throws OWLException, URISyntaxException{
			
			OWLDataFactory df = source.getOWLDataFactory();
			
			if(desc instanceof OWLClass){
				if(sig.contains(((OWLClass)desc)))
					return desc;
				else
					return this.nothing;
			}
			if(desc instanceof OWLObjectSomeRestriction || desc instanceof OWLDataSomeRestriction){
				if(sig.contains(((OWLRestriction)desc).getProperty())){
					if (desc instanceof OWLObjectSomeRestriction){
						OWLDescription res = df.getOWLObjectSomeRestriction((OWLObjectProperty)((OWLRestriction)desc).getProperty(), 
								replaceBottom(((OWLObjectSomeRestriction)desc).getDescription(), sig));
						return res;
					}
					return desc;
				}
				else
					return this.nothing;	
			}
			
			if(desc instanceof OWLObjectAllRestriction || desc instanceof OWLDataAllRestriction){
				if(sig.contains(((OWLRestriction)desc).getProperty())){
					if (desc instanceof OWLObjectAllRestriction){
						OWLDescription res = df.getOWLObjectAllRestriction((OWLObjectProperty)((OWLRestriction)desc).getProperty(), 
								replaceBottom(((OWLObjectAllRestriction)desc).getDescription(), sig));
						return res;
					}
					return desc;
				}
				else
					return this.thing;	
			}
			
			
			if (desc instanceof OWLCardinalityRestriction)
			{
				try {
					if(((OWLCardinalityRestriction)desc).isAtLeast() || ((OWLCardinalityRestriction)desc).isExactly())
						if(sig.contains(((OWLCardinalityRestriction)desc).getProperty()))
							return desc;
						else
							return this.nothing;
					else
						if(sig.contains(((OWLCardinalityRestriction)desc).getProperty()))
							return desc;
						else
							return this.thing;
						
				} catch (OWLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
			if (desc instanceof OWLNot){
				OWLDescription not = df.getOWLNot(replaceBottom(
						((OWLNot)desc).getOperand(),sig));
				return not;
				//return(builder.complementOf(replaceBottom(((OWLNot)desc).getOperand(), sig)));
			}
			
			if (desc instanceof OWLAnd){
				Set operands = new HashSet();
				operands = ((OWLAnd)desc).getOperands();
				OWLDescription conjunction = df.getOWLAnd(replaceBottom(operands, sig));
                return conjunction;  			
			}
			
			if (desc instanceof OWLOr){
				Set operands = new HashSet();
				operands = ((OWLOr)desc).getOperands();
				OWLDescription disjunction = df.getOWLOr(replaceBottom(operands, sig));
                return disjunction;  			
			}
			
			return null;
			
		}
		
		public Set makePropositional(Set s) throws OWLException, URISyntaxException{
			Set result = new HashSet();
			Iterator iter = s.iterator();
			while(iter.hasNext()){
				
				OWLObject desc = (OWLObject)iter.next();
				if(desc == null)
					System.out.println("Error");
				
				if(desc instanceof OWLDescription){
					OWLDescription prop = makePropositional((OWLDescription)desc);
					if(prop!= null)
						result.add(prop);
				}
				if(desc instanceof OWLClassAxiom){
					OWLObject ax = makePropositional((OWLClassAxiom)desc);
					if(ax!= null)
						result.add(ax);
				}
				if(desc instanceof OWLPropertyAxiom){
					OWLObject ax = makePropositional((OWLPropertyAxiom)desc);
					if(ax!= null)
						result.add(ax);
				}
				
			}
			return result;
		}
		
		
		
		public Set replaceBottom(Set s, Set sig) throws OWLException, URISyntaxException{
			Set result = new HashSet();
			Iterator iter = s.iterator();
			while(iter.hasNext()){
				OWLDescription desc = (OWLDescription)iter.next();
				result.add(replaceBottom(desc, sig));

⌨️ 快捷键说明

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