Skip to content

Commit

Permalink
refactor(zeroline/foundation): improve ied allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Vogelsang committed Jul 21, 2021
1 parent b9afb82 commit 174bf39
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions src/zeroline/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,71 @@ function containsReference(element: Element, iedName: string): boolean {
);
}

function isMultiparent(element: Element, iedName: string): boolean {
function isReferencedItself(element: Element, iedName: string): boolean {
return (
(<Element[]>Array.from(element.children)).filter(element =>
containsReference(element, iedName)
).length > 1
(<Element[]>Array.from(element.children)).filter(
child =>
child.tagName === 'LNode' && child.getAttribute('iedName') === iedName
).length !== 0
);
}

function isUniquechild(element: Element, iedName: string): boolean {
function hasReferencedChildren(
element: Element,
iedName: string,
multi: boolean
): boolean {
const threshold = multi ? 1 : 0;
return (
(<Element[]>Array.from(element.children)).filter(child =>
containsReference(child, iedName)
).length > threshold
);
}

function isReferencedAside(element: Element, iedName: string): boolean {
const isUnique =
(<Element[]>Array.from(element.children)).filter(element =>
containsReference(element, iedName)
(<Element[]>Array.from(element.children)).filter(child =>
containsReference(child, iedName)
).length === 1;

if (!isUnique) return false;
if (element.parentNode instanceof XMLDocument) return isUnique;
if (isUnique) return isUniquechild(element.parentElement!, iedName);
if (!isUnique) return true;
if (element.parentElement && !(element.parentElement instanceof XMLDocument))
return isReferencedAside(element.parentElement, iedName);

return true;
return false;
}

function isReference(element: Element, iedName: string): boolean {
return (
(<Element[]>Array.from(element.children)).filter(
element =>
element.tagName === 'LNode' &&
element.getAttribute('iedName') === iedName
).length !== 0
);
function isReferencedAbove(element: Element, iedName: string): boolean {
const isReferenced = isReferencedItself(element, iedName);

if (isReferenced) return true;

if (element.parentElement && !(element.parentElement instanceof XMLDocument))
return isReferencedAbove(element.parentElement, iedName);

return false;
}

export function attachedIeds(element: Element): Element[] {
const doc = element.ownerDocument;

const ieds = Array.from(doc.querySelectorAll(':root > IED'));

const attachedIeds: Element[] = [];

ieds.forEach(ied => {
const iedName = ied.getAttribute('name')!;
if (
(isMultiparent(element, iedName) &&
isUniquechild(element.parentElement!, iedName)) ||
(isReference(element, iedName) &&
isUniquechild(element.parentElement!, iedName))
)
attachedIeds.push(ied);

const belongsHere =
hasReferencedChildren(
element,
iedName,
element.tagName === 'Bay' ? false : true
) || isReferencedItself(element, iedName);
const belongsAbove =
isReferencedAbove(element.parentElement!, iedName) ||
isReferencedAside(element.parentElement!, iedName);

if (!belongsAbove && belongsHere) attachedIeds.push(ied);
});

return attachedIeds;
Expand All @@ -83,7 +101,7 @@ export function unreferencedIeds(doc: XMLDocument): Element[] {
ieds.forEach(ied => {
const iedName = ied.getAttribute('name')!;
if (
isMultiparent(root, iedName) ||
hasReferencedChildren(root, iedName, true) ||
Array.from(doc.querySelectorAll('LNode'))
.filter(isPublic)
.filter(lnode => lnode.getAttribute('iedName') === iedName).length === 0
Expand Down

0 comments on commit 174bf39

Please sign in to comment.