Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EMF Search Framework Extensibility

search core extensibility

Example:

This example is based on a plugin part of the ganymede EMF search release. The EMF GenModel is an Ecore based model defining the code generator configurator used as input for any EMF model code generation. In this case we want to define a search engine which aim to perform textual queries on GenModel meta element instances such as GenClass, GenEnum, GenClassifier, ...

Extending the existing Ecore based model search engine mechanism is done by registering to org.eclipse.emf.search.modelSearchEngine extension point and contributing required implementations.

Here is the extension declaration:

   <extension
         point="org.eclipse.emf.search.modelSearchEngine">
      <modelSearchEngine
            id="org.eclipse.emf.search.genModelSearchEngine"
            label="%GenModelSearchEngine.Label"
            modelResourceValidator="org.eclipse.emf.search.genmodel.engine.GenModelResourceValidator"
            queryParametersFactory="org.eclipse.emf.search.genmodel.factories.GenModelSearchQueryParametersFactory"
            searchQueryFactory="org.eclipse.emf.search.genmodel.factories.GenModelSearchQueryFactory">
      </modelSearchEngine>
   </extension>


 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 org.eclipse.emf.search.genmodel.engine.GenModelSearchQuery;
 
 public class GenModelSearchQueryFactory implements IModelSearchQueryFactory {
    private static GenModelSearchQueryFactory instance;
    public GenModelSearchQueryFactory() {}
    public static GenModelSearchQueryFactory getInstance() {
        return instance == null ? instance = new GenModelSearchQueryFactory() : instance;
    }
    public AbstractModelSearchQuery createModelSearchQuery(String expr, IModelSearchQueryParameters p) {
        return new GenModelSearchQuery(expr, p);
    }
 }
 import org.eclipse.emf.search.core.factories.IModelSearchQueryParametersFactory;
 import org.eclipse.emf.search.core.parameters.AbstractModelSearchQueryParameters;
 import org.eclipse.emf.search.core.parameters.IModelSearchQueryParameters;
 
 public class GenModelSearchQueryParametersFactory implements IModelSearchQueryParametersFactory {
    private static GenModelSearchQueryParametersFactory instance;
    public GenModelSearchQueryParametersFactory() {}
    public static GenModelSearchQueryParametersFactory getInstance() {
        return instance == null ? instance = new GenModelSearchQueryParametersFactory() : instance;
    }
    protected final class GenModelModelSearchQueryParameters extends AbstractModelSearchQueryParameters {
        @Override
        public String getModelSearchEngineID() {
            return "org.eclipse.emf.search.genModelSearchEngine"; //$NON-NLS-1$
        }	
    }
    public IModelSearchQueryParameters createSearchQueryParameters() {
        return new GenModelModelSearchQueryParameters();
    }
 }
 import java.util.ArrayList;
 import java.util.List;
 
 import org.eclipse.emf.codegen.ecore.genmodel.GenBase;
 import org.eclipse.emf.ecore.ENamedElement;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.search.core.engine.IModelSearchQuery;
 import org.eclipse.emf.search.ecore.engine.EcoreModelSearchQuery;
 import org.eclipse.emf.search.ecore.evaluators.EcoreTextualModelSearchQueryEvaluator;
 import org.eclipse.emf.search.ecore.regex.ModelSearchQueryTextualExpressionEnum;
 import org.eclipse.emf.search.ecore.regex.ModelSearchQueryTextualExpressionMatchingHelper;
 import org.eclipse.emf.search.genmodel.l10n.Messages;
 
 public class GenModelTextualModelSearchQueryEvaluator <Q extends IModelSearchQuery, T> extends EcoreTextualModelSearchQueryEvaluator<Q, T> {
    @Override
    public List<?> eval(Q query, T target, boolean notifiy) {
        List<Object> results = new ArrayList<Object>();
        ModelSearchQueryTextualExpressionEnum kind = ((EcoreModelSearchQuery)query).getKind();
 
        for (Object o : query.getValidParticipantMetaElements()) {
            if (o instanceof GenBase) {
                String text = query.getQueryExpression();
                text = (text == "" && kind == ModelSearchQueryTextualExpressionEnum.NORMAL_TEXT) ? "*" : text;
 
                EObject elem = ((GenBase)o).getEcoreModelElement();
                if (elem instanceof ENamedElement) {
                    String elemName = ((ENamedElement)elem).getName();
                    elemName = elemName != null ? elemName : ""; //$NON-NLS-1$
                    if (ModelSearchQueryTextualExpressionMatchingHelper.getInstance().lookAt(elemName, text, kind)) {
                        results.add(query.processSearchResultMatching(target, o, notifiy));				
                    }
                }
            }
        }
        return results;
    }	
 
    @Override
    public String getLabel() {
        return Messages.getString("GenModelTextualModelSearchQueryEvaluator.Label"); //$NON-NLS-1$
    }
 }

Back to the top