1 | package jp.ac.osaka_u.sanken.sparql.plugin.compare;
|
---|
2 |
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.OutputStreamWriter;
|
---|
10 | import java.io.PrintWriter;
|
---|
11 | import java.sql.Timestamp;
|
---|
12 | import java.util.Date;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import javax.swing.JFileChooser;
|
---|
18 |
|
---|
19 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
---|
20 | import com.ibm.icu.text.SimpleDateFormat;
|
---|
21 |
|
---|
22 | import jp.ac.osaka_u.sanken.sparql.EndpointSettings;
|
---|
23 | import jp.ac.osaka_u.sanken.sparql.EndpointSettingsManager;
|
---|
24 | import jp.ac.osaka_u.sanken.sparql.PlainSparqlAccessor;
|
---|
25 | import jp.ac.osaka_u.sanken.sparql.SparqlQueryListener;
|
---|
26 | import jp.ac.osaka_u.sanken.sparql.SparqlResultSet;
|
---|
27 |
|
---|
28 | public class Compare {
|
---|
29 |
|
---|
30 | private HashMap<String, PlainSparqlAccessor> accessorMap;
|
---|
31 | private List<String> labels;
|
---|
32 | private SparqlQueryListener listener;
|
---|
33 |
|
---|
34 | public Compare(File inputFile, File endpointFile){
|
---|
35 | this(inputFile, endpointFile, null);
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | public Compare(File inputFile, File endpointFile, SparqlQueryListener listener){
|
---|
40 | /*
|
---|
41 | * 繝サEndpoint繧定、�焚謖�ョ壹☆繧� |
---|
42 | * 繝サLabel縺御ク陦後↓荳縺、縺壹▽險倩ソー縺輔l縺鬱ext繧貞眠繧上○繧� |
---|
43 | * 繝サ險ュ螳壹�AccessorForm縺ョ繝��繧ソ繧貞茜逕ィ
|
---|
44 | */
|
---|
45 | accessorMap = new HashMap<String, PlainSparqlAccessor>();
|
---|
46 |
|
---|
47 | loadSetting("settings.xml");
|
---|
48 |
|
---|
49 | List<String> endpoints = FileUtil.readFileText(endpointFile, "UTF-8");
|
---|
50 |
|
---|
51 | setEndpoint(endpoints);
|
---|
52 |
|
---|
53 | labels = FileUtil.readFileText(inputFile, "UTF-8");
|
---|
54 | // setLabel();
|
---|
55 |
|
---|
56 | this.listener = listener;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void addEndpoint(String endpoint){
|
---|
60 | EndpointSettings settings = EndpointSettingsManager.instance.getSetting(endpoint);
|
---|
61 |
|
---|
62 | PlainSparqlAccessor sa = new PlainSparqlAccessor(settings);
|
---|
63 | accessorMap.put(endpoint, sa);
|
---|
64 | // sa.findTripleFromSubject("select ?s {?s <http://www.w3.org/2000/01/rdf-schema#label> "+"")
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void setEndpoint(List<String> endpoints){
|
---|
68 | for (String ep : endpoints){
|
---|
69 | addEndpoint(ep);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | private Thread thread;
|
---|
75 | private boolean finalizeThread = false;
|
---|
76 |
|
---|
77 | public void outputResult(int targetType, boolean fullMatch, File out, CompareResultListener listener) {
|
---|
78 | thread = new QueryThread(new Object[]{new Integer(targetType), new Boolean(fullMatch), out}, listener){
|
---|
79 | public void run(){
|
---|
80 | try {
|
---|
81 | Integer type = (Integer)((Object[])getOption())[0];
|
---|
82 | Boolean fullMatch = (Boolean)((Object[])getOption())[1];
|
---|
83 | File file = (File)((Object[])getOption())[2];
|
---|
84 | getCompareResultListener().resultReceived(outputResult(type, fullMatch, file));
|
---|
85 | } catch(Exception e){
|
---|
86 | throw new RuntimeException(e);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | };
|
---|
90 | thread.setUncaughtExceptionHandler(listener);
|
---|
91 | thread.start();
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void stop(){
|
---|
96 | println(getCurrentTime()+"Stop Request");
|
---|
97 | finalizeThread = true;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | public boolean outputResult(int targetType, boolean fullMatch, File out) throws IOException{
|
---|
102 | /*
|
---|
103 | // header
|
---|
104 | System.out.print("\"\",");
|
---|
105 | for (String endpoint : accessorMap.keySet()){
|
---|
106 | System.out.print("\""+endpoint + "\",");
|
---|
107 | }
|
---|
108 | System.out.println();
|
---|
109 |
|
---|
110 | for (String label : labels){
|
---|
111 | System.out.print("\"" + label + "\",");
|
---|
112 | for (String endpoint : accessorMap.keySet()){
|
---|
113 | SparqlAccessor sa = accessorMap.get(endpoint);
|
---|
114 | try {
|
---|
115 | List<Map<String, RDFNode>> res = sa.findSubject(label, true, 10, type);//executeQuery("select ?s {{?s <http://www.w3.org/2000/01/rdf-schema#label> \""+label+"\"@ja} UNION {?s <http://www.w3.org/2000/01/rdf-schema#label> \""+label+"\"@en} UNION {?s <http://www.w3.org/2000/01/rdf-schema#label> \""+label+"\"}}");
|
---|
116 | if (res != null && res.size() > 0){
|
---|
117 | Map<String, RDFNode> r = res.get(0);
|
---|
118 | RDFNode s = r.get("s");
|
---|
119 | System.out.print("\"" + s.toString() + "\",");
|
---|
120 | } else {
|
---|
121 | System.out.print(",");
|
---|
122 | }
|
---|
123 | } catch (Exception e) {
|
---|
124 | e.printStackTrace();
|
---|
125 | }
|
---|
126 | }
|
---|
127 | System.out.println();
|
---|
128 | }
|
---|
129 | */
|
---|
130 | if (out.getParentFile() != null && !out.getParentFile().exists()){
|
---|
131 | out.getParentFile().mkdirs();
|
---|
132 | }
|
---|
133 | out.createNewFile();
|
---|
134 | if (!out.canWrite()){
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 | return outputResult(targetType, fullMatch, new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), "SJIS")));
|
---|
139 | }
|
---|
140 |
|
---|
141 | private boolean outputResult(int targetType, boolean fullMatch, PrintWriter out){
|
---|
142 | String typeStr = "all";
|
---|
143 | if (targetType == PlainSparqlAccessor.FIND_TARGET_SUBJECT){
|
---|
144 | typeStr = "subject";
|
---|
145 | }
|
---|
146 | if (targetType == PlainSparqlAccessor.FIND_TARGET_SPECIFIC_OBJECT){
|
---|
147 | typeStr = "object (label only)";
|
---|
148 | }
|
---|
149 | if (targetType == PlainSparqlAccessor.FIND_TARGET_OBJECT){
|
---|
150 | typeStr = "object";
|
---|
151 | }
|
---|
152 | println("find type :["+typeStr+"]");
|
---|
153 | println("full match :["+fullMatch + "]");
|
---|
154 |
|
---|
155 | println("target endpoints:");
|
---|
156 | for (String endpoint : accessorMap.keySet()){
|
---|
157 | print("["+endpoint + "]");
|
---|
158 | }
|
---|
159 | println();
|
---|
160 | println(getCurrentTime()+"query start.");
|
---|
161 |
|
---|
162 | out.print("\"\",");
|
---|
163 | for (String endpoint : accessorMap.keySet()){
|
---|
164 | out.print("\""+endpoint + "\",");
|
---|
165 | }
|
---|
166 | out.println();
|
---|
167 |
|
---|
168 | int index = 0;
|
---|
169 |
|
---|
170 | for (String label : labels){
|
---|
171 | print(getCurrentTime()+ "No." + (++index)+":searching :["+label+"].");
|
---|
172 | out.print("\"" + label + "\",");
|
---|
173 | for (String endpoint : accessorMap.keySet()){
|
---|
174 | print("*");
|
---|
175 | PlainSparqlAccessor sa = accessorMap.get(endpoint);
|
---|
176 | try {
|
---|
177 | SparqlResultSet res = sa.findSubject(label, fullMatch, 10, null, targetType, null);//executeQuery("select ?s {{?s <http://www.w3.org/2000/01/rdf-schema#label> \""+label+"\"@ja} UNION {?s <http://www.w3.org/2000/01/rdf-schema#label> \""+label+"\"@en} UNION {?s <http://www.w3.org/2000/01/rdf-schema#label> \""+label+"\"}}");
|
---|
178 | List<Map<String, RDFNode>> list = res.getDefaultResult();
|
---|
179 | if (list != null && list.size() > 0){
|
---|
180 | Map<String, RDFNode> r = list.get(0);
|
---|
181 | RDFNode s = r.get("s");
|
---|
182 | out.print("\"" + s.toString() + "\",");
|
---|
183 | } else {
|
---|
184 | out.print(",");
|
---|
185 | }
|
---|
186 | } catch (Exception e) {
|
---|
187 | e.printStackTrace();
|
---|
188 | }
|
---|
189 | if (finalizeThread){
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | if (finalizeThread){
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | println();
|
---|
197 | out.println();
|
---|
198 | }
|
---|
199 | out.flush();
|
---|
200 | out.close();
|
---|
201 |
|
---|
202 | println(getCurrentTime()+"query end.");
|
---|
203 |
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 |
|
---|
207 | private String getCurrentTime(){
|
---|
208 | Timestamp time = new Timestamp(new Date().getTime());
|
---|
209 |
|
---|
210 | return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(time) + ":";
|
---|
211 | }
|
---|
212 |
|
---|
213 | private void println(){
|
---|
214 | println("");
|
---|
215 | }
|
---|
216 |
|
---|
217 | private void println(String str){
|
---|
218 | print(str + "\n");
|
---|
219 | }
|
---|
220 |
|
---|
221 | private void print(String str){
|
---|
222 | if (listener != null){
|
---|
223 | listener.sparqlExecuted(str);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | private int loadSetting(String settingFile){
|
---|
228 | // 蜃コ蜉帛�豎コ螳� |
---|
229 | File file = new File(settingFile);
|
---|
230 | try {
|
---|
231 | EndpointSettings[] settings = EndpointSettings.inputXML(new FileInputStream(file));
|
---|
232 | if (settings != null){
|
---|
233 | EndpointSettingsManager.instance.setSettings(settings);
|
---|
234 |
|
---|
235 | return 0;
|
---|
236 | }
|
---|
237 | } catch (FileNotFoundException e) {
|
---|
238 | e.printStackTrace();
|
---|
239 | return JFileChooser.ERROR;
|
---|
240 | } finally {
|
---|
241 | }
|
---|
242 | return JFileChooser.ERROR;
|
---|
243 | }
|
---|
244 |
|
---|
245 | private class QueryThread extends Thread {
|
---|
246 |
|
---|
247 | private Object option;
|
---|
248 | private CompareResultListener listener;
|
---|
249 |
|
---|
250 |
|
---|
251 | public QueryThread(Object option, CompareResultListener listener){
|
---|
252 | this.option = option;
|
---|
253 | this.listener = listener;
|
---|
254 | }
|
---|
255 |
|
---|
256 | protected Object getOption(){
|
---|
257 | return this.option;
|
---|
258 | }
|
---|
259 |
|
---|
260 | protected CompareResultListener getCompareResultListener(){
|
---|
261 | return this.listener;
|
---|
262 | }
|
---|
263 |
|
---|
264 | }
|
---|
265 | }
|
---|