Skip to content

Commit

Permalink
feat(uml): implement element import handler
Browse files Browse the repository at this point in the history
  • Loading branch information
DerYeger committed Dec 19, 2023
1 parent 0aac9e4 commit 9a274b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/uml-parser/src/metamodel/handlers/ElementImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { GraphNode } from '@cm2ml/ir'

import { Uml } from '../../uml'
import { ElementImport } from '../metamodel'

export const ElementImportHandler = ElementImport.createHandler(
(elementImport) => {
addEdge_importedElement(elementImport)
addEdge_importingNamespace(elementImport)
},
)

function addEdge_importedElement(elementImport: GraphNode) {
const importedElementId = elementImport.getAttribute(
Uml.Attributes.importedElement,
)?.value.literal
if (!importedElementId) {
throw new Error('Missing importedElement attribute on ElementImport')
}
const importedElement = elementImport.model.getNodeById(importedElementId)
if (!importedElement) {
throw new Error(
`Missing importedElement with id ${importedElementId} for ElementImport`,
)
}
elementImport.model.addEdge('importedElement', elementImport, importedElement)
}

function addEdge_importingNamespace(elementImport: GraphNode) {
const parent = elementImport.parent
if (!parent) {
throw new Error('Missing parent for ElementImport')
}
elementImport.model.addEdge('importingNamespace', elementImport, parent)
}
2 changes: 2 additions & 0 deletions packages/uml-parser/src/metamodel/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ClassHandler } from './Class'
import { DataTypeHandler } from './DataType'
import { DependencyHandler } from './Dependency'
import { ElementHandler } from './Element'
import { ElementImportHandler } from './ElementImport'
import { EnumerationHandler } from './Enumeration'
import { EnumerationLiteralHandler } from './EnumerationLiteral'
import { GeneralizationHandler } from './Generalization'
Expand Down Expand Up @@ -37,6 +38,7 @@ export const handlers: Record<`${string}Handler`, MetamodelElement> = {
DataTypeHandler,
DependencyHandler,
ElementHandler,
ElementImportHandler,
EnumerationHandler,
EnumerationLiteralHandler,
GeneralizationHandler,
Expand Down

0 comments on commit 9a274b0

Please sign in to comment.