ontologychangerenderer.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,066 行 · 第 1/3 页
JAVA
1,066 行
//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.change;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
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.utils.change.BooleanElementChange;
import org.mindswap.swoop.utils.change.EnumElementChange;
import org.semanticweb.owl.impl.model.OWLConnectionImpl;
import org.semanticweb.owl.io.owl_rdf.OWLRDFErrorHandler;
import org.semanticweb.owl.io.owl_rdf.OWLRDFParser;
import org.semanticweb.owl.io.vocabulary.XMLSchemaSimpleDatatypeVocabulary;
import org.semanticweb.owl.model.OWLAnd;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLDataFactory;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDataType;
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.OWLException;
import org.semanticweb.owl.model.OWLIndividual;
import org.semanticweb.owl.model.OWLNamedObject;
import org.semanticweb.owl.model.OWLObject;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOr;
import org.semanticweb.owl.model.OWLProperty;
import org.semanticweb.owl.model.OWLSameIndividualsAxiom;
import org.semanticweb.owl.model.OWLSubClassAxiom;
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.AddEquivalentClass;
import org.semanticweb.owl.model.change.AddImport;
import org.semanticweb.owl.model.change.AddIndividualAxiom;
import org.semanticweb.owl.model.change.AddIndividualClass;
import org.semanticweb.owl.model.change.AddObjectPropertyInstance;
import org.semanticweb.owl.model.change.AddObjectPropertyRange;
import org.semanticweb.owl.model.change.AddSuperClass;
import org.semanticweb.owl.model.change.AddSuperProperty;
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.RemoveEntity;
import org.semanticweb.owl.model.change.RemoveEquivalentClass;
import org.semanticweb.owl.model.change.RemoveImport;
import org.semanticweb.owl.model.change.RemoveIndividualAxiom;
import org.semanticweb.owl.model.change.RemoveIndividualClass;
import org.semanticweb.owl.model.change.RemoveObjectPropertyInstance;
import org.semanticweb.owl.model.change.RemoveObjectPropertyRange;
import org.semanticweb.owl.model.change.RemoveSuperClass;
import org.semanticweb.owl.model.change.RemoveSuperProperty;
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.OntologyHelper;
import org.xml.sax.SAXException;
/**
* @author Aditya Kalyanpur
* This class is used to serialize and deserialize OntologyChange objects
* into RDF/XML. It uses a special-purpose OWL-API ontology created for
* representing changes in RDF.
*
* The architecture has to be improved since it requires an instanceof
* ChangeLog to be passed to it during initialization and then cyclically calls itself
* during serialization. i.e. it calls ChangeLog.getComponentsString() which in turn calls
* addToOWLAPIOntology of this Class.
*
*/
public class OntologyChangeRenderer {
ChangeLog changeLog;
OWLOntology owlapiOntology, owlapiOnly;
static String owlapiLocation = "http://www.mindswap.org/dav/ontologies/owlapi.owl";
static String xsdString = XMLSchemaSimpleDatatypeVocabulary.XS + "string";
int argLimit = 10; // limit on no of arguments (parameters) of an Ontology Change
public OntologyChangeRenderer(ChangeLog changeLog) throws OWLException {
this.changeLog = changeLog;
try {
this.reloadOWLAPIOntology();
}
catch (Exception ex) {
System.out.println("Error loading OWL-API ontology from "+owlapiLocation);
throw new OWLException(ex);
}
}
public OWLOntology serializeOntologyChanges(List changeSet) {
for (Iterator iter = changeSet.iterator(); iter.hasNext(); ) {
OntologyChange change = (OntologyChange) iter.next();
changeLog.getChangeInformation(change, ChangeLog.CHANGE_RDFXML, this, new ArrayList(), null);
}
/*** finally, remove owl-api classes, properties from ontology ***/
this.removeRedundantOWLAPI();
return this.owlapiOntology;
}
/**
* Accepts an OWL Ontology (annotOnt) containing ontology change instances
* serialized in RDF/XML using the owlapiOntology and parses it to return a
* list of OntologyChange objects
* @param annotOnt - ontology containing change instances
* @return list of OntologyChanges
* @throws Exception
*/
public List deserializeOntologyChanges(OWLOntology annotOnt) {
SwoopModel swoopModel = changeLog.swoopModel;
List changes = new ArrayList();
Set indSet = new HashSet();
try {
indSet = annotOnt.getIndividuals();
}
catch (OWLException ex) {
ex.printStackTrace();
}
// iterate through change instances in the ontology
for (Iterator iter = indSet.iterator(); iter.hasNext(); ) {
OWLIndividual ind = (OWLIndividual) iter.next();
// get all values of property argument(i)
Map dataValues = null;
try {
dataValues = ind.getDataPropertyValues(annotOnt);
} catch (OWLException e) {
e.printStackTrace();
return changes;
}
String[] origArguments = new String[argLimit];
String[] arguments = new String[argLimit];
boolean validIndividual = false;
for (Iterator it = dataValues.keySet().iterator(); it.hasNext();) {
OWLDataProperty prop = (OWLDataProperty) it.next();
Set vals = (Set) dataValues.get(prop);
for (int i=0; i<argLimit; i++) {
try {
if (prop.getURI().toString().equals(owlapiLocation+"#argument"+String.valueOf(i+1))) {
origArguments[i] = vals.iterator().next().toString();
arguments[i] = origArguments[i];
if (arguments[i].indexOf("^^")>0) arguments[i] = arguments[i].substring(0, arguments[i].indexOf("^"));
if (arguments[i].endsWith("@EN")) arguments[i] = arguments[i].substring(0, arguments[i].length()-3);
if (i>=1) validIndividual = true;
}
} catch (OWLException e1) {
e1.printStackTrace();
}
}
}
if (!validIndividual) continue;
// iterate through types and check which change class type belongs to
// normally it better belong to only one Type!!
Set types = new HashSet();
try {
types = ind.getTypes(annotOnt);
} catch (OWLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (Iterator iter2 = types.iterator(); iter2.hasNext(); ) {
Object type = iter2.next();
// set ontology for the current change type
// the first argument for the change instance should be the ontology URI
OWLOntology changeOnt = null;
OWLDataFactory df = null;
try {
URI ontURI = new URI(arguments[0]);
changeOnt = swoopModel.getOntology(ontURI);
//*** if ontology is not present in SwoopModel, create a new ontology
// but set the URI so that it can be used to reference the ontology later ***/
if (changeOnt==null) {
changeOnt = (OWLOntology) this.createNewOWLObject(OWLOntology.class, ontURI, null);
}
df = changeOnt.getOWLDataFactory();
}
catch (Exception e2) {
e2.printStackTrace();
}
//*** 'type' better be OWLClass since all changes
// have been represented by OWLClass objects ***/
if (type instanceof OWLClass) {
OWLClass changeClass = (OWLClass) type;
String uri = "";
try {
uri = changeClass.getURI().toString();
} catch (OWLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
// start processing changes
if (uri.equals(owlapiLocation+"#AddImport")) {
try {
OWLOntology impOnt = swoopModel.getOntology(new URI(arguments[1]));
if (impOnt==null) {
// create new ontology with impOnt URI
impOnt = (OWLOntology) this.createNewOWLObject(OWLOntology.class, new URI(arguments[1]), null);
}
AddImport change = new AddImport(changeOnt, impOnt, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#RemoveImport")) {
try {
OWLOntology impOnt = swoopModel.getOntology(new URI(arguments[1]));
if (impOnt==null) {
// create new ontology with impOnt URI
impOnt = (OWLOntology) this.createNewOWLObject(OWLOntology.class, new URI(arguments[1]), null);
}
RemoveImport change = new RemoveImport(changeOnt, impOnt, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#AddCLASS")) {
try {
OWLClass newClass = df.getOWLClass(new URI(arguments[1]));
AddEntity change = new AddEntity(changeOnt, newClass, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#AddDATATYPE_PROPERTY")) {
try {
OWLDataProperty newProp = df.getOWLDataProperty(new URI(arguments[1]));
AddEntity change = new AddEntity(changeOnt, newProp, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#AddOBJECT_PROPERTY")) {
try {
OWLObjectProperty newProp = df.getOWLObjectProperty(new URI(arguments[1]));
AddEntity change = new AddEntity(changeOnt, newProp, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#AddINDIVIDUAL")) {
try {
OWLIndividual newProp = df.getOWLIndividual(new URI(arguments[1]));
AddEntity change = new AddEntity(changeOnt, newProp, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#RemoveCLASS")) {
try {
OWLClass remClass = (OWLClass) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.CLASSES);
RemoveEntity change = new RemoveEntity(changeOnt, remClass, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#RemoveDATATYPE_PROPERTY")) {
try {
OWLDataProperty remProp = (OWLDataProperty) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.DATAPROPERTIES);
RemoveEntity change = new RemoveEntity(changeOnt, remProp, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#RemoveOBJECT_PROPERTY")) {
try {
OWLObjectProperty remProp = (OWLObjectProperty) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.OBJPROPERTIES);
RemoveEntity change = new RemoveEntity(changeOnt, remProp, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#RemoveINDIVIDUAL")) {
try {
OWLIndividual remInd = (OWLIndividual) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.INDIVIDUALS);
RemoveEntity change = new RemoveEntity(changeOnt, remInd, null);
changes.add(change);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (uri.equals(owlapiLocation+"#AddEquivalentClass")) {
try {
OWLClass cla = (OWLClass) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.CLASSES);
OWLDescription desc = this.getOWLObjectNode(annotOnt, arguments[2], changeOnt);
AddEquivalentClass change = new AddEquivalentClass(changeOnt, cla, desc, null);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?