Umi Template Library as a JSON schema #481
Replies: 7 comments 9 replies
-
I made a little demo here, with accompanying code here. It still relies on serializing to JSON, which Pydantic does handle by creating deeply nested objects (rather than the old, tabular style with objects identified by ref). The problem with that is of course that when deserializing, you would lose all referential relationships and end up with a fully unique tree... except for a little trick I implemented in the class validator, which, makes it so that you can read the nested tree and still get all relationships out! I also implemented a simple method for outputting the old "flat"/tabular style of serialization in a much more concise way than the existing archetypal representation. Deserializing from that would really need more work, but ultimately if we want to go that route again, we really should use a db. You can also use instantly output a JSONSchema successfully, and then use Unfortunately, SQLModel does not support pydantic v2 yet, though progress on it seems to be coming along based off of github issues/pr. That would really be the ideal approach, as then we don't need to worry so much about deserialization etc - the schema and database handle all that automatically. In some sense, this should probably wait until SQLModel + pydantic v2 compatibility is online, since that would be an extremely seamless, natural solution. There's a lot more to talk about here, including the idea of updating classes fields dynamically from a new schema etc. Now an entirely separate but valid, though more challenging approach, would be to define our own meta schema - i.e. the schema for the schema, in otherwords, how UTL objects can be defined and extended. Let's talk more on a call. |
Beta Was this translation helpful? Give feedback.
-
Oh wow - just saw that Prisma has a python adapter now: https://prisma-client-py.readthedocs.io/en/stable/ And it plays with pydantic! We use Prisma at my startup and its fantastic. I'm going to try to play around with that as well. |
Beta Was this translation helpful? Give feedback.
-
@samuelduchesne I did find this fork of SQLModel which is most of the way to pydantic v2 compatibility, not entirely though: https://github.com/honglei/sqlmodel/ I pushed an example of setting up tables for a template library would look like here: https://github.com/samuelduchesne/archetypal/tree/pydantic-schema/archetypal/template/db/schema.py I also uploaded a sample db. in the same folder. I'll try the prisma adapter later in the week. All of these approaches have benefits and drawbacks to think through, some may be mergeable, etc etc. I have a feeling the Prisma approach might be the winner, not sure yet though. |
Beta Was this translation helpful? Give feedback.
-
@samuelduchesne I also added real time graph tracking to the Pure pydantic approach. I think pure pydantic will probably be the one that wins out... |
Beta Was this translation helpful? Give feedback.
-
Alright, so I added a few more things: You can now generate a graph of the schema itself, not just of the library: This would allow you to automatically construct a database schema if desired with full join/link table specification. I also improved (a little bit) the graph layout for libraries (note, the nodes below are actual objects, not classes - there just happens to be only one of a few of the objects so it looks somewhat similar to the graph above): I also added the ability to dereference an object, so in other words if you try to create a Construction by just passing This will mean that the tabular form will be a valid and compact way of representing the library (as it always has been, since it is effectively a relational database but in json) |
Beta Was this translation helpful? Give feedback.
-
@samuelduchesne Alright, I added in deserialization of flat libraries, leveraging the schema graph (i.e. which classes are leaf classes etc). Overall, the development is way more elegant/less verbose with the Pure Pydantic version than SQLModel; Prisma will also have some serious challenges, the main one being that it doesn't natively support constraints on fields (you need to add the I am pretty sure Pure Pydantic (supplemented with nx) will be the way to go. |
Beta Was this translation helpful? Give feedback.
-
I updated the title - technically it's a Directed Acyclic Graph, or DAG, not a tree. Every node in a tree has exactly one parent (except the root), whereas in DAGs, nodes can have multiple parents (which is what we have). If we had a tree, then Pydantic model dump would be trivially all we need. Because we have a DAG, we have our slightly more interesting requirements for data storage (ie tabular/relational joins, or just storing flat nodes/edge lists, etc) |
Beta Was this translation helpful? Give feedback.
-
It's a
tree!(actually, a DAG)Umi Template Libraries are essentially a tree of objects that have parents referencing children. For example, a
OpaqueConstruction
object references multipleOpaqueMaterial
objects;OpaqueMaterial
can be shared betweenOpaqueConstruction
objects.The problem
The library files themselves don’t have data validation (such as limits on the min, max, choices, uniqueness, etc.; documentation of each attribute, units, etc.) which makes it, for now, the role of the developer to check if values are “valid” for ArchSim/EnergyPlus. Each new tool being developed needs new validations (
archetypal
has its own set of validation functions).By using a python package called pydantic, we can define traditional python classes (in fact very similar to dataclasses) that can take attributes and a rich array of validator functions (handled by pydantic). Each time a class is initialized with data, the values are validated against the rules of the attribute.
Trying to create a MaterialLayer that is less than 3mm? An error would be raised. Objects can be serialized via model.json(…). Inversely, they can be parsed from json via model.parse_file(…). Now,
archetypal
does a lot of that kind of validation, because I coded those in, not because of the schema. A schema would standardize that.Standardization is key
Once all your python classes are defined (one for Materials, one for Schedules, one for Zones, etc,), pydantic can create a proper JSON Schema file that is compliant with JSON Schema Core, JSON Schema Validation and OpenAPI. That is where things become VERY interesting. You can use tools that create clients in various language in one command. You need a C# library, ingest the schema and out comes a C# library with all the same validation as the original python system. Need a javascript library? Same thing.
Requirements
What de we need?
Questions
Prisma
Beta Was this translation helpful? Give feedback.
All reactions