读取目录中的所有RDF文件

时间:2013-09-24 17:33:45

标签: 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();
}

2 个答案:

答案 0 :(得分:1)

现有代码的问题

Model.read 语句添加到模型中,因此代码如

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

将为您提供一个模型,其中包含您阅读过的所有内容中的所有三元组。但是,当您执行

时,您将在每次迭代时为model分配一个新的空模型
model = ModelFactory.createDefaultModel();

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

以下代码演示了此行为。有两个包含RDF文本的字符串,您可以在之间创建新模型的情况下连续read查看它们的效果。

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实际上使这个问题变得更加容易。您可以简单地创建模型,遍历文件系统,并将read每个文件的内容放入模型中。这是代码:

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 .

two.n3:

@prefix : <urn:ex:>.

:d :e :f .

three.n3:

@prefix : <urn:ex:>.

:g :h :i .

代码读取所有内容并将三元组放入model。输出是:

<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>

答案 1 :(得分:0)

还有一个库,它会查看文件夹并尝试解析链接数据文件,然后返回RDF图中文件夹中的内容。&#34;

来自https://github.com/nicola/folder-to-rdf