ontologyanalyzer.java

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

JAVA
904
字号
						return advStats;
					}
					//if ( children != null )
					//	System.out.println( "++++" + cls.getURI().toString() + " has " + children.size() + " children");
				}
				if ( hasMultipleTop )
					advStats.put( GRAPH_MORPHOLOGY, LISTS );
				else
					advStats.put( GRAPH_MORPHOLOGY, LIST );
				return advStats;
			}
			else // either a multitree or a (nore general) directed acyclic graph
			{				
				HashSet MIClasses = (HashSet)advStats.get( SwoopStatistics.MULTIPLE_INHERITANCE_CLASS );
				for ( Iterator it = MIClasses.iterator(); it.hasNext(); )
				{
					OWLClass cls = (OWLClass)it.next();
					
					HashSet checkedSupers = new HashSet();
					
					Set supers = reasoner.superClassesOf( cls );
					for ( Iterator iterator = supers.iterator(); iterator.hasNext(); )
					{
						Set set = (Set)iterator.next();
						for ( Iterator its = set.iterator(); its.hasNext(); )
						{
							OWLClass sup = (OWLClass)its.next();
							// get all ancestors (aside from owl:Thing) of a parent
							AncestorResult ar = getAncestorClasses( sup, reasoner, new HashSet() );
							HashSet ancs = ar.myResult;
							
							boolean hasCycle = ar.myHasCycle;
							// if has cycle and is using toldReasoner, then we label it as a GRAPH
							if ( (hasCycle) && ( reasoner.getClass().getName().equals("org.mindswap.swoop.reasoner.SwoopToldReasoner")) )
							{
								advStats.put( GRAPH_MORPHOLOGY, GRAPH );
								return advStats;
							}
							ancs.remove( owlThing );
							
							// copy the ancestor set
							HashSet copy = new HashSet();
							copy.addAll( ancs );
							
							// find intersection of ancs and checkedSupers;
							ancs.retainAll( checkedSupers );
							if ( ancs.size() > 0 ) // intersection found, diamonstructure is found
							{
								advStats.put( GRAPH_MORPHOLOGY, DAG );
								return advStats;
							}
							else
								checkedSupers.addAll( copy );
						}
					}
				}
				advStats.put( GRAPH_MORPHOLOGY, MULTITREE );
				return advStats;
			}
		}
		catch ( Exception e )
		{ 
			e.printStackTrace();
			throw new AnalysisException("Failed in Graph Morphology Stats Checking: " + e.toString() + "\n" + Utils.getExceptionTrace( e ) );
		}
	}
	
	private static AncestorResult getAncestorClasses( OWLClass cls, SwoopReasoner reasoner, HashSet ancestors ) throws OWLException
	{
		AncestorResult result = new AncestorResult( ancestors, false );
		result = getAncestorClasses( cls, cls, reasoner, result );
		return result;
	}
	
	/* Recursively compute ancestor (parents, grand parents, long-dead mummified
	 *  great great great great great grand parents classes of a given class
	 */
	private static AncestorResult getAncestorClasses( OWLClass orig_src, OWLClass cls, SwoopReasoner reasoner, AncestorResult result ) throws OWLException
	{
		HashSet ancestors = result.myResult;
		// basic case in recursion (owl:Thing)
		if ( cls.getURI().toString().equals( OWLTHING ) )
			return result;
		// recursively find all ancestors
		Set sup = reasoner.superClassesOf( cls );
		boolean hasCycle = false;
		for ( Iterator iter = sup.iterator(); iter.hasNext(); )
		{
			HashSet set = (HashSet)iter.next();
			for ( Iterator iterator = set.iterator(); iterator.hasNext(); )
			{
				OWLClass os = (OWLClass)iterator.next();
				//System.err.println("os: " + os.getURI().toString() );
				ancestors.add( os );
				
				if ( ancestors.contains( orig_src )) // we have a cycle, prevent inf. recurs. by breaking
				{
					System.out.println( " Cycle: in this class:" + cls.getURI() + "  has parent: " + orig_src.getURI() );
					hasCycle = true;
					break;
				}

				AncestorResult r = new AncestorResult( ancestors, hasCycle );
				// now apply depth first search on the new ancestor os
				r = getAncestorClasses( orig_src, os, reasoner, r );
				ancestors = r.myResult;
								
				if ( r.myHasCycle )
					hasCycle = r.myHasCycle;
			}
		}
		AncestorResult newResult = new AncestorResult( ancestors, hasCycle );
		return newResult;
	}
	
	private static String getExpressivity() throws AnalysisException
	{
		String exprShort = null;
		try
		{
        	String express = theFrame.swoopModel.getReasoner().getExpressivity();
        	exprShort= express;
        	if (express.indexOf("<br>")>=0) exprShort = express.substring(0, express.indexOf("<br>"));        
        	//System.out.println("DL Expressivity: " + exprShort );
		}
		catch ( Exception e )
		{ 
			e.printStackTrace(); 
			e.printStackTrace();
			throw new AnalysisException("Failed in extracting Expressivity Stats: " + e.toString() + "\n" + Utils.getExceptionTrace( e ) );
		}
		return exprShort;
	}

	private Hashtable getBasicStats( AutomatedSwoopModel swoopModel, OWLOntology ont ) throws AnalysisException
	{
		try
		{
			Hashtable stats = new Hashtable();
	    	int numberOfClasses, numberOfObjectProperties=0, numberOfDatatypeProperties, numberOfAnnotationProperties, numberOfInstances;
	        int totalClasses=0, totalObjProps=0, totalDataProps=0, totalAnnotatedProps=0, totalIndividuals=0;
	        //*******************************************
	        //Added for Econnections
	        //*******************************************
	        int numberOfLinkProperties=0, numberOfForeignEntities=0;
	        int numberOfForeignClasses=0, numberOfForeignProperties=0, numberOfForeignIndividuals=0;
	
	        numberOfClasses = ont.getClasses().size();
	        numberOfDatatypeProperties = ont.getDataProperties().size();
	      	
	        //***********************************************************
	        //Added for Econnections
	        //***********************************************************
	        Iterator it= ont.getObjectProperties().iterator();
	        Set foreignOntologies = new HashSet();
	        while(it.hasNext())
	        {        	
	        	OWLObjectProperty property=(OWLObjectProperty)it.next();
	        	if(property.isLink() && !(property.getLinkTarget()).equals(ont.getURI()))
	        	{
	        		numberOfLinkProperties++;
	        		foreignOntologies.add(property.getLinkTarget()); 
	        	}
	        	else
	        	{
	        		numberOfObjectProperties++;
	        	}
	        }
	        //numberOfObjectProperties = ont.getObjectProperties().size();
	        numberOfForeignEntities = ont.getForeignEntities().size();
	        Iterator j= ont.getForeignEntities().keySet().iterator();
	        while(j.hasNext()){
	        	OWLEntity e = (OWLEntity)j.next();
	        	if(e instanceof OWLClass)
	        		numberOfForeignClasses++;
	        	if(e instanceof OWLProperty)
	        		numberOfForeignProperties++;
	        	if(e instanceof OWLIndividual)
	        		numberOfForeignIndividuals++;
	        }
	        //************************************************************
	        numberOfAnnotationProperties = ont.getAnnotationProperties().size();
	        numberOfInstances = ont.getIndividuals().size();
	        
	        // get imported ontology statistics            
	        totalClasses = swoopModel.getEntitySet(ont, SwoopModel.TRANSCLOSE_ONT, SwoopOntologyInfo.SHOW_CLASSES).size();
	        //*****************************************************
	        //Changed for Econnections
	        //*****************************************************
	        totalObjProps = swoopModel.getEntitySet(ont, SwoopModel.TRANSCLOSE_ONT, SwoopOntologyInfo.SHOW_OBJPROPERTIES).size()-numberOfLinkProperties;
	        //***************************************************
	        totalDataProps = swoopModel.getEntitySet(ont, SwoopModel.TRANSCLOSE_ONT, SwoopOntologyInfo.SHOW_DATAPROPERTIES).size();
	        totalIndividuals = swoopModel.getEntitySet(ont, SwoopModel.TRANSCLOSE_ONT, SwoopOntologyInfo.SHOW_INDIVIDUALS).size();
	        totalAnnotatedProps = swoopModel.getEntitySet(ont, SwoopModel.TRANSCLOSE_ONT, SwoopOntologyInfo.SHOW_PROPERTIES).size() - totalDataProps - totalObjProps;
	        
	        String logicalURI = ont.getLogicalURI().toString() ;
	        String physicalURI = ont.getPhysicalURI().toString();
	        // num imported classes
	        int numImportedClasses = totalClasses - numberOfClasses;
	        // num imported datatype props
	        int numImportedDatatypeProps = totalDataProps - numberOfDatatypeProperties;
	        // num imported object props
	        int numImportedObjProps = totalObjProps - numberOfObjectProperties ;
	        // num imported annotation props
	        int numImportedAnnoProps = totalAnnotatedProps - numberOfAnnotationProperties;
	        // num imported individuals
	        int numImportedInd = totalIndividuals - numberOfInstances;
	        
	        stats.put( LOGICAL_URI, logicalURI );
	        stats.put( PHYSICAL_URI, physicalURI );
	        stats.put( NUM_IMPORTED_CLASSES, numImportedClasses + "" );
	        stats.put( NUM_DEFINED_CLASSES, numberOfClasses + "");
	        stats.put( NUM_IMPORTED_DATA_PROP, numImportedDatatypeProps + "" );
	        stats.put( NUM_DEFINED_DATA_PROP, numberOfDatatypeProperties + "" );
	        stats.put( NUM_IMPORTED_OBJ_PROP, numImportedObjProps + "");
	        stats.put( NUM_DEFINED_OBJ_PROP, numberOfObjectProperties + "");
	        stats.put( NUM_IMPORTED_ANNO_PROP, numImportedAnnoProps + "" );
	        stats.put( NUM_DEFINED_ANNO_PROP, numberOfAnnotationProperties + "" );
	        stats.put( NUM_LINKED_PROP, numberOfLinkProperties + "" );
	        stats.put( NUM_IMPORTED_IND, numImportedInd + "" );
	        stats.put( NUM_DEFINED_IND, numberOfInstances + "" );
	        
	        return stats;
		}
		catch ( Exception e )
		{ 
			e.printStackTrace(); 
			throw new AnalysisException( "Failed in computing Basic Stats: " + e.toString() + "\n" + Utils.getExceptionTrace( e ));
		}
	}
		
	/* If an ontology has classes, but the graph morph is NONE in told, and other than INCONSISTENT or NONE in Reasoned, then
	 *  it is using funny RDFS vocab 
	 */
	public static boolean isRDFS( OntologyAnalysis oa ) throws AnalysisException
	{
		Hashtable stats       = oa.getBasicStats();
		HashMap toldStats     = oa.getToldStats();
		HashMap reasonedStats = oa.getPelletStats();
		try
		{
			int numClasses = Integer.parseInt( (String)stats.get( OntologyAnalyzer.NUM_DEFINED_CLASSES )) + 
				             Integer.parseInt( (String)stats.get( OntologyAnalyzer.NUM_IMPORTED_CLASSES ) );
			
			String toldGM = (String)toldStats.get( OntologyAnalyzer.GRAPH_MORPHOLOGY );
			String reasonedGM = (String)reasonedStats.get( OntologyAnalyzer.GRAPH_MORPHOLOGY );
			
			//System.out.println( numClasses > 0 ); 
			//System.out.println( toldGM.equals( OntologyAnalyzer.NONE) );
			//System.out.println( !reasonedGM.equals( OntologyAnalyzer.NONE) );
			//System.out.println( !reasonedGM.equals( OntologyAnalyzer.INCONSISTENT) );
			//System.out.println( reasonedGM );
			
			if ( (numClasses > 0)  && (toldGM.equals( OntologyAnalyzer.NONE)) && 
					( !reasonedGM.equals( OntologyAnalyzer.NONE)) && 
					( !reasonedGM.equals( OntologyAnalyzer.INCONSISTENT)) )
				return true;
			
			return false;
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			throw new AnalysisException("Failed in testing for RDFS vocab" + e.toString() + "\n" + Utils.getExceptionTrace( e ) );						
		}
	}

	
    public static void main(String[] args) 
    {
    	// Create model
    	AutomatedSwoopModel model = new AutomatedSwoopModel();
		// Create application frame.
		theFrame = new SwoopFrame(model);
		
		// Do Not show frame
		theFrame.setVisible( false );
		
		OntologyAnalyzer ana = new OntologyAnalyzer();
		
		//String [] params = { "file:/C:/Documents%20and%20Settings/Dave%20Wang/Desktop/ontologies/koala.owl", "http://cvs.mygrid.org.uk/cgi-bin/viewcvs.cgi/mygrid/feta/etc/sampleData/service.rdfs?rev=1.2" };
		//String [] params = { "http://protege.stanford.edu/plugins/owl/owl-library/koala.owl", "http://cvs.mygrid.org.uk/cgi-bin/viewcvs.cgi/mygrid/feta/etc/sampleData/service.rdfs?rev=1.2" };
		//String [] params = { "http://www.mindswap.org/2004/owl/mindswappers" };
		
		try
		{
			if (IS_DEBUG)
				System.err.println(" >>> In Debugging Mode");
			BufferedReader reader = new BufferedReader( new FileReader( "test.txt" ) );
			ana.analyzeInput( model, reader, "output.txt", 1, "OntologyAnalyzerLog.txt" );
			if (IS_DEBUG)
				System.err.println(" >>> In Debugging Mode");
		}
		catch ( Exception e )
		{ e.printStackTrace(); }
		
		System.exit(0);
	}
}

⌨️ 快捷键说明

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