Page ‹#› 2004 John MylopoulosConstraint Language -- 1Information Systems Analysis and Design CSC340The Constraint Language (CL)The Constraint Language (CL)Sets and BagsSets and BagsSelectors and InvariantsSelectors and InvariantsExamplesExamplesPre-/Post-ConditionsPre-/Post-ConditionsXIII. A Constraint LanguageXIII. A Constraint Languagefor UMLfor UML 2004 John MylopoulosConstraint Language -- 2Information Systems Analysis and Design CSC340The Constraint LanguageThe Constraint LanguageI Some constraints can be adequately expressedgraphically (e.g., multiplicity of an association).I Some can not. For example, constraints withinoperation specifications (pre/post-conditions)I The Object Constraint Language (OCL)[Warmer99] is a formal language for specifyingconstraints for UML class diagrams.I We present a syntactic variant of a subset of OCL;let’s call it Constraint Language (CL, for short.) 2004 John MylopoulosConstraint Language -- 3Information Systems Analysis and Design CSC340Objects, Bags and SetsObjects, Bags and SetsI Objects are instances of classes, including predefinedclasses Integer, Number and String.I Bags include zero or more objects and/or sets and/or otherbags, possibly with duplicates, and no assumed ordere.g., {tom, maria, tom, sara, maria},{tom,{maria,tom},{}}I Two bags are equal iff they have the same number of thesame elements:{tom, maria, tom, sara, maria} ≠ {tom,maria,sara}I Sets are bags with no duplicates. 2004 John MylopoulosConstraint Language -- 4Information Systems Analysis and Design CSC340CL ExpressionsCL ExpressionsI CL expressions define constraints (or invariants) forclasses, which must be try for all their instances e.g., “every employee earns less than his CEO”I CL expressions also define conditions that must be truebefore an operation can be executed (preconditions) andconditions that must be true after (postconditions) e.g., “Before withdrawCash(acct,amount), it must bethat acct.balance ≥ amount” (precondition) Or, “After withdrawCash(acct,amount) is executed, itmust be thatacct.balance(new) = acct.balance(old) - amount” (postcondition) 2004 John MylopoulosConstraint Language -- 5Information Systems Analysis and Design CSC340Contexts for CL ExpressionsContexts for CL ExpressionsI Every expression has a context which is the classwithin which it is defined.I The special identifier self refers to an instance of theclass within which it appears.I The most basic CL expressions are called selectorsand they return an object or a bag. 2004 John MylopoulosConstraint Language -- 6Information Systems Analysis and Design CSC340 PersonageincomesexSelectors in ActionSelectors in Action0..1employmentself.addr (or just addr) -- returns addr of a particular company;self.employment -- returns the set of all employees;self.employee -- returns the set of all employees as well;self.president -- returns the singleton set of presidents;self.employment -- set of all employers of a person;self.employer -- set of all employers of a person.0..*employee CompanynameaddrsalesemployerExpressions appearing here0..11presidentPage ‹#› 2004 John MylopoulosConstraint Language -- 7Information Systems Analysis and Design CSC340nil and emptynil and emptyI When an attribute attr has no value for object obj, thenobj.attr returns nil (no value).I When there are no associated objects to an an object objthrough association assoc (or role rl), then obj.assoc andobj.rl return the empty bag {} or empty.I Note, nil ≠ {}.I Moreover, {nil} = {}, {Sara,nil, nil} = {Sara} etc.I This means that if Sara.age = nil, George.age = nil, then{Sara.age,George.age} = {} 2004 John MylopoulosConstraint Language -- 8Information Systems Analysis and Design CSC340Lin:PersonemploymentpresidentRobert:PersonIBM:CompanyJianguo:PersonSara:PersonCIBC:Companyemploymentemploymentemployment 2004 John MylopoulosConstraint Language -- 9Information Systems Analysis and Design CSC340Associations are Sets of TuplesI You can think of associations as sets (no duplicates!) oftuples.Sara.employment = {IBM}CIBC.employment = {Robert,Lin,Jianguo}CIBC.employee = {Robert,Lin,Jianguo}CIBC.employer -- syntax error!Lin:PersonemploymentpresidentRobert:PersonIBM:CompanyJianguo:PersonSara:PersonCIBC:Companyemploymentemploymentemploymentemployment(Sara,IBM)(Robert,CIBC)(Lin,CIBC)(Jianguo,CIBC)(Lin,CIBC) president(Jianguo,CIBC)X 2004 John MylopoulosConstraint Language -- 10Information Systems Analysis and Design CSC340 PersonageincomesexSelectors for Symmetric AssociationsSelectors for Symmetric Associationswife0..10..1husbandself.age (or just age) -- returns the age of a particular person;self.husband -- returns the set of all husbands;self.wife -- returns the set of all wives;self.spouse -- returns the set of all husbands and wives;self.friend -- returns the set of all friends.0..*0..*friendspouse 2004 John MylopoulosConstraint Language -- 11Information Systems Analysis and Design CSC340Lin:PersonfriendwifefriendfriendRobert:PersonMaria:PersonJianguo:PersonSara:Personfriend(Robert,Lin)(Jianguo,Lin)(Lin,Maria)husbandspouse(Robert,Sara)Sara.spouse = {Robert}Lin.friend = {Robert,Jianguo,Maria}Maria.friend = {Lin} 2004 John MylopoulosConstraint Language -- 12Information Systems Analysis and Design CSC340Applying Selectors to BagsApplying Selectors to BagsIbag.attrName = ∪ obj.attrNameIbag.assocName = ∪ obj.assocNameIbag.roleName = ∪ obj.roleNameI For example, supposeSara.friend = {Robert,Lin}Jianguo.friend = {Robert,Maria}{Sara,Jianguo}.friend = {Robert,Lin,Robert,Maria}{Sara,Jianguo,Sara}.friend = {Robert,Lin,Robert,Maria,Robert,Lin}obj ∈∈∈∈ bagobj ∈∈∈∈ bagobj ∈∈∈∈ bagPage ‹#› 2004 John MylopoulosConstraint Language -- 13Information Systems Analysis and Design CSC340Composition of SelectorsComposition of SelectorsI Selectors can be composed:self.sel1.sel2 ≡ (self.sel1).sel2 means that we take thevalue of self.sel1 (either a single value or a bag) andwe apply to it sel2.I For example, self.friend.income returns the bag of allincome values of objects in the bag self.friende.g., if self.friend = {Tom,Maria,Sara} and their incomesare respectively $16K, $19K and $16K, thenself.friend.income = {$16K,$19K,$16K} 2004 John MylopoulosConstraint Language -- 14Information Systems Analysis and Design CSC340
View Full Document