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/Addon/Databinding"

< VIATRA‎ | Addon
Line 5: Line 5:
 
[http://en.wikipedia.org/wiki/Data_binding 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).  
 
[http://en.wikipedia.org/wiki/Data_binding 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).  
  
EMF-IncQuery 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 EMF-IncQuery 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 EMF-IncQuery end-users too.  
+
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  ==
 
== Data binding related pattern annotations  ==
Line 12: Line 12:
  
 
*'''@QueryExplorer:''' the message parameter of the Query explorer annotation defines the label feature of the selected match.  
 
*'''@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. This annotation will also trigger the generation of an additional .databinding side-project next to your EMF-IncQuery project, which includes some helper classes that you can use to ease the integration of EMF-IncQuery into your user interface. <br>Annotation parameter(s):  
+
*'''@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. <br>Annotation parameter(s):  
 
**'''name '''(String): the name of the parameter  
 
**'''name '''(String): the name of the parameter  
 
**'''expression''' (String): the attribute definition without '$' marks. For example @ObservableValue(name = "Year",&nbsp;expression="Y.startingDate") <br>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"). <br>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.&nbsp;  
 
**'''expression''' (String): the attribute definition without '$' marks. For example @ObservableValue(name = "Year",&nbsp;expression="Y.startingDate") <br>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"). <br>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.&nbsp;  
Line 44: Line 44:
 
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.  
 
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.  
  
== EMF-IncQuery Data Binding API  ==
+
== VIATRA Data Binding API  ==
  
EMF-IncQuery contains the "org.eclipse.incquery.databinding.runtime" plug-in, which provides several useful features.  
+
VIATRA contains the "org.eclipse.viatra.addon.databinding.runtime" plug-in, which provides several useful features.  
  
=== Observing a single match  ===
+
=== ViatraObservables class ([http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/addon/plugins/org.eclipse.viatra.addon.databinding.runtime/src/org/eclipse/viatra/addon/databinding/runtime/api/ViatraObservables.java source]) ===
  
The DatabindingUtil ([http://git.eclipse.org/c/incquery/org.eclipse.incquery.git/tree/plugins/org.eclipse.incquery.databinding.runtime/src/org/eclipse/incquery/databinding/runtime/util/DatabindingUtil.java source], [http://eclipse.org/incquery/javadoc/milestones/m3/org/eclipse/incquery/databinding/runtime/util/DatabindingUtil.html javadoc]) 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:  
+
=== 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_NEVER in the UI to pattern match parameter binding  
 
*UpdateValueStrategy.POLICY_UPDATE in the pattern match parameter to UI 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 EMF-IncQuery matcher (see "observing match sets" below).  
+
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  ===
 
=== Observing match sets  ===
  
The [IncQueryObservables class ([http://git.eclipse.org/c/incquery/org.eclipse.incquery.git/tree/plugins/org.eclipse.incquery.databinding.runtime/src/org/eclipse/incquery/databinding/runtime/api/IncQueryObservables.java source], [http://eclipse.org/incquery/javadoc/milestones/m3/org/eclipse/incquery/databinding/runtime/api/IncQueryObservables.html javadoc]) 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 [[EMFIncQuery/UserDocumentation/API#Tracking_changes_in_match_sets_efficiently|here]] for an example usage and further notes.
+
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 [[VIATRA/Query/UserDocumentation/API#Tracking_changes_in_match_sets_efficiently|here]] for an example usage and further notes.
  
 
== Example and useful resources  ==
 
== Example and useful resources  ==
Line 65: Line 67:
 
*Please see the [http://incquery.net/incquery/new/examples/school school example] which demonstrates the usage of the above mentioned annotations.  
 
*Please see the [http://incquery.net/incquery/new/examples/school 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 following tutorial is an in depth document about JFace/SWT data binding: http://www.vogella.com/articles/EclipseDataBinding/article.html  
*The DetailObserver class of EMF-IncQuery's 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 ([http://git.eclipse.org/c/incquery/org.eclipse.incquery.git/tree/plugins/org.eclipse.incquery.tooling.ui/src/org/eclipse/incquery/tooling/ui/queryexplorer/content/detail/DetailObserver.java source], [http://eclipse.org/incquery/javadoc/milestones/m3/org/eclipse/incquery/tooling/ui/queryexplorer/content/detail/DetailObserver.html javadoc]) extends the AbstractObservableList class, thus can be used in data binding contexts. Within the EMF-IncQuery project it is used to server as the input for a TableViewer which diplays pattern match details. The TableViewer's content provider is ObservableListContentProvider, this way data binding is automatically created and maintained.  
+
*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 ([http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/query/plugins/org.eclipse.viatra.query.tooling.ui/src/org/eclipse/viatra/query/tooling/ui/queryexplorer/content/detail/DetailObserver.java 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 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.  
 
**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.
 
**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.

Revision as of 11:15, 10 March 2016

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