1 | package hozo.sparql.plugin.compare;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.OutputStreamWriter;
|
---|
9 | import java.io.PrintWriter;
|
---|
10 | import java.io.UnsupportedEncodingException;
|
---|
11 | import java.sql.Timestamp;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Date;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
---|
19 | import com.ibm.icu.text.SimpleDateFormat;
|
---|
20 |
|
---|
21 | import hozo.sparql.EndpointSettings;
|
---|
22 | import hozo.sparql.EndpointSettingsManager;
|
---|
23 | import hozo.sparql.PlainSparqlAccessor;
|
---|
24 | import hozo.sparql.SparqlQueryListener;
|
---|
25 |
|
---|
26 | public class CompareSubject {
|
---|
27 |
|
---|
28 | private File csvFile;
|
---|
29 |
|
---|
30 | private File propertyFile;
|
---|
31 | private HashMap<String, PlainSparqlAccessor> accessorMap;
|
---|
32 |
|
---|
33 | private static final String SEPARATOR = ",";
|
---|
34 |
|
---|
35 | private Thread thread;
|
---|
36 | private boolean finalizeThread = false;
|
---|
37 | private SparqlQueryListener listener;
|
---|
38 |
|
---|
39 | public CompareSubject(File csvFile, File endpointFile){
|
---|
40 | this(csvFile, endpointFile, null);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public CompareSubject(File csvFile, File endpointFile, SparqlQueryListener listener){
|
---|
44 | this.csvFile = csvFile;
|
---|
45 | this.propertyFile = endpointFile;
|
---|
46 | this.listener = listener;
|
---|
47 |
|
---|
48 | this.accessorMap = new HashMap<String, PlainSparqlAccessor>();
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | public void outputResult(File out, CompareResultListener listener){
|
---|
53 | thread = new QueryThread(new Object[]{out}, listener){
|
---|
54 | public void run(){
|
---|
55 | try {
|
---|
56 | File file = (File)((Object[])getOption())[0];
|
---|
57 | getCompareResultListener().resultReceived(outputResult(file));
|
---|
58 | } catch(Exception e){
|
---|
59 | throw new RuntimeException(e);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | };
|
---|
63 | thread.setUncaughtExceptionHandler(listener);
|
---|
64 | thread.start();
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | private boolean outputResult(File out) throws IOException {
|
---|
69 | if (out.getParentFile() != null && !out.getParentFile().exists()){
|
---|
70 | out.getParentFile().mkdirs();
|
---|
71 | }
|
---|
72 | out.createNewFile();
|
---|
73 | if (!out.canWrite()){
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return outputResult(new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), "SJIS")));
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void stop(){
|
---|
81 | println(getCurrentTime()+"Stop Request");
|
---|
82 | finalizeThread = true;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | private boolean outputResult(PrintWriter out){
|
---|
87 | HashMap<String, List<String>> propertyHash = readPropertyFile();
|
---|
88 |
|
---|
89 | getResources(propertyHash, out);
|
---|
90 |
|
---|
91 | out.flush();
|
---|
92 | out.close();
|
---|
93 |
|
---|
94 | println(getCurrentTime()+"query end.");
|
---|
95 |
|
---|
96 | return true; // TODO
|
---|
97 | }
|
---|
98 |
|
---|
99 | private HashMap<String, List<String>> readPropertyFile(){
|
---|
100 | HashMap<String, List<String>> propertyHash = new HashMap<String, List<String>>();
|
---|
101 | List<String> contents = FileUtil.readFileText(propertyFile, "UTF-8");
|
---|
102 |
|
---|
103 | for (String line : contents){
|
---|
104 | List<String> property = FileUtil.splitLine(line, SEPARATOR);
|
---|
105 | if (property != null && property.size() > 1){
|
---|
106 | // 荳繧ォ繝ゥ繝逶ョ縺ッendpoint
|
---|
107 | List<String> properties = new ArrayList<String>();
|
---|
108 | propertyHash.put(property.get(0), properties);
|
---|
109 | for (int i=1; i<property.size(); i++){
|
---|
110 | properties.add(property.get(i));
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | return propertyHash;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | private void println(){
|
---|
120 | println("");
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void println(String str){
|
---|
124 | print(str + "\n");
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void print(String str){
|
---|
128 | if (listener != null){
|
---|
129 | listener.sparqlExecuted(str);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | private void getResources(HashMap<String, List<String>> propertyHash, PrintWriter out){
|
---|
135 | List<String> endpoints = null;
|
---|
136 | List<String> files = FileUtil.readFileText(csvFile, "SJIS");
|
---|
137 |
|
---|
138 | println();
|
---|
139 | println(getCurrentTime()+"query start.");
|
---|
140 |
|
---|
141 | if (files != null && files.size() > 0){
|
---|
142 | // 荳陦檎岼縺ッ繝倥ャ繝
|
---|
143 | endpoints = getEndpoints(files.get(0));
|
---|
144 |
|
---|
145 | setEndpoint(endpoints);
|
---|
146 | }
|
---|
147 |
|
---|
148 | // header蜃コ蜉� |
---|
149 | StringBuilder line = new StringBuilder();
|
---|
150 | line.append("");
|
---|
151 | line.append(SEPARATOR);
|
---|
152 | for (String endpoint : endpoints){
|
---|
153 | line.append(endpoint);
|
---|
154 | line.append(SEPARATOR);
|
---|
155 | line.append("");
|
---|
156 | line.append(SEPARATOR);
|
---|
157 | line.append("");
|
---|
158 | line.append(SEPARATOR);
|
---|
159 | }
|
---|
160 | out.append(line.toString());
|
---|
161 | out.append("\n");
|
---|
162 |
|
---|
163 | for (int i=1; i<files.size(); i++){
|
---|
164 | if (!finalizeThread){
|
---|
165 | getResource(endpoints, FileUtil.splitLine(files.get(i), SEPARATOR), propertyHash, out);
|
---|
166 | } else {
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * 荳陦後#縺ィ縺ォ
|
---|
175 | * @param endpoints
|
---|
176 | * @param contents
|
---|
177 | * @param propertyHash
|
---|
178 | * @param out
|
---|
179 | */
|
---|
180 | private void getResource(List<String> endpoints, List<String> contents, HashMap<String, List<String>> propertyHash, PrintWriter out){
|
---|
181 | int index=1;
|
---|
182 | int max = 0;
|
---|
183 |
|
---|
184 | String label = contents.get(0);
|
---|
185 | List<HashMap<String, String>> ret = new ArrayList<HashMap<String,String>>();
|
---|
186 |
|
---|
187 | print(getCurrentTime()+"searching ["+label+"]");
|
---|
188 |
|
---|
189 | for (String endpoint : endpoints){
|
---|
190 | print("*");
|
---|
191 | HashMap<String, String> properToResultHash = new HashMap<String, String>();
|
---|
192 | ret.add(properToResultHash);
|
---|
193 | // 繧ィ繝ウ繝峨�繧、繝ウ繝医#縺ィ縺ョ繝ェ繧ス繝シ繧ケ蜿門セ� |
---|
194 | List<String> properties = propertyHash.get(endpoint);
|
---|
195 | if (properties != null){
|
---|
196 | HashMap<String, List<String>> results = getResource(endpoint, contents.get(index), properties);
|
---|
197 | if (max < results.size()){
|
---|
198 | max = results.size();
|
---|
199 | }
|
---|
200 | for (String property : results.keySet()){
|
---|
201 | // property縺斐→縺ョ邨先棡
|
---|
202 | String result = getResult(results.get(property));
|
---|
203 | properToResultHash.put(property, result);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | index++;
|
---|
207 | }
|
---|
208 | println();
|
---|
209 |
|
---|
210 | // TODO properToResultHash縺ョ譛螟ァ蛟、繧貞叙蠕励☆繧� |
---|
211 | // 縺昴�謨ー縺縺題。後r霑ス蜉縺吶k縲� |
---|
212 |
|
---|
213 | /* 1, endpoint1, , endpoint2, ,.....
|
---|
214 | * label1, property1-1, object1-1, property2-1, object2-1 ,....o縲縲竊� |
---|
215 | * label1, property1-2, object1-2, property2-2, object2-2 ,....o 縺薙�遽�峇繧貞昇繧� |
---|
216 | * label1, property1-3, object1-3, , ,....o縲縲竊� |
---|
217 | * label2, property1-4, object1-1, property2-3, object2-3 ,.....
|
---|
218 | * label2, property1-5, object1-2, property2-4, object2-4 ,.....
|
---|
219 | * label2, , , property2-5, object2-5 ,.....
|
---|
220 | */
|
---|
221 |
|
---|
222 |
|
---|
223 | for (int i=0; i<max; i++){
|
---|
224 | StringBuilder line = new StringBuilder();
|
---|
225 | line.append(label);
|
---|
226 | line.append(SEPARATOR);
|
---|
227 | int lp = 1;
|
---|
228 | for (HashMap<String, String> values : ret){
|
---|
229 | String[] hoge = values.keySet().toArray(new String[]{});
|
---|
230 | if (hoge.length > i && !contents.get(lp).trim().isEmpty()){
|
---|
231 | String resultProperty = hoge[i];
|
---|
232 | String resultObject = values.get(resultProperty);
|
---|
233 | line.append(contents.get(lp));
|
---|
234 | line.append(SEPARATOR);
|
---|
235 | line.append(resultProperty);
|
---|
236 | line.append(SEPARATOR);
|
---|
237 | line.append(resultObject);
|
---|
238 | line.append(SEPARATOR);
|
---|
239 | } else {
|
---|
240 | line.append(SEPARATOR);
|
---|
241 | line.append(SEPARATOR);
|
---|
242 | line.append(SEPARATOR);
|
---|
243 | }
|
---|
244 | lp++;
|
---|
245 | }
|
---|
246 | out.append(line.toString());
|
---|
247 | out.append("\n");
|
---|
248 | }
|
---|
249 |
|
---|
250 | }
|
---|
251 |
|
---|
252 | private String getResult(List<String> result){
|
---|
253 | if (result.size() == 0){
|
---|
254 | return "";
|
---|
255 | }
|
---|
256 | if (result.size() == 1){
|
---|
257 | return result.get(0);
|
---|
258 | }
|
---|
259 |
|
---|
260 | StringBuilder sb = new StringBuilder();
|
---|
261 | for (int i=0; i<result.size()-1; i++){
|
---|
262 | sb.append(result.get(i));
|
---|
263 | sb.append("<>"); // TODO 繧サ繝代Ξ繝シ繧ソ
|
---|
264 | }
|
---|
265 | sb.append(result.get(result.size()-1));
|
---|
266 |
|
---|
267 | return sb.toString();
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | *
|
---|
272 | * @param endpoint
|
---|
273 | * @param content
|
---|
274 | * @param properties
|
---|
275 | * @return property縺ィ縺昴�result縺ョmap
|
---|
276 | */
|
---|
277 | private HashMap<String, List<String>> getResource(String endpoint, String content, List<String> properties){
|
---|
278 | HashMap<String, List<String>> ret = new HashMap<String, List<String>>();
|
---|
279 | PlainSparqlAccessor sa = accessorMap.get(endpoint);
|
---|
280 | for (String property : properties){
|
---|
281 | List<String> res = new ArrayList<String>();
|
---|
282 | ret.put(property, res);
|
---|
283 |
|
---|
284 | if (!content.trim().isEmpty()){
|
---|
285 | String query = makeQuery(content, property);
|
---|
286 | try {
|
---|
287 | List<Map<String, RDFNode>> results = sa.executeQuery(query);
|
---|
288 | if (results != null){
|
---|
289 | for (Map<String, RDFNode> result : results){
|
---|
290 | RDFNode s = result.get("o");
|
---|
291 | res.add(s.toString());
|
---|
292 | }
|
---|
293 | }
|
---|
294 | } catch (Exception e) {
|
---|
295 | e.printStackTrace();
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | return ret;
|
---|
301 | }
|
---|
302 |
|
---|
303 | private String makeQuery(String s, String p){
|
---|
304 | return "select distinct ?o where {\n" +
|
---|
305 | "<" + s.trim() + "> <" + p.trim() + "> ?o \n" +
|
---|
306 | "}";
|
---|
307 |
|
---|
308 | }
|
---|
309 |
|
---|
310 | public void addEndpoint(String endpoint){
|
---|
311 | EndpointSettings settings = EndpointSettingsManager.instance.getSetting(endpoint);
|
---|
312 |
|
---|
313 | PlainSparqlAccessor sa = new PlainSparqlAccessor(settings);
|
---|
314 | accessorMap.put(endpoint, sa);
|
---|
315 | }
|
---|
316 |
|
---|
317 | private void setEndpoint(List<String> endpoints){
|
---|
318 | for (String ep : endpoints){
|
---|
319 | addEndpoint(ep);
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | private List<String> getEndpoints(String line){
|
---|
324 | List<String> headers = FileUtil.splitLine(line, SEPARATOR);
|
---|
325 |
|
---|
326 | if (headers.size() > 0){
|
---|
327 | headers.remove(0);
|
---|
328 | }
|
---|
329 |
|
---|
330 | return headers;
|
---|
331 | }
|
---|
332 |
|
---|
333 | private String getCurrentTime(){
|
---|
334 | Timestamp time = new Timestamp(new Date().getTime());
|
---|
335 |
|
---|
336 | return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(time) + ":";
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | private class QueryThread extends Thread {
|
---|
341 |
|
---|
342 | private Object option;
|
---|
343 | private CompareResultListener listener;
|
---|
344 |
|
---|
345 |
|
---|
346 | public QueryThread(Object option, CompareResultListener listener){
|
---|
347 | this.option = option;
|
---|
348 | this.listener = listener;
|
---|
349 | }
|
---|
350 |
|
---|
351 | protected Object getOption(){
|
---|
352 | return this.option;
|
---|
353 | }
|
---|
354 |
|
---|
355 | protected CompareResultListener getCompareResultListener(){
|
---|
356 | return this.listener;
|
---|
357 | }
|
---|
358 |
|
---|
359 | }
|
---|
360 |
|
---|
361 | public static void main(String[] args){
|
---|
362 | String epFile = "C:\\Users\\kato\\Desktop\\out\\endp_prop.txt";
|
---|
363 | String inFile = "C:\\works\\daily\\20130926\\input\\蜍戊ゥ樒エ「蠑�csv";
|
---|
364 | String outFile = "C:\\works\\daily\\20130926\\input\\蜍戊ゥ樒エ「蠑廟out.csv";
|
---|
365 |
|
---|
366 | try {
|
---|
367 | /*
|
---|
368 | for (int i=0; i<args.length; i++){
|
---|
369 | if (args[i].startsWith("-") && i < (args.length-1)){
|
---|
370 |
|
---|
371 | if (args[i].equals("-o")){
|
---|
372 | outFile = args[i+1];
|
---|
373 | }
|
---|
374 | if (args[i].equals("-i")){
|
---|
375 | inFile = args[i+1];
|
---|
376 | }
|
---|
377 | if (args[i].equals("-e")){
|
---|
378 | epFile = args[i+1];
|
---|
379 | }
|
---|
380 | i++;
|
---|
381 | }
|
---|
382 |
|
---|
383 | }
|
---|
384 | */
|
---|
385 |
|
---|
386 | EndpointSettings[] settings = EndpointSettings.inputXML(new FileInputStream("settings.xml"));
|
---|
387 | if (settings != null){
|
---|
388 | EndpointSettingsManager.instance.setSettings(settings);
|
---|
389 |
|
---|
390 | }
|
---|
391 |
|
---|
392 |
|
---|
393 | if (epFile == null || inFile == null || outFile == null){
|
---|
394 | System.out.println("usage:java -jar SparqlTestTool.jar Compare -e endpointFile -i inputFile -o outputFile -t type(s:subject/l:label object/o:all object)");
|
---|
395 | } else {
|
---|
396 | CompareSubject ct = new CompareSubject(new File(inFile), new File(epFile));
|
---|
397 | ct.outputResult(new File(outFile));
|
---|
398 | }
|
---|
399 | } catch (UnsupportedEncodingException e) {
|
---|
400 | // TODO 閾ェ蜍慕函謌舌&繧後◆ catch 繝悶Ο繝�け
|
---|
401 | e.printStackTrace();
|
---|
402 | } catch (FileNotFoundException e) {
|
---|
403 | // TODO 閾ェ蜍慕函謌舌&繧後◆ catch 繝悶Ο繝�け
|
---|
404 | e.printStackTrace();
|
---|
405 | } catch (IOException e) {
|
---|
406 | // TODO 閾ェ蜍慕函謌舌&繧後◆ catch 繝悶Ο繝�け
|
---|
407 | e.printStackTrace();
|
---|
408 | }
|
---|
409 |
|
---|
410 | }
|
---|
411 |
|
---|
412 | }
|
---|