aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCheng Chen2012-04-30 22:05:40 (EDT)
committerCheng Chen2012-04-30 22:05:40 (EDT)
commitfd1b0f45e04320f4fe34bca6f119e7c820d46aa1 (patch)
treea32dadadf401a7ce1069ad474f49fe7eda86b22d
parente8c2019f8db86c480d24881d554f1290b7fd3588 (diff)
downloadorg.eclipse.recommenders-fd1b0f45e04320f4fe34bca6f119e7c820d46aa1.zip
org.eclipse.recommenders-fd1b0f45e04320f4fe34bca6f119e7c820d46aa1.tar.gz
org.eclipse.recommenders-fd1b0f45e04320f4fe34bca6f119e7c820d46aa1.tar.bz2
[snipmatch] Local search implemention for SnipMatch, includerefs/changes/69/5769/2
localSearchThread, localSearchMatch and a simple string compare search engine class Signed-off-by: Cheng Chen <chengchendoc@gmail.com> Change-Id: I7583d7eb4b016e54a4a617309d1d33bab8f1e8cf
-rw-r--r--plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalMatchClient.java165
-rw-r--r--plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalSearchThread.java107
-rw-r--r--plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/StringCompareSearchEngine.java215
3 files changed, 487 insertions, 0 deletions
diff --git a/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalMatchClient.java b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalMatchClient.java
new file mode 100644
index 0000000..4cde689
--- /dev/null
+++ b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalMatchClient.java
@@ -0,0 +1,165 @@
+/**
+ * Copyright (c) 2011 Doug Wightman, Zi Ye, Cheng Chen
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.eclipse.recommenders.snipmatch.local;
+
+import org.eclipse.recommenders.snipmatch.core.Effect;
+import org.eclipse.recommenders.snipmatch.core.MatchEnvironment;
+import org.eclipse.recommenders.snipmatch.core.MatchNode;
+import org.eclipse.recommenders.snipmatch.search.SearchClient;
+import org.eclipse.recommenders.snipmatch.web.IDeleteEffectListener;
+import org.eclipse.recommenders.snipmatch.web.ILoadProfileListener;
+import org.eclipse.recommenders.snipmatch.web.ILoginListener;
+import org.eclipse.recommenders.snipmatch.web.ISearchListener;
+import org.eclipse.recommenders.snipmatch.web.ISendFeedbackListener;
+import org.eclipse.recommenders.snipmatch.web.ISubmitEffectListener;
+
+/**
+ * This class is used to search local snippets store.
+ */
+public final class LocalMatchClient implements SearchClient {
+ private String name;
+ private String version;
+ private String username;
+ private String password;
+ private LocalSearchThread workThread;
+
+ private static LocalMatchClient instance = new LocalMatchClient();
+
+ private LocalMatchClient() {
+
+ }
+
+ public static LocalMatchClient getInstance() {
+ return instance;
+ }
+
+ @Override
+ public void startSearch(String query, MatchEnvironment env, ISearchListener listener) {
+ workThread = new LocalSearchThread(this, env, query, listener);
+ workThread.start();
+ }
+
+ @Override
+ public void cancelWork() {
+
+ if (isWorking())
+ workThread.cancel();
+ }
+
+ @Override
+ public boolean isWorking() {
+
+ return workThread != null && !workThread.isDone();
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public void setVersion(String version) {
+ this.version = version;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ @Override
+ public void startSendFeedback(String query, MatchNode result, String comment, int rating, boolean flag,
+ boolean isLog, boolean isStartup, long clientId, boolean used, ISendFeedbackListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public boolean isLoggedIn() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public void logout() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void startProcessing() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void stopProcessing() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void setServerProcessingTime(float serverProcessingTime) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public float getServerProcessingTime() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public float getProcessingTime() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public void startLogin(String username, String password, ILoginListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void startDeleteEffect(Effect effect, IDeleteEffectListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void startSubmitEffect(Effect effect, boolean isPublic, ISubmitEffectListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void startLoadProfile(ILoadProfileListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+}
diff --git a/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalSearchThread.java b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalSearchThread.java
new file mode 100644
index 0000000..236b821
--- /dev/null
+++ b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/LocalSearchThread.java
@@ -0,0 +1,107 @@
+/**
+ * Copyright (c) 2012 University of Science and Technology Beijing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Cheng Chen - initial API and implementation.
+ */
+
+package org.eclipse.recommenders.snipmatch.local;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import org.eclipse.recommenders.snipmatch.core.MatchEnvironment;
+import org.eclipse.recommenders.snipmatch.core.MatchNode;
+import org.eclipse.recommenders.snipmatch.preferences.PreferenceConstants;
+import org.eclipse.recommenders.snipmatch.rcp.SnipMatchPlugin;
+import org.eclipse.recommenders.snipmatch.search.SnipMatchSearchEngine;
+import org.eclipse.recommenders.snipmatch.web.ISearchListener;
+
+/**
+ * A class for searching local snippets.
+ */
+public class LocalSearchThread extends Thread {
+ protected LocalMatchClient client;
+ protected boolean done;
+
+ private int waitTime = 450;
+ private MatchEnvironment env;
+ private String query;
+ private ISearchListener listener;
+
+ public LocalSearchThread(LocalMatchClient client, MatchEnvironment env, String query, ISearchListener listener) {
+
+ this.client = client;
+ this.env = env;
+ this.query = query;
+ this.listener = listener;
+ this.done = false;
+ }
+
+ @Override
+ public void run() {
+ SnipMatchSearchEngine searchEngine = SnipMatchPlugin.getDefault().getSearchEngine();
+
+ String snippetsDir = SnipMatchPlugin.getDefault().getPreferenceStore()
+ .getString(PreferenceConstants.SNIPPETS_STORE_DIR);
+ String indexDir = SnipMatchPlugin.getDefault().getPreferenceStore()
+ .getString(PreferenceConstants.SNIPPETS_INDEX_FILE);
+ // Everything is prepared, just wait for final query string
+ try {
+ sleep(waitTime);
+ } catch (Exception e) {
+ e.printStackTrace();
+ listener.searchFailed("Thread exception");
+ done = true;
+ return;
+ }
+ if (!searchEngine.isInitialized(snippetsDir, indexDir)) {
+ // Not initialed, initial it first
+ try {
+ searchEngine.initialize(snippetsDir, indexDir);
+ } catch (IOException e) {
+ e.printStackTrace();
+ done = true;
+ return;
+ }
+ }
+ if (done)
+ return;
+
+ List<MatchNode> resultList = searchEngine.search(query);
+ if (done)
+ return;
+
+ if (resultList == null)
+ listener.searchFailed("No snippets match for " + query);
+ else {
+ for (MatchNode match : resultList) {
+ if (match != null)
+ listener.matchFound(match);
+ }
+ listener.searchSucceeded();
+ done = true;
+ }
+ }
+
+ public boolean isDone() {
+
+ return done;
+ }
+
+ protected InputStream search() {
+ if (done)
+ return null;
+
+ return null;
+ }
+
+ public void cancel() {
+ done = true;
+ }
+}
diff --git a/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/StringCompareSearchEngine.java b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/StringCompareSearchEngine.java
new file mode 100644
index 0000000..47f2044
--- /dev/null
+++ b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/local/StringCompareSearchEngine.java
@@ -0,0 +1,215 @@
+/**
+ * Copyright (c) 2011 Doug Wightman, Zi Ye, Cheng Chen
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Cheng Chen - initial API and implementation.
+ */
+
+package org.eclipse.recommenders.snipmatch.local;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.recommenders.snipmatch.core.ArgumentMatchNode;
+import org.eclipse.recommenders.snipmatch.core.Effect;
+import org.eclipse.recommenders.snipmatch.core.EffectMatchNode;
+import org.eclipse.recommenders.snipmatch.core.EffectParameter;
+import org.eclipse.recommenders.snipmatch.core.MatchNode;
+import org.eclipse.recommenders.snipmatch.preferences.PreferenceConstants;
+import org.eclipse.recommenders.snipmatch.rcp.SnipMatchPlugin;
+import org.eclipse.recommenders.snipmatch.search.SnipMatchSearchEngine;
+import org.eclipse.recommenders.utils.gson.GsonUtil;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+
+import com.google.gson.reflect.TypeToken;
+
+public class StringCompareSearchEngine implements SnipMatchSearchEngine {
+ private boolean initialized = false;
+ private static SnipMatchSearchEngine instance = null;
+
+ private String snippetsDir;
+ private String indexFilePath;
+ private String commonSnippetsDir = "common";
+ private String anonymouseSnippetsDir = "local";
+ private List<SummaryFileMap> sfMapList = null;
+
+ public static SnipMatchSearchEngine getInstance() {
+ if (instance == null) {
+ synchronized (SnipMatchSearchEngine.class) {
+ SnipMatchSearchEngine inst = instance;
+ if (inst == null) {
+ synchronized (SnipMatchSearchEngine.class) {
+ inst = new StringCompareSearchEngine();
+ }
+ instance = inst;
+ }
+ }
+ }
+ return instance;
+ }
+
+ private StringCompareSearchEngine() {
+
+ }
+
+ @Override
+ public boolean isInitialized(String currentSnippetsDir, String currentIndexDir) {
+
+ return initialized && this.snippetsDir.equals(currentSnippetsDir) && currentIndexDir.equals(this.indexFilePath);
+ }
+
+ @Override
+ public void initialize(String snippetsDir, String indexDir) throws IOException {
+ File cDirFile = new File(snippetsDir, this.commonSnippetsDir);
+ if (!(cDirFile.exists()) && !(cDirFile.isDirectory()))
+ cDirFile.mkdirs();
+
+ File aDirFile = new File(snippetsDir, this.anonymouseSnippetsDir);
+ if (!(aDirFile.exists()) && !(aDirFile.isDirectory()))
+ aDirFile.mkdirs();
+
+ String indexFilePath = SnipMatchPlugin.getDefault().getPreferenceStore()
+ .getString(PreferenceConstants.SNIPPETS_INDEX_FILE);
+ File indexFile = new File(indexFilePath);
+ if (!indexFile.exists()) {
+ indexFile.createNewFile();
+ // updateIndex();
+ }
+
+ loadIndexFile(indexFile);
+ initialized = true;
+ this.snippetsDir = snippetsDir;
+ this.indexFilePath = indexFilePath;
+ }
+
+ private void loadIndexFile(File indexFile) {
+ Type listType = new TypeToken<List<SummaryFileMap>>() {
+ }.getType();
+ sfMapList = GsonUtil.deserialize(indexFile, listType);
+ }
+
+ @Override
+ public List<MatchNode> search(String query) {
+ List<MatchNode> result = new ArrayList<MatchNode>();
+ if (query.trim().equals(""))
+ return result;
+ if (sfMapList != null && sfMapList.size() > 0)
+ for (SummaryFileMap map : sfMapList) {
+ String summary = map.summary;
+
+ if (summary.toLowerCase().contains(query.toLowerCase().trim())) {
+ File jsonFile = new File(map.filePath);
+ if (jsonFile.exists()) {
+ Effect parent = GsonUtil.deserialize(jsonFile, Effect.class);
+ parent.setId(System.currentTimeMillis() + String.valueOf(Math.random()).substring(5));
+
+ MatchNode[] children = new MatchNode[parent.getParameters().length];
+ for (int i = 0; i < children.length; i++) {
+ EffectParameter param = parent.getParameters()[i];
+ ArgumentMatchNode childNode = new ArgumentMatchNode(param.getName(), param);
+ children[i] = childNode;
+ }
+
+ // pattern was used to display in content assist list
+ String pattern = summary;
+ for (String p : parent.getPatterns()) {
+ if (p.toLowerCase().contains(query.toLowerCase().trim())) {
+ pattern = p;
+ break;
+ }
+ }
+ result.add(new EffectMatchNode(parent, pattern, children));
+ }
+ }
+ }
+
+ return result;
+ }
+
+ @Override
+ public void updateIndex() {
+ if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
+ Shell shell = new Shell(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.NO_TRIM
+ | SWT.NO_FOCUS | SWT.NO_BACKGROUND);
+ try {
+ new ProgressMonitorDialog(shell).run(true, true, new CreateIndexOperation());
+ initialized = false;
+ MessageDialog.openInformation(shell, "Recommenders tips", "Update search engine index file success!");
+ } catch (Exception e) {
+ e.printStackTrace();
+ MessageDialog.openError(shell, "Recommenders tips",
+ "Fail to upate index, check your snippet store directory!");
+ }
+ }
+ }
+
+}
+
+class CreateIndexOperation implements IRunnableWithProgress {
+ private String indexFilePath = SnipMatchPlugin.getDefault().getPreferenceStore()
+ .getString(PreferenceConstants.SNIPPETS_INDEX_FILE);
+ private String dirPath = SnipMatchPlugin.getDefault().getPreferenceStore()
+ .getString(PreferenceConstants.SNIPPETS_STORE_DIR);
+
+ @Override
+ public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+ monitor.beginTask("Updating index file ...", 3);
+ LinkedList<File> list = new LinkedList<File>();
+ List<SummaryFileMap> mapList = new ArrayList<SummaryFileMap>();
+ File dir = new File(dirPath);
+ list.add(dir);
+ File file[] = null;
+ File tmp = null;
+ monitor.worked(1);
+ while (!list.isEmpty()) {
+ tmp = list.removeFirst();
+ if (tmp.isDirectory()) {
+ file = tmp.listFiles();
+ if (file == null)
+ continue;
+ for (int i = 0; i < file.length; i++)
+ list.add(file[i]);
+ } else if (tmp.getAbsolutePath().endsWith(".json")) {
+ File jsonFile = new File(tmp.getAbsolutePath());
+ if (jsonFile.exists()) {
+ Effect parent = GsonUtil.deserialize(jsonFile, Effect.class);
+ String summary = parent.getSummary();
+ for (String pattern : parent.getPatterns())
+ summary += ";" + pattern;
+ SummaryFileMap map = new SummaryFileMap(summary, tmp.getAbsolutePath());
+ mapList.add(map);
+ }
+ }
+ }
+ monitor.worked(1);
+ GsonUtil.serialize(mapList, new File(indexFilePath));
+ monitor.worked(1);
+ monitor.done();
+ }
+
+}
+
+class SummaryFileMap {
+ public String summary;
+ public String filePath;
+
+ public SummaryFileMap(String s, String file) {
+ this.summary = s;
+ this.filePath = file;
+ }
+} \ No newline at end of file