|
1 | 1 | package net.sansa_stack.inference.common |
2 | 2 |
|
| 3 | +import java.nio.file.{Path, Paths} |
| 4 | + |
3 | 5 | import net.sansa_stack.inference.rules._ |
4 | 6 | import net.sansa_stack.inference.rules.minimizer.DefaultRuleDependencyGraphMinimizer |
5 | 7 | import net.sansa_stack.inference.utils.GraphUtils._ |
6 | 8 | import net.sansa_stack.inference.utils.RuleUtils |
7 | 9 |
|
8 | 10 | /** |
| 11 | + * Computes a given set of rules and exports its rule dependency graph before and after minimization. |
| 12 | + * |
| 13 | + * |
9 | 14 | * @author Lorenz Buehmann |
10 | 15 | */ |
11 | 16 | object DependencyGraphTest { |
12 | 17 |
|
| 18 | + // the config object |
| 19 | + case class Config(in: Path = null, |
| 20 | + out: Path = null, |
| 21 | + profile: String = "", |
| 22 | + ruleNames: Seq[String] = Seq() |
| 23 | + ) |
| 24 | + |
| 25 | + implicit val pathRead: scopt.Read[Path] = |
| 26 | + scopt.Read.reads(Paths.get(_)) |
| 27 | + |
| 28 | + // the CLI parser |
| 29 | + val parser = new scopt.OptionParser[Config]("DependencyGraphTest") { |
| 30 | + |
| 31 | + head("DependencyGraphTest", "0.1.0") |
| 32 | + |
| 33 | + opt[Path]('i', "input").required().valueName("<path>"). |
| 34 | + action((x, c) => c.copy(in = x)). |
| 35 | + text("path to file containing the rules") |
| 36 | + |
| 37 | + opt[Path]('o', "out").required().valueName("<directory>"). |
| 38 | + action((x, c) => c.copy(out = x)). |
| 39 | + text("the output directory") |
| 40 | + |
| 41 | + opt[String]('p', "profile").required().valueName("<profile name>"). |
| 42 | + action((x, c) => c.copy(profile = x)). |
| 43 | + text("the name of the set of rules to process - will be used for output files") |
| 44 | + |
| 45 | + opt[Seq[String]]("rules").optional().valueName("<ruleName1>,<ruleName2>,..."). |
| 46 | + action((x, c) => { |
| 47 | + c.copy(ruleNames = x) |
| 48 | + }). |
| 49 | + text("list of rule names to process just a subset of the rules contained in the given input file") |
| 50 | + } |
| 51 | + |
13 | 52 | def main(args: Array[String]): Unit = { |
14 | 53 |
|
15 | | - val path = "/tmp" |
| 54 | + parser.parse(args, Config()) match { |
| 55 | + case Some(config) => |
| 56 | + run(config) |
| 57 | + case None => |
| 58 | + // scalastyle:off println |
| 59 | + println(parser.usage) |
| 60 | + // scalastyle:on println |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + def run(config: Config): Unit = { |
| 65 | + |
| 66 | + // make output dirs |
| 67 | + config.out.toFile.mkdirs() |
| 68 | + |
| 69 | + // load the rules |
| 70 | + var rules = RuleUtils.load(config.in.toAbsolutePath.toString) |
| 71 | + |
| 72 | + // filter if necessary |
| 73 | + if(config.ruleNames.nonEmpty) { |
| 74 | + rules = rules.filter(r => config.ruleNames.contains(r.getName)) |
| 75 | + } |
16 | 76 |
|
17 | 77 | // val names = Seq("rdfp13a", "rdfp13b", "rdfp13c", "rdfs5", "rdfs7") // property rules |
18 | | - val names = Seq("rdfp13a", "rdfp13b", "rdfp13c", "rdfs5", "rdfs7", "rdfp3", "rdfp4") // property rules + some instance rules |
| 78 | + val names = Seq("rdfp13a", "rdfp13b", "rdfp13c")//, "rdfs5", "rdfs7", "rdfp3", "rdfp4") // property rules + some instance rules |
19 | 79 | // val names = Seq("rdfs5", "rdfs7", "rdfp3", "rdfp4") // property TC rule + some instance rules |
20 | 80 |
|
21 | | - // define the rules |
22 | | - val rules = RuleSets.OWL_HORST.filter(r => names.contains(r.getName)) |
23 | | - val profile = ReasoningProfile.OWL_HORST |
24 | | -// val rules = RuleSets.RDFS_SIMPLE |
25 | | -// val profile = ReasoningProfile.RDFS_SIMPLE |
26 | | - |
27 | 81 | val minimizer = new DefaultRuleDependencyGraphMinimizer() |
28 | 82 |
|
29 | | - // export graphs |
30 | | - rules.foreach(rule => RuleUtils.asGraph(rule).export(s"${path}/rule-${rule.getName}.graphml")) |
| 83 | + // export graphs for each rule |
| 84 | + rules.foreach(rule => RuleUtils.asGraph(rule).export(config.out.resolve(s"rule_${rule.getName}.graphml").toAbsolutePath.toString)) |
31 | 85 |
|
32 | 86 | // generate the rule dependency graph |
33 | | - var dependencyGraph = RuleDependencyGraphGenerator.generate(rules) |
34 | | - dependencyGraph.export(s"${path}/rdg-${profile}.graphml", true) |
| 87 | + var dependencyGraph = RuleDependencyGraphGenerator.generate(rules.toSet) |
| 88 | + dependencyGraph.export(config.out.resolve(s"rdg_${config.profile}.graphml").toAbsolutePath.toString, showInFlowDirection = true) |
35 | 89 |
|
| 90 | + // generate the minimized graph |
36 | 91 | dependencyGraph = minimizer.execute(dependencyGraph) // RuleDependencyGraphGenerator.generate(rules, pruned = true) |
37 | | - dependencyGraph.export(s"${path}/rdg-${profile}-pruned.graphml", true) |
38 | | -// dependencyGraph.exportAsPDF(s"${path}/rdg-${profile}-pruned.pdf") |
| 92 | + dependencyGraph.export(config.out.resolve(s"rdg_${config.profile}_minimized.graphml").toAbsolutePath.toString, showInFlowDirection = true) |
39 | 93 |
|
40 | 94 | // generate the high-level dependency graph |
41 | 95 | val highLevelDependencyGraph = HighLevelRuleDependencyGraphGenerator.generate(dependencyGraph) |
|
0 commit comments