Skip to content

Commit bcae77e

Browse files
authored
Merge pull request #48 from ut-code/validation
Validation
2 parents 38a544d + a80a36f commit bcae77e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/store/component.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,72 @@ export function isIncluding(
154154
};
155155
return dfs(componentId);
156156
}
157+
158+
function validateComponent(store: CCStore, componentId: CCComponentId) {
159+
const component = store.components.get(componentId)!;
160+
if (component.isIntrinsic) return;
161+
162+
const nodes = store.nodes.getManyByParentComponentId(componentId);
163+
const connections = store.connections.getManyByParentComponentId(componentId);
164+
const parentComponentPins =
165+
store.componentPins.getManyByComponentId(componentId);
166+
167+
// check nodePins and componentPins of each node
168+
for (const node of nodes) {
169+
const nodePins = new Set(store.nodePins.getManyByNodeId(node.id));
170+
const componentPinIds = new Set(
171+
store.componentPins
172+
.getManyByComponentId(node.componentId)
173+
.map((pin) => pin.id)
174+
);
175+
invariant(componentPinIds.size === nodePins.size);
176+
for (const nodePin of nodePins) {
177+
invariant(componentPinIds.has(nodePin.componentPinId));
178+
179+
// check whether each nodePin without connections corresponds to a parentComponentPin
180+
// and nodePin with connections corresponds to no parentComponentPin
181+
const connectionsAssociatedWithNodePin =
182+
store.connections.getConnectionsByNodePinId(nodePin.id);
183+
const parentComponentPin = parentComponentPins.find(
184+
(pin) => pin.implementation === nodePin.id
185+
);
186+
if (connectionsAssociatedWithNodePin.length === 0) {
187+
invariant(parentComponentPin);
188+
} else {
189+
invariant(!parentComponentPin);
190+
}
191+
}
192+
}
193+
194+
// check whether each connection has valid nodePins and componentPins
195+
for (const connection of connections) {
196+
const fromNodePin = store.nodePins.get(connection.from);
197+
const toNodePin = store.nodePins.get(connection.to);
198+
invariant(fromNodePin && toNodePin);
199+
invariant(fromNodePin.nodeId !== toNodePin.nodeId);
200+
const fromComponentPin = store.componentPins.get(
201+
fromNodePin.componentPinId
202+
);
203+
const toComponentPin = store.componentPins.get(toNodePin.componentPinId);
204+
invariant(fromComponentPin && toComponentPin);
205+
invariant(fromComponentPin.type !== toComponentPin.type);
206+
}
207+
208+
// check whether implementation of each componentPin has no connections
209+
for (const componentPin of parentComponentPins) {
210+
invariant(componentPin.implementation);
211+
const implementationNodePin = store.nodePins.get(
212+
componentPin.implementation
213+
);
214+
invariant(implementationNodePin);
215+
const connectionsAssociatedWithNodePin =
216+
store.connections.getConnectionsByNodePinId(implementationNodePin.id);
217+
invariant(connectionsAssociatedWithNodePin.length === 0);
218+
}
219+
}
220+
221+
export function validateAllComponents(store: CCStore) {
222+
for (const component of store.components.toArray()) {
223+
validateComponent(store, component.id);
224+
}
225+
}

src/store/connection.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ export class CCConnectionStore extends EventEmitter<CCConnectionStoreEvents> {
117117
.map((connection) => connection.id);
118118
}
119119

120+
getManyByParentComponentId(parentComponentId: CCComponentId): CCConnection[] {
121+
return [...this.#connections.values()].filter(
122+
(connection) => connection.parentComponentId === parentComponentId
123+
);
124+
}
125+
120126
/**
121127
* Get connections by id of node and pin
122128
* @param nodeId id of node

0 commit comments

Comments
 (0)