Hello!
TL;DR: I’m trying to learn how to model data from relationships with Datomic/Datascript. I’m running into a problem where data references are “local”, and don’t really know how to handle that. Wall of text incoming.
I’d appreciate any hunches from more experienced users of Datomic/Datascript!
Problem definition
I’m working with data that’s hierarchical in the context I’m coming from, and I’d like to build a flat Datomic model that can reproduce the hierarchy. This sounds like the right approach to me, “derive hierarchy from a flat model”.
Let me describe the crux of the challenge. The data I’m working with already has relations, but these are allways local, in a context, within the hierarchy. Concretely: I’m modeling structures with computational mechanics. The foundational tools here are nodes and elements. A node has a position, and an element connects nodes. Something like this in a Clojurish syntax:
(def fem-model {:model.doc/description "A truss model of 1d elements for demonstration!"
:model.doc/figure " (3)
/ |
/ |
/ |
/ |
(1) -- (2)"
:model/nodes [{:node/id 1 :node/x 0.0 :node/y 0.0}
{:node/id 2 :node/x 1.0 :node/y 0.0}
{:node/id 3 :node/x 1.0 :node/y 1.0}]
:model/elements [{:element/id 1
:element/nodes [1 2]
:element/kind :element.kind/one-dimensional}
{:element/id 2
:element/nodes [2 3]
:element/kind :element.kind/one-dimensional}
{:element/id 3
:element/nodes [1 3]
:element/kind :element.kind/one-dimensional}]})
But I want to store multiple models in a single database. In fact, that’s the main value prop. We currently have (self contained) models in JSON files floating around. But that prevents us from making applications that reason across models. How much have results improved while we’ve been working on the model with respect to different analyses?
Possible approaches
- Model the data directly, don’t use Datomic relationships.
- Advantage: simple
- Disadvantage: I can’t use the database to resolve relationships, and would instead have to query a whole model and build the indexes myself. Sounds like just storing a blob in Datomic.
- Just use Datomic references directly
- Advantage: can query with Datomic
- Disadvantages
- I will need to map to local IDs when I export a model
- Relationship from element to nodes is ordered. :element/nodes [1 2 3] gives a triangle with an orientation opposite to :element/nodes [3 2 1].
- Ignore references, and store the whole model as EDN
- This smells like avoiding the problem.
I feel like in foreign territory here. Modeling relationships instead of objects feels like the right thing to do. But I’m not quite sure where to start. Read the f* manual may be the right response here. If so, I’ll take that. I haven’t read the docs in detail full detail.
So!
Does this problem seem familiar? How would you approach it? Loose hunches and hard facts welcome. I’ll happily provide more details, but I guess this post is long enough by now.
Thanks!
References for the interested reader
- Wikipedia: Finite Element Method in Structural Mechanics
- Example: FEM analysis with Abaqus CAE (PPT) (University of Florida). Shows a real example of what we might want to generate from this data model.