|
95 | 95 | "# Read SysML v2 textual notation using Syside Automator\n", |
96 | 96 | "model, doc, elements = read_sysml(\"\"\"\n", |
97 | 97 | "private import SI::*;\n", |
| 98 | + "private import SIPrefixes::*;\n", |
98 | 99 | "package ThePackage {\n", |
99 | 100 | " part def PartWithAttributes {\n", |
100 | 101 | " \n", |
|
110 | 111 | " attribute speed = 300 [m/s];\n", |
111 | 112 | " attribute pressure = 101325 [N/m^2]; // Normally you would use [Pa]\n", |
112 | 113 | " \n", |
113 | | - " // Compound unit defined in standard lib\n", |
| 114 | + " // Compound units defined in standard lib\n", |
114 | 115 | " attribute acceleration = 10.0 ['m⋅s⁻²'];\n", |
115 | 116 | "\n", |
116 | | - " // Compound unit not defined in the standard lib\n", |
| 117 | + " // Compound units not defined in the standard lib\n", |
117 | 118 | " attribute crazyUnits = -3.14 [K*kg*s**2/m^3];\n", |
| 119 | + " attribute filesize = 42 [mega*byte]; // Megabyte itself is not defined as a unit\n", |
118 | 120 | " \n", |
119 | 121 | " // Temperature\n", |
120 | 122 | " attribute absTemp = 15.0 ['°C_abs']; // = 288.15 K\n", |
|
131 | 133 | "units_helper = SysMLUnitsHelper(model)\n", |
132 | 134 | "\n", |
133 | 135 | "# Get the attributes of the part\n", |
134 | | - "part = elements[1].children.elements[0]\n", |
| 136 | + "part = elements[2].children.elements[0]\n", |
135 | 137 | "assert part.name == 'PartWithAttributes'\n", |
136 | | - "attrs = list(part.children.elements)" |
| 138 | + "attrs = {el.name: el for el in part.children.elements}" |
137 | 139 | ] |
138 | 140 | }, |
139 | 141 | { |
|
149 | 151 | "outputs": [], |
150 | 152 | "source": [ |
151 | 153 | "# Read attribute value quantity\n", |
152 | | - "q = units_helper.get_quantity(attrs[0])\n", |
| 154 | + "q = units_helper.get_quantity(attrs['length'])\n", |
153 | 155 | "assert q.magnitude == 1000\n", |
154 | 156 | "assert str(q.units) == 'meter'\n", |
155 | 157 | "\n", |
156 | 158 | "# Only read the unit\n", |
157 | | - "units, m_unit_attr = units_helper.get_units(attrs[0])\n", |
| 159 | + "units, m_unit_attr = units_helper.get_units(attrs['length'])\n", |
158 | 160 | "assert units == ureg.m\n", |
159 | 161 | "assert str(units) == 'meter'\n", |
160 | 162 | "\n", |
|
195 | 197 | " pressure = 101325 N/m²\n", |
196 | 198 | " acceleration = 10.0 m/s²\n", |
197 | 199 | " crazyUnits = -3.14 K·kg·s²/m³\n", |
| 200 | + " filesize = 42 MB\n", |
198 | 201 | " absTemp = 15.0 °C\n", |
199 | 202 | " deltaTemp = 15.0 Δ°C\n", |
200 | 203 | " density has no value\n", |
|
213 | 216 | " print(f' {feature.name} has no value')\n", |
214 | 217 | " \n", |
215 | 218 | "print('Attribute quantities:')\n", |
216 | | - "for attr in attrs:\n", |
| 219 | + "for attr in attrs.values():\n", |
217 | 220 | " _read_print_quantity(attr)\n", |
218 | 221 | "print('')" |
219 | 222 | ] |
|
241 | 244 | " pressure = inf Pa\n", |
242 | 245 | " acceleration = nan m/s²\n", |
243 | 246 | " crazyUnits = -6.28 K·kg·s²/m³\n", |
| 247 | + " filesize = 42.0 GB\n", |
244 | 248 | " absTemp = 288.15 K\n", |
245 | 249 | " deltaTemp = 15.0 K\n", |
246 | 250 | " density = 1.225 kg/m³\n", |
|
250 | 254 | ], |
251 | 255 | "source": [ |
252 | 256 | "# Convert value to another unit (m --> km)\n", |
253 | | - "q_m = units_helper.get_quantity(attrs[0])\n", |
| 257 | + "q_m = units_helper.get_quantity(attrs['length'])\n", |
254 | 258 | "q_km = q_m.to(ureg.km)\n", |
255 | | - "units_helper.set_quantity(attrs[0], q_km)\n", |
| 259 | + "units_helper.set_quantity(attrs['length'], q_km)\n", |
256 | 260 | "\n", |
257 | 261 | "# kg --> lbs\n", |
258 | 262 | "units_helper.set_quantity(\n", |
259 | | - " attrs[1], units_helper.get_quantity(\n", |
260 | | - " attrs[1]).to(ureg.lbs))\n", |
| 263 | + " attrs['mass'], units_helper.get_quantity(\n", |
| 264 | + " attrs['mass']).to(ureg.lbs))\n", |
261 | 265 | "\n", |
262 | 266 | "# Temperature conversion\n", |
263 | 267 | "units_helper.set_quantity(\n", |
264 | | - " attrs[8], units_helper.get_quantity(attrs[8]).to(ureg.K))\n", |
| 268 | + " attrs['absTemp'], units_helper.get_quantity(attrs['absTemp']).to(ureg.K))\n", |
265 | 269 | "units_helper.set_quantity(\n", |
266 | | - " attrs[9], units_helper.get_quantity(attrs[9]).to(ureg.K))\n", |
| 270 | + " attrs['deltaTemp'], units_helper.get_quantity(attrs['deltaTemp']).to(ureg.K))\n", |
267 | 271 | "\n", |
268 | 272 | "# Set quantity: quantity constructor\n", |
269 | 273 | "units_helper.set_quantity(\n", |
270 | | - " attrs[2], units_helper.quantity(150, ureg.kJ))\n", |
| 274 | + " attrs['energy'], units_helper.quantity(150, ureg.kJ))\n", |
271 | 275 | "\n", |
272 | | - "units_helper.set_quantity(attrs[3], 3.14 * ureg.m)\n", |
| 276 | + "units_helper.set_quantity(attrs['diameter'], 3.14 * ureg.m)\n", |
273 | 277 | "\n", |
274 | 278 | "# Set quantity: value and parsed unit str\n", |
275 | | - "units_helper.set_value_and_units(attrs[4], 42, 'km/h')\n", |
| 279 | + "units_helper.set_value_and_units(attrs['speed'], 42, 'km/h')\n", |
276 | 280 | "\n", |
277 | 281 | "# Set special values: inf and NaN\n", |
278 | | - "units_helper.set_value_and_units(attrs[5], math.inf, 'Pa')\n", |
279 | | - "units_helper.set_value_and_units(attrs[6], math.nan, 'm/s**2')\n", |
| 282 | + "units_helper.set_value_and_units(attrs['pressure'], math.inf, 'Pa')\n", |
| 283 | + "units_helper.set_value_and_units(attrs['acceleration'], math.nan, 'm/s**2')\n", |
280 | 284 | "\n", |
281 | 285 | "# Get and set compound units\n", |
282 | | - "quantity = units_helper.get_quantity(attrs[7])\n", |
| 286 | + "quantity = units_helper.get_quantity(attrs['crazyUnits'])\n", |
283 | 287 | "assert len(list(quantity.units._units.unit_items())) > 1\n", |
284 | | - "units_helper.set_quantity(attrs[7], quantity*2)\n", |
| 288 | + "units_helper.set_quantity(attrs['crazyUnits'], quantity*2)\n", |
| 289 | + "\n", |
| 290 | + "units_helper.set_quantity(attrs['filesize'], 42*ureg['GB'])\n", |
285 | 291 | "\n", |
286 | 292 | "# Get preferred units of a quantity base type\n", |
287 | | - "quantity_attr = attrs[10]\n", |
| 293 | + "quantity_attr = attrs['density']\n", |
288 | 294 | "assert units_helper.is_typed_by_quantity_value(quantity_attr)\n", |
289 | 295 | "units = units_helper.get_quantity_value_units(quantity_attr)\n", |
290 | 296 | "assert units == ureg['kg/m**3']\n", |
|
297 | 303 | "\n", |
298 | 304 | "# Read and print all attribute values\n", |
299 | 305 | "print('Modified attribute quantities:')\n", |
300 | | - "for attr in attrs:\n", |
| 306 | + "for attr in attrs.values():\n", |
301 | 307 | " _read_print_quantity(attr)\n", |
302 | 308 | "print('')" |
303 | 309 | ] |
|
318 | 324 | "output_type": "stream", |
319 | 325 | "text": [ |
320 | 326 | "private import SI::*;\n", |
| 327 | + "private import SIPrefixes::*;\n", |
321 | 328 | "package ThePackage {\n", |
322 | 329 | " part def PartWithAttributes {\n", |
323 | 330 | " attribute length = 1 [km];\n", |
|
332 | 339 | " attribute acceleration = null ['m⋅s⁻²'];\n", |
333 | 340 | "\n", |
334 | 341 | " attribute crazyUnits = -6.28 [K * kg * s^2 * m^(-3)];\n", |
| 342 | + " attribute filesize = 42 [giga * B];\n", |
335 | 343 | "\n", |
336 | 344 | " attribute absTemp = 288.15 [K];\n", |
337 | 345 | " attribute deltaTemp = 15 [K];\n", |
|
681 | 689 | "42 meter / second\n", |
682 | 690 | "attribute speed = 42 [SI::'m/s'];\n", |
683 | 691 | "\n", |
684 | | - "42 kelvin * kilogram / meter ** 3\n", |
685 | | - "attribute valueWithCustomUnits = 42 [SI::K * SI::kg * SI::m^(-3)];\n", |
| 692 | + "42 kilogram * megakelvin / meter ** 3\n", |
| 693 | + "attribute valueWithCustomUnits = 42 [SIPrefixes::mega * (SI::kg * SI::K * SI::m^(-3))];\n", |
686 | 694 | "\n" |
687 | 695 | ] |
688 | 696 | } |
|
699 | 707 | "units_helper.set_quantity(attr, quantity)\n", |
700 | 708 | "print(units_helper.to_text(attr))\n", |
701 | 709 | "\n", |
702 | | - "# Compound units not available as a separate units attribute: K*kg/m**3\n", |
703 | | - "model, _, elements = read_sysml(\"attribute valueWithCustomUnits = 42 [SI::K * SI::kg / SI::m^3];\")\n", |
| 710 | + "# Compound units not available as a separate units attribute: mega*K*kg/m**3\n", |
| 711 | + "model, _, elements = read_sysml(\"attribute valueWithCustomUnits = 42 [SIPrefixes::mega * SI::K * SI::kg / SI::m^3];\")\n", |
704 | 712 | "units_helper = SysMLUnitsHelper(model)\n", |
705 | 713 | "\n", |
706 | 714 | "attr = elements[0]\n", |
|
824 | 832 | "id": "1571c0b9-6fc5-4ca4-91e5-8a5cc5d12697", |
825 | 833 | "metadata": {}, |
826 | 834 | "source": [ |
827 | | - "Sometimes units are converted to their base definitions, for example when prefixes (e.g. kilo, mega) are used.\n", |
828 | | - "To support this, you can use the `scale` output of `set_units` to correct your value:" |
| 835 | + "Unit prefixes are automatically applied:" |
829 | 836 | ] |
830 | 837 | }, |
831 | 838 | { |
|
838 | 845 | "name": "stdout", |
839 | 846 | "output_type": "stream", |
840 | 847 | "text": [ |
841 | | - "attribute myAttribute = SI::W;\n", |
| 848 | + "attribute myAttribute = SIPrefixes::mega * SI::W;\n", |
842 | 849 | "\n" |
843 | 850 | ] |
844 | 851 | } |
845 | 852 | ], |
846 | 853 | "source": [ |
847 | | - "scale = units_helper.set_units(attr, 'MW')\n", |
848 | | - "assert scale == 1e6\n", |
| 854 | + "units_helper.set_units(attr, 'MW')\n", |
849 | 855 | "\n", |
850 | 856 | "units, _ = units_helper.get_units(attr)\n", |
851 | | - "assert units == ureg.W\n", |
| 857 | + "assert units == ureg('MW').units\n", |
852 | 858 | "print(units_helper.to_text(attr))" |
853 | 859 | ] |
854 | 860 | }, |
|
870 | 876 | "name": "stdout", |
871 | 877 | "output_type": "stream", |
872 | 878 | "text": [ |
873 | | - "attribute myAttribute = SI::K * SI::kg * SI::m^(-1);\n", |
| 879 | + "attribute myAttribute = SIPrefixes::mega * (SI::K * SI::kg * SI::m^(-1));\n", |
874 | 880 | "\n" |
875 | 881 | ] |
876 | 882 | } |
877 | 883 | ], |
878 | 884 | "source": [ |
879 | | - "units_helper.set_units(attr, 'K*kg/m')\n", |
| 885 | + "units_helper.set_units(attr, 'megaK*kg/m')\n", |
880 | 886 | "print(units_helper.to_text(attr))\n", |
881 | 887 | "\n", |
882 | 888 | "units, units_attr = units_helper.get_units(attr)\n", |
883 | | - "assert units == ureg('K*kg/m').units\n", |
| 889 | + "assert units == ureg('megaK*kg/m').units\n", |
884 | 890 | "assert units_attr is None # There is no single units attribute that represents these compound units" |
885 | 891 | ] |
886 | 892 | }, |
|
972 | 978 | "source": [ |
973 | 979 | "### Convert between Pint or str units and SysML units\n", |
974 | 980 | "\n", |
975 | | - "Note: this only works for non-compound units (working with SysML units expressions is only implemented for getting/setting units/quantities as feature values)!" |
| 981 | + "Note: this only works for non-compound units!\n", |
| 982 | + "Working with SysML units expressions is only implemented for getting/setting units/quantities as feature values." |
976 | 983 | ] |
977 | 984 | }, |
978 | 985 | { |
|
990 | 997 | "name": "stdout", |
991 | 998 | "output_type": "stream", |
992 | 999 | "text": [ |
993 | | - "Cannot map Pint units to SysML: kelvin * kilogram / meter\n" |
| 1000 | + "Could not convert pint units \"<K·kg/m> kelvin·kilogram/meter\" to SysML units\n" |
994 | 1001 | ] |
995 | 1002 | } |
996 | 1003 | ], |
|
0 commit comments