【问题标题】:Tree like view of Triplets and remove URI's三元组的树状视图并删除 URI
【发布时间】:2019-05-01 10:07:12
【问题描述】:

我在 java 中编写了一个读取本体并打印三元组的代码。代码工作正常。我想在输出中隐藏 URI,并以树层次结构形式打印输出。目前它给我一行输出。知道我该怎么做。

Tree Form Like:

Thing
     Class
         SubClass
            Individual
              so on ...

这是 ReadOntology 类,我在 servlet 中使用的这个类。

public class ReadOntology {

    public static OntModel model;

    public static void run(String ontologyInFile) {

        model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
        InputStream ontologyIn = FileManager.get().open(ontologyInFile);

        loadModel(model, ontologyIn);
    }

    protected static void loadModel(OntModel m, InputStream ontologyIn) {
        try {
             m.read(ontologyIn, "RDF/XML");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

这是小服务程序

public class Ontology extends HttpServlet{

    OntClass ontClass = null;

    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {

        PrintWriter out = res.getWriter();
        ServletContext context = this.getServletContext();
        String fullPath = context.getRealPath("/WEB-INF/Data/taxi.owl");
        ReadOntology.run(fullPath);

        SimpleSelector selector = new SimpleSelector(null, null, (RDFNode)null);

        StmtIterator iter = ReadOntology.model.listStatements(selector);
        while(iter.hasNext()) {
           Statement stmt = iter.nextStatement();
           out.print(stmt.getSubject().toString());
           out.print(stmt.getPredicate().toString());
           out.println(stmt.getObject().toString());
        }
    }
}

【问题讨论】:

    标签: eclipse servlets jena ontology


    【解决方案1】:

    作为实现目标的一步,这将按主题对语句进行分组,并且谓词仅显示本地名称:

    ResIterator resIt = ReadOntology.model.listSubjects()
    while (resIt.hasNext()) {
        Resource r = resIt.nextResource();
        out.println(r);
        StmtIterator iter = r.listProperties();
        while (iter.hasNext()) {
            Statement stmt = iter.nextStatement();
            out.print("   ");
            out.print(stmt.getPredicate().getLocalName());
            out.println(stmt.getObject());
        }
    }
    

    ResourceModel 的 API 中有很多有用的方法。

    要渲染完整的类树,请使用 OntModelOntClass 上的方法。也许:

    private void printClass(Writer out, OntClass clazz, int indentation) {
       String space = '    '.repeat(indentation);
    
       // print space + clazz.getLocalName()
       ...
       // iterate over clazz.listSubClasses(true)
       // and call printClass for each with indentation increased by 1
       ...
       // iterator over clazz.listInstances()
       // and print all their properties as in the
       // snippet above but with space added
    }
    

    然后在 service 方法中,遍历 OntModel 的类,对于 hasSuperClass() 为 false 的任何地方,调用 printClass(out, clazz, 0)

    【讨论】:

    • 是的,它显示名称。谢谢你。现在是我问题的第二部分。
    • 你的解释不是很清楚。 “在树的层次结构中”,你是什么意思?如果您用更清晰的解释编辑您的问题,那么也许有人可以提供帮助。
    • 是的,这更清楚了。我已经用一种方法的草图更新了我的答案。祝你好运!
    • 感谢您的帮助。
    猜你喜欢
    • 2011-03-04
    • 2020-05-22
    • 2018-01-29
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2019-01-16
    相关资源
    最近更新 更多