设置OCL上下文

时间:2019-03-18 11:38:25

标签: java eclipse eclipse-emf ocl

我正在尝试从Java应用程序更改OCL上下文。当前上下文是根节点。 ARPackage是根节点下的一个节点,我正在尝试将上下文设置为此节点。示例代码如下。

   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;
}

在此之后,设置上下文statement.execute()仍返回与根节点本身相对应的结果。我认为我设置上下文的方式不正确。

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

这是设置上下文的正确方法吗?

1 个答案:

答案 0 :(得分:0)

您似乎正在使用已淘汰的EMF查询。曾几何时,它代表了一种在类似SQL的API之后混淆OCL查询的方法。随着OCLinEcore的出现,EMF查询已无用。我怀疑您会找到任何可以帮助您的人。我建议您研究Eclipse OCL文档中的示例。