No edit summary |
No edit summary |
||
(11 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
=== What | === What Are These? === | ||
* Derived attributes use an OCL expression to calculate the presentation of another attribute. | * Derived attributes use an OCL expression to calculate the presentation of another attribute. | ||
* Derived '''settable''' attributes use EAL to do the reverse, i.e. setting the source attribute from the entered value. | * Derived '''settable''' attributes use EAL to do the reverse, i.e. setting the source attribute from the entered value. The user input is in the '''vInputParameter''' variable. | ||
This opens up special handling of user input and "user shortcuts" in the UI | This opens up special handling of user input and "user shortcuts" in the UI - for example, accepting only a number as a time entry. | ||
=== Example with Time (DateTime) and Decimal === | === Example with Time (DateTime) and Decimal === | ||
This example uses ''FromTimeText'', ''HoursText'' and ''ToTimeText'' for user input, but only '''FromTime''' and '''Hours''' for storing information in the database. | This example uses ''FromTimeText'', ''HoursText,'' and ''ToTimeText'' for user input, but only '''FromTime''' and '''Hours''' for storing information in the database. | ||
This special handling makes user input much simpler because it handles the user's need to change any of the three values, updating the others behind the scenes. | |||
[[File:TimeSpent class example.png|thumb|none]] | [[File:TimeSpent class example.png|thumb|none]] | ||
* '''/Date''' is a derived attribute for easy access to a neighboring class containing the date in DateTime format. | |||
'''/Date''' is a derived attribute for easy access to a neighboring class | * '''/EffectiveToTime''' is a derived attribute used as a companion to the FromTime, again for symmetry in accessing the information. | ||
'''/EffectiveToTime''' is a derived attribute used a companion to the FromTime, again for symmetry in | |||
Support function '''StringToTime()''' | Support function '''StringToTime()''' | ||
To help the other function with conversion, a help function like this was created | To help the other function with conversion, a help function like this was created: | ||
if aText.length <= 2 then | if aText.length <= 2 then | ||
Line 22: | Line 21: | ||
DateTime.Parse('1900-01-01 ' + aText) | DateTime.Parse('1900-01-01 ' + aText) | ||
endif | endif | ||
Note the addition of ''':00''' to the text if it is short | * Note the addition of "''':00'''" to the text if it is short. This helps the user out of entering just the hour part. | ||
* '1900-01-01' is an ugly hack to prevent storing the current date as part of the time entry. It would still work, but this is slightly better. | |||
'1900-01-01' is an ugly hack to prevent storing the current date as part of the time entry. It would work | |||
===== Property | ===== Property Inspector ===== | ||
Below is the property inspector setting for | Below is the property inspector setting for a derived settable attribute. Note the AttributeMode and then the OCL and OclSet code. | ||
[[File:DerivedSettable property inspector.png|none|thumb|382x382px]] | [[File:DerivedSettable property inspector.png|none|thumb|382x382px]] | ||
===== /FromTimeText: String? ===== | ===== /FromTimeText: String? ===== | ||
OCL | ''OCL'' | ||
if self.FromTime.notNull then | if self.FromTime.notNull then | ||
Line 39: | Line 37: | ||
endif | endif | ||
EAL (OclSet) | ''EAL (OclSet)'' | ||
self.FromTime := self.StringToTime(vInputParameter) | self.FromTime := self.StringToTime(vInputParameter) | ||
Line 45: | Line 43: | ||
'''/HoursText: String?''' | '''/HoursText: String?''' | ||
OCL | ''OCL'' | ||
In Sweden we use comma | In Sweden, we use the comma instead of the dot as a decimal separator. Hard-coded replacement: | ||
self.Hours.ToString('N2').Replace('.', ',') | self.Hours.ToString('N2').Replace('.', ',') | ||
EAL (OclSet) | ''EAL (OclSet)'' | ||
Enable use of both comma and dot as decimal | Enable the use of both the comma and dot as decimal separators. Round to 2 decimals. | ||
self.Hours := Decimal.Parse(vInputParameter.Replace(',', '.')).Round(2) | self.Hours := Decimal.Parse(vInputParameter.Replace(',', '.')).Round(2) | ||
Line 59: | Line 57: | ||
'''/ToTimeText: String?''' | '''/ToTimeText: String?''' | ||
The to-time is all calculated and only | The to-time is all calculated and only exists when showing and entering values. | ||
OCL | ''OCL'' | ||
if self.FromTime.notNull then | if self.FromTime.notNull then | ||
Line 69: | Line 67: | ||
endif | endif | ||
EAL (OclSet) | ''EAL (OclSet)'' | ||
Handle both setting '''Hours''' or '''FromTime''' based on what information is already present. | Handle both setting '''Hours''' or '''FromTime''' based on what information is already present. | ||
Line 79: | Line 77: | ||
0 | 0 | ||
endif | endif | ||
[[Category:MDriven Designer]] | |||
See also: | |||
[[Training:Derived attributes & associations|Derived attributes & associations]] | |||
[[Derived settable associations]] | |||
{{Edited|July|12|2024}} | |||
[[Category:TOC]] |
Latest revision as of 06:36, 22 May 2024
What Are These?
- Derived attributes use an OCL expression to calculate the presentation of another attribute.
- Derived settable attributes use EAL to do the reverse, i.e. setting the source attribute from the entered value. The user input is in the vInputParameter variable.
This opens up special handling of user input and "user shortcuts" in the UI - for example, accepting only a number as a time entry.
Example with Time (DateTime) and Decimal
This example uses FromTimeText, HoursText, and ToTimeText for user input, but only FromTime and Hours for storing information in the database.
This special handling makes user input much simpler because it handles the user's need to change any of the three values, updating the others behind the scenes.
- /Date is a derived attribute for easy access to a neighboring class containing the date in DateTime format.
- /EffectiveToTime is a derived attribute used as a companion to the FromTime, again for symmetry in accessing the information.
Support function StringToTime()
To help the other function with conversion, a help function like this was created:
if aText.length <= 2 then DateTime.Parse('1900-01-01 ' + aText + ':00') else DateTime.Parse('1900-01-01 ' + aText) endif
- Note the addition of ":00" to the text if it is short. This helps the user out of entering just the hour part.
- '1900-01-01' is an ugly hack to prevent storing the current date as part of the time entry. It would still work, but this is slightly better.
Property Inspector
Below is the property inspector setting for a derived settable attribute. Note the AttributeMode and then the OCL and OclSet code.
/FromTimeText: String?
OCL
if self.FromTime.notNull then self.FromTime.formatDateTime('HH:mm') else String.nullValue endif
EAL (OclSet)
self.FromTime := self.StringToTime(vInputParameter)
/HoursText: String?
OCL
In Sweden, we use the comma instead of the dot as a decimal separator. Hard-coded replacement:
self.Hours.ToString('N2').Replace('.', ',')
EAL (OclSet)
Enable the use of both the comma and dot as decimal separators. Round to 2 decimals.
self.Hours := Decimal.Parse(vInputParameter.Replace(',', '.')).Round(2)
/ToTimeText: String?
The to-time is all calculated and only exists when showing and entering values.
OCL
if self.FromTime.notNull then self.FromTime.AddHours(self.Hours.Value.ToDouble).formatDateTime('HH:mm') else String.nullValue endif
EAL (OclSet)
Handle both setting Hours or FromTime based on what information is already present.
if self.FromTime.notNull then self.Hours := Decimal.Create(self.StringToTime(vInputParameter).Subtract(self.FromTime).TotalHours) else self.FromTime := self.StringToTime(vInputParameter).AddHours(-self.Hours.ToDouble); 0 endif
See also: