java - 读取目录中的所有RDF文件

标签 java rdf jena

我试图读取目录中的所有 RDF 文件,但似乎我只读取第一个文件。我首先使用 File 对象获取文件名,然后尝试迭代它们,读取每个文件名。如果我必须做类似的事情,我不知道该怎么办

model = ModelFactory.createDefaultModel();

每次迭代后或关闭输入和输出流或其他内容。到目前为止我的代码如下:

String inputFileName  = "";
Model model = ModelFactory.createDefaultModel();
StringWriter out;
File folder = new File("D:/filepath");
File[] listOfFiles = folder.listFiles();
String result="";

for (int i = 0; i < listOfFiles.length; i++) {
    inputFileName= listOfFiles[i].getName();
    inputFileName = "D:/filepath/" +inputFileName;
    System.out.println(inputFileName);
    InputStream in = FileManager.get().open( inputFileName );

    if (in == null) {
        throw new IllegalArgumentException( "File: " + inputFileName + " not found");
    }

    model.read(in, "");
    String syntax = "RDF/XML-ABBREV"; 
    out = new StringWriter();
    model.write(out, syntax);
    result = out.toString();
    //  extractSomethingFrom(result);
    model = ModelFactory.createDefaultModel();
    in.close();
    out.close();
}

最佳答案

现有代码的问题

Model.read 向模型添加语句,因此代码如下

Model model = ...
for ( ... ) {
  model.read( ... );
}
// do things with model

会给你一个模型,其中包含你读过的所有内容的所有三元组。但是,当您执行此操作时,您将在每次迭代时为 model 分配一个新的空模型

model = ModelFactory.createDefaultModel();

在循环内。这就是为什么每次写出模型时,您只能看到在该迭代中读取的文件中的三元组。

以下代码演示了此行为。有两个包含 RDF 文本的字符串,您可以看到连续读取它们的效果(无论是否在中间创建新模型)。

import java.io.ByteArrayInputStream;
import java.io.IOException;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class ReadMultipleDocuments {
    public static void main(String[] args) throws IOException {
        final String text1 = "@prefix : <urn:ex:>. :a :b :c .";
        final String text2 = "@prefix : <urn:ex:>. :d :e :f .";

        final String[] texts = new String[] { text1, text2 };

            // reset determines whether or not a new model is assigned 
            // to model after reading each text.
        for ( final boolean reset : new boolean[] { true, false } ) {
            System.out.println( "* reset = "+reset );
                    // create the first model
            Model model = ModelFactory.createDefaultModel();
            for ( final String text : texts ) {
                            // read the RDF from the text. This is analogous to reading
                            // the data from a file.
                model.read( new ByteArrayInputStream( text.getBytes() ), null, "TTL" );
                System.out.println( "  * after reading, model size is "+model.size() );
                            // if a new model is created and assigned to the variable
                            // model, then the triples read during this iteration will 
                            // no longer be available (since you've lost the model that 
                            // they were in).
                if ( reset ) {
                    model = ModelFactory.createDefaultModel();
                }
            }
        }
    }
}

如何将目录中的RDF文件读取到单个模型中

使用 Java 中的新文件 IO 实际上可以使这个问题变得更容易。您可以简单地创建一个模型,遍历文件系统,并将每个文件的内容到模型中。这是执行此操作的代码:

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class ReadRDFFilesInDirectory {
    public static void main(String[] args) throws IOException {
        final Model model = ModelFactory.createDefaultModel();
        Files.walkFileTree( Paths.get( "/home/taylorj/tmp/rdfs/" ), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile( final Path file, final BasicFileAttributes attrs) throws IOException {
                model.read( file.toUri().toString() );
                return super.visitFile(file, attrs);
            }
        });
        model.write( System.out );
    }
}

在目录“/home/taylorj/tmp/rdfs/”中,我有三个文件。

one.n3:

@prefix : <urn:ex:>.

:a :b :c .

两个.n3:

@prefix : <urn:ex:>.

:d :e :f .

三.n3:

@prefix : <urn:ex:>.

:g :h :i .

代码读取所有这些并将三元组放入模型中。输出为:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="urn:ex:" > 
  <rdf:Description rdf:about="urn:ex:d">
    <e rdf:resource="urn:ex:f"/>
  </rdf:Description>
  <rdf:Description rdf:about="urn:ex:a">
    <b rdf:resource="urn:ex:c"/>
  </rdf:Description>
  <rdf:Description rdf:about="urn:ex:g">
    <h rdf:resource="urn:ex:i"/>
  </rdf:Description>
</rdf:RDF>

关于java - 读取目录中的所有RDF文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18988306/

相关文章:

java - 像这样嵌套 try/finally 子句安全吗?

java - 使用 xml 文件中的 jena 创建 RDF 三元组和 RDF 存储

java - 耶拿的 SPARQL 查询打印文字

java - 尝试在 jena api 中查找 RDFNode 对象的父类

client - Jena 桌面 SPARQL 客户端 (TDB)?

java - Android,Java 只从字符串数组中获取最后一个字

java - Android:在多个 fragment 之一中使用 MapFragment

rdf - OWL域范围联合

html - 我们从实现 RDF 和链接数据到我们的网站中得到什么?

java - SonarQube java.lang.OutOfMemoryError : GC overhead limit exceeded 错误