我在 Protege 中创建了 Ontologu ,并在 Eclipse 中导入.My ontology 已经10 实例,我想添加更多实例。以下代码将实例添加到本体的现有类(名词)中。在执行之后,它不会更新本体模型并显示相同数量的实例。
public static void main(String[] args) throws OWLException, IOException{
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
File file = new File("D:\\word.owl");{
OntModel model=ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
System.out.println("Model is called successfully");
OWLOntology ont = manager.loadOntologyFromOntologyDocument(file);
System.out.println("Loaded ontology: " + ont);
String SOURCE = ("D:\\word.owl");
String NS = SOURCE;
OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
base.read( SOURCE, "" );
OntModel inf =ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, base );
OntClass Noun = base.getOntClass( NS + "Noun" );
Individual jack = base.createIndividual( NS + "Jack", Noun );
Individual Helley = base.createIndividual( NS + "Helley", Noun );
manager.saveOntology(ont);
System.out.println("Number of individuals: " + ont.getIndividualsInSignature().size());
}
}
}
输出
Model is called successfully
Loaded ontology:
Number of individuals: 10
答案 0 :(得分:1)
我不使用该API,但我可以看到您的问题。
在代码开头创建OWLOntology
对象:
OWLOntology ont = manager.loadOntologyFromOntologyDocument(file);
这就是问题,你不要在你的代码中的任何地方改变ont
,所以当你拨打下面这一行时,它只显示/保存你在文件开头加载的10个人。你的代码:
manager.saveOntology(ont);
System.out.println("Number of individuals: " + ont.getIndividualsInSignature().size());
因此,要解决此问题,您需要以某种方式修改ont
以在使用上述行之前包含新的个人。
答案 1 :(得分:0)
更新代码它在线程" main"中提供了异常。显示java.lang.NullPointerException 在Mgt.main(Mgt.java:29)
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
public class Mgt {
static OntModel jenaModel = null;
public static void main(String[] args) throws Exception{
String file =("D:\\word.owl");
jenaModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
InputStream in = FileManager.get().open(file);
try
{
jenaModel.read(in, null);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Ontology " + file + " loaded.");
OntClass Noun = jenaModel.getOntClass( "http://www.semanticweb.org/Word#Noun" );
Individual Organization = Noun.createIndividual(file + "Organization");
FileWriter out = null;
try {
// XML format - long and verbose
out = new FileWriter( file);
jenaModel.write( out, "RDF/XML" );
}
finally {
if (out != null) {
try {out.close();
} catch (IOException ignore) {}
}
}
}
}
答案 2 :(得分:0)
这有效:
import java.io.FileWriter;
import java.io.InputStream;
import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
import org.apache.jena.vocabulary.RDF;
public class Mgt {
public static void main(String[] args) throws Exception {
String namespace = "http://www.semanticweb.org/Word#";
String file = "word.owl";
OntModel jenaModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
InputStream in = FileManager.get().open(file);
jenaModel.read(in, null);
OntClass Noun = jenaModel.getOntClass(namespace + "Noun");
Individual Organization = Noun.createIndividual(namespace + "Organization");
jenaModel.add(Organization, RDF.type, Noun);
FileWriter out = new FileWriter("word.out.owl");
jenaModel.getWriter("RDF/XML-ABBREV").write(jenaModel, out, namespace);
out.close();
}
}
请注意,命名空间与文件名无关。