Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

CustomizingOclEnvironments

Implicitly define a variable named 'help' in the context of every OCL expression that is parsed. Docu follows

public static class MyEnvironmentFactory extends AbstractEnvironmentFactory {
  public EvaluationEnvironment createEvaluationEnvironment() {
     EvaluationEnvironment evalEnv = new EvalEnvironment();
     evalEnv.add("help", OCLHelperFactory.eINSTANCE.createHelper());
     return evalEnv;
  }
  protected Environment createEnvironment(EPackage packageContext) {
     return new MyEnvironment(packageContext);
  }
  protected EClassifier asEClassifier(Object context) {
     return (EClassifier) context;
  }
  protected EOperation asEOperation(Object operation) {
     return (EOperation) operation;
  }
  protected EStructuralFeature asEStructuralFeature(Object property) {
     return (EStructuralFeature) property;
  }
  public Environment createEnvironment(Environment parent) {
     return new MyEnvironment(parent);
  }
}

private static class MyEnvironment extends EcoreEnvironment {
  public MyEnvironment(EPackage packageContext) {
     super(packageContext);
     init();
  }
  private void init() {
     Variable vdcl = ExpressionsFactory.eINSTANCE.createVariable();
     vdcl.setName("help");
     vdcl.setType(TypeUtil.resolveType(this, OCLHelperPackage.eINSTANCE.getEClassifier("Helper")));
     this.addElement("help", vdcl, true);
  }
  public MyEnvironment(Environment parent) {
     super(parent);
     init();
  }
  @Override
  protected void collectStates(EClassifier owner, List pathPrefix, List states) {
     super.collectStates(owner, pathPrefix, states);
  }
  @Override
  public EClassifier lookupPathName(List names) {
     if (names.get(0).equals("Helper")) {
        return OCLHelperPackage.eINSTANCE.getEClassifier((String) names.get(0));				      }
     return super.lookupPathName(names);
  }
}

IOCLHelper helper = HelperUtil.createOCLHelper(new MyEnvironmentFactory());
helper.setContext(OrganisationPackage.eINSTANCE.getOrganisation());
System.out.println(helper.evaluate(org, "help.testmethod(self.name)"));

--Chris.lenz.uibk.ac.at 08:39, 10 October 2006 (EDT)

Back to the top