# Simple Constraints

Umple supports basic OCL-type Boolean constraints that limit what umple-generated mutator methods (set, add, etc.) and constructors can do. If a constraint is violated set methods will return false, and constructors will throw an exception.

Constraints are specified within square brackets. Simple constraints can refer to attributes and literals (numbers, strings). These can be compared using standard comparison operators such as < and >. Expressions can be conjoined by the and operator && or the or operator ||. An exclamation mark is the not operator. Parentheses can be used to adjust the standard order of operations.

Guards on state machines are also Boolean expressions and, like simple constraints, are also enclosed within square brackets.

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

### Example with simple constraints

// 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) Java {
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");
}
}

public static void main(String [ ] args) Python {
import Client
c = Client.Client(40)
for i in [-1, 10, 17, 18, 19, 50, 119, 120, 122, 1000]:
print("Trying to initialize age to", i)
print("  Success" if c.setAge(i) else "  FAILURE")
}
}

Load the above code into UmpleOnline

### Example with more operators

// 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

//negativeConstraint : ( ! | not ) ( [[constraintName]] | [[constraint]] ) fixed issue1536 'Umple does not parse properly the guards'
negativeConstraint :
( ! | [!constraintNegSymbol:not\s+]
)
( [[constraintName]] | [[constraint]]
)

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

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

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|<]