现有代码的问题
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 文件读入单个 Model
使用 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/"
中,我有三个文件。
一个.n3:
@prefix : <urn:ex:>.
:a :b :c .
二.n3:
@prefix : <urn:ex:>.
:d :e :f .
三.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>