Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions apps/editor/docs/associations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ How the editor and server handle a framework's associations: what gets imported,
| **Owner** (source) | The framework whose associations an alignment framework holds. An alignment framework always belongs to exactly one owner. |
| **External association** | An association from one of a framework's items to a node in a *different* framework. It can be stored inline or in an alignment framework. |
| **Hosted** / **remote** | Whether the framework an association points into is on this OpenCASE server (hosted) or not (remote). This is about where the destination lives, not where the association is stored. |
| **Self alignment** | An association between two items of the *same* framework that the author drew in the Tree View (not `isChildOf`). |
| **Self alignment** | A non-hierarchical association between two items of the *same* framework (not `isChildOf` or `isPartOf`), such as `precedes` from one item to another. |
| **Structure** | The `isChildOf` associations that make up a framework's tree. They are always inline and are never treated as alignments. |

"Inline / alignment framework" describes **where an association is stored**. "Hosted / remote" describes **where its destination lives**. The two are independent: an inline association can point at a hosted or a remote framework.
Expand Down Expand Up @@ -121,6 +121,12 @@ The right-hand panel lists **Associated Frameworks**. Each entry is a target the

The view also applies the edits made in this session (`applyInlineAlignmentEdits`), so it shows the associations as they'll be saved.

### Self alignments

`selfAlignmentsOf(framework)` picks the inline associations between two of the framework's own items, with the same rules (no `isChildOf`/`isPartOf`, no links to a CFDocument, no non-CASE targets). When there are any, the framework is listed as its own target, `<title> (this framework)`, and the associations are drawn between the left and right copies of its tree. The Tree View passes the editor's live items to both helpers, so an item added in this session counts as one of the framework's own.

Inline associations are drawn in teal, associations in an alignment framework in purple.

### Hosted or remote

The URI can't tell us: imported associations keep their source's URIs (e.g. `https://case.example.org/…`) even when the target framework has been imported here too. Instead the editor asks the server:
Expand All @@ -135,21 +141,35 @@ On opening, `handleDiscoverAlignedTargets` lists the alignment frameworks that i

## Authoring in the Tree View

The author expands a target and drags an item from the left panel onto an item on the right. The association type defaults to `isRelatedTo`. The badge popover lets the author change the type (Exact Match Of, Is Related To, Is Peer Of, Precedes, Is Replaced By) or remove the association. `isChildOf` can't be authored here, so it never ends up in an alignment framework.
The author expands a target and drags an item from the left panel onto an item on the right. The association type defaults to `isRelatedTo`. The badge popover lets the author change the type (Exact Match Of, Is Related To, Is Peer Of, Precedes, Replaced By) or remove the association. `isChildOf` can't be authored here, so it never ends up in an alignment framework.

Where the new association goes:

| Target | Setting `inline` | Setting `alignmentFramework` |
|---|---|---|
| Hosted framework | Inline | That target's alignment framework |
| Remote | Inline | Inline |
| This framework (self) | Alignment framework | Alignment framework |
| This framework (self) | Inline | Alignment framework |

**Inline** associations are kept in `editorReducer` as `InlineAlignmentEdits` (`added`, `removedIds`, `typeChanges`). They mark the framework dirty, are applied to it when it's saved, and are cleared after a successful save. Every edit is idempotent, so applying it to a framework that already has it changes nothing.

**Alignment framework** associations are saved separately, with the target's own **Save** button in the Associated Frameworks list. `buildAlignmentFrameworkPackage` writes the alignment framework, owned by this framework. If the last association is removed, the alignment framework is deleted.

Inline associations to hosted frameworks are read-only while the setting is `alignmentFramework`. That only happens between switching the setting and saving, after which they've moved.
Inline associations to hosted frameworks are read-only while the setting is `alignmentFramework`. That only happens between switching the setting and saving, after which they've moved. Inline self alignments stay editable: saving never moves them.

The same link (same items, type `isRelatedTo`) isn't added twice when it's dropped again.

## Authoring in the item panel

