diff options
Diffstat (limited to 'src/main/scala/rsacomb/Main.scala')
-rw-r--r-- | src/main/scala/rsacomb/Main.scala | 69 |
1 files changed, 56 insertions, 13 deletions
diff --git a/src/main/scala/rsacomb/Main.scala b/src/main/scala/rsacomb/Main.scala index 830f1e0..64343f5 100644 --- a/src/main/scala/rsacomb/Main.scala +++ b/src/main/scala/rsacomb/Main.scala | |||
@@ -2,6 +2,10 @@ package rsacomb | |||
2 | 2 | ||
3 | /* Java imports */ | 3 | /* Java imports */ |
4 | import java.io.File | 4 | import java.io.File |
5 | import java.util.HashMap | ||
6 | import scala.collection.JavaConverters._ | ||
7 | |||
8 | import tech.oxfordsemantic.jrdfox.client.UpdateType | ||
5 | 9 | ||
6 | /* Local imports */ | 10 | /* Local imports */ |
7 | import rsacomb.RSA._ | 11 | import rsacomb.RSA._ |
@@ -46,25 +50,64 @@ object RSAComb extends App { | |||
46 | * case. | 50 | * case. |
47 | */ | 51 | */ |
48 | 52 | ||
49 | val ontology = RSA.loadOntology(ontoPath) | 53 | val ontology: RSAOntology = RSA.loadOntology(ontoPath) |
50 | if (ontology.isRSA) { | 54 | if (ontology.isRSA) { |
51 | 55 | ||
52 | /* Build canonical model */ | ||
53 | //val tboxCanon = rsa.canonicalModel() | ||
54 | |||
55 | // DEBUG: print program to generate canonical model | ||
56 | { | ||
57 | ontology.canonicalModel.foreach(println) | ||
58 | } | ||
59 | |||
60 | /* Load query */ | 56 | /* Load query */ |
61 | val query = RDFoxUtil.parseQuery( | 57 | val query = RDFoxUtil.parseQuery( |
62 | "SELECT ?X WHERE {?X ?Y ?Z}" | 58 | """ |
59 | SELECT ?uno | ||
60 | WHERE { | ||
61 | ?uno a :D ; | ||
62 | :R ?due . | ||
63 | ?due :S ?tre . | ||
64 | ?tre a :D . | ||
65 | } | ||
66 | """ | ||
63 | ) | 67 | ) |
64 | 68 | ||
65 | val filter = query map { q => ontology.filteringProgram(q) } | 69 | /* Compute answers to query */ |
66 | 70 | query match { | |
67 | /* ... */ | 71 | case Some(query) => { |
72 | // Open connection to RDFox | ||
73 | val (server, data) = RDFoxUtil.openConnection("AnswerComputation") | ||
74 | |||
75 | // Gather canonical model and filtering rules | ||
76 | val canon = ontology.canonicalModel | ||
77 | val filter = ontology.filteringProgram(query) | ||
78 | |||
79 | // Import relevant data | ||
80 | data.importData(UpdateType.ADDITION, RSA.Prefixes, ":a a :A .") | ||
81 | data.addRules(canon.rules.asJava) | ||
82 | data.addRules(filter.rules.asJava) | ||
83 | |||
84 | // Collect answers to query | ||
85 | for ((v, i) <- filter.variables.view.zipWithIndex) { | ||
86 | println(s"Variable $i:") | ||
87 | val query = s"SELECT ?X ?Y WHERE { ?X internal:Ans_$i ?Y }" | ||
88 | val cursor = | ||
89 | data.createCursor( | ||
90 | RSA.Prefixes, | ||
91 | query, | ||
92 | new HashMap[String, String]() | ||
93 | ); | ||
94 | var mul = cursor.open() | ||
95 | while (mul > 0) { | ||
96 | printf( | ||
97 | "Ans_%d(%s,%s)", | ||
98 | i, | ||
99 | cursor.getResource(0), | ||
100 | cursor.getResource(1) | ||
101 | ) | ||
102 | mul = cursor.advance() | ||
103 | } | ||
104 | } | ||
105 | |||
106 | // Close connection to RDFox | ||
107 | RDFoxUtil.closeConnection(server, data) | ||
108 | } | ||
109 | case None => {} | ||
110 | } | ||
68 | } | 111 | } |
69 | } | 112 | } |
70 | 113 | ||