Skip to content

Commit

Permalink
fix: add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
trusz committed Dec 9, 2024
1 parent 9e8a1cd commit 08aee5b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/core/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ export type {
EditCompletedEvent,
EditCompletedDetail,
} from './foundation/edit-completed-event.js';

/** @returns the cartesian product of `arrays` */
export function crossProduct<T>(...arrays: T[][]): T[][] {
return arrays.reduce<T[][]>(
(a, b) => <T[][]>a.flatMap(d => b.map(e => [d, e].flat())),
[[]]
);
}
89 changes: 89 additions & 0 deletions packages/core/foundation/scl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { crossProduct } from '../foundation.js';

function getDataModelChildren(parent: Element): Element[] {
if (['LDevice', 'Server'].includes(parent.tagName))
return Array.from(parent.children).filter(
child =>
child.tagName === 'LDevice' ||
child.tagName === 'LN0' ||
child.tagName === 'LN'
);

const id =
parent.tagName === 'LN' || parent.tagName === 'LN0'
? parent.getAttribute('lnType')
: parent.getAttribute('type');

return Array.from(
parent.ownerDocument.querySelectorAll(
`LNodeType[id="${id}"] > DO, DOType[id="${id}"] > SDO, DOType[id="${id}"] > DA, DAType[id="${id}"] > BDA`
)
);
}

export function existFcdaReference(fcda: Element, ied: Element): boolean {
const [ldInst, prefix, lnClass, lnInst, doName, daName, fc] = [
'ldInst',
'prefix',
'lnClass',
'lnInst',
'doName',
'daName',
'fc',
].map(attr => fcda.getAttribute(attr));

const sinkLdInst = ied.querySelector(`LDevice[inst="${ldInst}"]`);
if (!sinkLdInst) return false;

const prefixSelctors = prefix
? [`[prefix="${prefix}"]`]
: ['[prefix=""]', ':not([prefix])'];
const lnInstSelectors = lnInst
? [`[inst="${lnInst}"]`]
: ['[inst=""]', ':not([inst])'];

const anyLnSelector = crossProduct(
['LN0', 'LN'],
prefixSelctors,
[`[lnClass="${lnClass}"]`],
lnInstSelectors
)
.map(strings => strings.join(''))
.join(',');

const sinkAnyLn = ied.querySelector(anyLnSelector);
if (!sinkAnyLn) return false;

const doNames = doName?.split('.');
if (!doNames) return false;

let parent: Element | undefined = sinkAnyLn;
for (const doNameAttr of doNames) {
parent = getDataModelChildren(parent).find(
child => child.getAttribute('name') === doNameAttr
);
if (!parent) return false;
}

const daNames = daName?.split('.');
const someFcInSink = getDataModelChildren(parent).some(
da => da.getAttribute('fc') === fc
);
if (!daNames && someFcInSink) return true;
if (!daNames) return false;

let sinkFc = '';
for (const daNameAttr of daNames) {
parent = getDataModelChildren(parent).find(
child => child.getAttribute('name') === daNameAttr
);

if (parent?.getAttribute('fc')) sinkFc = parent.getAttribute('fc')!;

if (!parent) return false;
}

if (sinkFc !== fc) return false;

return true;
}
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"exports": {
".": "./dist/foundation.js",
"./foundation/scl.js": "./dist/foundation/scl.js",
"./foundation/deprecated/editor.js": "./dist/foundation/deprecated/editor.js",
"./foundation/deprecated/open-event.js": "./dist/foundation/deprecated/open-event.js",
"./foundation/deprecated/settings.js": "./dist/foundation/deprecated/settings.js",
Expand Down Expand Up @@ -159,4 +160,4 @@
"prettier --write"
]
}
}
}

0 comments on commit 08aee5b

Please sign in to comment.