| /******************************************************************************* |
| * Copyright (c) 2004-2008 Peter Pasztor, Akos Horvath, Gergely Varro, Istvan Rath and Daniel Varro |
| * 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: |
| * Peter Pasztor, Akos Horvath, Gergely Varro, Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.gtasm.patternmatcher.extension; |
| |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.core.runtime.IConfigurationElement; |
| import org.eclipse.core.runtime.IExtension; |
| import org.eclipse.core.runtime.IExtensionPoint; |
| import org.eclipse.core.runtime.IExtensionRegistry; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.core.runtime.Plugin; |
| import org.osgi.framework.BundleContext; |
| |
| /** |
| * The activator class controls the plug-in life cycle |
| * |
| * @author Akos Horvath |
| */ |
| public class ViatraGTASMPatternMatcherPlugin extends Plugin { |
| |
| // The plug-in ID |
| public static final String PLUGIN_ID = "org.eclipse.viatra2.gtasm.interpreter"; |
| // Extension points |
| public static final String PATTERN_MATCHER_EXT_ID = "org.eclipse.viatra2.gtasm.patternmatcher.patternmatcher"; |
| public static final String GT_MATCHER_EXT_ID = "org.eclipse.viatra2.gtasm.patternmatcher.gtrulematcher"; |
| // Pattern IDs |
| public static final String INCREMENTAL_PM_FACTORY_ID = "org.eclipse.viatra2.patternmatcher.incremental_rete"; |
| //public static final String INCREMENTAL_PARALLEL_PM_FACTORY_ID = "org.eclipse.viatra2.patternmatcher.incremental_rete_parallel"; |
| public static final String LOCAL_PM_FACTORY_ID = "org.eclipse.viatra2.patternmatcher.local_search"; |
| public static final String DEFAULT_PM_FACTORY_ID = LOCAL_PM_FACTORY_ID; |
| // public static final String DEFAULT_PM_FACTORY_ID = |
| // INCREMENTAL_PM_FACTORY_ID; |
| // GT rule ID-s |
| public static final String LOCAL_GT_FACTORY_ID = "org.eclipse.viatra2.gtmatcher.local_search"; |
| // public static final String ALTERNATIVE_PATTERN_MATCHER_FOR_GT_FACTORY_ID = "org.eclipse.viatra2.gtmatcher.alternative"; |
| public static final String DEFAULT_GT_FACTORY_ID = LOCAL_GT_FACTORY_ID; |
| |
| // The shared instance |
| private static ViatraGTASMPatternMatcherPlugin plugin; |
| |
| /** |
| * The constructor |
| */ |
| public ViatraGTASMPatternMatcherPlugin() { |
| plugin = this; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext) |
| */ |
| @Override |
| public void start(BundleContext context) throws Exception { |
| super.start(context); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) |
| */ |
| @Override |
| public void stop(BundleContext context) throws Exception { |
| plugin = null; |
| super.stop(context); |
| } |
| |
| /** |
| * Returns the shared instance |
| * |
| * @return the shared instance |
| */ |
| public static ViatraGTASMPatternMatcherPlugin getDefault() { |
| return plugin; |
| } |
| |
| /** PATTERN MATCHER FACTORIES */ |
| private Map<String, IPatternMatcherFactory> patternMatcherFactories; |
| |
| public Map<String, IPatternMatcherFactory> getPatternMatcherFactories() { |
| if (patternMatcherFactories == null) { |
| patternMatcherFactories = initPatternMatcherFactories(); |
| } |
| return patternMatcherFactories; |
| } |
| |
| /** PATTERN MATCHER IDS INDEXED BY ANNOTATION NAME */ |
| private Map<String, String> patternMatcherIDbyAnnotationName; |
| |
| public Map<String, String> getPatternMatcherIDbyAnnotationName() { |
| if (patternMatcherIDbyAnnotationName == null) { |
| initPatternMatcherFactories(); |
| } |
| return patternMatcherIDbyAnnotationName; |
| } |
| |
| /** |
| * Returns the pattern matcher registered with the input id |
| * |
| * @param id |
| * The id of the registered patternMatcher plugin id |
| * @return the IPatternMatcherFactory for the patternMatcher |
| */ |
| public IPatternMatcherFactory getPatternMatcherFactory(String id) { |
| if (patternMatcherFactories == null) { |
| patternMatcherFactories = initPatternMatcherFactories(); |
| } |
| return patternMatcherFactories.containsKey(id) ? patternMatcherFactories |
| .get(id) |
| : patternMatcherFactories.get(DEFAULT_PM_FACTORY_ID); |
| } |
| |
| private Map<String, IPatternMatcherFactory> initPatternMatcherFactories() { |
| IExtensionRegistry reg = Platform.getExtensionRegistry(); |
| IExtensionPoint poi = reg.getExtensionPoint(PATTERN_MATCHER_EXT_ID); |
| HashMap<String, IPatternMatcherFactory> r = new HashMap<String, IPatternMatcherFactory>(); |
| Map<String, String> annotMap = new HashMap<String, String>(); |
| |
| if (poi == null) |
| return r; |
| IExtension[] exts = poi.getExtensions(); |
| for (int i = 0; i < exts.length; i++) { |
| IConfigurationElement[] el = exts[i].getConfigurationElements(); |
| for (int j = 0; j < el.length; j++) { |
| try { |
| Object o = el[j].createExecutableExtension("class"); |
| if (o instanceof IPatternMatcherFactory) { |
| String id = el[j].getAttribute("id"); |
| r.put(id, (IPatternMatcherFactory) o); |
| |
| String annotationName = el[j].getAttribute("annotation_name"); |
| annotMap.put(annotationName, id); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| patternMatcherIDbyAnnotationName = Collections.unmodifiableMap(annotMap); |
| return r; |
| } |
| |
| /** GT MATCHER FACTORIES */ |
| private Map<String, IGTRuleMatcherFactory> gtMatcherFactories; |
| |
| public Map<String, IGTRuleMatcherFactory> getgtMatcherFactories() { |
| if (gtMatcherFactories == null) { |
| gtMatcherFactories = initgtMatcherFactories(); |
| } |
| return gtMatcherFactories; |
| } |
| |
| /** |
| * Returns the gt matcher registered with the input id |
| * |
| * @param id |
| * The id of the registered patternMatcher plugin id |
| * @return the IPatternMatcherFactory for the patternMatcher |
| */ |
| public IGTRuleMatcherFactory getGTMatcherFactory(String id) { |
| if (gtMatcherFactories == null) { |
| gtMatcherFactories = initgtMatcherFactories(); |
| } |
| return gtMatcherFactories.containsKey(id) ? gtMatcherFactories.get(id) |
| : gtMatcherFactories.get(DEFAULT_GT_FACTORY_ID); |
| } |
| |
| private Map<String, IGTRuleMatcherFactory> initgtMatcherFactories() { |
| IExtensionRegistry reg = Platform.getExtensionRegistry(); |
| IExtensionPoint poi = reg.getExtensionPoint(GT_MATCHER_EXT_ID); |
| HashMap<String, IGTRuleMatcherFactory> r = new HashMap<String, IGTRuleMatcherFactory>(); |
| if (poi == null) |
| return r; |
| IExtension[] exts = poi.getExtensions(); |
| for (int i = 0; i < exts.length; i++) { |
| IConfigurationElement[] el = exts[i].getConfigurationElements(); |
| for (int j = 0; j < el.length; j++) { |
| try { |
| Object o = el[j].createExecutableExtension("class"); |
| if (o instanceof IGTRuleMatcherFactory) { |
| String id = el[j].getAttribute("id"); |
| r.put(id, (IGTRuleMatcherFactory) o); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| return r; |
| } |
| |
| } |