java - Reading all the RDF files in a directory -
i trying read rdf files in directory, seems reading first file. first file names using file object , try iterate through them, reading each one. dont know if have like
model = modelfactory.createdefaultmodel();
after each iteration or close input , output streams or else. code far follows:
string inputfilename = ""; model model = modelfactory.createdefaultmodel(); stringwriter out; file folder = new file("d:/filepath"); file[] listoffiles = folder.listfiles(); string result=""; (int = 0; < 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(); }
the problem existing code
model.read
adds statements model, code like
model model = ... ( ... ) { model.read( ... ); } // things model
will give model containing triples things you've read. however, you're assigning new, empty model model
on each iteration when do
model = modelfactory.createdefaultmodel();
inside loop. that's why each time write out model you're seeing triples file read on iteration.
the following code demonstrates behavior. there 2 strings containing rdf text, , can see effect of read
ing them successively , without creating new model in between.
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 new model assigned // model after reading each text. ( final boolean reset : new boolean[] { true, false } ) { system.out.println( "* reset = "+reset ); // create first model model model = modelfactory.createdefaultmodel(); ( final string text : texts ) { // read rdf text. analogous reading // data file. model.read( new bytearrayinputstream( text.getbytes() ), null, "ttl" ); system.out.println( " * after reading, model size "+model.size() ); // if new model created , assigned variable // model, triples read during iteration // no longer available (since you've lost model // in). if ( reset ) { model = modelfactory.createdefaultmodel(); } } } } }
how read rdf files in directory single model
this problem made easier new file io in java. can create model, walk filesystem, , read
contents of each file model. here's code that:
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 ); } }
in directory "/home/taylorj/tmp/rdfs/"
, i've got 3 files.
one.n3:
@prefix : <urn:ex:>. :a :b :c .
two.n3:
@prefix : <urn:ex:>. :d :e :f .
three.n3:
@prefix : <urn:ex:>. :g :h :i .
the code reads of them , puts triples model
. output is:
<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>
Comments
Post a Comment