Eclipse OCL Development

Source: Internet
Author: User

The OCL parser provides two classes for parsing OCL expressions: oclhelper and OCL:
The oclhelper interface is designed for parsing expressions, while OCL is used as the starting point for parsing.

To resolve OCL, you must first create an environment for it, for example, whether it is based on the UML model or the ecore model.
You can specify the corresponding environmentfactory In the constructor. For example:
OCL <?, Eclassifier ,?, ?, ?, ?, ?, ?, ?, Constraint, eclass, eobject> OCL;
OCL = OCL. newinstance (ecoreenvironmentfactory. instance); // ecore Environment

In the OCL class, the getenvironment () and getevaluationenvironment () methods are used to obtain the root environment and evaluation environment of the model respectively. These two environments are created by the environmentfactory interface.
In the root environment, you can nest some sub-environments to define the package namespace to obtain the context of the package. First, let's look at a simple OCL description:
-- Package context <br/> package company <br/> -- class context <br/> context person <br/> inv: name <> 'hangsan' <br/> -- operation context <br/> context person: getcurrentspouse (): person <br/> pre: Self. ismarried = true <br/> body: Self. mariages-> select (M | M. ended = false ). spouse <br/> -- attribute context <br/> context person: Income: integer <br/> init: parents. income-> sum () * 1% -- pocket allowance <br/> endpackage <br/>

In the OCL description, the hierarchical structure is embodied by context organization, for example:
The package can traverse the context of classifer;
Classifer can traverse the context of a method or attribute;
With this hierarchical structure, the package context can traverse any model component.
Therefore, the various lookup methods in the environment interface mainly use the package context to perform related lookup operations.

After the OCL object is instantiated, you can use the createoclhelper () method to obtain the oclhelper object.
The oclhelper object declares a large number of methods for creating OCL queries and constraints.
Different constraints require different context environments. setcontext (), setoperationcontext (), and setattributecontext () indicate whether the constraint is for classifer, operation, or attribute.

Oclhelper <eclassifier ,?, ?, Constraint> helper = OCL. createoclhelper (); <br/> helper. setcontext (extlibrarypackage. literals. library); // set the context <br/> constraint = helper. createinvariant (// create constraints <br/> "books-> forall (B1, B2 | B1 <> B2 implies b1.title <> b2.title )"); </P> <p> oclexpression <eclassifier> query = helper. createquery (// create a query <br/> "books-> collect (B: Book | B. category)-> asset () "); </P> <p>

As shown above: the createinvariant method parses the constraint expression. The createquery method parses the query expression. The two methods return the constraint object and the oclexpression object respectively.
With query and Constraint Objects, you can use query objects to evaluate them.
Query <eclassifier, eclass, eobject> queryeval = OCL. createquery (query); // obtain the query evaluation object <br/> query <eclassifier, eclass, eobject> constrainteval = OCL. createquery (constraint); // obtain the constraint evaluation object <br/> List <library> libraries = getlibraries (); <br/> for (library next: libraries) {<br/> If (constrainteval. check (next) {// If the constraint is passed <br/> @ suppresswarnings ("unchecked") <br/> set <bookcategory> categories = (set <bookcategory>) queryeval. evaluate (next); // perform query evaluation <br/> system. out. printf ("% s: % S % N", next. getname (), categories); <br/>}< br/>

The query API provides a practical method, which can be abbreviated as a for loop:
For (library next: constrainteval. Select (Libraries )){
@ Suppresswarnings ("unchecked ")
Set <bookcategory> categories = (set <bookcategory>) queryeval. Evaluate (next );
System. Out. printf ("% s: % S % N", next. getname (), categories );
}

In practical applications, model definitions and model constraints are often written into different resource files to facilitate model management.
OCL provides methods to parse the constraint file and construct an oclinput object.
OCL <?, Eclassifier ,?, ?, ?, ?, ?, ?, ?, Constraint, eclass, eobject> OCL; <br/> OCL = OCL. newinstance (ecoreenvironmentfactory. instance); <br/> ifile file = getworkspacefile ("/OCL/constraints. OCL "); // obtain the OCL constraint file <br/> inputstream in = file. getcontents (); <br/> // store the parsed constraints in hashmap <br/> Map <string, constraint> constraintmap = new hashmap <string, constraint> (); <br/> try {<br/> oclinput document = new oclinput (in, file. getcharset (); // construct an oclinput object <br/> List <constraint> constraints = OCL. parse (input); // parse the OCL document <br/> for (constraint next: Constraints) {<br/> constraintmap. put (next. getname (), next); <br/> oclexpression <eclassifier> body = next. getspecification (). getbodyexpression (); <br/> system. out. printf ("% s: % S % N", next. getname (), body); <br/>}< br/>}finally {<br/> in. close (); <br/>}< br/>

The content of constraints. OCL is as follows:
Package extlibrary </P> <p> context library <br/> -- get all books with a title in a library and its branches (recursively) <br/> Def: getbooks (Title: string): Set (book) = <br/> books-> select (B | B. title = title)-> asset ()-> Union (<br/> branches. getbooks (title) </P> <p> context book <br/> -- the library containing a book <br/> Def: Library: library = library. allinstances ()-> Any (books-> Primary des (Self) </P> <p> -- book titles are unique within their library branch (and its sub-branches) <br/> inv unique_title: Not library. oclisundefined () implies <br/> library. getbooks (title) = set {self}) </P> <p> endpackage </P> <p>

Through the above parsing, the constraints are stored in the form of map, and the constraint object can be obtained through the constraint name, such:
OCL. Check (book, constraintmap. Get ("unique_title"); // perform unique title constraints on the book object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.