ontologyanalyzer.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 904 行 · 第 1/3 页
JAVA
904 行
/*
* Created on Dec 23, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.mindswap.swoop.automation;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.mindswap.swoop.SwoopFrame;
import org.mindswap.swoop.SwoopModel;
import org.mindswap.swoop.TermsDisplay;
import org.mindswap.swoop.reasoner.PelletReasoner;
import org.mindswap.swoop.reasoner.SwoopReasoner;
import org.mindswap.swoop.reasoner.SwoopToldReasoner;
import org.mindswap.swoop.renderer.ontology.SwoopOntologyInfo;
import org.mindswap.swoop.utils.SwoopStatistics;
import org.mindswap.swoop.utils.owlapi.CorrectedRDFRenderer;
import org.semanticweb.owl.io.RendererException;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLEntity;
import org.semanticweb.owl.model.OWLException;
import org.semanticweb.owl.model.OWLIndividual;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLProperty;
/**
* @author Dave Wang
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class OntologyAnalyzer
{
static class AncestorResult
{
public HashSet myResult = null;
public boolean myHasCycle = false;
AncestorResult( HashSet set, boolean hasCycle )
{
myResult = set;
myHasCycle = hasCycle;
}
}
public static final String LOGICAL_URI = "LOGICALURI";
public static final String PHYSICAL_URI = "PHYSICALURI";
public static final String NUM_IMPORTED_CLASSES = "NUMIMPORTEDCLASSES";
public static final String NUM_DEFINED_CLASSES = "NUMDEFINEDCLASSES";
public static final String NUM_IMPORTED_DATA_PROP = "NUMIMPORTEDDATAPROPS";
public static final String NUM_DEFINED_DATA_PROP = "NUMDEFINEDDATAPROPS";
public static final String NUM_IMPORTED_OBJ_PROP = "NUMIMPORTEDOBJPROPS";
public static final String NUM_DEFINED_OBJ_PROP = "NUMDEFINEDOBJPROPS";
public static final String NUM_IMPORTED_ANNO_PROP = "NUMIMPORTEDANNOPROPS";
public static final String NUM_DEFINED_ANNO_PROP = "NUMDEFINEDANNOPROPS";
public static final String NUM_LINKED_PROP = "NUMLINKEDPROPS";
public static final String NUM_IMPORTED_IND = "NUMIMPORTEDIND";
public static final String NUM_DEFINED_IND = "NUMDEFINEDIND";
public static final String CONSISTENCY = "CONSISTENCY";
public static final String EXPRESSIVITY = "EXPRESSIVITY";
public static final String GRAPH_MORPHOLOGY = "GRAPHMORPHOLOGY";
// graph morphology categories
public static final String LIST = "List";
public static final String LISTS = "Lists";
public static final String TREE = "Tree";
public static final String TREES = "Trees";
public static final String MULTITREE = "Multitree";
public static final String DAG = "DAG";
public static final String GRAPH = "Graph";
public static final String NONE = "None";
public static final String INCONSISTENT = "INCONSISTENT";
// individual stats constants
public static final String NUM_TYPE_ASSERTIONS = "NUMTYPEASSERTIONS";
public static final String NUM_DPROP_ASSERTIONS = "NUMDPROPASSERTIONS";
public static final String NUM_OPROP_ASSERTIONS = "NUMOPROPASSERTIONS";
public static final String OWLTHING = "http://www.w3.org/2002/07/owl#Thing";
public static SwoopFrame theFrame;
public static boolean IS_DEBUG = false;
public static boolean INFERRED_STATS = false; // whether to get reasoned stats
public Vector analyzeInput(AutomatedSwoopModel model, String[] args)
{
Vector analyses = new Vector();
for (int i = 0; i < args.length; i++) {
try
{
OntologyAnalysis oa = analyze( model, new URI(args[i]) );
SwoopFrame frame = model.getFrame();
OWLOntology ontology = model.getSelectedOntology();
model.clearCaches( ontology ); // force clearing!
model.removeOntStats( ontology ); // force clearing!
model.removeOntology( ontology.getURI() ); // force removing!
frame.termDisplay.removeFromCache(ontology);
frame.clearWorkspace( false ); // just in case
//model.getFrame().clearWorkspace( false ); // false for "No popup confirmation"
}
catch (Exception exception)
{
exception.printStackTrace();
if ( IS_DEBUG )
System.out.println(" * OntologyAnalyzer: Could not load ontology " + args[i]);
}
}
return analyses;
}
public Vector analyzeInput(AutomatedSwoopModel model, BufferedReader reader, String outputfile, String logfilename )
{ return analyzeInput(model, reader, outputfile, 1, logfilename); }
/*
* pass in null for logfilename if no logging is desired
*/
public Vector analyzeInput(AutomatedSwoopModel model, BufferedReader reader, String outputfile, int startline, String logfilename )
{
if ( startline < 1 )
startline = 1;
int count = startline; // keep track of how many ontologies we looked at
Vector analyses = new Vector();
String line = null;
// setting up log file option
boolean isToLog = true;
if ( logfilename == null )
isToLog = false;
BufferedWriter logWriter = null;
try
{
// setting up log file
if ( isToLog )
logWriter = new BufferedWriter( new FileWriter( logfilename ) );
// setting up output file
BufferedWriter writer = new BufferedWriter( new FileWriter( outputfile ) );
StatsPrinter.writeFileHeader( writer );
// skip number of lines given
for ( int i = 1; i < startline; i++ )
reader.readLine();
int analyzedOntologies = 0;
int exceptionedOntologies = 0;
int skippedOntologies = 0;
while ( ( line = reader.readLine() ) != null )
{
try
{
// if line starts with "*", we skip it
if ( line.startsWith("*"))
{
if ( isToLog )
{
logWriter.write( "[" + count + "] Skipping <" + line + ">... ");
logWriter.newLine();
skippedOntologies++;
}
continue;
}
if ( isToLog )
{
logWriter.write( "[" + count + "] Analyzing <" + line + ">... ");
logWriter.newLine();
logWriter.flush();
}
long startTime = System.currentTimeMillis();
OntologyAnalysis oa = analyze( model, new URI(line) );
// pelletStats can be null if INFERRED_STATS is set to false;
if (( oa.getPelletStats() != null ) && ( isRDFS( oa ) ))
{
System.out.println(" >>>> IS rdfs, fixing... ");
if ( isToLog )
{
logWriter.write( " - RDFS vocabulary detected instead of owl. Attempting to fix and reanalyze...");
logWriter.newLine();
logWriter.flush();
}
oa = analyze( model, new URI(line), true );
}
StatsPrinter.printToFileTabDelim( writer, oa.getBasicStats(), oa.getToldStats(), oa.getPelletStats() );
long stopTime = System.currentTimeMillis();
double runTime = (stopTime - startTime)/1000;
analyzedOntologies++;
if ( isToLog )
{
logWriter.write( " Successful. Took " + runTime + " seconds.");
logWriter.newLine();
}
}
catch ( URISyntaxException e )
{
e.printStackTrace();
String trace = Utils.getExceptionTrace( e );
exceptionedOntologies++;
writer.newLine(); // write new line so output lines up with input
if ( isToLog )
{
logWriter.write( trace );
logWriter.newLine();
}
}
catch ( AnalysisException e )
{
e.printStackTrace();
String trace = Utils.getExceptionTrace( e );
exceptionedOntologies++;
writer.newLine(); // write new line so output lines up with input
if ( isToLog )
{
logWriter.write( trace );
logWriter.newLine();
logWriter.write( " - Caused by " + e.toString() );
logWriter.newLine();
}
}
finally
{
// clean up workspace
// increment count
if (isToLog)
{
logWriter.newLine();
logWriter.flush();
}
model.getFrame().clearWorkspace( false ); // false for "No popup confirmation"
count++;
}
}
if ( isToLog )
{
logWriter.write("Analyzed Ontologies: " + analyzedOntologies );
logWriter.newLine();
logWriter.write("Exceptioned Ontologies: " + exceptionedOntologies );
logWriter.newLine();
logWriter.write("Skipped Ontologies: " + skippedOntologies );
logWriter.newLine();
logWriter.flush();
}
}
catch ( IOException e )
{
e.printStackTrace();
System.err.println("Unrecoverable IOException... halting. ");
if ( IS_DEBUG )
System.out.println(" * OntologyAnalyzer: Could not load ontology " + line );
}
return analyses;
}
public OntologyAnalysis analyze( AutomatedSwoopModel model, URI ontURI ) throws AnalysisException
{ return analyze( model, ontURI, false); }
public OntologyAnalysis analyze( AutomatedSwoopModel model, URI ontURI, boolean isRDFS ) throws AnalysisException
{
TermsDisplay display = model.getFrame().termDisplay;
OWLOntology ont = null;
try
{
ont = model.loadOntology( ontURI );
model.setReasonerWithThreadBlock( new SwoopToldReasoner() );
model.setSelectedOntology( ont );
model.setShowImportsWithThreadBlock( true );
if ( isRDFS ) // RDFS, we fix the ont by serializing it to OWL, and reload it to the model
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?