EMF Search Framework Extensibility---org.eclipse.emf.search.modelSearchEngine

From Eclipsepedia

Jump to: navigation, search

org.eclipse.emf.search.modelSearchEngine

Users wanting to contribute an custom ecore metamodel based search should extends modelSearchEngine.

* id - model search engine unique ID
* label - Query label to be displayed as a prefix of in search result pages
* searchQueryFactory - This extension point element describes a model search engine compliant with org.eclipse.emf.search.providers.IModelSearchQueryFactory APIs.
* queryParametersFactory - parameter factory compliant with IModelSearchQueryParametersFactory
* modelResourceValidator - resource validator compliant with IModelSearchQueryParametersFactory and specializing AbstractModelResourceValidator


<plugin>
   <extension
         point="org.eclipse.emf.search.modelSearchEngine">
      <modelSearchEngine
            id="search.MoviesDbSearchEngine"
            label="MoviesDb Search Engine"
            modelResourceValidator="search.engine.ModelResourceValidator"
            queryParametersFactory="search.factories.ModelSearchQueryParametersFactory"
            searchQueryFactory="search.factories.ModelSearchQueryFactory"/>
   </extension>
</plugin>
import org.eclipse.emf.search.core.resource.AbstractModelResourceValidator;
 
/**
 * Allows users to describe all the specific query search supported model editor extensions.
 */
public class ModelResourceValidator extends AbstractModelResourceValidator {
	public ModelResourceValidator() {
 
		addModelFileExtension("movies"); //$NON-NLS-1$
		addModelFileExtension("customers"); //$NON-NLS-1$
		addModelFileExtension("orders"); //$NON-NLS-1$
	}
}


import org.eclipse.emf.search.core.factories.IModelSearchQueryParametersFactory;
import org.eclipse.emf.search.core.parameters.AbstractModelSearchQueryParameters;
import org.eclipse.emf.search.core.parameters.IModelSearchQueryParameters;
 
/**
 * Wraps ModelSearchQueryParameters creation.
 */
public class ModelSearchQueryParametersFactory implements
		IModelSearchQueryParametersFactory {
	private static ModelSearchQueryParametersFactory instance;
 
	public ModelSearchQueryParametersFactory() {
	}
 
	public static ModelSearchQueryParametersFactory getInstance() {
		return instance == null ? instance = new ModelSearchQueryParametersFactory()
				: instance;
	}
 
	protected final class ModelSearchQueryParameters extends
			AbstractModelSearchQueryParameters {
		public String getModelSearchEngineID() {
			return "search.MoviesDbSearchEngine"; //$NON-NLS-1$
		}
	}
 
	public IModelSearchQueryParameters createSearchQueryParameters() {
		return new ModelSearchQueryParameters();
	}
}
import org.eclipse.emf.search.core.engine.AbstractModelSearchQuery;
import org.eclipse.emf.search.core.factories.IModelSearchQueryFactory;
import org.eclipse.emf.search.core.parameters.IModelSearchQueryParameters;
import search.engine.ModelSearchQuery;
 
/**
 * Wraps ModelSearchQuery creation.
 */
public class ModelSearchQueryFactory implements IModelSearchQueryFactory {
	private static ModelSearchQueryFactory instance;
 
	public ModelSearchQueryFactory() {
	}
 
	public static ModelSearchQueryFactory getInstance() {
		return instance == null ? instance = new ModelSearchQueryFactory()
				: instance;
	}
 
	public AbstractModelSearchQuery createModelSearchQuery(String expr,
			IModelSearchQueryParameters p) {
		return new ModelSearchQuery(expr, p);
	}
}
import java.util.ArrayList;
import java.util.List;
 
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.search.core.engine.IModelSearchQuery;
import org.eclipse.emf.search.core.eval.IModelSearchQueryEvaluator;
import org.eclipse.emf.search.core.parameters.IModelSearchQueryParameters;
import org.eclipse.emf.search.core.results.IModelResultEntry;
import org.eclipse.emf.search.ecore.engine.EcoreMetaModelIntrospector;
import org.eclipse.emf.search.ecore.engine.EcoreModelSearchQuery;
import org.eclipse.emf.search.ecore.results.EcoreModelSearchResultEntry;
import org.eclipse.emf.search.ecore.utils.EcoreModelLoaderUtil;
 
import search.Activator;
import search.l10n.Messages;
import search.evaluators.ModelTextualModelSearchQueryEvaluator;
 
import org.eclipse.emf.search.tests.models.movies.MoviesPackage;
import org.eclipse.emf.search.tests.models.customers.CustomersPackage;
import org.eclipse.emf.search.tests.models.orders.OrdersPackage;
 
/**
 * Gather all model search settings to run a specific query.
 * TODO: the name of the class and its file should include modelName
 * 
 */
public final class ModelSearchQuery extends EcoreModelSearchQuery {
	public final static String MoviesDb_MODEL_SEARCH_IMAGE_PATH = "icons/esearch.gif"; //$NON-NLS-1$
	public final static String MoviesDb_MODEL_SEARCH_RESULT_IMAGE_PATH = "icons/esearch.gif"; //$NON-NLS-1$
 
