使用 Jena 列出类和子类的问题
我不会列出一个类的所有子类。我列出了类,我的算法检查每个类是否有子类。如果为真,则列出所有子类。但这并没有发生,它似乎忽略了条件“if (essaClasse.hasSubClass)。任何人都可以帮助我吗?下面是代码部分。
谢谢!
Debora (里约热内卢 - 巴西)
完整代码:
package testejena;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import java.io.InputStream;
import java.util.Iterator;
public class testeProp {
static final String inputFileName = "OBRecortada3.owl";
public static void main(String args[]) {
try {
//create the reasoning model using the base
OntModel inf = ModelFactory.createOntologyModel();
// use the FileManager to find the input file
InputStream in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException("File: " + inputFileName + " not found");
}
inf.read(in, "");
String URI = "http://www.owl-ontologies.com/OntologyBase.owl#";
ExtendedIterator classes = inf.listClasses();
while (classes.hasNext()) {
OntClass essaClasse = (OntClass) classes.next();
String vClasse = essaClasse.getLocalName().toString();
if (essaClasse.hasSubClass()) {
System.out.println("Classe: " + vClasse);
OntClass cla = inf.getOntClass(URI + vClasse);
for (Iterator i = cla.listSubClasses(); i.hasNext();) {
OntClass c = (OntClass) i.next();
System.out.print(" " + c.getLocalName() + " " + "\n");
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}}
I'm not getting to list all subclasses of a class. I list the classes, my algorithm checks if each class has a subclass. If true, was to list all subclasses. But this doesn´t happen, it seems to ignore the condition "if (essaClasse.hasSubClass). Can anyone help me? Bellow the code part.
Thanks!
Debora
(Rio de Janeiro - Brasil)
The full code:
package testejena;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import java.io.InputStream;
import java.util.Iterator;
public class testeProp {
static final String inputFileName = "OBRecortada3.owl";
public static void main(String args[]) {
try {
//create the reasoning model using the base
OntModel inf = ModelFactory.createOntologyModel();
// use the FileManager to find the input file
InputStream in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException("File: " + inputFileName + " not found");
}
inf.read(in, "");
String URI = "http://www.owl-ontologies.com/OntologyBase.owl#";
ExtendedIterator classes = inf.listClasses();
while (classes.hasNext()) {
OntClass essaClasse = (OntClass) classes.next();
String vClasse = essaClasse.getLocalName().toString();
if (essaClasse.hasSubClass()) {
System.out.println("Classe: " + vClasse);
OntClass cla = inf.getOntClass(URI + vClasse);
for (Iterator i = cla.listSubClasses(); i.hasNext();) {
OntClass c = (OntClass) i.next();
System.out.print(" " + c.getLocalName() + " " + "\n");
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的算法不起作用,因为您没有在
OntModel
中指定OntModelSpec
。指定OntModelSpec
此代码完美运行!your algorithm wasn't working because you didn't specify an
OntModelSpec
in yourOntModel
. Specifying anOntModelSpec
this code works perfectly!您没有显示您的数据或完整代码(包括设置 OntModel 对象的部分),因此很难给出明确的建议。
hasSubClass
方法在 Jena 单元测试中进行了测试,因此它不太可能(尽管并非不可能)包含错误。我建议检查:在运行上述代码之前,您是否正确地将数据加载到
模型
中,使用调试器或日志语句来显示例如加载的三元组的数量< /p>您正在加载的本体加载实际上包含子类语句,包括检查用于在任何
rdfs:subClassOf
三元组中定义rdfs
的前缀声明(它必须完全http://www.w3.org/2000/01/rdf-schema#
)You don't show your data or complete code (including the bit where you set up the OntModel object), so it's hard to give definitive advice. The
hasSubClass
method is tested in the Jena unit tests, so it's unlikely (though not impossible) that it contains a bug. I would suggest checking that:you are correctly loading the data into the
Model
before you run the above code, using a debugger or log statements to show, for example, the number of triples loadedthat ontology you are loading does in fact contain sub-class statements, including checking the prefix declaration used to define
rdfs
in anyrdfs:subClassOf
triples (it must be exactlyhttp://www.w3.org/2000/01/rdf-schema#
)