1

我想使用小程序来更改存储库。为此,我编写了以下代码:

加载本体:

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 包含到构建路径中,可以在此处下载

4

1 回答 1

1

根据我知道的片段,我建议尝试这个 HTML。

<html>
<body>
<APPLET 
    CODE="owlapi.InJava" 
    NAME="owlapi" 
    archive="owlapi/owl1.jar,owlapi/owl2.jar"
    HEIGHT=100 
    WIDTH=100> 
</APPLET>  
</body>
</html>

这个 HTML “静态”嵌入了一个小程序——不使用 JavaScript。类名(code属性)应该是类的完全限定名,而不是文件名。虽然后者通常是可以容忍的,但它是不正确的。它显示了如何从owlapi目录中导入 API,根据需要更改名称。

同样值得关注的是混合松散的类文件和罐子。这往往会使开发人员和 JRE 感到困惑。相反,如果将owlapi.InJava类放在里面my.jar,并且那个 Jar 放在与其余目录相同的目录中,则codebase可以添加 a 并使archive路径更短。像这样:

<html>
<body>
<APPLET 
    CODE="owlapi.InJava" 
    NAME="owlapi" 
    codebase="owlapi"
    archive="my.jar,owl1.jar,owl2.jar"
    HEIGHT=100 
    WIDTH=100> 
</APPLET>  
</body>
</html>

这不是有效的 HTML。这只是检查基础知识的测试。稍后,您需要使其成为有效的 HTML 并(可能)使用小程序信息deployJava.js中提到的小程序部署小程序。页

于 2012-04-28T13:27:53.937 回答