11package net .sansa_stack .inference .flink
22
3+ import java .io .File
4+
35import net .sansa_stack .inference .flink .data .{RDFGraphLoader , RDFGraphWriter }
4- import net .sansa_stack .inference .flink .forwardchaining .ForwardRuleReasonerRDFS
6+ import net .sansa_stack .inference .flink .forwardchaining .{ForwardRuleReasonerOWLHorst , ForwardRuleReasonerRDFS }
7+ import net .sansa_stack .inference .rules .ReasoningProfile
8+ import net .sansa_stack .inference .rules .ReasoningProfile ._
59import org .apache .flink .api .java .utils .ParameterTool
610import org .apache .flink .api .scala .ExecutionEnvironment
711
@@ -13,38 +17,81 @@ import org.apache.flink.api.scala.ExecutionEnvironment
1317 */
1418object RDFGraphMaterializer {
1519
16-
1720 def main (args : Array [String ]) {
21+ parser.parse(args, Config ()) match {
22+ case Some (config) =>
23+ run(args, config.in, config.out, config.profile, config.writeToSingleFile, config.sortedOutput)
24+ case None =>
25+ println(parser.usage)
26+ }
27+ }
1828
29+ def run (args : Array [String ], input : Seq [File ], output : File , profile : ReasoningProfile , writeToSingleFile : Boolean , sortedOutput : Boolean ): Unit = {
1930 // get params
2031 val params : ParameterTool = ParameterTool .fromArgs(args)
2132
22- if (params.has(" input" ) && params.has(" output" )) {
33+ // set up the execution environment
34+ val env = ExecutionEnvironment .getExecutionEnvironment
2335
24- // set up the execution environment
25- val env = ExecutionEnvironment .getExecutionEnvironment
36+ // make parameters available in the web interface
37+ env.getConfig.setGlobalJobParameters(params)
2638
27- // make parameters available in the web interface
28- env.getConfig.setGlobalJobParameters(params)
39+ // load triples from disk
40+ val graph = RDFGraphLoader .loadFromDisk(input(0 ), env)
41+ print(graph.size())
2942
30- // load triples from disk
31- val graph = RDFGraphLoader .loadFromFile(params.get(" input" ), env)
43+ // create reasoner
44+ val reasoner = profile match {
45+ case RDFS => new ForwardRuleReasonerRDFS (env)
46+ case OWL_HORST => new ForwardRuleReasonerOWLHorst (env)
47+ }
48+
49+ // compute inferred graph
50+ val inferredGraph = reasoner.apply(graph)
51+ println(s " |G| = $inferredGraph.size() " )
3252
33- // create reasoner
34- val reasoner = new ForwardRuleReasonerRDFS (env )
53+ // write triples to disk
54+ RDFGraphWriter .writeToDisk(inferredGraph, output )
3555
36- // compute inferred graph
37- val inferredGraph = reasoner.apply(graph)
56+ env.execute(s " RDF ${profile} Reasoning " )
3857
39- // write triples to disk
40- RDFGraphWriter .writeToFile(inferredGraph, params.get(" output" ))
58+ }
4159
42- env.execute(" RDF graph materialization" )
60+ // the config object
61+ case class Config (
62+ in : Seq [File ] = Seq (),
63+ out : File = new File (" ." ),
64+ profile : ReasoningProfile = ReasoningProfile .RDFS ,
65+ writeToSingleFile : Boolean = false ,
66+ sortedOutput : Boolean = false )
4367
44- } else {
45- System .err.println(" Usage: RDFGraphMaterializer --input <sourceFile> --output <targetFile>" )
46- System .exit(1 )
47- }
68+ // read ReasoningProfile enum
69+ implicit val profilesRead : scopt.Read [ReasoningProfile .Value ] =
70+ scopt.Read .reads(ReasoningProfile forName _.toLowerCase())
71+
72+ // the CLI parser
73+ val parser = new scopt.OptionParser [Config ](" RDFGraphMaterializer" ) {
74+ head(" RDFGraphMaterializer" , " 0.1.0" )
75+
76+ opt[Seq [File ]]('i' , " input" ).required().valueName(" <path1>,<path2>,..." ).
77+ action((x, c) => c.copy(in = x)).
78+ text(" path to file or directory that contains the input files (in N-Triple format)" )
79+
80+ opt[File ]('o' , " out" ).required().valueName(" <directory>" ).
81+ action((x, c) => c.copy(out = x)).
82+ text(" the output directory" )
83+
84+ opt[Unit ](" single-file" ).optional().action( (_, c) =>
85+ c.copy(writeToSingleFile = true )).text(" write the output to a single file in the output directory" )
86+
87+ opt[Unit ](" sorted" ).optional().action( (_, c) =>
88+ c.copy(sortedOutput = true )).text(" sorted output of the triples (per file)" )
89+
90+ opt[ReasoningProfile ]('p' , " profile" ).required().valueName(" {rdfs | owl-horst | owl-el | owl-rl}" ).
91+ action((x, c) => c.copy(profile = x)).
92+ text(" the reasoning profile" )
93+
94+ help(" help" ).text(" prints this usage text" )
4895 }
4996
5097}
0 commit comments