我想使用小程序来更改存储库。为此,我编写了以下代码:
加载本体:
package owlapi.loader;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.*;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.util.OWLEntityRenamer;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class LoadOntology {
private static String ontologyDir = "./owlapi/loader/featurepool.owl";
private static OWLOntology localOntology;
private static OWLOntologyManager manager;
private static OWLDataFactory factory;
private static PrefixManager pm = new DefaultPrefixManager("http://wise.vub.ac.be/Members/lamia/variability/Feature_Assembly/FAM.owl#");
public LoadOntology() {
manager = OWLManager.createOWLOntologyManager();
File file = new File(ontologyDir);
try {
localOntology = manager.loadOntologyFromOntologyDocument(file);
System.out.println("Loaded ontology: " + localOntology);
factory = manager.getOWLDataFactory();
}
catch (UnparsableOntologyException e) {
System.out.println("Could not parse the ontology: " + e.getMessage());
Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
for (OWLParser parser : exceptions.keySet()) {
System.out.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
System.out.println("Failed because: " + exceptions.get(parser).getMessage());
}
}
catch (UnloadableImportException e) {
System.out.println("Could not load import: " + e.getImportsDeclaration());
OWLOntologyCreationException cause = e.getOntologyCreationException();
System.out.println("Reason: " + cause.getMessage());
}
catch (OWLOntologyCreationException e) {
System.out.println("Could not load ontology: " + e.getMessage());
}
}
public void addInstance(String Class, String individual) {
OWLClass owlClass = factory.getOWLClass(Class, pm);
OWLClass superClass = factory.getOWLThing();
OWLIndividual instance = factory.getOWLNamedIndividual(individual, pm);
OWLClassAssertionAxiom classAssertion1 = factory.getOWLClassAssertionAxiom(superClass, instance);
manager.addAxiom(localOntology, classAssertion1);
OWLClassAssertionAxiom classAssertion2 = factory.getOWLClassAssertionAxiom(owlClass, instance);
manager.addAxiom(localOntology, classAssertion2);
System.out.println("Instance added");
}
}
InJava(在 javascript 中加载的小程序):
public class InJava extends Applet{
private static LoadOntology loadedOntology;
public void addInstance(String Class, String individual) {
Graphics g = getGraphics();
g.drawString("Test", 10, 10);
loadedOntology = new LoadOntology();
loadedOntology.addInstance(Class, individual);
}
}
当我加载小程序时,根本没有错误,但是当我尝试调用方法 addInstance 时:
<input type="button" value="call Java Applet method" onClick = 'document.owlapi.addInstance("Abstract_Feature", "New_Test")'>
<APPLET CODE="owlapi/InJava.class" NAME="owlapi" HEIGHT=100 WIDTH=100> </APPLET>
浏览器抛出错误Uncaught Error: Error calling method on NPObject.
在stackoverflow上阅读了一些关于这个的问题后,我得出的结论是插件一定有问题。
我在有效的方法(drawString("Test", 10, 10)) 中添加了一些测试代码,所以我建议 LoadOntology 类的构造会产生一些问题(这个类在其自身完美的作品中单独进行了测试)。有人看到我做错了吗?
更新:我添加了小程序使用的 java 类的一部分:存储库加载器。所有提供的代码都应该编译。
更新:我没有使用MASCRIPT/SCRIPTABLE
,因为代码基于this example,它适用于我当前的浏览器(chrome)
udpate:您必须将 owlapi jar 包含到构建路径中,可以在此处下载