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

VIATRA/Addon/Databinding

This page completely supersedes its original source, the original documentation on incquery.net.

Data binding overview

Data binding is general technique that binds two data/information sources together and maintains synchronization of data. In UI data binding data objects are bound to UI elements and if the binding is done in the proper manner the changes in the data will be automatically reflected on the UI elements (for example a label will be automatically refreshed with new contents).

VIATRA provides a simple data binding facility that can be used to bind pattern matches to UI elements. The feature is mainly intended to be used to integrate VIATRA queries to newly developed user interfaces, however, the Query Explorer component also uses some related annotations. In summary, this document is intended to be used mainly by developers but the section dealing with data binding related annotations may be useful for VIATRA end-users too.

Data binding related pattern annotations

The following annotation can be used on patterns within the data binding context:

  • @QueryExplorer: the message parameter of the Query explorer annotation defines the label feature of the selected match.
  • @ObservableValue: allows the developer to customize the appearance of a match inside the Details panel. It defines an observable value (as defined in JFace databinding) which can be bound to an Eclipse/JFace UI.
    Annotation parameter(s):
    • name (String): the name of the parameter
    • expression (String): the attribute definition without '$' marks. For example @ObservableValue(name = "Year", expression="Y.startingDate")
      The parameters of the pattern are considered the default observable values of the matcher. This means, it is not required to present values like @ObservableValue(name="Y" expression="Y"). However, it is possible to redefine these values by simply defining a new value, e.g. @ObservableValue(name="Y" expression="Y.startingDate").
      It is also possible to use the annotation without parameters: in this case, it only represents that a default Observable matcher object should be generated with the default observable values. 
    • labelExpression: this annotation makes it possible to create observable string properties such as @ObservableValue(name = "TeacherStudent", labelExpression = "Teacher $T.name$ teaches Student $S.name$"), which are useful for presentation purposes inside a JFace viewer component.
The following example is from the school tutorial (see link on the bottom of this page under the 'Examples' section). Here, a pattern is given with various annotations.
// Step 7: combine everything, @PatternUI, @ObservableValue, @Handler
 
   /*
    * 
    * We want to find those years, which had courses taught by the busiest teacher
    * and included the most sociable students
    * 
    */
    @QueryExplorer(message="The busiest teacher $T.name$ taught the most sociable student $S.name$ in $Y.startingDate$")
    @ObservableValue(name = "Year", expression="Y.startingDate")
    @ObservableValue(name = "Teacher", expression="T.name")
    @ObservableValue(name = "Student", expression="S.name")
    @ObservableValue(name = "TeacherStudent", labelExpression = "Teacher $T.name$ teaches Student $S.name$")
    @Handler(fileExtension = "school")
    pattern finalPattern(Y:Year,C:Course,T:Teacher,S:Student) = {
    	Year.schoolClasses.courses(Y,C);
    	Course.teacher(C,T);
    	Student.schoolClass.courses(S,C);
 
    	find theOnesWithTheBiggestCircle(S);
    	find teachesTheMostCourses(T);
    }

The @QueryExplorer annotation will result that the match of the finalPattern pattern will have a label with the form of 'The busiest teacher $T.name$ taught the most sociable student $S.name$ in $Y.startingDate$' inside the Query Explorer. The attribute markers will be replaced with the appropriate attribute values based on the current pattern match.

VIATRA Data Binding API

VIATRA contains the "org.eclipse.viatra.addon.databinding.runtime" plug-in, which provides several useful features.

ViatraObservables class (source)

Observing match values

This class provides the getDatabindingAdapter factory method, which you can use to create DatabindingAdapter instances (based on a Pattern defintion object) that provide direct access to JFace observables based on the annotations provided for your pattern definition. Important note: if you are binding an IObservableValue instance obtained from the above mentioned class, it is important to pay attention on the binding's update strategy as you should not use a two-way updating strategy (because it would modify the pattern match parameter). For example if you use a DatabindingContext instance's bindValue method to create the binding, the suggested UpdateValueStrategy is the following:

  • UpdateValueStrategy.POLICY_NEVER in the UI to pattern match parameter binding
  • UpdateValueStrategy.POLICY_UPDATE in the pattern match parameter to UI binding

It is also worth noting that you must take care of the IObservableValue instances' life-cycle as the pattern match may be removed from the match set of the VIATRA query matcher (see "observing match sets" below).

Observing match sets

The ViatraObservables class provides means to observe the entire match set (query result set) as an observable list or set. This is the recommended and very efficient way of tracking changes in the query result set efficiently. See here for an example usage and further notes.

Example and useful resources

  • Please see the school example which demonstrates the usage of the above mentioned annotations.
  • The following tutorial is an in depth document about JFace/SWT data binding: http://www.vogella.com/articles/EclipseDataBinding/article.html
  • The DetailObserver class of the VIATRA Query Explorer gives a hint how the generated data binding code can be used (in this case for the Query Explorer's Details view). The DetailObserver class (source) extends the AbstractObservableList class, thus can be used in data binding contexts. Within the VIATRA Queries project it is used to serve as the input for a TableViewer which displays pattern match details. The TableViewer's content provider is an ObservableListContentProvider, this way data binding is automatically created and maintained.
    • The constuctor initializes the data structures and registers the IValueChangeListener instance on all pattern match parameters which were declared with an @ObservableValue annotation, thus having a getter for its observable value in the appropriate DatabindingAdapter subclass.
    • The addDetail and removeDetail methods modifiy the contents inside the view and notify (with the fireListChange call) the UI element that the backing content has changed.
    • We have registered the IValueChangeListener instance on all pattern match parameters in the constructor. Upon attribute modification this listener will be called and the appropriate element can be changed in the details view.

Back to the top