Skip to content

Commit

Permalink
Started to add some documentation (ZeraGmbH#19)
Browse files Browse the repository at this point in the history
* Introduced model cache helper.

* Use automatic model dependency resolving to simplify customization.

* Review and cleanups.

* Added EnumBlock.md.

* ModelBlock.md added.

* Added BlocklyExtensions.md.
  • Loading branch information
JMS-1 authored Jul 25, 2024
1 parent fd68e60 commit 29a7f5c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Library/Extensions/Builder/BlocklyExtensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Customization

[tbd]

In addition to adding custom block implementations the customization infrastructure can create [enumeration](EnumBlock.md) or [data model](ModelBlock.md) blocks directly from C# types.
22 changes: 22 additions & 0 deletions Library/Extensions/Builder/EnumBlock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Enumeration blocks

Using _AddEnum<T>(blocklyType, displayName)_ a blockly definition is created which describes the enumeration indicated by the model type parameter _T_ which is contraint to be some _Enum_.

The corresponding implementation type will be _EnumBlock<T>_ where the evaluation will just use the string field _VALUE_ and convert it to the target type using _Enum.Parse(typeof(T), ...)_. The field will be provided as a blockly _field_dropdown_.

```csharp
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ErrorComparisionResult
{
NotAvailable = 0,
InRange = 1,
TooLow = 2,
TooHigh = 3,
}
```

```csharp
builder.AddEnum<ErrorComparisionResult>("error_measurement_comparison", "Error Measurement Comparision");
```

![Enumeration block](EnumBlock.png)
Binary file added Library/Extensions/Builder/EnumBlock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Library/Extensions/Builder/ModelBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public PropertyInformation(PropertyInfo info) : this(info.Name, info.PropertyTyp
private static bool TestSupported(Type type, ModelCache models, Func<Type, string, string, bool> modelFactory, string propertyName)
{
// Enums are always good.
if (type.IsEnum) return true;
if (type.IsEnum && models.Contains(type)) return true;

// Some of .NET base types.
if (_supportedTypes.ContainsKey(type)) return true;
Expand Down
33 changes: 33 additions & 0 deletions Library/Extensions/Builder/ModelBlock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Model blocks

A blocky definition for a model block is created using _AddModel&lt;T>(blocklyType, displayName)_. The type parameter _T_ is constraint to be a _class_ and having a parameterless constructor. The block implementation is based on _ModelBlock&lt;T>_ and the evaluation method will start with using the parameterless constructor to create a new empty instance of a model. Then all supported properties of the model will be calculated using the corresponding blockly value and stored in the new instance - including some basic conversions to adjust to the required C# type if possible. This instance is then the result of the evaluation of the model block.

For each supported property the blocky definition will get a corresponding value. A property is supported if the data type conforms to one of the following rules:

1. some enumeration
2. _bool_, _bool?_, _double_, _double?_, _long_, _long?_, _int_, _int?_ or _string_
3. another model block
4. _Dictionary&lt;TKey, TValue>_ - the _TKey_ key type must be an enumeration and _TValue_ a supported type
5. _List&lt;T>_ where _T_ is a supported type - but not a _List_ itself

Properties attributed with the _System.Text.Json.Serialization.JsonIgnoreAttribute_ or which are read only or write only are not included in the block definition.

The block definition will include a _check_ on each property made available as a value. For _List_ _check_ will contain two checks: one the untyped blockly _Array_ and a special type _Array(type)_ where _type_ is the blockly type name of each array element. This can be used in the frontend to provide a more sophisticated choice of allowed values for a property.

If the property type is supported by one of the two first rules a corresponding shadow block is added to the toolbox entry.

```csharp
public class DutRegisterInfo
{
public DutRegisterTypes Type { get; set; }
public string Address { get; set; } = null!;
public DutRegisterUnits Unit { get; set; }
public double Scale { get; set; }
}
```

```csharp
builder.AddModel<DutRegisterInfo>("dut_register_info", "Register Information for a Device under Test");
```

![Model block](ModelBlock.png)
Binary file added Library/Extensions/Builder/ModelBlock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 29a7f5c

Please sign in to comment.