pelletreasoner.java

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

JAVA
613
字号
//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.package org.mindswap.swoop.reasoner;import java.net.URI;import java.util.Calendar;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.TimeZone;import org.mindswap.pellet.EconnectedKB;import org.mindswap.pellet.owlapi.Reasoner;import org.mindswap.pellet.taxonomy.ClassifyProgress;import org.mindswap.pellet.taxonomy.DefaultClassifyProgress;import org.mindswap.pellet.taxonomy.Taxonomy;import org.mindswap.pellet.taxonomy.TaxonomyNode;import org.mindswap.pellet.utils.ATermUtils;import org.mindswap.pellet.utils.Timer;import org.mindswap.swoop.utils.ExpressivityChecker;import org.mindswap.swoop.utils.exceptions.UserCanceledException;import org.semanticweb.owl.io.ShortFormProvider;import org.semanticweb.owl.io.vocabulary.OWLVocabularyAdapter;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLDescription;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLIndividual;import org.semanticweb.owl.model.OWLIndividualAxiom;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.OWLSameIndividualsAxiom;import org.semanticweb.owl.model.helper.OntologyHelper;import aterm.ATermAppl;//import com.vladium.utils.IObjectProfileNode;//import com.vladium.utils.ObjectProfiler;/** * @author Evren Sirin */public class PelletReasoner extends Reasoner implements SwoopReasoner {    	public static boolean AUTO_CLASSIFY = true;    public boolean doExplanation = false; // glass box - clash detection    									  // and for dependency axioms    public DependencyReasoner depFinder;        private HashMap sameAs, differentFrom;       public synchronized void setOntologyWithoutClassif(OWLOntology ontology) throws OWLException 	{		// default call is to show progress bar (3rd argument)		setOntology( ontology, false, false);	}	    	public synchronized void setOntology(OWLOntology ontology) throws OWLException {	    Timer timers = new Timer("Pellet Processing");	    timers.start();		depFinder = null;		setOntology(ontology, true);		timers.stop();		System.out.println(timers);	}		public synchronized void setOntology(OWLOntology ontology, boolean realize) throws OWLException 	{		// default call is to show progress bar (3rd argument)		setOntology( ontology, realize, true);	}		public synchronized void setOntologyWithoutClassif(OWLOntology ontology, boolean realize) throws OWLException 	{		// default call is to show progress bar (3rd argument)		setOntology( ontology, false, false);	}		public synchronized void setOntology(OWLOntology ontology, boolean realize, boolean showProgress) throws OWLException {//	    System.out.println("Explanation: " + getDoExplanation());	    		super.setOntology(ontology);		//		PelletOptions.USE_PSEUDO_NOMINALS = true;		Taxonomy.DEBUG = false;//		PelletOptions.SHOW_CLASSIFICATION_PROGRESS = true;		if(isConsistent() && AUTO_CLASSIFY) {//		    setDoExplanation(true);		    		    if(realize) {		        System.out.print("Pellet classifying...");		        		        // if showing progress, use DefaultClassifyProgress		        if ( showProgress )		        {		        	ClassifyProgress progress = new DefaultClassifyProgress();		        	kb.getTaxonomyBuilder().setListener( progress ); 		        }		        else // if not showing progress, use SilentClassifyProgress (by passing in null)		        	kb.getTaxonomyBuilder().setListener( null );		        		    	kb.realize();		    			    	System.out.println( "done " );		    			    	if( !kb.isRealized() ) {		    	    kb = null;		    	    System.out.println( "User canceled" );		    	    throw new UserCanceledException( "User canceled classification " );		    	}		    			    }		}				kb.setOntology(ontology.getURI().toString());				if(realize) {			//Aditya: This stuff for obtaining relations between individuals			// sameAs and differentFrom has been taken from the RDFS reasoner			// We need to use Pellet to infer same/different b/w individuals			sameAs = new HashMap();			differentFrom = new HashMap();			this.computeIndividualRelations(ontology);		}				// size of expTable//		ExplanationTable expTable = kb.getExplanationTable();////		expTable = new ExplanationTable();//		IObjectProfileNode profile = ObjectProfiler.profile (expTable);//        System.out.println ("ExplanationTable size = " + profile.size () + " bytes");	}		public String getName() {		return "Pellet";	}	public synchronized boolean isConsistent() {		return kb.isConsistent();	}		public void printClassTree(){		kb.getTaxonomy().print();	}		public void printPropertyTree(){		kb.getRoleTaxonomy().print();	}	public Taxonomy getRoleTaxonomy(){		return kb.getRoleTaxonomy();			}		public Taxonomy getTaxonomy(){		return kb.getTaxonomy();			}		/* (non-Javadoc)	 * @see org.mindswap.swoop.SwoopReasoner#disjointClassesOf(org.semanticweb.owl.model.OWLClass)	 */	public Set disjointClassesOf(OWLClass c) throws OWLException 	{		// tw7		OWLClass owlNothing = this.getOntology().getOWLDataFactory().getOWLNothing();		ATermAppl aa = ATermUtils.makeTermAppl( c.getURI().toString() );			Set disjointAtermSets = kb.getDisjoints( aa );		// now convert Aterms to OWLClasss		// a set of sets (sets of equivalent OWLClasses that are disjoint with c)		HashSet disjoints = new HashSet();		try		{			for ( Iterator it = disjointAtermSets.iterator(); it.hasNext(); )			{				// this is a set of sets (sets of equivalent ATerms that are disjoint with c)				Set disjointAtermSet = (Set)it.next();				// build one that contains OWLClasses as opposed to ATerms				HashSet oneDisjointEquivalents = new HashSet(); 				for ( Iterator iter = disjointAtermSet.iterator(); iter.hasNext(); )				{					ATermAppl aterm = (ATermAppl)iter.next();					String representation = aterm.toString();					URI uri = new URI( representation );					OWLClass aClass = super.getClass( uri );					if ( representation.equals("not(_TOP_)") )						aClass = owlNothing;										if ( aClass == null )						throw new Exception("Cannot convert aterm to class with <" + uri + ">");					else					{						oneDisjointEquivalents.add( aClass );					}				}				disjoints.add( oneDisjointEquivalents );			}			return disjoints;		}		catch ( Exception e )		{ e.printStackTrace(); }				// if exception occurred		return Collections.EMPTY_SET;	}	/* (non-Javadoc)	 * @see org.mindswap.swoop.SwoopReasoner#complementClassesOf(org.semanticweb.owl.model.OWLClass)	 */	public Set complementClassesOf(OWLClass c) throws OWLException {		// tw7		OWLClass owlNothing = this.getOntology().getOWLDataFactory().getOWLNothing();		OWLClass owlThing   = this.getOntology().getOWLDataFactory().getOWLThing();		ATermAppl aa = ATermUtils.makeTermAppl( c.getURI().toString() );			Set complementAtermSet = kb.getComplements( aa );		// now convert Aterms to OWLClasss		// a set of sets (sets of equivalent OWLClasses that are complment of c)		HashSet complements = new HashSet();		try		{			for ( Iterator it = complementAtermSet.iterator(); it.hasNext(); )			{				// this is a set ATerms that are complement with c				ATermAppl aterm = (ATermAppl)it.next();				String representation = aterm.toString();				URI uri = new URI( representation );				OWLClass aClass = super.getClass( uri );				if ( representation.equals("not(_TOP_)") )					aClass = owlNothing;				if ( representation.equals("_TOP_") )					aClass = owlThing;				if ( aClass == null )					throw new Exception("Cannot convert aterm to class with <" + uri + ">");				complements.add( aClass );			}			return complements;		}		catch ( Exception e )		{ e.printStackTrace(); }				// if exception occurred		return Collections.EMPTY_SET;	}	/* (non-Javadoc)	 * @see org.mindswap.swoop.SwoopReasoner#isConsistent(org.semanticweb.owl.model.OWLClass)	 */	public synchronized boolean isConsistent(OWLClass c) throws OWLException {	    // in an inconsistent ontology every class is inconsistent//		Timer timers = new Timer("Pellet Consistency Check");//	    timers.start();		if(!isConsistent())		    return false;		//		System.out.println("DEBUG: Remove me!");//		return true;		boolean cons = isConsistent((OWLDescription) c);//		timers.stop();//		System.out.println(timers);		return cons;	}	public Set equivalentClassesOf(OWLDescription c) throws OWLException {	    if(!isConsistent())	        return new HashSet();	    	    return super.equivalentClassesOf(c);	}		public Set equivalentClassesOf(OWLClass c) throws OWLException {	    if(!isConsistent())	        return new HashSet();	    	    return super.equivalentClassesOf(c);	}		public Set subClassesOf(OWLClass c) throws OWLException {	    if(!isConsistent())	        return new HashSet();	    	    return super.subClassesOf(c);		}		public Set superClassesOf(OWLClass c) throws OWLException {	    if(!isConsistent())	        return new HashSet();	    	    return super.superClassesOf(c);	

⌨️ 快捷键说明

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