使用 Jena 列出类和子类的问题

发布于 2024-10-31 11:15:38 字数 1926 浏览 1 评论 0原文

我不会列出一个类的所有子类。我列出了类,我的算法检查每个类是否有子类。如果为真,则列出所有子类。但这并没有发生,它似乎忽略了条件“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫人生路 2024-11-07 11:15:38

您的算法不起作用,因为您没有在 OntModel 中指定 OntModelSpec。指定 OntModelSpec 此代码完美运行!

OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);

your algorithm wasn't working because you didn't specify an OntModelSpec in your OntModel. Specifying an OntModelSpec this code works perfectly!

OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
白日梦 2024-11-07 11:15:38

您没有显示您的数据或完整代码(包括设置 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 loaded

  • that ontology you are loading does in fact contain sub-class statements, including checking the prefix declaration used to define rdfs in any rdfs:subClassOf triples (it must be exactly http://www.w3.org/2000/01/rdf-schema#)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文