uOttawaUniversity of Ottawa - Canadas University
list of dots

Umple User Manual    [Previous]   [Next]   

Loading

Simple Constraints

Umple supports basoc OCL-type constraints that limit what umple-generated mutator methods (set, add, etc.) and constructors can do.

Constraints are specified within square brackets, in the same manner as guards on state machines.

Additional capabilities are being developed in Umple to allow other types of constraints.

Example

// The following demonstrates two simple constraints limiting the range of age
// The test code demonstrates that this works as expected.
class Client {
  const Integer minAge =18;
  Integer age;
  [age >= minAge]
  [age <= 120]
  
  public static void main(String [ ] args) {
    Client c = new Client(40);
    for (int i: new int[] {-1,10,17,18,19,50,119,120,122,1000}) {
       System.out.println("Trying to initialize age to "+i);
       System.out.println(c.setAge(i) ? "  Success" : "  FAILURE");
    }
  }
}


      

Load the above code into UmpleOnline

 

Another Example

// Example with a few more operators
class X {
  Integer i;
  Integer j;
  [i > j]
  [i < 5 && j > 0]
  [! (i == 10)]
}

      

Load the above code into UmpleOnline

 

Syntax


// A constraint is an expression optionally be surrounded in round brackets or negated
constraint- : [[constraintBody]] [[linkingOp]]? | [[constraintExpr]] [[linkingOp]]?

negativeConstraint : ( ! | not ) ( [[constraintName]] | [[constraint]] )

// A constraint body is a constraint expression (possibly with a linking operator such as && or ||).
constraintBody : ( [[constraint]] )

linkingOp- : ( [=andOp:and|&&|&]
    | [[equalityOperator]]
    | [!orOp:(or|\Q||\E)] ) [[linkingOpBody]]

constraintExpr- : [[statemachineExpr]]
    | [[stringExpr]]
    | [[boolExpr]]
    | [[numExpr]]
    | [[associationExpr]]
    | [[genExpr]]
    | [[loneBoolean]]
    | { [!fakeContraint:[^\n]+] }

//must be string
stringExpr : [[stringExprOperator]] | [[stringExprPlain]]

//basically the "other" catagory, contains everything that can be equal to something else
genExpr : [[constraintVariable]] [[equalityOperator]] [[constraintVariable]]

//for floats, doubles and ints
numExpr : [[numberExpression1]]
    | [[numberExpression2]]
    | [[numberExpression3]]
    | [[numberExpression4]]

ordinalOp- : [=greaterOp:greater|>=|=>|=>=]
    | [=lessOp:less|<=|=<|=<=]
    | [=moreOp:larger|>]
    | [=smallerOp:smaller|<]