📄 owlreasonerimpl.java
字号:
// The MIT License
//
// Copyright (c) 2004 Evren Sirin
//
// 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.
/*
* Created on Dec 29, 2003
*
*/
package org.mindswap.owl.impl;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLReader;
import org.mindswap.owl.Util;
import org.mindswap.owl.vocabulary.OWL;
import org.mindswap.pellet.ATermUtils;
import org.mindswap.pellet.OWLParser;
import org.mindswap.pellet.OWLReasoner;
import aterm.ATermAppl;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
/**
* @author Evren Sirin
*
*/
public class OWLReasonerImpl implements org.mindswap.owl.OWLReasoner {
OWLReader parser;
OWLReasoner reasoner;
HashSet loadedFiles;
/**
*
*/
public OWLReasonerImpl() {
parser = OWLFactory.createOWLReader();
reasoner = new OWLReasoner();
loadedFiles = new HashSet();
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReasoner#load(java.lang.String)
*/
public void load(URI uri) throws Exception {
OWLReasoner temp = null;
Model[] models = parser.readSeparate(uri);
for(int i = 1; i < models.length; i++) {
URI name = parser.getModelURI(models[i]);
if(loadedFiles.contains(Util.standardURI(name)))
continue;
loadedFiles.add(Util.standardURI(name));
OWLParser.SHOW_INFO = false;
temp = new OWLReasoner();
temp.loadOntology(models[i]);
int nonDL = org.mindswap.pellet.OWLParser.nonDL.size();
if(nonDL < 5) {
System.out.println("Load OWL DL document (" + nonDL + ") " + name);
org.mindswap.pellet.OWLParser.SHOW_INFO = true;
reasoner.loadOntology(models[i]);
}
}
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReasoner#isClass(org.mindswap.owl.URI)
*/
public boolean isClass(URI uri) {
return reasoner.isClass(Util.toResource(uri));
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReasoner#isSubclassOf(org.mindswap.owl.URI, org.mindswap.owl.URI)
*/
public boolean isSubclassOf(URI uri1, URI uri2) {
try {
reasoner.classify();
return reasoner.isSubclassOf(Util.toResource(uri1), Util.toResource(uri2));
} catch (RuntimeException e) {
return false;
}
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReasoner#getSubclasses(org.mindswap.owl.URI)
*/
public List getSubClasses(URI uri) {
try {
reasoner.classify();
List list = reasoner.getClassification().getSubClasses(Util.toATerm(uri));
return toURIList(list);
} catch (RuntimeException e) {
return new ArrayList();
}
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReasoner#getSuperclasses(org.mindswap.owl.URI)
*/
public List getSuperClasses(URI uri) {
try {
reasoner.classify();
List list = reasoner.getClassification().getSuperClasses(Util.toATerm(uri));
return toURIList(list);
} catch (RuntimeException e) {
return new ArrayList();
}
}
private List toURIList(List list) {
List results = new ArrayList();
Iterator i = list.iterator();
while(i.hasNext()) {
ATermAppl c = (ATermAppl) i.next();
URI uri = null;
if(c.equals(ATermUtils.TOP))
uri = OWL.Thing;
else if(c.equals(ATermUtils.BOTTOM))
uri = OWL.Nothing;
else
results.add(Util.toURI(c));
}
return results;
}
/* (non-Javadoc)
* @see org.mindswap.owls.process.ValueRewriter#rewrite(java.lang.String, java.net.URI, java.net.URI)
*/
public String rewrite(String individual, URI sourceC, URI targetC) {
if (sourceC.equals(targetC))
return individual;
System.out.println ("Before transformation ==>\n" + individual);
String src = sourceC.toString();
String tgt = targetC.toString();
Model model = ModelFactory.createDefaultModel ();
Model newModel = ModelFactory.createDefaultModel();
model.read(new StringReader (individual), "");
StmtIterator si = model.listStatements();
while (si.hasNext ())
{
Statement s2 = null;
Statement s = si.nextStatement();
if (s.getSubject().toString().equals(src))
{
s2 = model.createStatement(model.createResource(tgt), s.getPredicate(), s.getObject());
newModel.add(s2);
}
else if (s.getObject().toString().equals(src))
{
s2 = model.createStatement(s.getSubject(), s.getPredicate(), model.createResource(tgt));
newModel.add(s2);
}
else
newModel.add (s);
}
StringWriter sw = new StringWriter ();
newModel.write(sw);
String result = sw.toString();
System.out.println ("After transformation ==>\n" + result);
return result;
// String result = individual;
//
// // if both types are equal don't need rewriting
// if (sourceC.equals(targetC))
// return result;
//
// // get namespace and local name information
// String sourceNS = Util.getNameSpace(sourceC);
// String sourceName = Util.getLocalName(sourceC);
// String targetNS = Util.getNameSpace(targetC);
// String targetName = Util.getLocalName(targetC);
//
// // Let's just assume for now that all properties used in both concepts
// // are using same names. Later we can do something about equivalent and
// // subproperties
//
// if (!sourceNS.equals(targetNS))
// result = result.replaceAll(sourceNS, targetNS);
//
// if (!sourceName.equals(targetName))
// result = result.replaceAll(sourceName, targetName);
//
// return result;
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReasoner#isDisjointWith(java.net.URI, java.net.URI)
*/
public boolean isDisjoint(URI uri1, URI uri2) {
return reasoner.isDisjoint(Util.toResource(uri1), Util.toResource(uri2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -