使用jena api

时间:2016-04-19 07:11:26

标签: java jena owl

我在package oopn.ontology

中有这段代码
public class Ontology {

private static Ontology ontology = new Ontology();
public static OntModel ontologyModel; //this is the main of Ontology model
public static File ontologyFile; //this is the file of owl
public static String ontologyFilePath; //this is the path of owl file
public static JTree ontologyModelTree;
public static int saveAction;
public static String copiedText;
public static JTree tree;

private Ontology() {
}

public static Ontology getInstance() {
    return ontology;
}

public void resetOntology() {
    ontologyModel = null;
    ontologyFile = null;
    ontologyFilePath = null;
    ontologyModelTree = null;
}

然后这段代码创建本体模型:

public OntModel createOntologyModelFromFile(String owlFile) {
    InputStream in;
    in = FileManager.get().open(owlFile);
OntModel model1 = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
OntModel model2 = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
model1.read(in, "");
ontologyModel = model1;
    return model1;
}

要打开文件我有这个:

File openOntologyFile() {
    JFileChooser fileChooser;
    File chosenFile;
    fileChooser = new JFileChooser(new File("mpasi.owl"));
    fileChooser.showOpenDialog(fileChooser);
    chosenFile = fileChooser.getSelectedFile();
    if (chosenFile != null) {
        //loadOntologyFile(chosenFile);
    } else {
        JOptionPane.showMessageDialog(fileChooser, this);
    }
    //enableControls(true);
    Ontology.ontologyFilePath = chosenFile.getAbsolutePath();
    return chosenFile;
}

在主要课程中这一个:

public class Dss_mpasi extends javax.swing.JFrame {

JFileChooser fileDialog;
public Dss_mpasi() {
    initComponents();
    if (Ontology.ontologyFile != null) {
        Model tempModel ;
        if (Ontology.ontologyModel == null) {
            tempModel = Ontology.getInstance().createOntologyModelFromFile(Ontology.ontologyFilePath);
        } else {
            tempModel = Ontology.ontologyModel;
        }
        StringWriter writer = new StringWriter();
        tempModel.write(writer, "RDF/XML");            
    } 
}

但它仍然无法运作......我希望有人可以帮助我让它发挥作用吗?

1 个答案:

答案 0 :(得分:0)

考虑更换 tempModel = Ontology.getInstance().createOntologyModelFromFile(Ontology.ontologyFilePath);有类似这样的内容:

OntModel tempModel = ModelFactory.createOntologyModel();
FileManager.get().readModel( tempModel , Ontology.ontologyFilePath);

您可能会发现selected answer on this post对您有用。