Skip to main content
Jump to: navigation, search

VIATRA/Query/UserDocumentation/API/Advanced/Archive

Advanced API - Archive

Initializing matchers and accessing results (IncQuery 0.7.x)

Sample code

Using the Generic API:

public String executeDemo_GenericAPI(String modelPath, String patternFQN) {
 final StringBuilder results = new StringBuilder();
 Resource resource = loadModel(modelPath);
 if (resource != null) {
  try {
   // get all matches of the pattern
   // create an *unmanaged* engine to ensure that noone else is going
   // to use our engine
   AdvancedIncQueryEngine engine = AdvancedIncQueryEngine.createUnmanagedEngine(resource);
   // instantiate a pattern matcher through the registry, by only knowing its FQN
   // assuming that there is a pattern definition registered matching 'patternFQN'
   Pattern p = null;
   IQuerySpecification<?> querySpecification = null;
 
   // use a trick to load Pattern models from a file
   ResourceSet resourceSet = new ResourceSetImpl();
   // here, we make use of the (undocumented) fact that the Pattern model 
   // is stored inside the hidden "queries" directory inside an EMF-IncQuery project
   URI fileURI = URI.createPlatformPluginURI("headlessQueries.incquery/queries/globalEiqModel.xmi", false);
   Resource patternResource = resourceSet.getResource(fileURI, true);
   // navigate to the pattern definition that we want
   if (patternResource != null) {
     if (patternResource.getErrors().size() == 0 && patternResource.getContents().size() >= 1) {
       EObject topElement = patternResource.getContents().get(0);
          if (topElement instanceof PatternModel) {
        	for (Pattern _p  : ((PatternModel) topElement).getPatterns()) {
             		if (_p.getName().equals(patternFQN)) {
              			p = _p; break;
               		}
               	}
          }
     }
  }
 
   if (p!=null) {
    querySpecification = QuerySpecificationRegistry.getQuerySpecification(p);
   }
   else {
    // fall back to the registry in case the pattern model extraction didn't work
    querySpecification = QuerySpecificationRegistry.getQuerySpecification(patternFQN);
   }
 
   if (querySpecification!=null) {
    IncQueryMatcher<? extends IPatternMatch> matcher = querySpecification.getMatcher(engine);
    Collection<? extends IPatternMatch> matches = matcher.getAllMatches();
    prettyPrintMatches(results, matches);
   }
   // wipe the engine
   engine.wipe();
   // after a wipe, new patterns can be rebuilt with much less overhead than 
   // complete traversal (as the base indexes will be kept)
   // completely dispose of the engine once's it is not needed
   engine.dispose();
   resource.unload();
  } catch (IncQueryException e) {
   e.printStackTrace();
   results.append(e.getMessage());
  } 
 } else {
  results.append("Resource not found");
 }
 return results.toString();
}

Loading EIQ resources programmatically

/**
 * Returns the match set for patternFQN over the model in modelPath in pretty printed form
 * 
 * @param modelPath
 * @param patternFQN
 * @return
 */
public String executeDemo_GenericAPI_LoadFromEIQ(String modelPath, String patternFQN) {
 final StringBuilder results = new StringBuilder(); 
 Resource resource = loadModel(modelPath);
 if (resource != null) {
  try {
    // get all matches of the pattern
    // create an *unmanaged* engine to ensure that noone else is going
    // to use our engine
    AdvancedIncQueryEngine engine = AdvancedIncQueryEngine.createUnmanagedEngine(resource);
    // instantiate a pattern matcher through the registry, by only knowing its FQN
    // assuming that there is a pattern definition registered matching 'patternFQN'
    Pattern p = null;
    // Xtext resource magic -- this is needed for EIQ resources;
    new EMFPatternLanguageStandaloneSetup()
    {
     @Override
     public Injector createInjector() { return Guice.createInjector(new GeneratorModule()); }
    }
   .createInjectorAndDoEMFRegistration();
    // use a trick to load Pattern models from a file
    ResourceSet resourceSet = new ResourceSetImpl();
    URI fileURI = URI.createPlatformResourceURI("test/src/test/test.eiq", false);
    Resource patternResource = resourceSet.getResource(fileURI, true);
    // navigate to the pattern definition that we want
    if (patternResource != null) {
     if (patternResource.getErrors().size() == 0 && patternResource.getContents().size() >= 1) {
      EObject topElement = patternResource.getContents().get(0);
       if (topElement instanceof PatternModel) {
        for (Pattern _p  : ((PatternModel) topElement).getPatterns()) {
         if (_p.getName().equals(patternFQN)) {
          p = _p; break;
         }
        }
       }
      }
     }
     IncQueryMatcher<? extends IPatternMatch> matcher = engine.getMatcher(p);
     if (matcher!=null) {
      Collection<? extends IPatternMatch> matches = matcher.getAllMatches();
       prettyPrintMatches(results, matches);
      }
     // wipe the engine
     engine.wipe();
     // after a wipe, new patterns can be rebuilt with much less overhead than 
     // complete traversal (as the base indexes will be kept)
     // completely dispose of the engine once's it is not needed
     engine.dispose();
     resource.unload();
    } catch (IncQueryException e) {
      e.printStackTrace();
     results.append(e.getMessage());
    }
   } else {
    results.append("Resource not found");
  }
 return results.toString();
}

Back to the top