Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "VIATRA/Query/UserDocumentation/API/RunOnce"

 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{caution|Old information|This page is not updated anymore; for more up-to-date details look at the query api documentation at https://www.eclipse.org/viatra/documentation/query-api.html instead.}}
 
== Overview ==
 
== Overview ==
  
This page describes how EMF-IncQuery can be used to carry out one-time query evaluation which is useful in the following cases:
+
This page describes how VIATRA Query can be used to carry out one-time query evaluation which is useful in the following cases:
 
* You want less (steady-state) memory consumption instead of incremental evaluation.
 
* You want less (steady-state) memory consumption instead of incremental evaluation.
* You have derived features that are not [[EMFIncQuery/UserDocumentation/Query_Based_Features#Well-behaving_structural_features|well-behaving]], but you want to include them in queries.
+
* You have derived features that are not [[VIATRA/Addon/Query_Based_Features#Well-behaving_structural_features|well-behaving]], but you want to include them in queries.
* You like the query language of EMF-IncQuery, but you don't need incremental evaluation and the batch performance is better than the sum of model modification overheads between query usages.
+
* You like the query language of VIATRA Query, but you don't need incremental evaluation and the batch performance is better than the sum of model modification overheads between query usages.
  
 
These scenarios are now supported by a "run-once" query engine that will perform the evaluation on a given query and return the match set then dispose of the Rete network and base index to free up memory.
 
These scenarios are now supported by a "run-once" query engine that will perform the evaluation on a given query and return the match set then dispose of the Rete network and base index to free up memory.
Line 10: Line 11:
 
=== Example ===
 
=== Example ===
  
The up-to-date sample source code to this page is found in Git here: http://git.eclipse.org/c/incquery/org.eclipse.incquery.examples.git/tree/minilibrary Most notably,  
+
The up-to-date sample source code to this page is found in Git here: http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/examples/minilibrary Most notably,  
  
*the patterns are found in [http://git.eclipse.org/c/incquery/org.eclipse.incquery.examples.git/tree/minilibrary/org.eclipse.incquery.runtime.runonce.tests/src/org/eclipse/incquery/runtime/runonce/tests/eiqlibrary.eiq eiqlibrary.eiq]  
+
*the patterns are found in [http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/examples/minilibrary/org.eclipse.viatra.query.runtime.runonce.tests/src/org/eclipse/viatra/query/runtime/runonce/tests/eiqlibrary.vql eiqlibrary.vql]  
*and the API usage samples are found in [http://git.eclipse.org/c/incquery/org.eclipse.incquery.examples.git/tree/minilibrary/org.eclipse.incquery.runtime.runonce.tests/src/org/eclipse/incquery/runtime/runonce/tests/RunOnceTest.java RunOnceTest.java]
+
*and the API usage samples are found in [http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/examples/minilibrary/org.eclipse.viatra.query.runtime.runonce.tests/src/org/eclipse/viatra/query/runtime/runonce/tests/RunOnceTest.java RunOnceTest.java]
  
 
== Usage ==
 
== Usage ==
 +
 +
=== Run-once then dispose ===
  
 
The API of the run-once query engine is very simple, just instantiate the engine with the constructor using the proper scope (EObject, Resource or ResourceSet) and call the getAllMatches with a query specfication:
 
The API of the run-once query engine is very simple, just instantiate the engine with the constructor using the proper scope (EObject, Resource or ResourceSet) and call the getAllMatches with a query specfication:
Line 23: Line 26:
 
// using generated query specification
 
// using generated query specification
 
Collection<SumOfPagesInLibraryMatch> allMatches = engine.getAllMatches(SumOfPagesInLibraryMatcher.querySpecification());
 
Collection<SumOfPagesInLibraryMatch> allMatches = engine.getAllMatches(SumOfPagesInLibraryMatcher.querySpecification());
// using generic query specification
+
// if you only have Pattern object
GenericQuerySpecification specification = new GenericQuerySpecification(BooksWithMultipleAuthorsMatcher.querySpecification().getPattern());
+
IQuerySpecification<ViatraQueryMatcher<IPatternMatch>> specification = (IQuerySpecification<ViatraQueryMatcher<IPatternMatch>>) QuerySpecificationRegistry.getOrCreateQuerySpecification(BooksWithMultipleAuthorsMatcher.querySpecification().getPattern());
Collection<GenericPatternMatch> matches = engine.getAllMatches(specification);
+
Collection<IPatternMatch> matches = engine.getAllMatches(specification);
 
</source>
 
</source>
  
 
Note that each invocation of getAllMatches will traverse the model completely, index the classes, features and data types that are required for the query, collect the match set than dispose the indexes.
 
Note that each invocation of getAllMatches will traverse the model completely, index the classes, features and data types that are required for the query, collect the match set than dispose the indexes.
 +
 +
=== Automatic re-sampling ===
 +
 +
In many cases, the derived features are only a small part of the queries and it would be better to keep the indices once they are built. However, in this case, we need a way to update the values of all derived features that are indexed.
 +
 +
The run-once query engine supports automatic re-sampling by listening to model modifications and updating values before returning match results.The following example shows how you can enable this mode:
 +
 +
<source lang="java">
 +
RunOnceQueryEngine engine = new RunOnceQueryEngine(notifier);
 +
engine.setAutomaticResampling(true); // enable re-sampling mode
 +
Collection<SumOfPagesInLibraryMatch> allMatches = engine.getAllMatches(SumOfPagesInLibraryMatcher.querySpecification());
 +
// some model modification
 +
// only re-sampling of derived features, not complete traversal
 +
allMatches = engine.getAllMatches(SumOfPagesInLibraryMatcher.querySpecification());
 +
</source>
 +
 +
If you no longer need automatic re-sampling, you can turn it off. In this case the engine that was kept incrementally updated is removed from memory.
 +
 +
<source lang="java">
 +
engine.setAutomaticResampling(false); // disable re-sampling mode, indices removed
 +
</source>
 +
 +
Finally, if the value of derived features change without any model modifications (not recommended), you can tell the engine to run the re-sampling next time:
 +
 +
<source lang="java">
 +
engine.resampleOnNextCall();
 +
</source>

Latest revision as of 15:25, 14 November 2017

Stop.png
Old information
This page is not updated anymore; for more up-to-date details look at the query api documentation at https://www.eclipse.org/viatra/documentation/query-api.html instead.

Overview

This page describes how VIATRA Query can be used to carry out one-time query evaluation which is useful in the following cases:

  • You want less (steady-state) memory consumption instead of incremental evaluation.
  • You have derived features that are not well-behaving, but you want to include them in queries.
  • You like the query language of VIATRA Query, but you don't need incremental evaluation and the batch performance is better than the sum of model modification overheads between query usages.

These scenarios are now supported by a "run-once" query engine that will perform the evaluation on a given query and return the match set then dispose of the Rete network and base index to free up memory.

Example

The up-to-date sample source code to this page is found in Git here: http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/examples/minilibrary Most notably,

Usage

Run-once then dispose

The API of the run-once query engine is very simple, just instantiate the engine with the constructor using the proper scope (EObject, Resource or ResourceSet) and call the getAllMatches with a query specfication:

RunOnceQueryEngine engine = new RunOnceQueryEngine(notifier);
// using generated query specification
Collection<SumOfPagesInLibraryMatch> allMatches = engine.getAllMatches(SumOfPagesInLibraryMatcher.querySpecification());
// if you only have Pattern object
IQuerySpecification<ViatraQueryMatcher<IPatternMatch>> specification = (IQuerySpecification<ViatraQueryMatcher<IPatternMatch>>) QuerySpecificationRegistry.getOrCreateQuerySpecification(BooksWithMultipleAuthorsMatcher.querySpecification().getPattern());
Collection<IPatternMatch> matches = engine.getAllMatches(specification);

Note that each invocation of getAllMatches will traverse the model completely, index the classes, features and data types that are required for the query, collect the match set than dispose the indexes.

Automatic re-sampling

In many cases, the derived features are only a small part of the queries and it would be better to keep the indices once they are built. However, in this case, we need a way to update the values of all derived features that are indexed.

The run-once query engine supports automatic re-sampling by listening to model modifications and updating values before returning match results.The following example shows how you can enable this mode:

RunOnceQueryEngine engine = new RunOnceQueryEngine(notifier);
engine.setAutomaticResampling(true); // enable re-sampling mode
Collection<SumOfPagesInLibraryMatch> allMatches = engine.getAllMatches(SumOfPagesInLibraryMatcher.querySpecification());
// some model modification
// only re-sampling of derived features, not complete traversal
allMatches = engine.getAllMatches(SumOfPagesInLibraryMatcher.querySpecification());

If you no longer need automatic re-sampling, you can turn it off. In this case the engine that was kept incrementally updated is removed from memory.

engine.setAutomaticResampling(false); // disable re-sampling mode, indices removed

Finally, if the value of derived features change without any model modifications (not recommended), you can tell the engine to run the re-sampling next time:

engine.resampleOnNextCall();

Back to the top