0

I am trying to change the OCL context from the Java application. The present context is root node. ARPackage is a node under the root node and I am trying to set the context to this node. Sample code is given below.

   public void getPortInterfaces(Resource srcResource) throws ParserException {
    OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance();
    OCL.Helper helper = ocl.createOCLHelper();

    IQueryResult OEMPackage = GetIQueryResult(srcResource,"self->forAll(b|b.shortName='OEM')","ARPackage");
    if(null != OEMPackage){
        for (EObject obj : OEMPackage.getEObjects()) {
            String shortName = ((ARPackage)obj).getShortName();
            System.out.println("shortName: "+shortName);

            if(shortName.equals("OEM")){
                helper.setContext(Autosar40Package.eINSTANCE.getEClass("ARPackage"));

                EObjectCondition condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(ocl.getEnvironment(),
                        "context ARPackage inv:self.shortName<> 'null'", Autosar40Package.eINSTANCE.getEClass("ARPackage"));

                SELECT statement = new SELECT(SELECT.UNBOUNDED, false, new FROM((obj.eResource()).getContents()),new WHERE(condition), new NullProgressMonitor());
                IQueryResult results = statement.execute();
                if(null != results){
                    for (EObject obj1 : results.getEObjects()) {
                        System.out.println(((ARPackage)obj1).getShortName());
                    }
                }
            }
        }
    }
}

private IQueryResult GetIQueryResult(Resource srcResource,String arg0,String arg1)
{
    OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance();

    EObjectCondition condition;
    IQueryResult results = null;
    try {
        condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(ocl.getEnvironment(),
                arg0, Autosar40Package.eINSTANCE.getEClass(arg1));
        SELECT statement = new SELECT(SELECT.UNBOUNDED, false, new FROM((srcResource).getContents()),new WHERE(condition), new NullProgressMonitor());
        results = statement.execute();

    } catch (ParserException e) {
        e.printStackTrace();
    }       
    return results;
}

Here after settings context statement.execute() still returns the results corresponding to the root node itself. I believe the way in which I am setting the context is incorrect.

helper.setContext(Autosar40Package.eINSTANCE.getEClass("ARPackage"));   

Is this the right way to set the context?

FaisalM
  • 724
  • 5
  • 18

1 Answers1

0

You appear to be using EMF Query which is obsolete. Once upon a time it represented a way to obfuscate OCL queries behind an SQL-like API. With the advent of OCLinEcore, EMF Query has no use. I doubt that you will find anyone who can help you with it. I suggest you study the examples in the Eclipse OCL documentation.

Ed Willink
  • 1,205
  • 7
  • 8
  • Ok I will check the OCL documentation. For setting the context, i used the OCL API setContext(). Do you see any problem with it? – FaisalM Mar 18 '19 at 12:32
  • 1
    The OCL API is for OCL evaluation. I cannot comment on how it interacts with EMF Query. – Ed Willink Mar 18 '19 at 18:14