Features¶
OCL support¶
Collection of classes and functions to ease the translation of OCL expressions into python.
- = -> ==
- x.isUndefined() -> isUndefined(x)
- x.isUndefined() -> isUndefined(x)
Number¶
- x.abs() -> abs(x)
- x.min(y) -> min(x,y)
- x.max(y) -> max(x,y)
Integer¶
- div /
- mod %
Real¶
- x.floor() -> floor(x)
- x.round() -> round(x)
String¶
- s1.size() -> len(s1)
- s1.contact(s2) -> s1+s2
- s1.substring(i1,i2) -> s1[i1,i2] TODO: check
- s1.toUpper() -> s1.upper()
- s1.toLower() -> s1.lower()
Boolean¶
- true -> True
- false -> False
- xor -> ^ but it must be applied between boolean
- implies -> |implies|
- if c then a else b endif -> ( a if c else b )
Enumeration¶
- E::x -> E.x
Collection¶
- coll C(coll)
- coll->op(...) C(coll).op(...)
- Set{ ... } -> Set( ... )
- Bag{ ... } -> Bag( ... )
- OrderedSet{ ... } -> OrderedSet( ... )
- Sequence{ ... } -> Seq( ... )
- Sequence {1..5, 10..20} -> Seq.new(range(1,5)+range(10,20))
UML based features¶
- oclIsNew -> Not available. Can be use only with post condition
- oclAsType -> Not necessary thanks for dynamic typing in python.
Examples¶
This modules provides most core features of PyAlaOCL. It defines the various functions and classes that constitutes the OCL library.
- pyalaocl.isUndefined(value)[source]¶
Indicates if the given parameter is undefined (None) or not. :param value: any kind of value. :type value: any :return: True if the value is None. :rtype: bool
- Examples:
>>> print isUndefined(3) False >>> print isUndefined(None) True
- pyalaocl.oclIsKindOf(value1, value2)[source]¶
Evaluates to True if the type of the value is exactly the type given as a second parameter is an instance of type or one of its subtypes directly or indirectly. Use the method oclIsTypeOf if you want to check if a value is exactly of a given type.
Parameters: - value (Any) – A scalar value, a collection or an object.
- aType (type) – The type to check the value against (e.g. int, float, str, unicode, bool or a class)
Returns: True if value is compatible with the type aType.
Return type: bool
- Examples:
>>> print oclIsKindOf(3,int) True >>> print oclIsKindOf("3",int) False >>> print oclIsKindOf(2.5,float) True >>> print oclIsKindOf("hello",basestring) True >>> print oclIsKindOf(True,bool) True >>> class Person(object): pass >>> print oclIsKindOf(Person(),Person) True >>> print oclIsKindOf(Person(),object) True >>>
- pyalaocl.oclIsTypeOf(value1, value2)[source]¶
Return True if the type of the value is exactly the type given as a second parameter. This function does not take into account sub-typing relationships. If this is what is intended, use oclIsKindOf instead.
Parameters: - value (Any) – A scalar value, a collection or an object.
- aType (type) – The type to check the value against (e.g. int, float, str, unicode, bool or a class)
Returns: True if value is compatible with the type aType.
Return type: bool
- Examples:
>>> print oclIsTypeOf("hello",str) True >>> print oclIsTypeOf("hello",basestring) False >>> print oclIsTypeOf(u"çüabè",unicode) True
- class pyalaocl.Collection[source]¶
Base class for OCL collections. Collections are either: * sets (Set), * ordered set (OrderedSet) * bags (Bag), * sequences (Seq)
- class pyalaocl.Set(*args)[source]¶
Set of elements.
This class mimics OCL Sets. Being a set, there are no duplicates and no ordering of elements. By contrast to OCL Sets, here a set can contain any kind of elements at the same time. OCL sets are homogeneous, all elements being of the same type (or at least same supertype).
- collectNested(expression)[source]¶
Return a bag of values resulting from the evaluation of the given expression on all elements of the set.
The transformation from this set to a bag is due to the fact that the expression can generate duplicates.
Parameters: expression (X -> Y) – A function returning any kind of value. Returns: The bag of values produced. Rtype Bag[Y]: - Examples:
>>> Set(2,3,5,-5).collectNested(lambda e:e*e) == Bag(25,25,4,9) True >>> Set(2,3).collectNested(lambda e:Bag(e,e)) == Bag(Bag(2,2),Bag(3,3)) True
- count(value)[source]¶
Return the number of occurrence of the value in the set (0 or 1). :param value: The element to search in the set. :type value: any :return: 1 if the element is in the set, 0 otherwise. :rtype: bool
- Examples:
>>> Set(1,3,"a").count("3") 0 >>> Set(1,3,3,3).count(3) 1
- difference(anyCollection)[source]¶
Remove from the set all values in the collection. :param anyCollection: Any collection of values to be discarded from this set. :type anyCollection: collection :return: This set without the values in the collection. :rtype: Set
Examples:
>>> Set(1,3,"a").difference([2,3,2,'z']) == Set(1,"a") True >>> Set(1,3,3,3).difference(Set(1,3)) == Set() True >>> Set().difference(Set()) == Set() True >>> Set(1,3) - [2,3] == Set(1) True
- excluding(value)[source]¶
Excludes a value from the set (if there). :param value: The element to add to the set. :type value: any :return: A set including this element. :rtype: Set
- Examples:
>>> Set(1,3,"a").excluding("3") == Set(1,3,"a") True >>> Set(1,3,3,3).excluding(3) == Set(1) True
- flatten()[source]¶
If the set is a set of collections, then return the set-union of all its elements. :return: Set :rtype: Set
- Examples:
>>> Set(Set(2)).flatten() == Set(2) True >>> Set(Set(Set(2)),Set(2)).flatten() == Set(2) True >>> Set(Set(2,3),Set(4),Set(),Bag("a"),Bag(2,2)).flatten() == Set(2,3,4,"a") True
#>>> Set().flatten() == Set() # True # >>> Set(2,3).flatten() == Set(2,3) # True # >>> Set(2,Set(3),Set(Set(2))).flatten() == Set(2,3) # True
- hasDuplicates()[source]¶
Return always False for sets.
This method is an extension to OCL. It makes is defined on sets just for consistency but is more useful for Bags or Sequences. :return: True :rtype: bool
- including(value)[source]¶
Add the element to the set if not already there. :param value: The element to add to the set. :type value: any :return: A set including this element. :rtype: Set
- Examples:
>>> Set(1,3,"a").including("3") == Set(1,"3",3,"a") True >>> Set(1,3,3,3).including(3) == Set(3,1) True
- intersection(anyCollection)[source]¶
Retain only elements in the intersection between this set and the given collection.
Parameters: anyCollection (collection) – A collection of values to be added to this set. Returns: A set including all values added plus previous set elements. Return type: Set - Examples:
>>> Set(1,3,"a").intersection(["a","a",8]) == Set("a") True >>> Set(1,3,3,3).intersection(Set(1,3)) == Set(1,3) True >>> Set(2).intersection(Set()) == Set() True >>> Set(2) & Set(3,2) == Set(2) True
- isEmpty()[source]¶
- Examples:
>>> Set().isEmpty() True >>> Set(Set()).isEmpty() False >>> Set(2,3).isEmpty() False
- select(predicate)[source]¶
Retain in the set only the elements satisfying the expression.
Parameters: predicate – A predicate, that is a function returning a boolean. Returns: The set with only the selected elements. Rtype Set: - Examples:
>>> Set(2,3,2.5,-5).select(lambda e:e>2) == Set(3,2.5) True >>> Set(Set(1,2,3,4),Set()).select(lambda e:e.size()>3) == Set(Set(1,2,3,4)) True
- selectWithCount(number)[source]¶
Return the set if 1 is selected as count. Otherwise return an empty set because there is no duplicated elements in a set. :param number: :type number: :return: :rtype:
- size()[source]¶
Return the size of the set. :return: The size of the set. :rtype: int
- Examples:
>>> Set(1,4,2,1,1).size() 3 >>> Set().size() 0
- symmetricDifference(anyCollection)[source]¶
Return the elements that are either in one set but not both sets.
In fact this method accept any collection, but it is first converted to a set.
Parameters: anyCollection (collection) – A collection to make the difference with. Returns: The symmetric difference. Return type: Set - Examples:
>>> Set(1,2).symmetricDifference(Set(3,2)) == Set(1,3) True >>> Set(Set()).symmetricDifference(Set()) == Set(Set()) True
- union(anyCollection)[source]¶
Add all elements from the collection given to the set. :param anyCollection: A collection of values to be added to this set. :type anyCollection: collection :return: A set including all values added plus previous set elements. :rtype: Set
- Examples:
>>> Set(1,3,'a').union([2,3,2]) == Set(1,3,"a",2) True >>> Set(1,3,3,3).union(Set(2,1,8)) == Set(1,2,3,8) True >>> Set().union(Set()) == Set() True >>> Set(1,3) | [2,3] == Set(1,2,3) True
- pyalaocl.asSet(collection)[source]¶
Convert the given collection to a Set :param collection: :return: :rtype: Set
- pyalaocl.asSeq(anyCollection)[source]¶
Convert the given collection to a Seq :param anyCollection: :return: :rtype: Seq
- pyalaocl.isCollection(value, language=None)[source]¶
Parameters: - value –
- language –
Returns: >>> isCollection((2,3)) True >>> isCollection([]) True >>> isCollection(12) False >>> isCollection(Counter()) True >>> isCollection("text") False
- pyalaocl.asCollection(anyCollection)[source]¶
Convert any collection into the proper (OCL) collection.
Parameters: anyCollection – A python, java or ocl collection. Returns: The OCL collection Return type: Collection - Examples:
>>> asCollection({2,3}) == Set(3,2) True >>> asCollection(frozenset({1,5,1})) == Set(1,5) True >>> asCollection(Counter([1,1,3,1])) == Bag(1,1,1,3) True >>> asCollection(Counter({'hello':2,-1:0})) == Bag('hello','hello') True >>> asCollection([1,2,3,4]) == Seq(1,2,3,4) True >>> asCollection((1,2,3,4)) == Seq(1,2,3,4) True >>> asCollection(deque([1,2,3,4])) == Seq(1,2,3,4) True