@@ -31,11 +31,11 @@ object RdfFromJellyPrint extends RdfCommandPrintUtil[RdfFormat.Writeable]:
3131 " If no input file is specified, the input is read from stdin.\n " +
3232 " If no output file is specified, the output is written to stdout.\n " +
3333 " If an error is detected, the program will exit with a non-zero code.\n " +
34- " Otherwise, the program will exit with code 0.\n " +
34+ " Otherwise, the program will exit with code 0.\n\n " +
3535 " Note: this command works in a streaming manner where possible and scales well to\n " +
3636 " large files. Non-streaming formats (e.g. RDF/XML) by default work on a\n " +
3737 " frame-by-frame basis, but they can be combined into one dataset with the\n " +
38- " --combine option. RDF/XML will only serialize the default model. " ,
38+ " --combine option." ,
3939)
4040@ ArgsName (" <file-to-convert>" )
4141case class RdfFromJellyOptions (
@@ -56,10 +56,18 @@ case class RdfFromJellyOptions(
5656 )
5757 takeFrames : String = " " ,
5858 @ HelpMessage (
59- " Add to combine the results into one dataset, when using a non-streaming output format. " +
60- " Ignored otherwise. Take care with input size, as this option will load everything into memory." ,
59+ " Add to combine all stream frames into one dataset, when using a non-streaming output format. " +
60+ " Ignored otherwise. Take care with input size, as this option will load everything into memory. " +
61+ " Default: false." ,
6162 )
6263 combine : Boolean = false ,
64+ @ HelpMessage (
65+ " Discard the named graph information, treating the input as triples in the default graph. " +
66+ " This allows you to convert a Jelly file containing quads to Turtle/N-Triples in a lossy manner. " +
67+ " This option has no impact on frame boundaries. To merge frames, use the --combine option. " +
68+ " Default: false." ,
69+ )
70+ mergeGraphs : Boolean = false ,
6371 @ Recurse
6472 rdfPerformanceOptions : RdfPerformanceOptions = RdfPerformanceOptions (),
6573) extends HasJellyCommandOptions
@@ -72,8 +80,14 @@ object RdfFromJelly extends RdfSerDesCommand[RdfFromJellyOptions, RdfFormat.Writ
7280
7381 lazy val printUtil : RdfCommandPrintUtil [RdfFormat .Writeable ] = RdfFromJellyPrint
7482
75- val defaultAction : (InputStream , OutputStream ) => Unit =
76- (in, out) => jellyToLang(in, StreamRDFWriter .getWriterStream(out, RdfFormat .NQuads .jenaLang))
83+ val defaultAction : WriteAction =
84+ (in, out, opt) =>
85+ jellyToLang(
86+ in,
87+ StreamRDFWriter .getWriterStream(out, RdfFormat .NQuads .jenaLang),
88+ RdfFormat .NQuads ,
89+ opt,
90+ )
7791
7892 private def takeFrames : IndexRange = IndexRange (getOptions.takeFrames, " --take-frames" )
7993
@@ -84,34 +98,34 @@ object RdfFromJelly extends RdfSerDesCommand[RdfFromJellyOptions, RdfFormat.Writ
8498 takeFrames
8599 val (inputStream, outputStream) =
86100 this .getIoStreamsFromOptions(remainingArgs.remaining.headOption, options.outputFile)
87- parseFormatArgs(inputStream, outputStream, options.outputFormat, options.outputFile)
101+ parseFormatArgs(inputStream, outputStream, options.outputFormat, options.outputFile, options )
88102
89103 override def matchFormatToAction (
90104 format : RdfFormat .Writeable ,
91- ): Option [( InputStream , OutputStream ) => Unit ] =
105+ ): Option [WriteAction ] =
92106 (format, getOptions.combine) match
93107 case (j : RdfFormat .Jena .StreamWriteable , _) =>
94- Some ((in, out) => jellyToLang(in, StreamRDFWriter .getWriterStream(out, j.jenaLang)))
108+ Some ((in, out, opt) =>
109+ jellyToLang(in, StreamRDFWriter .getWriterStream(out, j.jenaLang), j, opt),
110+ )
95111 case (j : RdfFormat .Jena .BatchWriteable , true ) =>
96- Some ((in, out) =>
97- StreamRdfCombiningBatchWriter (out, j.jenaLang).runAndOutput(x => jellyToLang(in, x)),
112+ Some ((in, out, opt) =>
113+ StreamRdfCombiningBatchWriter (out, j.jenaLang).runAndOutput(x =>
114+ jellyToLang(in, x, j, opt),
115+ ),
98116 )
99117 case (j : RdfFormat .Jena .BatchWriteable , false ) =>
100- Some ((in, out) => jellyToLang(in, StreamRdfBatchWriter (out, j.jenaLang)))
118+ Some ((in, out, opt ) => jellyToLang(in, StreamRdfBatchWriter (out, j.jenaLang), j, opt ))
101119 case (RdfFormat .JellyText , _) => Some (jellyBinaryToText)
102120
103121 /** This method reads the Jelly file, rewrites it to specified format and writes it to some output
104122 * stream
105- * @param jenaLang
106- * Language that jelly should be converted to
107- * @param inputStream
108- * InputStream
109- * @param outputStream
110- * OutputStream
111123 */
112124 private def jellyToLang (
113125 inputStream : InputStream ,
114126 writer : StreamRDF ,
127+ format : RdfFormat ,
128+ options : RdfFromJellyOptions ,
115129 ): Unit =
116130 // Whether the output is active at this moment
117131 var outputEnabled = false
@@ -125,7 +139,18 @@ object RdfFromJelly extends RdfSerDesCommand[RdfFromJellyOptions, RdfFormat.Writ
125139 }
126140
127141 override def handleQuad (subject : Node , predicate : Node , `object` : Node , graph : Node ): Unit = {
128- if outputEnabled then writer.quad(Quad .create(graph, subject, predicate, `object`))
142+ if outputEnabled then
143+ if options.mergeGraphs then writer.triple(Triple .create(subject, predicate, `object`))
144+ else if format.supportsQuads then
145+ writer.quad(Quad .create(graph, subject, predicate, `object`))
146+ else if Quad .isDefaultGraph(graph) then
147+ writer.triple(Triple .create(subject, predicate, `object`))
148+ else
149+ throw new CriticalException (
150+ f " Encountered a quad in the input ( $subject $predicate ${`object`} $graph), " +
151+ f " but the output format ( $format) does not support quads. Either choose a different output format " +
152+ " or use the --merge-graphs option to merge all named graphs into the default graph." ,
153+ )
129154 }
130155 }
131156
@@ -153,13 +178,12 @@ object RdfFromJelly extends RdfSerDesCommand[RdfFromJellyOptions, RdfFormat.Writ
153178
154179 /** This method reads the Jelly file, rewrites it to Jelly text and writes it to some output
155180 * stream
156- * @param inputStream
157- * InputStream
158- * @param outputStream
159- * OutputStream
160181 */
161- private def jellyBinaryToText (inputStream : InputStream , outputStream : OutputStream ): Unit =
162-
182+ private def jellyBinaryToText (
183+ inputStream : InputStream ,
184+ outputStream : OutputStream ,
185+ opt : RdfFromJellyOptions ,
186+ ): Unit =
163187 inline def writeFrameToOutput (f : RdfStreamFrame , frameIndex : Int ): Unit =
164188 // we want to write a comment to the file before each frame
165189 val comment = f " # Frame $frameIndex\n "
0 commit comments