Source entry of an acyclic-graph constrained collection

How can I know that a specific node in a collection with an acyclic-graph constraint is a source node (no references to previous nodes)?
Example code (incomplete!):

'Stock batches': collection ['Stock batch'] {
    'Stock batch': text @default: auto-increment || "200000"
    'Batch amount': number 'pieces'
    'Batch weight': number 'kg'
    'Stock date': number 'date'
    'Batches': collection ['Batch'] 
        'Source': acyclic-graph
    {
        'Batch': text @default: guid
        'Source batch': text -> sibling in ('Source')
        'Amount': number 'pieces'
        'Weight': number 'kg'
        'Batch creation date': number 'date'
        'Days after source batch': number 'days' = ( recurse 'Source' ) switch ...? // <-- What to switch on here?
            | ...? as $'stock' => diff 'date' ( .'Batch creation date' , $'stock'.'Stock date' )
            | ...? as $'source' => diff 'date' ( .'Batch creation date' , $'source'.'Batch creation date' )
    }
}

To calculate the Days after source batch I would like to know if I’ve arrived at the source of the acyclic-graph. If so, I need to use the date of the Stock batch. Otherwise, the Batch creation date of the Source batch.
Thanks in advance!

I think I’ve found the solution. And it’s easier than I thought. :smiley:
This part should change into:

'Days after source batch': number 'days' = ( recurse 'Source' ) switch >'Source batch' (
    | none => diff 'date' ( .'Batch creation date' , ^ .'Stock date' )
    | node as $'source' => diff 'date' ( .'Batch creation date' , $'source'.'Batch creation date' )
)
1 Like

In your code sample, every Batches item has to reference a Source batch. But in that way, it is impossible for the references to form an acyclic graph. For example, if you have two Batches entries A and B, then A would have to refer to B, and B to A.

You can solve it like this:

'Stock batches': collection ['Stock batch'] {
    'Stock batch': text @default: auto-increment || "200000"
    'Batch amount': number 'pieces'
    'Batch weight': number 'kg'
    'Stock date': number 'date'
    'Batches': collection ['Batch'] 
        'Source': acyclic-graph
    {
        'Batch': text @default: guid
        'Is main batch': stategroup (
            'Yes' { }
            'No' { 
                'Source batch': text -> ^ sibling in ('Source')
            }
        )
        'Amount': number 'pieces'
        'Weight': number 'kg'
        'Batch creation date': number 'date'
        'Days after source batch': number 'days' = ( recurse 'Source' ) switch .'Is main batch' (
            |'Yes' => diff 'date' ( .'Batch creation date' , ^ .'Stock date' )
            |'No' as $'source' => diff 'date' ( .'Batch creation date' , $'source'>'Source batch'.'Batch creation date' )
        )
    }
}