An item's properties panel has an **Associations** section (`ItemAssociationsSection`, from `itemAssociationsOf` in `src/domain/framework/itemAssociations.ts`):

- **Outgoing:** the item's non-hierarchical associations, to an item of this framework, an item of another framework, or a non-CASE target (`targetType: "ext:…"`). The author can change their type or remove them. Associations to other frameworks are read-only while the setting is `alignmentFramework`, as in the Tree View.
- **Incoming:** associations from other items of this framework to this one, read-only. They're edited on the item they start from, which is one click away.
- **Add association:** a type from the CASE 1.1 vocabulary (without `isChildOf`/`isPartOf`) and a destination in this framework, in another framework, or a non-CASE target with an identifier, URI, optional title and a `targetType` that follows the CASE 1.1 pattern `ext:<name>`. An association that the item already has is refused.

Everything is stored inline through the same `InlineAlignmentEdits` as the Tree View, so it's saved with the framework, as CASE 1.1 stores an association in the document of its origin. The section is read-only for a read-only framework.

With the setting `alignmentFramework`, the section only shows what's stored in the framework itself, and the form only offers a non-CASE target (those are always inline). Associations to items on this server, this framework's own included, are added in the Tree View, which puts them in the alignment frameworks.

---

Expand Down Expand Up @@ -204,7 +224,8 @@ Associations interact with forking in four ways:
| Import (inline setting, `ext:opencase`) | `apps/opencase/src/application/case/endpoints/ImportFramework.ts` |
| Hosted lookup endpoint | `apps/opencase/src/application/case/endpoints/LookupItemDocuments.ts`, `CFItemsManagementController.lookupDocuments` |
| Fork identifiers and data-change detection (server) | `apps/opencase/src/application/case/services/mirrorFork.ts`, `CreateFramework.ts` |
| Inline external associations, hosted/remote grouping | `src/domain/framework/externalAlignments.ts` |
| Inline external associations and self alignments, hosted/remote grouping | `src/domain/framework/externalAlignments.ts` |
| An item's associations (item panel) | `src/domain/framework/itemAssociations.ts`, `src/ui/editor/components/ItemAssociationsSection.tsx` |
| Alignment framework shape, ownership, merge, fork remap | `src/domain/framework/alignmentFrameworks.ts` |
| Inline edits made in the Tree View | `src/domain/framework/inlineAlignmentEdits.ts`, `editorReducer` (`inlineAlignment/*`) |
| Moving associations on save | `src/application/framework/services/AlignmentStorage.ts` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ function mapAssociationType(rawType?: string): AssociationType {
'ispeerof': 'isPeerOf',
'exactmatchof': 'exactMatchOf',
'replacedby': 'replacedBy',
'haspart': 'hasPart',
'exemplar': 'exemplar',
'hasskillevel': 'hasSkillLevel',
'hasskilllevel': 'hasSkillLevel',
'istranslationof': 'isTranslationOf',
}

return knownTypes[t] ?? (rawType as AssociationType) ?? 'isRelatedTo'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ describe('saveWithAlignmentStorage', () => {
expect(associationIds(af)).toEqual(['to-math'])
})

it('keeps self alignments in the framework when the others move into alignment frameworks', async () => {
const withinScience: CFAssociation = {
...toMath,
identifier: 'within',
destinationNodeURI: { title: 'SCI.2', identifier: 'sci-2', uri: '/ims/case/v1p1/CFItems/sci-2' },
}
const framework = science('alignmentFramework', [toMath, withinScience])
framework.items.set('sci-2' as ItemId, { id: 'sci-2' as ItemId, statement: 'SCI.2' })
const { port, stored, saveFramework } = fakeServer([toMath, withinScience])
await saveWithAlignmentStorage({ framework, cfPackage: { CFAssociations: [toMath, withinScience] }, saveFramework, port })

expect(associationIds(stored.get('doc-sci'))).toEqual(['within'])
expect(associationIds(stored.get('af-new-1'))).toEqual(['to-math'])
})

