Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 348321 - Two interesting class for EMF Query 2
Summary: Two interesting class for EMF Query 2
Status: NEW
Alias: None
Product: EMF
Classification: Modeling
Component: Query2 (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL: http://www.eclipse.org/forums/index.p...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-05 08:05 EDT by littleRathi CLA
Modified: 2011-06-08 11:09 EDT (History)
1 user (show)

See Also:


Attachments
Helper class for managing EMF Query 2 (4.46 KB, text/plain)
2011-06-08 11:07 EDT, littleRathi CLA
no flags Details
Helper class for helping loading .query files and more comfortable access (2.26 KB, text/plain)
2011-06-08 11:08 EDT, littleRathi CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description littleRathi CLA 2011-06-05 08:05:01 EDT
Build Identifier: 20110301-1815

REF: http://www.eclipse.org/forums/index.php/m/676570/
Class in a testphase and are uncommented yet and not complete and at time adjust to my needs. But can perhaps be in a better version interesting for EMF Query 2?

Reproducible: Always

Steps to Reproduce:
Class1:
public class IndexQuery2Manager {
	// The index:
	private static Index index = IndexFactory.getInstance();
	private static ResourceIndexer indexer = new ResourceIndexer();
	
	private static Map<String, URI> indexedURIs = new HashMap<String, URI>();
	private static Set<String> knownPackages = new HashSet<String>();
	// Additional
	private static ResourceSet resourceSet = new ResourceSetImpl();
	private static URIConverter normalizer = resourceSet.getURIConverter();
	private static EPackage.Registry rsRegistry = resourceSet.getPackageRegistry();
	private static EPackage eCore = EcorePackage.eINSTANCE;
	
	public IndexQuery2Manager() {
		addPackages(eCore);
	}
	
	public void addPackages(final EPackage... packages) {
		index.executeUpdateCommand(new UpdateCommandAdapter() {
			@Override
			public void execute(final IndexUpdater updater) {
				for (EPackage pack: packages) {
					if (knownPackages.add(pack.getNsURI())) {
						indexer.resourceChanged(updater, pack.eResource());
						rsRegistry.put(pack.getNsURI(), pack);
					} else
						System.err.println("[ERROR:01]"+ pack.getNsURI() +" already added!");
				}
			}
		});
	}
	
	public URI[] addData(final URI... URIs) {
		final URI[] normalizedURIs = new URI[URIs.length];
		index.executeUpdateCommand(new UpdateCommandAdapter() {
			private Map<String, URI> allURIs = indexedURIs;
			@Override
			public void execute(IndexUpdater updater) {
				for (int x = 0; x < URIs.length; x++) {
					URI newURI = normalizer.normalize(URIs[x]);
					
					if (!allURIs.containsKey(newURI.toString().trim())) {
						allURIs.put(newURI.toString().trim(), newURI);
						Resource resource = resourceSet.getResource(URIs[x], true);
						try {
							resource.load(null);
							
							indexer.resourceChanged(updater, resource);
							resource.unload();
							
							normalizedURIs[x] = normalizer.normalize(URIs[x]);
						} catch(IOException e) {
							e.printStackTrace();
							normalizedURIs[x] = null;
						}
					} else {
						normalizedURIs[x] = null;
						System.err.println("[ERROR:02]"+ normalizer.normalize(URIs[x]).toString() +" already added!");
					}
				}
			}
		});
		return normalizedURIs;
	}
	
	public QueryContext createQueryContext(final ResourceSet resourceSet, final URI... useURIs) {
		return new QueryContext() {
			private Map<String, URI> allURIs = indexedURIs;
			@Override
			public URI[] getResourceScope() {
				URI[] result = new URI[useURIs.length];
				
				for (int x = 0; x < useURIs.length; x++) {
					URI normalizedUse = normalizer.normalize(useURIs[x]);
					if (allURIs.containsKey(normalizedUse.toString().trim()))
					{
						result[x] = normalizedUse;
					}
					else
						System.err.println("[ERROR:03]"+ normalizedUse +" not indexed!");
				}
				
				return result;
			}
			@Override
			public ResourceSet getResourceSet() {
				return resourceSet;
			}
		};
	}
	
	public QueryContext createQueryContextAll(final ResourceSet resourceSet) {
		return new QueryContext() {
			private Map<String, URI> allURIs = indexedURIs;
			@Override
			public URI[] getResourceScope() {
				return allURIs.values().toArray(new URI[allURIs.size()]);
			}
			@Override
			public ResourceSet getResourceSet() {
				return resourceSet;
			}
		};
	}
	
	public ResultSet execute(Query query, QueryContext queryContext) {
		QueryProcessor queryProcessor = QueryProcessorFactory.getDefault().createQueryProcessor(index);
		return queryProcessor.execute(query, queryContext);
	}
}

class2:
public class QueryCommandsManager {
	private Map<String, Query> queries;
	
	// helper objects:
	private Injector injector = new QueryStandaloneSetup().createInjectorAndDoEMFRegistration();
	private XtextResourceSet set = injector.getInstance(XtextResourceSet.class);
	
	QueryCommandsManager() {
		queries = new HashMap<String, Query>();
	}
		
	public boolean addQuery(String queryName, Query query) {
		if (queries.containsKey(queryName))
			return false;
		queries.put(queryName, query);
		return true;
	}
	
	public int loadQueryFile(URI queryFile) {
		int loadedQueries = 0;
		set.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		URI normalizedURI = set.getURIConverter().normalize(queryFile);
		LazyLinkingResource xtextResource = (LazyLinkingResource)set.getResource(normalizedURI, true);
		
		Model model = (Model)xtextResource.getContents().get(0);
		
		EList<NamedQuery> queryList = model.getNamedQueries();
		
		for (NamedQuery nQuery: queryList) {
			if (nQuery != null)
			{
				Query query = QueryTransformer.transform(nQuery.getQuery());
				
				if (!queries.containsKey(nQuery.getName())) {
					loadedQueries++;
					queries.put(nQuery.getName(), query);
				}
			} else
				System.err.println("[ERROR:01] nQuery == null");
		}
		
		return loadedQueries;
	}
	
	public String[] getAllQueryName() {
		Set<String> keys = queries.keySet();
		return keys.toArray(new String[keys.size()]);
	}
	
	public Query getQuery(String queryName) {
		return queries.get(queryName);
	}
	
	public int size() {
		return queries.size();
	}
}
Comment 1 littleRathi CLA 2011-06-08 11:07:01 EDT
Created attachment 197613 [details]
Helper class for managing EMF Query 2
Comment 2 littleRathi CLA 2011-06-08 11:08:04 EDT
Created attachment 197614 [details]
Helper class for helping loading .query files and more comfortable access
Comment 3 littleRathi CLA 2011-06-08 11:09:07 EDT
I now see that i could upload my files, so here in a more comfortable way i think. At time no change.