1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - index based searching (#4255)
12 *******************************************************************************/
13 package org.simantics.debug.ui;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.LinkedList;
20 import java.util.List;
22 import java.util.regex.Pattern;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.Statement;
27 import org.simantics.db.common.request.Queries;
28 import org.simantics.db.common.request.ReadRequest;
29 import org.simantics.db.common.utils.NameUtils;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.request.Read;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.utils.strings.EString;
34 import org.simantics.utils.threads.IThreadWorkQueue;
36 public class ResourceSearch extends ReadRequest {
38 public static final IResourceFilter FILTER_ALL = new IResourceFilter() {
40 public boolean acceptResource(ReadGraph g, Resource r) {
45 public static final IResourceFilter FILTER_RELATIONS = new IResourceFilter() {
47 public boolean acceptResource(ReadGraph g, Resource r) throws DatabaseException {
48 return g.isInstanceOf(r, Layer0.getInstance(g).Relation);
52 public static final IResourceFilter FILTER_TYPES = new IResourceFilter() {
54 public boolean acceptResource(ReadGraph g, Resource r) throws DatabaseException {
55 Layer0 L0 = Layer0.getInstance(g);
56 return g.isInstanceOf(r, L0.Type) && !g.isInstanceOf(r, L0.Relation);
61 * Create filter that matches URI, ID and NAME using wildcard compare
65 public static final IResourceFilter createFilter(String txt) {
66 final Pattern p = EString.compileSimplePattern(txt);
67 return new IResourceFilter() {
69 public boolean acceptResource(ReadGraph g, Resource r) {
71 String uri = g.syncRequest(Queries.possibleUri(r));
72 if (uri != null && p.matcher(uri).matches())
74 } catch (Throwable t) {}
77 String name = NameUtils.getSafeName(g, r);
78 if (p.matcher(name).matches())
80 } catch (Throwable t) {}
82 String id = Long.toString(r.getResourceId());
83 if (p.matcher(id).matches())
91 public interface IResourceFilter {
92 boolean acceptResource(ReadGraph g, Resource r) throws DatabaseException;
95 public interface SearchListener {
100 * @param g graph if listening is not async, if so then null
102 void onResourceFound(Read<?> s, Collection<Resource> r, ReadGraph g) throws DatabaseException;
103 void onSearchComplete(Read<?> s);
104 void onError(Read<?> s, Throwable e);
107 boolean canceled = false;
108 IResourceFilter filter;
109 SearchListener listener;
110 IThreadWorkQueue listenerThread;
111 ResFoundQueue resFoundQueue;
112 boolean asyncListening;
114 public ResourceSearch(IResourceFilter f, SearchListener l, IThreadWorkQueue listenerThread, boolean asyncListening)
116 assert(f!=null && l!=null);
119 this.listenerThread = listenerThread;
120 this.asyncListening = asyncListening;
121 if (listenerThread!=null)
122 resFoundQueue = new ResFoundQueue();
125 public void cancel() {
129 public boolean isCanceled() {
133 private class ResFoundQueue implements Runnable {
135 List<Resource> list = new ArrayList<Resource>();
139 * @return true if other resources are still unhandled
141 synchronized boolean addResource(Resource r) {
143 return list.size()>1;
148 Collection<Resource> l = null;
150 if (list.size()==0) return;
151 if (list.size()<10) {
152 listener.onResourceFound(ResourceSearch.this, list, g);
156 l = new ArrayList<Resource>(list);
159 listener.onResourceFound(ResourceSearch.this, l, g);
160 } catch (DatabaseException e) {
167 public void run(ReadGraph g) {
169 if (!asyncListening && resFoundQueue!=null) resFoundQueue.g = g;
170 Resource root = g.getResource("http:/");
171 Layer0 L0 = Layer0.getInstance(g);
172 LinkedList<Resource> queue = new LinkedList<Resource>();
174 Set<Resource> queued = new HashSet<Resource>();
175 while(!queue.isEmpty() && !canceled) {
176 Resource r = queue.removeFirst();
177 if (filter.acceptResource(g, r)) {
178 if (listenerThread==null)
179 listener.onResourceFound(ResourceSearch.this, Collections.singletonList(r), g);
181 if (!resFoundQueue.addResource(r)) {
183 listenerThread.asyncExec(resFoundQueue);
185 listenerThread.syncExec(resFoundQueue);
189 for (Statement stm : g.getStatements(r, L0.IsWeaklyRelatedTo))
191 Resource n = stm.getPredicate();
192 if (!queued.contains(n)) {
198 if (!queued.contains(n)) {
203 if (listenerThread==null)
204 listener.onSearchComplete(ResourceSearch.this);
206 Runnable run = new Runnable() {
209 ResourceSearch.this.listener.onSearchComplete(ResourceSearch.this);
212 listenerThread.asyncExec(run);
214 listenerThread.syncExec(run);
217 } catch (final Throwable e) {
218 if (listenerThread==null)
219 listener.onError(ResourceSearch.this, e);
221 Runnable r = new Runnable() {
224 listener.onError(ResourceSearch.this, e);
227 listenerThread.asyncExec(r);
229 listenerThread.syncExec(r);