it('adds to the alignment framework it already owns for that target', async () => {
const earlier = { ...toMath, identifier: 'earlier' }
const { port, stored, saveFramework } = fakeServer([toMath], [alignmentFramework('doc-math', [earlier])])
Expand Down
57 changes: 57 additions & 0 deletions apps/editor/src/domain/framework/externalAlignments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
groupExternalAlignments,
hostedLookupCandidates,
isRemoteTargetId,
selfAlignmentsOf,
type ExternalAlignment,
} from './externalAlignments'
import type { Association, Framework, Item } from '@/domain/framework/model/types'
Expand Down Expand Up @@ -106,6 +107,62 @@ describe('externalAlignmentsOf', () => {
expect(a.originUri).toBe('https://case.example/CFItems/item-1')
expect(a.destinationUri).toBe('urn:case:item:other-item')
})

it('treats items the caller counts as its own (e.g. added in the editor) as inside the framework', () => {
const fw = framework(items, [
assoc('to-new', 'item-1', 'new-item'),
assoc('from-new', 'new-item', 'other-item'),
])
const isOwnItem = (id: string) => ['item-1', 'item-2', 'new-item'].includes(id)

expect(externalAlignmentsOf(fw, isOwnItem).map((a) => a.id)).toEqual(['from-new'])
})
})

describe('selfAlignmentsOf', () => {
const items = [item('item-1'), item('item-2'), item('item-3')]

it('returns non-hierarchical associations between two items of the framework', () => {
const fw = framework(items, [
assoc('a-1', 'item-1', 'item-2', 'precedes', { destinationTitle: 'Item 2' }),
assoc('a-2', 'item-3', 'item-1', 'isRelatedTo', { destinationTargetType: 'CASE' }),
])

expect(selfAlignmentsOf(fw)).toEqual([
{
id: 'a-1',
fromItemId: 'item-1',
toItemId: 'item-2',
associationType: 'precedes',
originUri: 'https://case.example/CFItems/item-1',
destinationUri: 'urn:case:item:item-2',
destinationTitle: 'Item 2',
destinationTargetType: undefined,
},
expect.objectContaining({ id: 'a-2', fromItemId: 'item-3', toItemId: 'item-1' }),
])
})

it('skips hierarchy, links to itself, the CFDocument, outside nodes and non-CASE targets', () => {
const fw = framework(items, [
assoc('child', 'item-2', 'item-1', 'isChildOf'),
assoc('part', 'item-3', 'item-1', 'isPartOf'),
assoc('loop', 'item-1', 'item-1'),
assoc('doc', 'item-1', 'doc-1', 'exactMatchOf'),
assoc('outside', 'item-1', 'other-item'),
assoc('incoming', 'other-item', 'item-1'),
assoc('non-case', 'item-1', 'item-2', 'isRelatedTo', { destinationTargetType: 'ext:outcomes' }),
])

expect(selfAlignmentsOf(fw)).toEqual([])
})

it('includes items the caller counts as its own', () => {
const fw = framework(items, [assoc('a-1', 'item-1', 'new-item')])

expect(selfAlignmentsOf(fw).map((a) => a.id)).toEqual([])
expect(selfAlignmentsOf(fw, (id) => id === 'new-item' || fw.items.has(id as ItemId)).map((a) => a.id)).toEqual(['a-1'])
})
})

