package jp.ac.osaka_u.sanken.sparql; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import com.hp.hpl.jena.rdf.model.RDFNode; public class CrossSparqlAccessor implements ThreadedSparqlAccessor { private List settings; private Map accessorHash; private Map crossOffsetMap; public CrossSparqlAccessor(List settings, SparqlQueryListener listener){ accessorHash = new LinkedHashMap(); for (EndpointSettings setting : settings){ accessorHash.put(setting, SparqlAccessorFactory.createSparqlAccessor(setting, listener)); } this.settings = settings; init(); } public CrossSparqlAccessor(List settings){ accessorHash = new LinkedHashMap(); for (EndpointSettings setting : settings){ accessorHash.put(setting, SparqlAccessorFactory.createSparqlAccessor(setting)); } this.settings = settings; init(); } private void init(){ crossOffsetMap = new LinkedHashMap(); } @Override public List> executeQuery(String queryString) throws Exception { // TODO 未対応で良い? throw new UnsupportedOperationException("Can't Execute Query"); } @Override public SparqlResultSet findSubject(String word, boolean fullMatch, Integer limit, Integer offset, int type, String[] propList) throws Exception { SparqlResultSet ret = new SparqlResultSet(new ArrayList>()); Integer limit_ = limit; Integer offset_ = offset; CrossOffset cOffset = null; if (offset != null){ crossOffsetMap.get(new Integer(offset)); if (cOffset == null){ cOffset = new CrossOffset(0, offset); } } for (int i=0; i> findTripleFromSubject(String subject) throws Exception { List> ret = new ArrayList>(); for (EndpointSettings setting : settings){ boolean hit = false; for (String ns : setting.getNamespaceList()){ if (subject.startsWith(ns)){ hit = true; } } if (hit){ SparqlAccessor sa = accessorHash.get(setting); List> set = sa.findTripleFromSubject(subject); ret.addAll(set); } } return ret; } private boolean isThereTargetEndpoint(String subject){ for (EndpointSettings setting : settings){ for (String ns : setting.getNamespaceList()){ if (subject.startsWith(ns)){ return true; } } } return false; } @Override public boolean executeQuery(String queryString, SparqlResultListener resultListener) { // TODO 未対応で良い? throw new UnsupportedOperationException("Can't Execute Query"); } @Override public boolean findSubject(String word, boolean fullMatch, Integer limit, Integer offset, int type, String[] propList, SparqlResultListener resultListener) { Thread thread = new QueryThread(word, new Object[]{new Boolean(fullMatch), limit, offset, new Integer(type), propList}, resultListener){ public void run(){ try { Boolean fullMatch = (Boolean)((Object[])getOption())[0]; Integer limit = (Integer)((Object[])getOption())[1]; Integer offset = (Integer)((Object[])getOption())[2]; Integer type = (Integer)((Object[])getOption())[3]; String[] propList = (String[])((Object[])getOption())[4]; getSparqlResultListener().resultReceived(findSubject(getQueryString(), fullMatch, limit, offset, type, propList)); } catch(Exception e){ throw new RuntimeException(e); } } }; thread.setUncaughtExceptionHandler(resultListener); thread.start(); return true; } @Override public boolean findTripleFromSubject(String subject, SparqlResultListener listener) { if (!isThereTargetEndpoint(subject)){ // 対象endpointなし return false; } Thread thread = new QueryThread(subject, listener){ public void run(){ try { getSparqlResultListener().resultReceived(new SparqlResultSet(findTripleFromSubject(getQueryString()))); } catch(Exception e){ throw new RuntimeException(e); } } }; thread.setUncaughtExceptionHandler(listener); thread.start(); return true; } @Override public List> findPropertyList() throws Exception { List> ret = new ArrayList>(); Map result = new LinkedHashMap(); ret.add(result); for (EndpointSettings setting : settings){ SparqlAccessor sa = accessorHash.get(setting); List> set = sa.findPropertyList(); for (Map r : set){ for (String key : r.keySet()){ result.put(key, r.get(key)); } } } return ret; } @Override public boolean findPropertyList(SparqlResultListener listener) { Thread thread = new QueryThread(null, listener){ public void run(){ try { getSparqlResultListener().resultReceived(new SparqlResultSet(findPropertyList())); } catch(Exception e){ throw new RuntimeException(e); } } }; thread.setUncaughtExceptionHandler(listener); thread.start(); return true; } private class CrossOffset { public int endpointIndex; public int offset; public CrossOffset(int endpointIndex, int offset){ this.endpointIndex = endpointIndex; this.offset = offset; } } }