Home » Modeling » OCL » parsing OCL as a pure abstract syntax tree
| |
Re: parsing OCL as a pure abstract syntax tree [message #54993 is a reply to message #54804] |
Tue, 06 May 2008 09:32   |
Eclipse User |
|
|
|
Christian W. Damus wrote:
> Hi, Jean-Marc,
>
> The OCL language's abstract syntax is fundamentally dependent on the
> contextual classifier for construction of the abstract syntax tree.
>
> Perhaps the concrete-syntax tree will suit your needs? See the
> OCLAnalyzer::parseConcreteSyntax() API.
Thanks Christian.
I did this:
Environment env = EcoreEnvironmentFactory.INSTANCE.createEnvironment();
OCLAnalyzer a = new OCLAnalyzer( env, oclSource );
CSTNode csTree = a.parseConcreteSyntax();
Then csTree is null in almost all cases, except that a name alone is
interpreted as a variable.
Note that everything is templatized by:
<EPackage, EClassifier, EOperation, EStructuralFeature, EEnumLiteral,
EParameter, EObject, CallOperationAction, SendSignalAction, Constraint,
EClass, EObject>
>
> HTH,
>
> Christian
>
> On Friday 05-02-2008 (10:29), Jean-Marc Vanel wrote:
>
>> Hi
>
>> Is it possible to parse syntactically an OCL expression, i.e. without a
>> reference to a Class context, obtaining a pure abstract syntax tree ?
|
|
|
Re: parsing OCL as a pure abstract syntax tree [message #55020 is a reply to message #54993] |
Tue, 06 May 2008 11:38   |
Eclipse User |
|
|
|
Originally posted by: give.a.damus.gmail.com
Hi, Jean-Marc,
You are probably getting nulls because your input text is not one of
the top expressions defined by the OCL concrete-syntax grammar
(Chapter 9 of the spec).
You must supply text conforming to one of these productions:
packageDeclarationCS
invOrDefCS
prePostOrBodyDeclCS
initOrDerValueCS
variableCS
This last one (which worked for you) isn't really supposed to be a top
expression, but the API needs it for its own reasons.
Have a look in the test_constraints.ocl file in the
org.eclipse.ocl.ecore.tests project for an example of valid top
expressions. In all cases, the "package <qname> ... endpackage"
context is optional; the contents are also top expressions.
HTH,
Christian
On Tuesday 05-06-2008 (09:32), Jean-Marc Vanel wrote:
> Christian W. Damus wrote:
>> Hi, Jean-Marc,
>>
>> The OCL language's abstract syntax is fundamentally dependent on
>> the contextual classifier for construction of the abstract syntax
>> tree.
>>
>> Perhaps the concrete-syntax tree will suit your needs? See the
>> OCLAnalyzer::parseConcreteSyntax() API.
> Thanks Christian.
> I did this:
> Environment env =
> EcoreEnvironmentFactory.INSTANCE.createEnvironment();OCLAnal yzer a =
> new OCLAnalyzer( env, oclSource );
> CSTNode csTree = a.parseConcreteSyntax();
> Then csTree is null in almost all cases, except that a name alone is
> interpreted as a variable.
> Note that everything is templatized by:
> <EPackage, EClassifier, EOperation, EStructuralFeature,
> EEnumLiteral, EParameter, EObject, CallOperationAction,
> SendSignalAction, Constraint, EClass, EObject>
-----8<-----
--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo
|
|
| | |
Re: parsing OCL as a pure abstract syntax tree [message #55099 is a reply to message #55047] |
Tue, 06 May 2008 14:01   |
Eclipse User |
|
|
|
Originally posted by: give.a.damus.gmail.com
Hi, Jean-Marc,
As you're using an EcoreEnvironment, it will have an OCLProblemHandler
installed.
Have a look at the OCLUtil::checkForErrors(...) methods to see how you
can get Diagnostics describing any syntactic problems found by the
parser.
HTH,
Christian
On Tuesday 05-06-2008 (01:39), Jean-Marc Vanel wrote:
> Christian W. Damus wrote:
>> Hi, Jean-Marc,
>>
>> You are probably getting nulls because your input text is not one
>> of the top expressions defined by the OCL concrete-syntax grammar
>> (Chapter 9 of the spec).
>>
>> You must supply text conforming to one of these productions:
>>
>> packageDeclarationCS
>> invOrDefCS
>> prePostOrBodyDeclCS
>> initOrDerValueCS
>> variableCS
> It works much better now, prefixing my stuff with inv: or body: .
> But is there a way to get the error messages ?
--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo
|
|
|
Re: parsing OCL as a pure abstract syntax tree [message #55154 is a reply to message #55074] |
Tue, 06 May 2008 14:37   |
Eclipse User |
|
|
|
Originally posted by: give.a.damus.gmail.com
Hi, Jean-Marc,
I'm thinking that the Abstract Syntax Tree will be more useful to you
for this purpose. The CST cannot distinguish between constructs that
look similar but are semantically different. For example, a reference
to a static attribute and an enumeration literal look identical; the
difference in semantics can only be determined by looking at the model
to see whether the named element is an attribute or an enumeration
literal. Likewise, what looks in the concrete syntax like a variable
could be a reference to an operation parameter, let-variable,
attribute, or type.
Using the AST, you also get a handy visitor framework in the
org.eclipse.ocl.utilities.AbstractVisitor class.
If you prefer to work with the CST, then you can use a popular pattern
to implement a visitor-like iteration: create a subclass of the
CSTSwitch class, and override the defaultCase(EObject) method to
iterate the eContents of the object and call doSwitch on each in turn.
Then, for any caseXyz(Xyz) method that you override, ensure that the
last thing it does is to call the defaultCase explicitly to continue
the recursion.HTH,
Christian
On Tuesday 05-06-2008 (01:53), Jean-Marc Vanel wrote:
> I forgot to ask:
> Now that I have a root CSNode object, is there some code to
> recursively iterate on it ?
> Would need to distinguish plain identifiers and collection mathods.
--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo
|
|
| |
Re: parsing OCL as a pure abstract syntax tree [message #55207 is a reply to message #55180] |
Wed, 07 May 2008 15:53   |
Eclipse User |
|
|
|
Originally posted by: give.a.damus.gmail.com
Hi, Jean-Marc,
Right, a new problem-handler wouldn't have any problems in it. You
would have to use the problem-handler already associated with your
OCLAnalyzer.
In any case, your solution seems to work, so that's good!
Cheers,
Christian
Jean-Marc Vanel wrote:
> Christian W. Damus wrote:
>
>> Hi, Jean-Marc,
>>
>> As you're using an EcoreEnvironment, it will have an OCLProblemHandler
>> installed.
>>
>> Have a look at the OCLUtil::checkForErrors(...) methods to see how you
>> can get Diagnostics describing any syntactic problems found by the
>> parser.
>
> Thanks.
>
> To get the error messages, I did:
>
> env = EcoreEnvironmentFactory.INSTANCE.createEnvironment();
> OCLAnalyzer a = new OCLAnalyzer( env, oclSource );
> CSTNode csTree = a.parseConcreteSyntax();
> if( csTree == null )
> try {
> Diagnostic errors = OCLUtil.checkForErrors( env );
> if( errors != null )
> System.err.println( errors ); // line not reached
> } catch (SyntaxException e) {
> System.err.println( oclSource + "\n" + e.getDiagnostic() ); // line
> reached
> } catch (SemanticException e) {
> System.err.println( oclSource + "\n" + e.getDiagnostic() );
> }
>
>
> Note that the other method checkForErrors() doesn't bring anything :
>
> handler = new OCLProblemHandler( a.getParser() );
> OCLUtil.checkForErrors( handler );
|
|
|
Re: parsing OCL as a pure abstract syntax tree [message #540956 is a reply to message #54705] |
Thu, 17 June 2010 15:09   |
Eclipse User |
|
|
|
I found this page is too late,
because I meet the same problem for a few months ago,
and I still can not solve.
I write the code belowed:
============================================================ ===
OCL<?,EClassifier,?,?,?,?,?,?,?,Constraint,EClass,EObject> ocl;
ocl=OCL.newInstance(env);//EcoreEnvironmentFactory.INSTANCE) ;
OCLInput input=new OCLInput("inv: size>0");
List<Constraint> constraints=ocl.parse(input);
============================================================ ===
but I always got an SemanticException.
It says "expected parser to return PackageDeclarationCS but got InvCS"
if I changed input to "pre: size>0" or "post: size>0",
It says "expected parser to return PackageDeclarationCS but got PrePostOrBodyDeclCS"
And how can I use the parser result to continued my work ???
I am almost newer to OCL,
and found it can improve diagrams (either Ecore or UML),
but I am stuck at parse.
Please help me ! Please ~~~
[Updated on: Thu, 17 June 2010 15:10] by Moderator
|
|
|
Re: parsing OCL as a pure abstract syntax tree [message #540963 is a reply to message #540956] |
Thu, 17 June 2010 15:31   |
Eclipse User |
|
|
|
Hi Victorino
The OCL.parse method is for parsing OCL Documents.
See
help/topic/org.eclipse.ocl.doc/references/overview/parsingCo nstraints.html
in the OCL Developer Guide for
instrctions on using e.g. ocl.createHelper().createPostCondition();
If you're using OCL to enrich Ecore, ytou might want to check out
the OCLinEcore tutorial in the Helios OCL documentation and see
the much improved OCL and Ecore integration.
[Helios is officially released on June 23rd; RC4 is available now.]
Regards
Ed Willink
On 17/06/2010 20:09, Victorino wrote:
> I found this page is too late,
> because I meet the same problem for a few months ago,
> and I still can not solve.
> I write the code belowed:
> ============================================================ ===
> OCL<?,EClassifier,?,?,?,?,?,?,?,Constraint,EClass,EObject> ocl;
> ocl=OCL.newInstance(env);//EcoreEnvironmentFactory.INSTANCE) ;
> OCLInput input=new OCLInput("inv: size>0");
> List<Constraint> constraints=ocl.parse(input);
> ============================================================ ===
> but I always got an SemanticException.
> It says "expected parser to return PackageDeclarationCS but got InvCS"
>
> if I changed input to "pre: size>0",
> It says "expected parser to return PackageDeclarationCS but got
> PrePostOrBodyDeclCS"
>
> And how can I use the parser result to continued my work ???
> I am almost newer to OCL,
> and found it can improve diagrams (either Ecore or UML),
> but I am stuck at parse.
> Please help me ! Please ~~~
|
|
| | |
Re: parsing OCL as a pure abstract syntax tree [message #541057 is a reply to message #54705] |
Fri, 18 June 2010 04:06   |
Eclipse User |
|
|
|
I met a problem again,
Since I can got AST Tree, is it means I can evaluated ?
Like " perform " the query is satisfied or not ?
I wrote the code (even followed the guide still has some problem):
============================================================ =====
OCL<?,EClassifier,?,?,?,?,?,?,?,Constraint,EClass,EObject> ocl;
ocl=OCL.netInstance(EcoreEnvironmentFactory.INSTANCE);
OCLHelper<EClassifier,?,?,Constraint> helper=ocl.createOCLHelper();
Constraint invariant=helper.createInvariant("books->forAll(b1, b2 | b1 <> b2 implies b1.title <> b2.title)");
============================================================ =====
I got org.eclipse.ocl.ParserException as below:
org.eclipse.ocl.ParserException
at org.eclipse.ocl.internal.helper.OCLHelperImpl.propagate(OCLH elperImpl.java:403)
at org.eclipse.ocl.internal.helper.OCLHelperImpl.createInvarian t(OCLHelperImpl.java:218)
at pkg.Test.main(Test.java:135)
Caused by: java.lang.NullPointerException
at org.eclipse.ocl.AbstractEnvironment.getContextClassifier(Abs tractEnvironment.java:206)
at org.eclipse.ocl.parser.AbstractOCLAnalyzer.invOrDefCS(Abstra ctOCLAnalyzer.java:1405)
at org.eclipse.ocl.parser.OCLAnalyzer.parseInvOrDefCS(OCLAnalyz er.java:266)
at org.eclipse.ocl.internal.helper.HelperUtil.parseInvariant(He lperUtil.java:206)
at org.eclipse.ocl.internal.helper.OCLHelperImpl.createInvarian t(OCLHelperImpl.java:215)
... 1 more
Would you mind hint me where is going wrong?
And Would you write an simplest code example me,
only include created inv, pre, post and evaluate them.
Best Regards.
[Updated on: Fri, 18 June 2010 04:15] by Moderator
|
|
|
Re: parsing OCL as a pure abstract syntax tree [message #541079 is a reply to message #541057] |
Fri, 18 June 2010 05:23   |
Eclipse User |
|
|
|
Hi
The example works fine for me.
But I can reproduce your error by deleting:
helper.setContext(EXTLibraryPackage.Literals.LIBRARY);
Try getting the example to work before you improve it.
Regards
Ed Willink
On 18/06/2010 09:06, Victorino wrote:
> I met a problem again,
> Since I can got AST Tree, is it means I can evaluated ?
> Like " perform " the query is satisfied or not ?
> I wrote the code (even followed the guide still has some problem):
> ============================================================ =====
> OCLHelper<EClassifier,?,?,Constraint> helper=ocl.createOCLHelper();
> Constraint invariant=helper.createInvariant("books->forAll(b1, b2 | b1
> <> b2 implies b1.title <> b2.title)");
> ============================================================ =====
> I got org.eclipse.ocl.ParserException as below:
> org.eclipse.ocl.ParserException
> at org.eclipse.ocl.internal.helper.OCLHelperImpl.propagate(OCLH
> elperImpl.java:403)
> at org.eclipse.ocl.internal.helper.OCLHelperImpl.createInvarian
> t(OCLHelperImpl.java:218)
> at pkg.Test.main(Test.java:135)
> Caused by: java.lang.NullPointerException
> at org.eclipse.ocl.AbstractEnvironment.getContextClassifier(Abs
> tractEnvironment.java:206)
> at org.eclipse.ocl.parser.AbstractOCLAnalyzer.invOrDefCS(Abstra
> ctOCLAnalyzer.java:1405)
> at org.eclipse.ocl.parser.OCLAnalyzer.parseInvOrDefCS(OCLAnalyz
> er.java:266)
> at org.eclipse.ocl.internal.helper.HelperUtil.parseInvariant(He
> lperUtil.java:206)
> at org.eclipse.ocl.internal.helper.OCLHelperImpl.createInvarian
> t(OCLHelperImpl.java:215)
> ... 1 more
> Would you mind hint me where is going wrong?
> And Would you write an simplest code example me,
> only include created inv, pre, post and evaluate them.
|
|
| | | | | | |
Re: parsing OCL as a pure abstract syntax tree [message #541167 is a reply to message #54705] |
Fri, 18 June 2010 10:11   |
Eclipse User |
|
|
|
============================================================ ========
If you used genmodel then you have an XXXPackage that you can use
instead of ExtLibraryPackage.
============================================================ ========
Sorry, I don't understand and what is this means ?
I didn't find any XXXPackage in my genmodel.
Maybe I have to describing more detail.
I use that genmodel to drawing and definition new one.
(It means this genmodel is basic type, and use this genmodel you can drawing lots of diagram according to your mind. Like EMF done, you can drawing any kind of class diagram you want.)
Now, after I use EMF to generating the new diagram, it will have three addition folder on my "Package Explorer" automatically, there are "Parttimejob", "Parttimejob.diagram", "Parttimejob.edit".
These three is the basic component when I am drawing new diagram.
It's means I use the EMF definition some basic node,
and using these basis node to describing new diagram.
(Imagine Parttimejob define some basic "node", and use these node to drawing new class diagram, state diagram or some others diagram, and OCL expression is a EString on your newest drawing diagram, in this situation, how can I parsing single OCL expression ? Called "single", just because they are not in the *.ecore, and they don't know any source to helping OCLParser.)
[Updated on: Fri, 18 June 2010 10:16] by Moderator
|
|
|
Re: parsing OCL as a pure abstract syntax tree [message #541172 is a reply to message #541167] |
Fri, 18 June 2010 10:21   |
Eclipse User |
|
|
|
Hi
I am sorry but I have to prepare for a symposium next week.
Perhaps someone else could explain how genmodel produces a Package.
Regards
Ed Willink
On 18/06/2010 15:11, Victorino wrote:
> ============================================================ ========
> If you used genmodel then you have an XXXPackage that you can use
> instead of ExtLibraryPackage.
> ============================================================ ========
> Sorry, I don't understand and what is this means ?
> I didn't find any XXXPackage in my genmodel.
> Maybe I have to describing more detail.
> I use that genmodel to drawing and definition new one.
> (It means this genmodel is basic type, and use this genmodel you can
> drawing lots of diagram according to your mind. Like EMF done, you can
> drawing any kind of class diagram you want.)
> Now, after I use EMF to generating the new diagram, it will have three
> addition folder on my "Package Explorer" automatically, there are
> "Parttimejob", "Parttimejob.diagram", "Parttimejob.edit".
> Looks these three is the basic component when I am drawing new diagram.
> It's means I use the EMF definition some basic node,
> and using these basis node to describing new diagram.
> (Imagine EMF define some basic "node", and use these node to drawing new
> class diagram, state diagram or some others diagram, and OCL expression
> is a EString on your newest drawing diagram, in this situation, how can
> I parsing single OCL expression ? Called "single", just because they are
> not in the *.ecore.)
>
|
|
| |
Re: parsing OCL as a pure abstract syntax tree [message #541203 is a reply to message #541177] |
Fri, 18 June 2010 12:31   |
Eclipse User |
|
|
|
Hi, Victorino,
This means that you supplied the parser with text that consists only of
an OCL invariant constraint:
inv <name>: <expression>
omitting the context declaration that indicates to which classifier,
operation, or attribute the constraint pertains and (optionally) in
which package to resolve unqualified names.
Successful use of the OCL APIs with EMF-based models really requires:
(a) Familiarity with Ecore and the EMF run-time framework.
There are good introductory tutorials on EMF's homepage:
http://www.eclipse.org/modeling/emf/
(b) Reading the OCL Programmer's Guide on-line help that Ed
already indicated, and in particular, working through the
tutorial and the examples provided in the SDK
HTH,
Christian
On 18/06/10 10:43 AM, Victorino wrote:
> I have to say thank you at all.
> It's my problem,
> but you give me lots of suggestion.
> Any way,
> Wish your symposium everything going fine.
> And if you have time you can answer "expected parser to return
> PackageDeclarationCS but got InvCS" for me.
> Even a little bit hint, I will very appreciated.
> Best Regards
|
|
| |
Re: parsing OCL as a pure abstract syntax tree [message #541518 is a reply to message #541375] |
Mon, 21 June 2010 08:40   |
Eclipse User |
|
|
|
Hi, Victorino,
I'm afraid I don't understand your question.
The API methods that parse OCL text (in the OCL facade class and the
OCLHelper) return the ASTs, be they Constraints or OCLExpressions.
These are the results.
What kind of type information are you trying to extract from the parsed
results?
- the kind of OCL object that it is? The API distinguishes these
via the Constraint and OCLExpression interfaces
- the kind of context classifier? This is simply the type of the
"self" variable owned by the constraint
- the kind of expression that it is (e.g., operation call, let, etc.)?
These are distinguished as subtypes of the OCLExpression interface
Have you read the tutorial and other documentation? Have you played
with the examples to see how the API works? Have you used the debugger
to inspect a parsed OCLExpression to see what the structure of the AST
is like?
Cheers,
Christian
On 20/06/10 11:16 AM, Victorino wrote:
> Thx for helping,
> I will following your suggestion to do.
> And if I use analyzer to parse Concrete syntax,
> how can I get or managed the result?
> Like to reading token or distinguishing what type is this?
> Best Regard
|
|
| |
Re: parsing OCL as a pure abstract syntax tree [message #541569 is a reply to message #541543] |
Mon, 21 June 2010 11:17   |
Eclipse User |
|
|
|
Hi, Victorino,
You don't want to generate a Concrete Syntax Tree, but rather an
Abstract Syntax Tree. Perhaps you should try practising the tutorial in
addition to simply reading it. For example, if you emulate the code in
the "Parsing OCL Expressions" section of this page:
http://help.eclipse.org/galileo/topic/org.eclipse.ocl.doc/tu torials/oclInterpreterTutorial.html
then you may find something like what you need.
Christian
On 21/06/10 09:58 AM, Victorino wrote:
> I readed the document but I don't practiced it.
> Because I wanna parse the single sentence,
> not the whole ocl document,
> and ocl single sentence like "a>b" "a<100" (I tried it),
> and return CSTNode,
> but I can't traversal this CSTNode.
> Is there some way can help me ?
> Best Regards
|
|
| |
Re: parsing OCL as a pure abstract syntax tree [message #545498 is a reply to message #541569] |
Thu, 08 July 2010 01:06  |
Eclipse User |
|
|
|
Hi, Christian W. Damus.
I just tried your suggestion and visited that page again.
And I found some error happened in my program even I just copy and paste from that page,
that is simply code:
============================================================ ====
@SuppressWarnings({"unchecked","hiding"})
public static <EClassifier, Constraint, EClass, EObject> void main(String[] args) {
boolean valid;
org.eclipse.ocl.expressions.OCLExpression<EClassifier> query = null;
try {
// create an OCL instance for Ecore
OCL< EPackage,org.eclipse.emf.ecore.EClassifier,EOperation,EStruc turalFeature,EEnumLiteral,EParameter,org.eclipse.emf.ecore.E Object,CallOperationAction,SendSignalAction,org.eclipse.ocl. ecore.Constraint,org.eclipse.emf.ecore.EClass,org.eclipse.em f.ecore.EObject > ocl;
ocl=OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
// create an OCL helper object
OCLHelper< org.eclipse.emf.ecore.EClassifier,EOperation,EStructuralFeat ure,org.eclipse.ocl.ecore.Constraint > helper=ocl.createOCLHelper();
// set the OCL context classifier
helper.setContext(EXTLibraryPackage.Literals.WRITER);
query=(org.eclipse.ocl.expressions.OCLExpression<EClassifier >) helper.createQuery("self.books->collect(b : Book | b.category)->asSet()");
// record success
valid=true;
}
catch (ParserException e) {
// record failure to parse
valid=false;
System.err.println(e.getLocalizedMessage());
}
}
============================================================ ====
And in " helper.setContext(EXTLibraryPackage.Literals.WRITER); " that line,
It's always happened error and have a red line below it.
I know is missing package - " EXTLibraryPackage ",
but I don't wanna to use EXTLibraryPackage.
So, let me represented my question now:
How can I replace that package?
Does I created one package from my *.ecore at first?
And how to created?
I tried your way to passing this problem. (Trust me, I have worked this way more than one week.)
But I still stock at this " begin " problem.
If possible, please tell me how to do in detail.
Best Regard
Christian W. Damus wrote on Mon, 21 June 2010 11:17 | Hi, Victorino,
You don't want to generate a Concrete Syntax Tree, but rather an
Abstract Syntax Tree. Perhaps you should try practising the tutorial in
addition to simply reading it. For example, if you emulate the code in
the "Parsing OCL Expressions" section of this page:
http://help.eclipse.org/galileo/topic/org.eclipse.ocl.doc/tu torials/oclInterpreterTutorial.html
then you may find something like what you need.
Christian
On 21/06/10 09:58 AM, Victorino wrote:
> I readed the document but I don't practiced it.
> Because I wanna parse the single sentence,
> not the whole ocl document,
> and ocl single sentence like "a>b" "a<100" (I tried it),
> and return CSTNode,
> but I can't traversal this CSTNode.
> Is there some way can help me ?
> Best Regards
|
|
|
|
Goto Forum:
Current Time: Sun May 11 04:03:35 EDT 2025
Powered by FUDForum. Page generated in 0.07092 seconds
|