Reasoning through a restock contract

An Inventory object is linked through the role products to three Product objects. Each Product has stock : Integer and maxStock : Integer. Product p has stock 37 and maxStock 120, product q has stock 95 and maxStock 100, and product r has stock 82 and maxStock 90. The operation restock has this contract:


Plain text
context Inventory::restock(item : Product, qty : Integer)
pre:  self.products->includes(item) and qty > 0 and
      item.stock + qty <= item.maxStock
post: item.stock = item.stock@pre + qty and
      self.products->excluding(item)->forAll(x | x.stock = x.stock@pre)


Find four values, each as a whole number of units: the largest qty for which the precondition of restock(p, qty) holds; then, for a valid call restock(p, 50), the value of item.stock@pre when the postcondition is evaluated, the value of p.stock after the call, and the value of self.products.stock->sum() after the call.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Object Constraint Language (OCL) Questions: