/** * Checks body and languages list of an opaque expression. * * <p>It returns <code>true</code> if both lists have the same size. It returns <code>false</code> * if one of the list was bigger than the other one. In this latter case, one of the list was * corrected, ie enough elements where added in the list * * @param opaqueExpression the opaque expression to check * @return <code>true</code> if both lists already had the same size, <code>false</code> in other * cases. */ public static boolean checkAndCorrectLists( org.eclipse.uml2.uml.OpaqueExpression opaqueExpression) { // both lists, languages and bodies, should have the same size final int bodySize = opaqueExpression.getBodies().size(); final int languageSize = opaqueExpression.getLanguages().size(); // check both size // if equals, lists are supposed synchronized, it is ok // if less body than languages, add bodies // if more body, add enough languages if (bodySize == languageSize) { return true; } else { final int difference = languageSize - bodySize; if (difference > 0) { // more languages strings than body strings, add enough bodies for (int i = 0; i < difference; i++) { opaqueExpression.getBodies().add(""); } } else { // more body strings than language strings, add enough languages for (int i = 0; i < (-difference); i++) { opaqueExpression.getLanguages().add(""); } } // lists had to be modified, return false... return false; } }
/** * @see * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) * @param event * @return null * @throws ExecutionException */ public Object execute(ExecutionEvent event) throws ExecutionException { EObject selectedObject = getSelectedElement(); // test if this is a constraint if (selectedObject instanceof Constraint) { Constraint constraint = (Constraint) selectedObject; // test if this is an opaqueExpression if (constraint.getSpecification() instanceof OpaqueExpression) { OpaqueExpression opaqueExpression = ((OpaqueExpression) constraint.getSpecification()); // look for the good body named OCL int indexOfOCLBody = -1; for (int i = 0; i < opaqueExpression.getLanguages().size() && indexOfOCLBody == -1; i++) { if (opaqueExpression.getLanguages().get(i).equals("OCL")) { indexOfOCLBody = i; } } if (indexOfOCLBody != -1) { try { OCLEvaluationView view = (OCLEvaluationView) HandlerUtil.getActiveWorkbenchWindow(event) .getActivePage() .showView(OCLEvaluationView.ID); view.compute(constraint.getContext(), opaqueExpression.getBodies().get(indexOfOCLBody)); } catch (PartInitException e) { e.printStackTrace(); } } } } return null; }
/** @generated */ public void init_Constraint_2008(Constraint instance) { try { OpaqueExpression newInstance_0_0 = UMLFactory.eINSTANCE.createOpaqueExpression(); instance.setSpecification(newInstance_0_0); Object value_0_0_0 = UMLOCLFactory.getExpression(13, UMLPackage.eINSTANCE.getOpaqueExpression(), null) .evaluate(newInstance_0_0); if (value_0_0_0 instanceof Collection) { newInstance_0_0.getLanguages().clear(); newInstance_0_0.getLanguages().addAll(((Collection) value_0_0_0)); } else { newInstance_0_0.getLanguages().add((String) value_0_0_0); } Object value_0_0_1 = UMLOCLFactory.getExpression(14, UMLPackage.eINSTANCE.getOpaqueExpression(), null) .evaluate(newInstance_0_0); if (value_0_0_1 instanceof Collection) { newInstance_0_0.getBodies().clear(); newInstance_0_0.getBodies().addAll(((Collection) value_0_0_1)); } else { newInstance_0_0.getBodies().add((String) value_0_0_1); } } catch (RuntimeException e) { UMLDiagramEditorPlugin.getInstance() .logError("Element initialization failed", e); // $NON-NLS-1$ } }
public OpaqueExpression createExpression(String language, String body) { // TODO: We can choose between something which matches UML 1.4 in name // or something that matches in functionality. We've chosen // functionality for now, but this will create a name conflict during // the migration process. - tfm OpaqueExpression expression = UMLFactory.eINSTANCE.createOpaqueExpression(); expression.getLanguages().add(language); expression.getBodies().add(body); return expression; }
/** * sets the body for an OpaqueExpression for the given language. * * <p>If the language was already defined, it replaces the corresponding body. If the language was * not already defined, it adds it to the list of languages and adds the corresponding body. * * <p>A utility method, {@link * OpaqueExpression#checkAndCorrectLists(org.eclipse.uml2.uml.OpaqueExpression)} is used to * correct the language and body lists. * * @param opaqueExpression the opaque expression to edit. * @param language the language in which the body is written * @param body the body to save */ public static void setBodyForLanguage( org.eclipse.uml2.uml.OpaqueExpression opaqueExpression, String language, String body) { // checks both lists by size checkAndCorrectLists(opaqueExpression); // checks if language exists, if not, creates one if (!opaqueExpression.getLanguages().contains(language)) { opaqueExpression.getLanguages().add(language); opaqueExpression.getBodies().add(body); } else { // retrieve the index of the given language in the opaque Expression int index = opaqueExpression.getLanguages().indexOf(language); // sets the body at the given index in the list of bodies. opaqueExpression.getBodies().set(index, body); } }
protected static void addOcl() { Operation find = example .getConstructionCase() .createOwnedOperation( "findRoomPlan", list("nameToFind"), list((Type) example.getType("String"))); Parameter result = find.createOwnedParameter("result", example.getRoomPlans()); result.setDirection(ParameterDirectionKind.RETURN_LITERAL); result.setUpper(-1); result.setIsUnique(false); OpaqueExpression ocl = (OpaqueExpression) find.createBodyCondition("body") .createSpecification( "spec", example.getRoomPlans(), UMLPackage.eINSTANCE.getOpaqueExpression()); ocl.getLanguages().add("ocl"); ocl.getBodies().add("housePlan.wallPlans->collect(roomPlans->any(rp|rp.name=nameToFind))"); }
/** * Returns the body for an OpaqueExpression for the given language * * @param opaqueExpression the opaque expression to edit. * @param language the language in which the body is written * @return the body for the given language or the empty string if the language was not found */ public static String getBodyForLanguage( org.eclipse.uml2.uml.OpaqueExpression opaqueExpression, String language) { String body = ""; if (language == null) { if (!opaqueExpression.getBodies().isEmpty()) { body = opaqueExpression.getBodies().get(0); } } else { // retrieve the index of the given language in the opaque Expression int index = opaqueExpression.getLanguages().indexOf(language); if (index != -1) { // language found. return the corresponding body in the bodies list. // List should be synchronized, ie having the same size, but be sure... if (index < opaqueExpression.getBodies().size()) { body = opaqueExpression.getBodies().get(index); } } } return body; }
/** * Sets the value of a specification, using a string value * * @param specification the value specification to update * @param value the value to set */ public static void restoreSpecificationValue(ValueSpecification specification, String value) { if (value == null) { return; } switch (specification.eClass().getClassifierID()) { case UMLPackage.LITERAL_STRING: restoreLiteralString((LiteralString) specification, value); break; case UMLPackage.LITERAL_BOOLEAN: restoreLiteralBoolean((LiteralBoolean) specification, value); break; case UMLPackage.LITERAL_INTEGER: restoreLiteralInteger((LiteralInteger) specification, value); break; case UMLPackage.LITERAL_UNLIMITED_NATURAL: restoreLiteralUnlimitedNatural((LiteralUnlimitedNatural) specification, value); break; case UMLPackage.LITERAL_NULL: restoreLiteralNull((LiteralNull) specification, value); break; case UMLPackage.OPAQUE_EXPRESSION: OpaqueExpression exp = (OpaqueExpression) specification; if (!exp.getLanguages().isEmpty()) { restoreOpaqueExpression( (org.eclipse.uml2.uml.OpaqueExpression) specification, exp.getLanguages().get(0), value); } else { restoreOpaqueExpression((org.eclipse.uml2.uml.OpaqueExpression) specification, value); } break; default: { break; } } }
public static TransitionDetails transitionDetails(Transition trans) { String condition = ""; TransitionDetails transition = new TransitionDetails(); /// ****** guard condition *********************** EList<Constraint> ownedRules = trans.getOwnedRules(); com.mbe.umlce.dataobjects.stateMachine.Guard guard = new com.mbe.umlce.dataobjects.stateMachine.Guard(); for (Constraint Rule : ownedRules) { ValueSpecification Specifications = Rule.getSpecification(); guard.setName(Rule.getLabel()); OpaqueExpression expr = (OpaqueExpression) Specifications; condition += expr.getBodies().toString(); guard.setBody(removeSquareBrackets(condition)); // condition = expr.getLanguages(); // System.out.println("Condition : "+condition); } transition.setGuard(guard); Effect effect = new Effect(); String methodBody = null; if ((OpaqueBehavior) trans.getEffect() != null) { methodBody = ""; effect.setName(trans.getEffect().getLabel()); // methodBody += ("\nif ( "+removeSquareBrackets(condition)+" ){\n"); methodBody += removeSquareBrackets(((OpaqueBehavior) trans.getEffect()).getBodies().toString()); // System.out.println("Effect : "+methodBody); effect.setBody(methodBody); } // Trigger yet to read// // transition.setEffect(effect); /* TimeEvent timeEvent=null; ChangeEvent changeEvent = null; */ // Triggers reading CallEvent callEvent = null; Operation operation = null; EList<Trigger> trigger = trans.getTriggers(); com.mbe.umlce.dataobjects.stateMachine.Trigger trig = new com.mbe.umlce.dataobjects.stateMachine.Trigger(); for (Trigger triger : trigger) { // System.out.println("Triger : "+triger.getQualifiedName()); // if(triger.getEvent().getName().contains("CallEvent")){ callEvent = (CallEvent) (triger.getEvent()); operation = callEvent.getOperation(); if (operation != null) { // System.out.println("Operation : "+operation.getLabel()); EList<Parameter> parameters = operation.getOwnedParameters(); ArrayList<String> param = new ArrayList<>(); ArrayList<String> paramClass = new ArrayList<>(); for (Parameter pm : parameters) { param.add(pm.getLabel()); paramClass.add(pm.getClass().getName()); // System.out.println("Parameters : "+pm.getLabel()); } trig.setOpName(operation.getLabel()); trig.setOpParameters(param); trig.setParametersClass(paramClass); } } transition.setTrigger(trig); // } return transition; }
/** * Get a string representing of a ValueSpecification * * @param specification */ public static String getSpecificationValue(ValueSpecification specification) { String value = ""; // $NON-NLS-1$ if (specification != null && specification.eClass() != null) { switch (specification.eClass().getClassifierID()) { case UMLPackage.LITERAL_STRING: value = ((LiteralString) specification).getValue(); break; case UMLPackage.LITERAL_BOOLEAN: value = Boolean.toString(((LiteralBoolean) specification).booleanValue()); break; case UMLPackage.LITERAL_INTEGER: value = Integer.toString(((LiteralInteger) specification).getValue()); break; case UMLPackage.LITERAL_UNLIMITED_NATURAL: value = Integer.toString(((LiteralUnlimitedNatural) specification).getValue()); if ("-1".equals(value)) { // $NON-NLS-1$ value = UNLIMITED_KEYWORD; // $NON-NLS-1$ } break; case UMLPackage.LITERAL_NULL: break; case UMLPackage.OPAQUE_EXPRESSION: OpaqueExpression exp = (OpaqueExpression) specification; if (!exp.getLanguages().isEmpty()) { value = OpaqueExpressionUtil.getBodyForLanguage( exp, exp.getLanguages().get(0)); // $NON-NLS-1$ } break; case UMLPackage.INSTANCE_VALUE: value = ((InstanceValue) specification).getInstance().getName(); break; case UMLPackage.EXPRESSION: Expression expr = (Expression) specification; if (!expr.getOperands().isEmpty()) { StringBuffer operandsBuff = new StringBuffer(expr.getSymbol()); operandsBuff.append("("); int initialLength = operandsBuff.length(); for (ValueSpecification operand : expr.getOperands()) { if (operandsBuff.length() > initialLength) { operandsBuff.append(","); } operandsBuff.append(getSpecificationValue(operand)); } operandsBuff.append(")"); value = operandsBuff.toString(); } else { value = expr.getSymbol(); } break; case UMLPackage.STRING_EXPRESSION: // TODO break; case UMLPackage.DURATION: Duration durationExpr = (Duration) specification; if (durationExpr.getExpr() != null) { value = getSpecificationValue(durationExpr.getExpr()); } else if (durationExpr.getObservations().size() > 0) { value = durationExpr.getObservations().get(0).getName(); } break; case UMLPackage.TIME_EXPRESSION: TimeExpression timeExpr = (TimeExpression) specification; if (timeExpr.getExpr() != null) { value = getSpecificationValue(timeExpr.getExpr()); } else if (timeExpr.getObservations().size() > 0) { value = timeExpr.getObservations().get(0).getName(); } break; case UMLPackage.INTERVAL: case UMLPackage.TIME_INTERVAL: case UMLPackage.DURATION_INTERVAL: Interval interval = (Interval) specification; String min = getSpecificationValue(interval.getMin()); String max = getSpecificationValue(interval.getMax()); value = String.format(INTERVAL_FORMAT, min, max); break; default: { break; } } } return value; }