Mercurial > hg > semantic-sia
comparison src/org/qmul/eecs/c4dm/sia/SiaTecMain.java @ 44:d1fed3d54a82
organised imports
changed various public/private static final Strings
no longer uses Pellet - now uses a basic jena ontology model
reads/writes model to/from an external TDB dataset instead of flat files
performs sparql updates (inserts) when creating the vector table instead of sparql constructs followed by adding new triples to the model
removed unnecessary sparql select and ask queries
the whole procedure is now timed
in printSetOfTranslators, now uses compareToIgnoreDatapoints instead of compareTo
author | stevenh |
---|---|
date | Tue, 02 Apr 2013 21:57:19 +0100 |
parents | b43260af3de6 |
children |
comparison
equal
deleted
inserted
replaced
43:f3f6ef9bd5bd | 44:d1fed3d54a82 |
---|---|
1 package org.qmul.eecs.c4dm.sia; | 1 package org.qmul.eecs.c4dm.sia; |
2 | 2 |
3 import java.io.File; | |
4 import java.io.FileNotFoundException; | |
5 import java.io.FileOutputStream; | |
6 import java.util.ArrayList; | 3 import java.util.ArrayList; |
7 import java.util.Collections; | 4 import java.util.Collections; |
8 import java.util.List; | 5 import java.util.List; |
9 | 6 |
10 import org.mindswap.pellet.jena.PelletReasonerFactory; | |
11 import org.qmul.eecs.c4dm.sia.exceptions.DimensionException; | 7 import org.qmul.eecs.c4dm.sia.exceptions.DimensionException; |
12 import org.qmul.eecs.c4dm.sia.model.Datapoint; | 8 import org.qmul.eecs.c4dm.sia.model.Datapoint; |
13 import org.qmul.eecs.c4dm.sia.model.NDimensionalObject; | 9 import org.qmul.eecs.c4dm.sia.model.NDimensionalObject; |
14 import org.qmul.eecs.c4dm.sia.model.SiatecX; | 10 import org.qmul.eecs.c4dm.sia.model.SiatecX; |
15 import org.qmul.eecs.c4dm.sia.model.VectorTable; | 11 import org.qmul.eecs.c4dm.sia.model.VectorTable; |
16 import org.qmul.eecs.c4dm.sia.model.VectorTableElement; | 12 import org.qmul.eecs.c4dm.sia.model.VectorTableElement; |
17 import org.qmul.eecs.c4dm.sia.rdf.Namespaces; | |
18 import org.qmul.eecs.c4dm.sia.utilities.Display; | 13 import org.qmul.eecs.c4dm.sia.utilities.Display; |
19 import org.qmul.eecs.c4dm.sparql.utilities.SparqlWrapperMethods; | |
20 | 14 |
21 import com.hp.hpl.jena.ontology.OntModel; | 15 import com.hp.hpl.jena.ontology.OntModel; |
16 import com.hp.hpl.jena.ontology.OntModelSpec; | |
17 import com.hp.hpl.jena.query.Dataset; | |
18 import com.hp.hpl.jena.query.ReadWrite; | |
22 import com.hp.hpl.jena.rdf.model.Model; | 19 import com.hp.hpl.jena.rdf.model.Model; |
23 import com.hp.hpl.jena.rdf.model.ModelFactory; | 20 import com.hp.hpl.jena.rdf.model.ModelFactory; |
21 import com.hp.hpl.jena.tdb.TDBFactory; | |
22 import com.hp.hpl.jena.update.UpdateAction; | |
24 | 23 |
25 /** | 24 /** |
26 * <p> | 25 * <p> |
27 * Title: SiaTecMain | 26 * Title: SiaTecMain |
28 * </p> | 27 * </p> |
34 * | 33 * |
35 * @author Steve Hargreaves, C4DM, Queen Mary University of London | 34 * @author Steve Hargreaves, C4DM, Queen Mary University of London |
36 */ | 35 */ |
37 public class SiaTecMain { | 36 public class SiaTecMain { |
38 | 37 |
39 // The ontology loaded as dataset | 38 // UPDATE queries |
40 private static final String ontology = "file:src/rdf/siaDatapointOntology.n3"; | 39 private static final String insertSiatecVectorTableBNodesQuery = "file:src/sparql/insert_siatec_vector_table_bnodes.sparql"; |
41 | 40 private static final String insertVectorTableDetailsQuery = "file:src/sparql/insert_vector_table_details.sparql"; |
42 // The final output file | 41 |
43 private static final String finalModelFileName = "src/rdf/finalSiaTecModel"; | |
44 | |
45 // SPARQL queries | |
46 private static final String[] selectQueries = new String[] { | |
47 // A SPARQL-DL query | |
48 "file:src/sparql/select_all.sparql" | |
49 }; | |
50 | |
51 // CONSTRUCT queries | |
52 private static final String[] constructQueries = new String[] { | |
53 // SPARQL-DL CONSTRUCT queries | |
54 "file:src/sparql/construct_siatec_vector_table_bnodes.sparql", | |
55 "file:src/sparql/construct_vector_table_details.sparql" }; | |
56 | |
57 // ASK queries | |
58 private static final String[] askQueries = new String[] { | |
59 // A SPARQL-DL ASK query | |
60 }; | |
61 | |
62 public void run() { | 42 public void run() { |
63 | 43 |
64 // First create a Jena ontology model backed by the Pellet reasoner | 44 // Obtain a dataset context |
65 // (note, the Pellet reasoner is required) | 45 Dataset dataset = TDBFactory.assembleDataset(SiaMain.assemblerFile); |
66 OntModel ontModel = ModelFactory | 46 |
67 .createOntologyModel(PelletReasonerFactory.THE_SPEC); | 47 // Get the sia graph, as a model, from the dataset |
68 | 48 Model siaModel = dataset.getNamedModel(SiaMain.graph); |
69 // Then read the data from the file into the ontology model | 49 |
70 ontModel.read(ontology, "N3"); | 50 OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, siaModel); |
71 | |
72 // Set the value of SIA_NS_URI as the namespace for 'sia' found in | |
73 // the ontology | |
74 Namespaces.SIA_NS_URI = ontModel.getNsPrefixURI("sia"); | |
75 | 51 |
76 // Create custom sia Datapoint objects | 52 // Create custom sia Datapoint objects |
77 List<Datapoint> datapoints = SiaDatapointFactory.create(ontModel); | 53 List<Datapoint> datapoints = SiaDatapointFactory.create(ontModel); |
78 | 54 |
79 // STEP 1 - Order the datapoints | 55 // STEP 1 - Order the datapoints |
80 Collections.sort(datapoints); | 56 Collections.sort(datapoints); |
81 | 57 |
82 // Add datapoint order info to model | 58 // Add datapoint order info to model |
83 SiaDatapointFactory.assertOrder(ontModel, datapoints); | 59 SiaDatapointFactory.assertOrder(ontModel, datapoints); |
84 | 60 |
85 // STEP 2 - Compute the vector table W, and sort | 61 // STEP 2 - Compute the vector table |
86 // Run all the CONSTRUCT queries | 62 // Run all the UPDATE queries |
87 for (int i = 0; i < constructQueries.length; i++) { | 63 UpdateAction.readExecute(insertSiatecVectorTableBNodesQuery, ontModel); |
88 String constructQuery = constructQueries[i]; | 64 UpdateAction.readExecute(insertVectorTableDetailsQuery, ontModel); |
89 | 65 |
90 Model newModel = SparqlWrapperMethods.executeConstructQuery(constructQuery, ontModel); | |
91 | |
92 // Add new triples to the current model | |
93 ontModel.add(newModel); | |
94 } | |
95 VectorTable vectorTableW = SiaVectorTableElementFactory.createW(ontModel, datapoints); | 66 VectorTable vectorTableW = SiaVectorTableElementFactory.createW(ontModel, datapoints); |
96 | 67 |
97 // STEP 3 - Create custom SIATEC VectorTableElement objects (V) | 68 // STEP 3 - Create custom SIATEC VectorTableElement objects (V) |
98 VectorTable vectorTableV = SiaVectorTableElementFactory.createV(ontModel, datapoints); | 69 VectorTable vectorTableV = SiaVectorTableElementFactory.createV(ontModel, datapoints); |
99 List<VectorTableElement> vteListV = vectorTableV.getVteList(); | 70 List<VectorTableElement> vteListV = vectorTableV.getVteList(); |
125 iq.setQ(q); | 96 iq.setQ(q); |
126 x.add(iq); | 97 x.add(iq); |
127 i = j; | 98 i = j; |
128 } | 99 } |
129 | 100 |
130 System.out.println("X"); | |
131 printX(x); | |
132 | |
133 // Sort the elements of x | 101 // Sort the elements of x |
134 Collections.sort(x); | 102 Collections.sort(x); |
135 | 103 |
136 // Rename as y to match algorithm description in paper | 104 // Rename as y to match algorithm description in paper |
137 ArrayList<SiatecX> y = (ArrayList<SiatecX>) x.clone(); | 105 ArrayList<SiatecX> y = (ArrayList<SiatecX>) x.clone(); |
138 | |
139 System.out.println("\nY = Sorted X"); | |
140 printX(y); | |
141 | 106 |
142 // Display results using algorithm figures 23, 24 and 25 of the research paper | 107 // Display results using algorithm figures 23, 24 and 25 of the research paper |
143 int r = y.size(); | 108 int r = y.size(); |
144 m = vteListV.size(); | 109 m = vteListV.size(); |
145 i = 0; | 110 i = 0; |
149 { | 114 { |
150 do | 115 do |
151 { | 116 { |
152 int j = y.get(i).getI() - 1; | 117 int j = y.get(i).getI() - 1; |
153 List<Integer> iList = new ArrayList<Integer>(); | 118 List<Integer> iList = new ArrayList<Integer>(); |
154 while (j < m && (vteListV.get(j).getVector().compareTo(vteListV.get(y.get(i).getI() - 1).getVector()) == 0)) | 119 while (j < m && (vteListV.get(j).compareToIgnoreDatapoints(vteListV.get(y.get(i).getI() - 1)) == 0)) |
155 { | 120 { |
156 iList.add(vteListV.get(j).getFromDatapoint().getOrderedIndex()); | 121 iList.add(vteListV.get(j).getFromDatapoint().getOrderedIndex()); |
157 j++; | 122 j++; |
158 } | 123 } |
159 System.out.print("<"); | 124 System.out.print("<"); |
173 } | 138 } |
174 } while (i < r); | 139 } while (i < r); |
175 } | 140 } |
176 System.out.println("}"); | 141 System.out.println("}"); |
177 | 142 |
178 // Run all the SELECT queries | 143 // Write the model to the TDB dataset |
179 for (i = 0; i < selectQueries.length; i++) { | 144 dataset.begin(ReadWrite.WRITE) ; |
180 | 145 try { |
181 String selectQuery = selectQueries[i]; | 146 dataset.replaceNamedModel(SiaMain.graph, ontModel); |
182 SparqlWrapperMethods.queryTheModel(selectQuery, ontModel); | 147 dataset.commit(); |
183 | 148 System.out.println("dataset.commit() done"); |
184 } | 149 } finally { |
185 | 150 dataset.end(); |
186 // Run all the ASK queries | 151 System.out.println("dataset.end() done"); |
187 for (i = 0; i < askQueries.length; i++) { | 152 } |
188 | 153 |
189 String askQuery = askQueries[i]; | 154 dataset.close(); |
190 SparqlWrapperMethods.askTheModel(askQuery, ontModel); | 155 System.out.println("dataset.close() done"); |
191 | 156 |
192 } | 157 System.out.println("Number of Datapoints (n) = " + datapoints.size() + ", k = 3"); |
193 | |
194 // Write the model to a file | |
195 File outFileRdf = new File(finalModelFileName + ".rdf"); | |
196 File outFileN3 = new File(finalModelFileName + ".n3"); | |
197 FileOutputStream outFileOutputStreamRdf; | |
198 FileOutputStream outFileOutputStreamN3; | |
199 | |
200 // RDF/XML version | |
201 try { | |
202 outFileOutputStreamRdf = new FileOutputStream(outFileRdf); | |
203 ontModel.writeAll(outFileOutputStreamRdf, "RDF/XML", null); | |
204 } catch (FileNotFoundException e) { | |
205 System.out.println("Unable to write to file: " | |
206 + outFileRdf.getAbsolutePath()); | |
207 e.printStackTrace(); | |
208 System.exit(1); | |
209 } | |
210 | |
211 // N3 version | |
212 try { | |
213 outFileOutputStreamN3 = new FileOutputStream(outFileN3); | |
214 ontModel.writeAll(outFileOutputStreamN3, "N3", null); | |
215 } catch (FileNotFoundException e) { | |
216 System.out.println("Unable to write to file: " | |
217 + outFileN3.getAbsolutePath()); | |
218 e.printStackTrace(); | |
219 System.exit(1); | |
220 } | |
221 | |
222 System.out.println("Model written to files: " | |
223 + outFileRdf.getAbsolutePath() + " and " + outFileN3.getAbsolutePath()); | |
224 } | 158 } |
225 | 159 |
226 private void printSetOfTranslators(List<Integer> iList, List<Datapoint> datapoints, VectorTable vectorTableW) { | 160 private void printSetOfTranslators(List<Integer> iList, List<Datapoint> datapoints, VectorTable vectorTableW) { |
227 int p = iList.size(); | 161 int p = iList.size(); |
228 int n = datapoints.size(); | 162 int n = datapoints.size(); |
256 { | 190 { |
257 jList.set(k, jList.get(k - 1) + 1); | 191 jList.set(k, jList.get(k - 1) + 1); |
258 } | 192 } |
259 | 193 |
260 while (jList.get(k) <= (n - p + k + 1) | 194 while (jList.get(k) <= (n - p + k + 1) |
261 && (vectorTableW.getVector(iList.get(k), jList.get(k)).compareTo(vectorTableW.getVector(iList.get(k - 1), jList.get(k - 1))) < 0)) | 195 && (((VectorTableElement)vectorTableW.getVector(iList.get(k), jList.get(k))).compareToIgnoreDatapoints(((VectorTableElement)vectorTableW.getVector(iList.get(k - 1), jList.get(k - 1)))) < 0)) |
262 { | 196 { |
263 jList.set(k, jList.get(k) + 1); | 197 jList.set(k, jList.get(k) + 1); |
264 } | 198 } |
265 | 199 |
266 if (jList.get(k) > n - p + k + 1) | 200 if (jList.get(k) > n - p + k + 1) |
267 { | 201 { |
268 finished = true; | 202 finished = true; |
269 } | 203 } |
270 else if (vectorTableW.getVector(iList.get(k), jList.get(k)).compareTo(vectorTableW.getVector(iList.get(k - 1), jList.get(k - 1))) > 0) | 204 else if (((VectorTableElement)vectorTableW.getVector(iList.get(k), jList.get(k))).compareToIgnoreDatapoints(((VectorTableElement)vectorTableW.getVector(iList.get(k - 1), jList.get(k - 1)))) > 0) |
271 { | 205 { |
272 k = 1; | 206 k = 1; |
273 jList.set(0, jList.get(0) + 1); | 207 jList.set(0, jList.get(0) + 1); |
274 if (jList.get(0) > n - p + 1) | 208 if (jList.get(0) > n - p + 1) |
275 { | 209 { |
336 | 270 |
337 /** | 271 /** |
338 * @param args | 272 * @param args |
339 */ | 273 */ |
340 public static void main(String[] args) { | 274 public static void main(String[] args) { |
275 long startTime = System.currentTimeMillis(); | |
341 SiaTecMain app = new SiaTecMain(); | 276 SiaTecMain app = new SiaTecMain(); |
342 app.run(); | 277 app.run(); |
278 long endTime = System.currentTimeMillis(); | |
279 long elapsedTime = endTime - startTime; | |
280 System.out.println("Completed in " + elapsedTime + " ms"); | |
343 } | 281 } |
344 | 282 |
345 } | 283 } |