我无法更新 ontology 模型
I am not able to update ontology model
我在 Protege 中制作了一个 Ontologu 并导入到 Eclipse.My ontology 已经有 10 个 实例 我想添加更多 instances.The 下面的一段代码 添加实例 到 class(名词)的 ontology。执行后它不会更新 ontology 模型并显示相同数量的实例。
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
我不使用那个 API 但我可以看到你的问题。
在代码的开头,您创建了一个 OWLOntology
对象:
OWLOntology ont = manager.loadOntologyFromOntologyDocument(file);
这就是问题所在,您没有更改 ont
代码中的任何地方,因此当您调用下面的行时,它只会 show/save 您从文件中加载的 10 个相同的人在代码的开头:
manager.saveOntology(ont);
System.out.println("Number of individuals: " + ont.getIndividualsInSignature().size());
因此,要解决此问题,您需要以某种方式修改 ont
以在使用上述行之前包含新的 Individual。
更新代码 它在线程中给出异常 "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) {}
}
}
}
}
这个有效:
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();
}
}
注意命名空间与文件名无关。
我在 Protege 中制作了一个 Ontologu 并导入到 Eclipse.My ontology 已经有 10 个 实例 我想添加更多 instances.The 下面的一段代码 添加实例 到 class(名词)的 ontology。执行后它不会更新 ontology 模型并显示相同数量的实例。
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
我不使用那个 API 但我可以看到你的问题。
在代码的开头,您创建了一个 OWLOntology
对象:
OWLOntology ont = manager.loadOntologyFromOntologyDocument(file);
这就是问题所在,您没有更改 ont
代码中的任何地方,因此当您调用下面的行时,它只会 show/save 您从文件中加载的 10 个相同的人在代码的开头:
manager.saveOntology(ont);
System.out.println("Number of individuals: " + ont.getIndividualsInSignature().size());
因此,要解决此问题,您需要以某种方式修改 ont
以在使用上述行之前包含新的 Individual。
更新代码 它在线程中给出异常 "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) {}
}
}
}
}
这个有效:
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();
}
}
注意命名空间与文件名无关。