describe('hostedLookupCandidates', () => {
Expand Down
76 changes: 56 additions & 20 deletions apps/editor/src/domain/framework/externalAlignments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Framework } from '@/domain/framework/model/types'
import type { Association, Framework } from '@/domain/framework/model/types'
import type { ItemId } from '@/domain/shared/types'

/**
* A non-hierarchical association stored in this framework's own CFPackage
Expand Down Expand Up @@ -33,6 +34,17 @@ export function isRemoteTargetId(id: string): boolean {
return id.startsWith(REMOTE_TARGET_PREFIX)
}

/**
* Whether an identifier is one of the framework's own items. Defaults to the
* items of the framework as given; the editor passes its live items, so items
* added in this session count too.
*/
export type IsOwnItem = (identifier: string) => boolean

function itemsOf(framework: Framework): IsOwnItem {
return (identifier) => framework.items.has(identifier as ItemId)
}

/**
* The framework's inline external alignments: non-hierarchical associations
* from one of its items to a node that isn't one of its items.
Expand All @@ -41,36 +53,60 @@ export function isRemoteTargetId(id: string): boolean {
* framework's `exactMatchOf` to each subject framework) aren't item-to-item
* alignments and are left out.
*/
export function externalAlignmentsOf(framework: Framework): ExternalAlignment[] {
export function externalAlignmentsOf(framework: Framework, isOwnItem: IsOwnItem = itemsOf(framework)): ExternalAlignment[] {
const frameworkId = String(framework.id)
const result: ExternalAlignment[] = []
for (const a of framework.associations.values()) {
if (HIERARCHICAL_TYPES.has(a.associationType)) continue
if (!framework.items.has(a.fromItemId)) continue
if (framework.items.has(a.toItemId)) continue
const toItemId = String(a.toItemId)
if (toItemId === frameworkId) continue
if (!isOwnItem(String(a.fromItemId))) continue
if (isOwnItem(String(a.toItemId))) continue
if (String(a.toItemId) === frameworkId) continue

const md = a.metadata ?? {}
const destinationUri = md.destinationUri ?? `urn:case:item:${toItemId}`
if (/\/CFDocuments\//.test(destinationUri)) continue
const alignment = toAlignment(framework, a)
if (/\/CFDocuments\//.test(alignment.destinationUri)) continue
result.push(alignment)
}
return result
}

/**
* The framework's self alignments: non-hierarchical associations from one of
* its items to another of its items (e.g. `precedes` or `isRelatedTo` within
* the framework). CASE 1.1 keeps them in the framework itself, like its inline
* external alignments; the Tree View shows them under the framework as its own
* target. Non-CASE targets aren't items and are left out.
*/
export function selfAlignmentsOf(framework: Framework, isOwnItem: IsOwnItem = itemsOf(framework)): ExternalAlignment[] {
const result: ExternalAlignment[] = []
for (const a of framework.associations.values()) {
if (HIERARCHICAL_TYPES.has(a.associationType)) continue
const fromItemId = String(a.fromItemId)
const itemUri = (framework.items.get(a.fromItemId)?.metadata as Record<string, unknown> | undefined)?.caseUri
result.push({
id: String(a.id),
fromItemId,
toItemId,
associationType: a.associationType,
originUri: md.originUri ?? (typeof itemUri === 'string' ? itemUri : `urn:case:item:${fromItemId}`),
destinationUri,
destinationTitle: md.destinationTitle,
destinationTargetType: md.destinationTargetType,
})
const toItemId = String(a.toItemId)
if (fromItemId === toItemId || !isOwnItem(fromItemId) || !isOwnItem(toItemId)) continue
const targetType = a.metadata?.destinationTargetType
if (targetType && targetType !== 'CASE') continue
result.push(toAlignment(framework, a))
}
return result
}

function toAlignment(framework: Framework, a: Association): ExternalAlignment {
const md = a.metadata ?? {}
const fromItemId = String(a.fromItemId)
const toItemId = String(a.toItemId)
const itemUri = (framework.items.get(a.fromItemId)?.metadata as Record<string, unknown> | undefined)?.caseUri
return {
id: String(a.id),
fromItemId,
toItemId,
associationType: a.associationType,
originUri: md.originUri ?? (typeof itemUri === 'string' ? itemUri : `urn:case:item:${fromItemId}`),
destinationUri: md.destinationUri ?? `urn:case:item:${toItemId}`,
destinationTitle: md.destinationTitle,
destinationTargetType: md.destinationTargetType,
}
}

/** Destination identifiers worth asking the server about — non-CASE targets can never be hosted. */
export function hostedLookupCandidates(alignments: ExternalAlignment[]): string[] {
return [...new Set(alignments.filter((a) => !isNonCaseTarget(a)).map((a) => a.toItemId))]
Expand Down
Loading
Loading