Re: OCL tool



Re: OCL tool

From: Kyriakos Anastasakis <spam_at_anastasakis.net>
Date: Thu, 21 May 2009 02:03:36 +0300
Message-ID: <4A148C48.4020403@anastasakis.net>
Hi Jakub,

    In the past I have successfully used  the Kent OCL library to parse 
OCL (i.e. to generate a parse tree of a statement). There is a detailed 
technical report by Akehurst et al. entitled "OCL 2.0: Implementing the 
Standard" that explains extensively how the tool works.

    I had already developed some test Java code that uses the Kent 
library to Parse OCL, so I tried it with a random def statement I found 
on the web and it seems to work. My test code is at the bottom of this 
message. As far as I know the Kent OCL library does not provide a GUI, 
but it was my perception that OSLO is based on the Kent OCL library and 
additionally provides a GUI for parsing OCL.

Hope this helps,
    Kyriakos

<test code>
import uk.ac.kent.cs.ocl20.syntax.ast.contexts.ClassifierContextDeclAS;
import uk.ac.kent.cs.ocl20.syntax.ast.contexts.ConstraintAS;
import uk.ac.kent.cs.ocl20.syntax.ast.contexts.OperationContextDeclAS;
import uk.ac.kent.cs.ocl20.syntax.ast.contexts.PackageDeclarationAS;
import uk.ac.kent.cs.ocl20.syntax.parser.OclParser;
import uk.ac.kent.cs.ocl20.syntax.parser.OclParserImpl;

public class OCLParserAS {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String ocl = "context Component def: inputConnectors(): 
Set(Connector) = Connector.allInstances()->select(c | c.target = 
self)->asSet()";
       
        OclParser oclp;
       
        oclp = new OclParserImpl();
       
        try {
            ConstraintAS cas = OCLParserAS.getConstraintAS(ocl, oclp);
            // DEF
             System.out.println(cas.getDefOperation().getName());
             System.out.println(cas.getDefOperation().getType());
             System.out.println(cas.getBodyExpression());
        } catch (Exception e) {           
            e.printStackTrace();
        }
       
       
       
    }

   
public static ConstraintAS getConstraintAS(String ocl, OclParser oclp_) 
throws Exception{
       
        if (oclp_ == null){
            throw new Exception
                ("ERROR. Can not initialise OCL processor!");           
        }
       
       
        PackageDeclarationAS pdas = oclp_.parse(ocl);
       
        if (pdas==null){
                throw new Exception("Can not parse expression: " + ocl + 
System.getProperty("line.separator") +
                        " Please make sure it is well-formed.");           
        }
       
        if (pdas.getContextDecls().get(0) instanceof 
ClassifierContextDeclAS){
            ClassifierContextDeclAS ccdAS = (ClassifierContextDeclAS) 
pdas.getContextDecls().get(0);
            ConstraintAS cas = (ConstraintAS) ccdAS.getConstraints().get(0);
            return cas;
        }
        else if (pdas.getContextDecls().get(0) instanceof 
OperationContextDeclAS){
            OperationContextDeclAS opcAS = (OperationContextDeclAS) 
pdas.getContextDecls().get(0);
            ConstraintAS cas = (ConstraintAS) opcAS.getConstraints().get(0);
            return cas;
        }         
        else
            throw new Exception();
    }
}
</test code>
Received on Thu 21 May 2009 - 00:04:08 BST