	final static class MoviesDbSupportedElements {
		private static List<EClassifier> getMoviesDbEClassifiersLiterals() {
			List<EClassifier> eclassifiersLiteralsList = new ArrayList<EClassifier>();
 
			try {
 
				for (Object o : MoviesPackage.eINSTANCE.getEClassifiers()) {
					if (o instanceof ENamedElement) {
						eclassifiersLiteralsList.add((EClassifier) o);
					}
				}
 
				for (Object o : CustomersPackage.eINSTANCE.getEClassifiers()) {
					if (o instanceof ENamedElement) {
						eclassifiersLiteralsList.add((EClassifier) o);
					}
				}
 
				for (Object o : OrdersPackage.eINSTANCE.getEClassifiers()) {
					if (o instanceof ENamedElement) {
						eclassifiersLiteralsList.add((EClassifier) o);
					}
				}
 
			} catch (Throwable t) {
				Activator
						.getDefault()
						.getLog()
						.log(
								new Status(
										IStatus.ERROR,
										Activator.PLUGIN_ID,
										0,
										Messages
												.getString("ModelSearchQuery.MoviesDbClassifiersWalkErrorMessage"), t)); //$NON-NLS-1$
			}
 
			return eclassifiersLiteralsList;
		}
 
		public static List<EClassifier> getSupportedElements(
				List<? extends Object> participantEClassList) {
			ArrayList<EClassifier> definitiveMetaElementParticipantList = new ArrayList<EClassifier>();
			for (EClassifier eClass : getMoviesDbEClassifiersLiterals()) {
				if (participantEClassList.contains(eClass)) {
					definitiveMetaElementParticipantList.add(eClass);
				}
			}
			return definitiveMetaElementParticipantList;
		}
	}
 
	public ModelSearchQuery(String expr, IModelSearchQueryParameters parameters) {
		super(expr, parameters);
	}
 
	@Override
	public String getQueryImagePath() {
		return MoviesDb_MODEL_SEARCH_IMAGE_PATH;
	}
 
	@Override
	public String getResultImagePath() {
		return MoviesDb_MODEL_SEARCH_RESULT_IMAGE_PATH;
	}
 
	@Override
	public String getBundleSymbolicName() {
		return Activator.getDefault().getBundle().getSymbolicName();
	}
 
	@Override
	public IStatus search(Object resource, boolean notify) {
		try {
			EObject root = EcoreModelLoaderUtil.openFile(resource, false);
 
			validParticipantMetaElements = EcoreMetaModelIntrospector
					.discriminateValidMetaElements((EObject) root,
							MoviesDbSupportedElements
									.getSupportedElements(participantElements));
 
			((IModelSearchQueryEvaluator<IModelSearchQuery, Object>) getEvaluator())
					.eval(this, resource, notify);
		} catch (Exception e) {
			return Status.CANCEL_STATUS;
		}
		return Status.OK_STATUS;
	}
 
	public IStatus search(Object resource, boolean notify,
			IProgressMonitor monitor) {
		try {
			if (monitor.isCanceled()) {
				return Status.CANCEL_STATUS;
			}
 
			EObject root = EcoreModelLoaderUtil.openFile(resource, false);
 
			if (monitor.isCanceled()) {
				return Status.CANCEL_STATUS;
			}
 
			validParticipantMetaElements = EcoreMetaModelIntrospector
					.discriminateValidMetaElements((EObject) root,
							MoviesDbSupportedElements
									.getSupportedElements(participantElements));
 
			if (monitor.isCanceled()) {
				return Status.CANCEL_STATUS;
			}
 
			((IModelSearchQueryEvaluator<IModelSearchQuery, Object>) getEvaluator())
					.eval(this, resource, notify);
 
			monitor.setTaskName(getLabel());
 
			if (monitor.isCanceled()) {
				return Status.CANCEL_STATUS;
			}
		} catch (Exception e) {
			return Status.CANCEL_STATUS;
		}
		return Status.OK_STATUS;
	}
 
	// Recursively build EObject containment hierarchy
	private IModelResultEntry buildSearchResultEntryHierarchy(
			IModelResultEntry intermediate, Object resource, EObject current,
			EObject leaf) {
		if (current instanceof EObject) {
			IModelResultEntry entryContainer = new EcoreModelSearchResultEntry(
					null, resource, current, false);
			entryContainer.addChildren(intermediate);
			intermediate.setParent(entryContainer);
			return buildSearchResultEntryHierarchy(entryContainer, resource,
					current.eContainer(), leaf);
		} else {
			return intermediate;
		}
	}
 
	@Override
	public IModelSearchQueryEvaluator<IModelSearchQuery, ?> getEvaluator() {
		evaluator = getModelSearchParameters().getEvaluator();
		//TODO: get the name of evalutar's class compound of modelName and string "TextualModelSearchQueryEvaluator"
		return evaluator != null ? evaluator
				: (evaluator = new ModelTextualModelSearchQueryEvaluator<IModelSearchQuery, Object>());
	}
 
	@Override
	public String getName() {
		switch (getKind()) {
		case NORMAL_TEXT:
			return Messages
					.getString("MoviesDbModelSearchQuery.NormalTextMessage"); //$NON-NLS-1$
		case CASE_SENSITIVE:
			return Messages
					.getString("MoviesDbModelSearchQuery.CaseSensitiveMessage"); //$NON-NLS-1$
		case REGULAR_EXPRESSION:
			return Messages
					.getString("MoviesDbModelSearchQuery.RegularExpressionMessage"); //$NON-NLS-1$
		}
 
		return Messages
				.getString("MoviesDbModelSearchQuery.DefaultSearchKindMessage"); //$NON-NLS-1$
	}
}