dsl
dsl
¶
This module provides a DSL
class, which allows users to define specifications in a declarative manner using a fluent interface.
DSL
¶
A domain-specific language (DSL) to define component specifications.
This class provides a interface for defining specifications in a declarative manner. It allows users to specify the name and group of each parameter, as well as filter.
Examples:
DSL() .Parameter("x", int) .Parameter("y", int, lambda vars: vars["x"] + 1) .Parameter("z", str) .ParameterConstraint(lambda vars: len(vars["z"]) == vars["x"] + vars["y"]) .Suffix(
constructs a specification for a function with three parameters: - x
: an integer - y
: an integer, the value of which is x
+ 1 - z
: a string, whose length is equal to x
+ y
The Suffix
method specifies the function, which uses the variables x
, y
, and z
.
Source code in src/cosy/dsl.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
__init__() -> None
¶
argument(name: str, specification: Type) -> DSL
¶
Introduce a new variable.
group
is a Type
, and an instance will be generated for each tree, satisfying the specification given by the type. Since this can only be done in the enumeration step, you can only use these variables in predicates, that themselves belong to variables whose group
is a Type
.
:param name: The name of the new variable. :type name: str :param specification: The type of the variable. :type specification: Type :return: The DSL object. :rtype: DSL
Source code in src/cosy/dsl.py
constraint(constraint: Callable[[Mapping[str, Any]], bool]) -> DSL
¶
Constraint on the previously defined parameter variables and argument variables.
:param constraint: A constraint deciding, if the currently chosen values are valid. The values of variables are passed by a dictionary, where the keys are the names of the variables and the values are the corresponding values. :type constraint: Callable[[Mapping[str, Any]], bool] :return: The DSL object. :rtype: DSL
Source code in src/cosy/dsl.py
parameter(name: str, group: str, candidates: Callable[[dict[str, Any]], Sequence[Any]] | None = None) -> DSL
¶
Introduce a new parameter variable.
group
is a string, and an instance of this specification will be generated for each valid literal in the corresponding literal group. You can use this variable as Var(name) in all Type
s, after the introduction and in all predicates. Optionally, you can specify a sequence of candidate values, that will be used to generate the literals. This sequence is parameterized by the values of previously defined literal variables. This is useful, if you want to restrict the values of a variable to a subset of the values in the corresponding literal group.
:param name: The name of the new variable. :type name: str :param group: The group of the variable. :type group: str :param candidates: Parameterized sequence of candidate values, that will be used to generate the literals. :type candidates: Callable[[dict[str, Any]], Sequence[Any]] | None :return: The DSL object. :rtype: DSL
Source code in src/cosy/dsl.py
parameter_constraint(constraint: Callable[[Mapping[str, Any]], bool]) -> DSL
¶
Constraint on the previously defined parameter variables.
:param constraint: A constraint deciding, if the currently chosen parameter values are valid. The values of variables are passed by a dictionary, where the keys are the names of the parameter variables and the values are the corresponding values. :type constraint: Callable[[Mapping[str, Any]], bool] :return: The DSL object. :rtype: DSL
Source code in src/cosy/dsl.py
suffix(suffix: Type) -> Specification
¶
Constructs the final specification wrapping the given Type
suffix
.
:param suffix: The wrapped type. :type suffix: Type :return: The constructed specification. :rtype: Abstraction | Type