Skip to content

Commit

Permalink
adapt to ID generation changes in KLighD (#152)
Browse files Browse the repository at this point in the history
* adapt to ID generation changes in KLighD

* make ID update work even with non-updated KLighD
  • Loading branch information
NiklasRentzCAU authored Feb 26, 2024
1 parent fb33953 commit 2b5de15
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
63 changes: 32 additions & 31 deletions packages/klighd-core/src/skgraph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* http://rtsys.informatik.uni-kiel.de/kieler
*
* Copyright 2019-2022 by
* Copyright 2019-2023 by
* + Kiel University
* + Department of Computer Science
* + Real-Time and Embedded Systems Group
Expand Down Expand Up @@ -104,44 +104,45 @@ export function findRendering(element: SKGraphElement, id: string): KRendering |
let currentElement: KRendering = element.data.find((possibleRendering) =>
isRendering(possibleRendering)
) as KRendering
const idPath = id.split('$')
// The real rendering ID starts after the graph element ID prefix, delimited by a $$$.
const renderingId = id.split('$$$')[1] ?? id
if (renderingId === undefined) {
return undefined
}
const idPath = renderingId.split('$')
if (currentElement.type === K_RENDERING_REF) {
// KRenderingRefs' ids always start with the identifying name of the reference and may continue with $<something> to refer to renderings within that reference.
// Start with index 1 since the currentElement already contains the rendering with the identifying name.
// for (let i = 1; i < idPath.length; i++) {
if (idPath.length > 1) {
console.error('looking up renderings in rendering references is not supported yet.')
return undefined
// TODO:looking up renderings in rendering references is not supported yet.
return undefined
}
// The rendering id is build hierarchically and the first rendering is already found, so start with index 1 as a $ sign can be skipped.
for (let i = 1; i < idPath.length; i++) {
let nextElement
if (isContainerRendering(currentElement)) {
// First, look for the ID in the child renderings.
nextElement = currentElement.children.find((childRendering) =>
id.startsWith(childRendering.properties['klighd.lsp.rendering.id'] as string)
) as KRendering
}
} else {
// The rendering id is build hierarchically and the first rendering is already found, so start with index 1 as a $ sign can be skipped.
for (let i = 1; i < idPath.length; i++) {
let nextElement
if (isContainerRendering(currentElement)) {
// First, look for the ID in the child renderings.
nextElement = currentElement.children.find((childRendering) =>
id.startsWith(childRendering.properties['klighd.lsp.rendering.id'] as string)
) as KRendering
}
if (nextElement === undefined && currentElement.type === K_POLYLINE) {
// If the rendering was not found yet, take the junction point rendering.
if (
id.startsWith(
(currentElement as KPolyline).junctionPointRendering.properties[
'klighd.lsp.rendering.id'
] as string
)
) {
nextElement = (currentElement as KPolyline).junctionPointRendering
}
if (nextElement === undefined && currentElement.type === K_POLYLINE) {
// If the rendering was not found yet, take the junction point rendering.
if (
id.startsWith(
(currentElement as KPolyline).junctionPointRendering.properties['klighd.lsp.rendering.id'] as string
)
) {
nextElement = (currentElement as KPolyline).junctionPointRendering
}
if (nextElement === undefined) {
// This ID does not exist in the renderings, therefore does not belong to them.
return undefined
}
currentElement = nextElement
}
if (nextElement === undefined) {
// This ID does not exist in the renderings, therefore does not belong to them.
return undefined
}
currentElement = nextElement
}

// Now the currentElement should be the element searched for by the id.
if ((currentElement.properties['klighd.lsp.rendering.id'] as string) !== id) {
console.error(`The found element does not match the searched id! id: ${id}, found element: ${currentElement}`)
Expand Down
4 changes: 3 additions & 1 deletion packages/klighd-core/src/views-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,9 @@ export function getKRendering(datas: KGraphData[], context: SKGraphModelRenderer
for (const data of datas) {
if (data !== null && data.type === K_RENDERING_REF) {
if (context.kRenderingLibrary) {
const id = (data as KRenderingRef).properties['klighd.lsp.rendering.id'] as string
let id = (data as KRenderingRef).properties['klighd.lsp.rendering.id'] as string
// trim the ID to remove the leading parent graph element ID that is prefixed in rendering refs
id = id.substring(id.indexOf('$$lib$$$'))
for (const rendering of context.kRenderingLibrary.renderings) {
if (((rendering as KRendering).properties['klighd.lsp.rendering.id'] as string) === id) {
context.boundsMap = (data as KRenderingRef).properties[
Expand Down

0 comments on commit 2b5de15

Please sign in to comment.