Compiler
- class Compiler
Description
A non-recursive compiler/virtual machine for KerML and SysML expressions.
Note that some patterns require cache of standard library elements to compute. See methods for more details.
Inheritance:
Members defined in
Compiler(6 members)RWMaximum number of steps an expression evaluation may take.
RWMaximum recursion depth evaluation may reach.
Evaluate an expression
expr. Constructor expressions evaluate as their return parameters because they conform to the constructed object implicitly.Evaluate
featureinscope, taking into account redefinitions.Evaluate
filterexpression on the metadata oftarget.Attributes
- property max_steps: int
Maximum number of steps an expression evaluation may take.
When this is reached, evaluation stops with an error. Actual steps are an implementation detail.
- property recursion_depth: int
Maximum recursion depth evaluation may reach.
When this is reached, evaluation stops with an error distinct from max steps. This is typically triggered by recursive feature evaluations. Keeping this depth low will error out from recursive evaluations before reaching the step limit.
Methods
- evaluate(context: syside.vm.Context, expr: syside.Expression | .syside.CalculationUsage | .syside.ConstraintUsage, scope: syside.Type | None | None = None, experimental_quantities: bool = False) tuple[syside.vm.Value | None, syside.vm.CompilationReport]
Evaluate an expression
expr. Constructor expressions evaluate as their return parameters because they conform to the constructed object implicitly.Note that
selffeatures are assumed to always be namedself. Ensure thatreserved-feature-namediagnostic is enabled and not ignored to catch misuses.Quantity Evaluation
Quantity evaluation is a work-in-progress as hinted by the experimental
unitsmodule. Currently, only scalar measurement units with natural zeros are supported, with the standard library temperature units a builtin exception. Note that there is no quantity safety performed during evaluation in general, only dimensional safety. Some predefined standard library units have a stricter policy, e.g. sum or subtraction ofHzandBqwhich measure incompatible quantities — frequency and activity respectively, despite having the same dimensions.The provided
Contextis used to memoize units and their compatibility in expressions. For performance, reuse context whenever possible. If the model is changed, consider creating and using a new context.Additionally,
prefix * reference unitexpressions are not supported — useConversionByPrefixpatterns used in the standard library. Lastly, all units are assumed to be statically resolvable and represented using a subset of expressions — references, multiplication, division, and exponentiation, without using chain expressions outside of quantity power factors. Passing units through functions to construct quantities is not yet implemented. Some limitations are expected to be lifted in the future.Otherwise, if
experimental_quantitiesis enabled, compiler will attempt to evaluate scalar quantity expressions in untyped SI units, other systems of quantities are not supported. The results will be returned in scalarfloatand no type-checking will be performed. This option will be removed in the future when typed quantity evaluation is stabilised.Note that quantity evaluation with
experimental_quantitiesrequiresStdlibto have found elements fromMeasurementReferenceslibrary. Additionally, evaluation is tested to work with standard library quantities and prefixes. Custom package-level prefixes and quantities are also expected to work, however any nested feature chaining may or may not work.Reflection
Added in version 0.8.5.
Note that reflection (metadata access) is evaluated immediately on whatever symbol is referenced on the left. This means that inheritance and redefinitions have no effect - symbols will not be substituted based on scope during evaluation.
User-defined Function Evaluation
Added in version 0.8.5.
InvocationExpressionsof simple user-defined function are supported as long they have either aResultExpressionMembership, declared as the last expression inside the body without a trailing;, or areturnfeature. The former is preferred for performance reasons. As long the result or return expression is evaluatable by Syside and all input arguments have values, the invocation will be evaluated.
- evaluate_feature(context: syside.vm.Context, feature: syside.Feature, scope: syside.Type, experimental_quantities: bool = False) tuple[syside.vm.Value | None, syside.vm.CompilationReport]
Evaluate
featureinscope, taking into account redefinitions.If
featureis anExpression, this effectively performsevaluate.
- evaluate_filter(context: syside.vm.Context, target: syside.Element, filter: syside.Expression, functions: bool = True) tuple[syside.vm.Value | None, syside.vm.CompilationReport]
Evaluate
filterexpression on the metadata oftarget.Added in version 0.8.5.
Be aware that absolute feature references are resolved at the package level, thus if the filter expressions depends on a value of metadata attribute, use
ascast:metadata def Safety { attribute isMandatory; } filter (as Safety).isMandatory;
The cast automatically doubles as a classification test
@which can be omitted.Note that this does not support quantities because they are not model-level evaluable according to the specification, while filter expressions must be.
While all non-operator functions are also not model-level evaluable, this allows evaluating function invocations if
functionsis set to give users more control over programmatic filtering. If specification conformant evaluation is required, usefunctions=False.