Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakae committed Dec 21, 2023
1 parent 9e63afa commit a1b2800
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from "../../generated/ast";
import { namedFtaElement } from "../utils";

/* element for which the cut sets were determined */
export let topOfAnalysis: string | undefined;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ export class FtaDiagramGenerator extends LangiumDiagramGenerator {
/** Saves the Ids of the generated SNodes */
protected idToSNode: Map<string, SNode> = new Map();

/** Saves the parent node of a gate */
protected parentOfGate: Map<string, SNode> = new Map();
/** Saves the description node of a gate */
protected descriptionOfGate: Map<string, SNode> = new Map();
/** Saves the port of a node */
protected nodeToPort: Map<string, FTAPort> = new Map();

constructor(services: FtaServices) {
Expand Down
6 changes: 6 additions & 0 deletions extension/src-language-server/fta/diagram/fta-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ export interface FTANode extends SNode {
n?: number;
}

/**
* FTA Graph.
*/
export interface FTAGraph extends SGraph {
modelOrder?: boolean;
}

/**
* Description node of a fault tree.
*/
export interface DescriptionNode extends SNode {
name: string;
inCurrentSelectedCutSet?: boolean;
Expand Down
12 changes: 12 additions & 0 deletions extension/src-language-server/fta/diagram/fta-synthesis-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const analysisCategoryOption: ValuedSynthesisOption = {
currentValue: 0,
};

/**
* Boolean option to toggle the visualization gate descriptions.
*/
const showGateDescriptionsOptions: ValuedSynthesisOption = {
synthesisOption: {
id: showGateDescriptionsID,
Expand All @@ -61,6 +64,9 @@ const showGateDescriptionsOptions: ValuedSynthesisOption = {
currentValue: true,
};

/**
* Boolean option to toggle the visualization node descriptions.
*/
const showComponentDescriptionsOptions: ValuedSynthesisOption = {
synthesisOption: {
id: showComponentDescriptionsID,
Expand All @@ -74,6 +80,9 @@ const showComponentDescriptionsOptions: ValuedSynthesisOption = {
currentValue: false,
};

/**
* Option to highlight the components of a cut set.
*/
const cutSets: ValuedSynthesisOption = {
synthesisOption: {
id: cutSetsID,
Expand Down Expand Up @@ -123,6 +132,9 @@ export class FtaSynthesisOptions extends SynthesisOptions {
}
}

/**
* Resets the cutSets option to no cut set.
*/
resetCutSets(): void {
const option = this.getOption(cutSetsID);
if (option) {
Expand Down
2 changes: 1 addition & 1 deletion extension/src-language-server/fta/diagram/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function getFTNodeType(node: AstNode): FTNodeType {
*/
export function getTargets(node: AstNode): AstNode[] {
const targets: AstNode[] = [];
// only the top event and gates have children
// only the top event and gates have children/targets
if (isTopEvent(node) && node.child.ref) {
targets.push(node.child.ref);
}
Expand Down
5 changes: 2 additions & 3 deletions extension/src-language-server/fta/fta-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* SPDX-License-Identifier: EPL-2.0
*/

import { AstNode } from "langium";
import { LangiumSprottySharedServices } from "langium-sprotty";
import { Connection } from "vscode-languageserver";
import { ModelFTA } from "../generated/ast";
Expand Down Expand Up @@ -49,10 +48,10 @@ function addCutSetsHandler(
ftaServices: FtaServices,
sharedServices: LangiumSprottySharedServices
): void {
connection.onRequest("cutSets/generate", async (content: {uri: string, startId?: string}) => {
connection.onRequest("cutSets/generate", async (content: { uri: string; startId?: string }) => {
return cutSetsRequested(content.uri, ftaServices, sharedServices, false, content.startId);
});
connection.onRequest("cutSets/generateMinimal", async (content: {uri: string, startId?: string}) => {
connection.onRequest("cutSets/generateMinimal", async (content: { uri: string; startId?: string }) => {
return cutSetsRequested(content.uri, ftaServices, sharedServices, true, content.startId);
});
connection.onRequest("cutSets/reset", () => {
Expand Down
1 change: 1 addition & 0 deletions extension/src-language-server/fta/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { Range } from "vscode-languageserver";
import { Component, Condition, Gate, ModelFTA, TopEvent, isAND, isInhibitGate, isKNGate, isOR } from "../generated/ast";

/** FTA elements that have a name. */
export type namedFtaElement = Component | Condition | Gate | TopEvent;

/**
Expand Down
17 changes: 12 additions & 5 deletions extension/src-language-server/stpa/ftaGeneration/fta-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import type { Reference } from "langium";
import { LangiumSprottySharedServices } from "langium-sprotty";
import { Children, Component, Hazard, LossScenario, Model, ModelFTA, OR, TopEvent, UCA, isOR } from "../../generated/ast";
import { Children, Component, Hazard, LossScenario, Model, ModelFTA, OR, TopEvent } from "../../generated/ast";
import { getModel } from "../../utils";

/**
Expand All @@ -34,7 +34,7 @@ export async function createFaultTrees(uri: string, shared: LangiumSprottyShared
const scenarios: Map<string, LossScenario[]> = sortScenarios(model);
// create fault tree for each hazard
for (const hazard of model.hazards) {
faultTrees.push(createFaulTreeForHazard(model, scenarios, hazard));
faultTrees.push(createFaulTreeForHazard(scenarios, hazard));
}
return faultTrees;
}
Expand All @@ -58,11 +58,10 @@ function sortScenarios(model: Model): Map<string, LossScenario[]> {

/**
* Creates a fault tree with {@code hazard} as top event.
* @param stpaModel The stpa model that contains the {@code hazard}.
* @param hazard The hazard for which the fault tree should be created.
* @returns the AST of the created fault tree with {@code hazard} as top event.
*/
function createFaulTreeForHazard(stpaModel: Model, scenarios: Map<string, LossScenario[]>, hazard: Hazard): ModelFTA {
function createFaulTreeForHazard(scenarios: Map<string, LossScenario[]>, hazard: Hazard): ModelFTA {
const ftaModel = {} as ModelFTA;
ftaModel.components = [];
ftaModel.gates = [];
Expand Down Expand Up @@ -108,7 +107,9 @@ function createFaulTreeForHazard(stpaModel: Model, scenarios: Map<string, LossSc
}

// create gate to connect top event with all other gates
const gateChildren = ftaModel.gates.map((gate) => { return { ref: gate, $refText: gate.name } as Reference<Children>; });
const gateChildren = ftaModel.gates.map((gate) => {
return { ref: gate, $refText: gate.name } as Reference<Children>;
});
const gate = {
name: `G0`,
children: gateChildren,
Expand All @@ -129,6 +130,12 @@ function createFaulTreeForHazard(stpaModel: Model, scenarios: Map<string, LossSc
return ftaModel;
}

/**
* Adds {@code value} to the list of {@code map} at {@code key}.
* @param map The map to which the value should be added.
* @param key The key at which the value should be added.
* @param value The value which should be added.
*/
function addToListMap(map: Map<string, any[]>, key: string, value: any): void {
if (map.has(key)) {
const currentValues = map.get(key);
Expand Down
9 changes: 9 additions & 0 deletions extension/src-language-server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export async function getModel(
return currentDoc.parseResult.value;
}

/**
* Creates a list of labels containing the given {@code description} respecting the {@code labelManagement} and {@code labelWidth}.
* @param description The text for the label to create.
* @param labelManagement The label management option.
* @param labelWidth The desired width of the label.
* @param nodeId The id of the node for which the label is created.
* @param idCache The id cache.
* @returns a list of labels containing the given {@code description} respecting the {@code labelManagement} and {@code labelWidth}.
*/
export function getDescription(
description: string,
labelManagement: labelManagementValue,
Expand Down
4 changes: 3 additions & 1 deletion extension/src-webview/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { inject } from "inversify";
import { CommandExecutionContext, CommandResult, HiddenCommand, TYPES, isExportable, isHoverable, isSelectable, isViewport } from "sprotty";
import { RequestAction, ResponseAction, generateRequestId, Action } from "sprotty-protocol";
import { Action, RequestAction, ResponseAction, generateRequestId } from "sprotty-protocol";


/** Requests the current SVG from the client. */
Expand Down Expand Up @@ -93,6 +93,7 @@ export class SvgCommand extends HiddenCommand {
}
}

/** Send from client to server to start a cut set analysis with the start node given by the startId */
export interface CutSetAnalysisAction extends Action {
kind: typeof CutSetAnalysisAction.KIND;
startId: string
Expand All @@ -108,6 +109,7 @@ export namespace CutSetAnalysisAction {
}
}

/** Send from client to server to start a minimal cut set analysis with the start node given by the startId */
export interface MinimalCutSetAnalysisAction extends Action {
kind: typeof MinimalCutSetAnalysisAction.KIND;
startId: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class ContextMenuProvider implements IContextMenuItemProvider {
if (root.type === FTA_GRAPH_TYPE) {
// find node that was clicked on
let clickedNode: FTANode | undefined;

root.children.forEach((child) => {
if (child.type === FTA_NODE_TYPE) {
if ((child as FTANode).selected) {
Expand All @@ -41,6 +40,7 @@ export class ContextMenuProvider implements IContextMenuItemProvider {
}
}
});
// create context menu items
return Promise.resolve([
{
label: "Cut Set Analysis",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* SPDX-License-Identifier: EPL-2.0
*/

import { injectable, inject } from "inversify";
import { inject, injectable } from "inversify";
import { Anchor, IContextMenuService, MenuItem } from "sprotty";
import { ActionNotification } from "sprotty-vscode-protocol";
import { VsCodeMessenger } from "sprotty-vscode-webview/lib/services";
Expand Down
8 changes: 4 additions & 4 deletions extension/src-webview/di.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ import {
TYPES,
configureCommand,
configureModelElement,
contextMenuModule,
loadDefaultModules,
overrideViewerOptions,
contextMenuModule
overrideViewerOptions
} from "sprotty";
import { SvgCommand } from "./actions";
import { ContextMenuService } from "./context-menu/context-menu-services";
import { ContextMenuProvider } from "./context-menu/context-menu-provider";
import { ContextMenuService } from "./context-menu/context-menu-services";
import pastaContextMenuModule from "./context-menu/di.config";
import { SvgPostprocessor } from "./exportPostProcessor";
import { CustomSvgExporter } from "./exporter";
import { DescriptionNode, FTAEdge, FTAGraph, FTANode, FTAPort, FTA_DESCRIPTION_NODE_TYPE, FTA_EDGE_TYPE, FTA_GRAPH_TYPE, FTA_INVISIBLE_EDGE_TYPE, FTA_NODE_TYPE, FTA_PORT_TYPE } from "./fta/fta-model";
Expand Down Expand Up @@ -72,7 +73,6 @@ import {
STPAGraphView,
STPANodeView,
} from "./stpa/stpa-views";
import pastaContextMenuModule from "./context-menu/di.config";

const pastaDiagramModule = new ContainerModule((bind, unbind, isBound, rebind) => {
rebind(TYPES.ILogger).to(ConsoleLogger).inSingletonScope();
Expand Down
6 changes: 6 additions & 0 deletions extension/src-webview/fta/fta-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ export class FTANode extends SNode {
n?: number;
}

/**
* FTA Graph.
*/
export class FTAGraph extends SGraph {
modelOrder?: boolean;
}

/**
* Description node of a fault tree.
*/
export class DescriptionNode extends SNode {
static readonly DEFAULT_FEATURES = [
connectableFeature,
Expand Down
11 changes: 8 additions & 3 deletions extension/src-webview/fta/fta-views.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
/** @jsx svg */
import { injectable } from 'inversify';
import { VNode } from "snabbdom";
import { Hoverable, IViewArgs, Point, PolylineEdgeView, RectangularNodeView, RenderingContext, SEdge, SGraph, SGraphView, SShapeElement, Selectable, svg } from 'sprotty';
import { renderAndGate, renderEllipse, renderHorizontalLine, renderInhibitGate, renderKnGate, renderOrGate, renderOval, renderRectangle, renderRoundedRectangle, renderVerticalLine } from "../views-rendering";
import { IViewArgs, Point, PolylineEdgeView, RectangularNodeView, RenderingContext, SEdge, SGraph, SGraphView, svg } from 'sprotty';
import { renderAndGate, renderEllipse, renderHorizontalLine, renderInhibitGate, renderKnGate, renderOrGate, renderRectangle, renderRoundedRectangle, renderVerticalLine } from "../views-rendering";
import { DescriptionNode, FTAEdge, FTAGraph, FTANode, FTAPort, FTA_DESCRIPTION_NODE_TYPE, FTA_EDGE_TYPE, FTA_NODE_TYPE, FTA_PORT_TYPE, FTNodeType } from './fta-model';

@injectable()
Expand Down Expand Up @@ -56,6 +56,7 @@ export class FTAInvisibleEdgeView extends PolylineArrowEdgeViewFTA {
@injectable()
export class DescriptionNodeView extends RectangularNodeView {
render(node: DescriptionNode, context: RenderingContext): VNode | undefined {
// render the description node similar to an on edge label
const element = renderRectangle(node);
const border1 = renderHorizontalLine(node);
const border2 = renderHorizontalLine(node);
Expand Down Expand Up @@ -172,7 +173,11 @@ export class FTAGraphView extends SGraphView {

return super.render(model, context);
}

/**
* Highlights the nodes and edges connected to the selected cut set.
* @param model The FTAGraph.
* @param currentNode The current node, which should be handled including its targets.
*/
protected highlightConnectedToCutSet(model: SGraph, currentNode: FTANode): void {
for (const port of currentNode.children.filter(child => child.type === FTA_PORT_TYPE)) {
const edge = model.children.find(child => child.type === FTA_EDGE_TYPE && (child as FTAEdge).sourceId === port.id) as FTAEdge;
Expand Down
14 changes: 14 additions & 0 deletions extension/src-webview/views-rendering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export function renderOval(node: SNode): VNode {
ry={Math.max(node.size.height, 0) / 2.0} />;
}

/**
* Creates an ellipse for {@code node}.
* @param x The x-coordinate of the ellipse.
* @param y The y-coordinate of the ellipse.
* @param width The width of the ellipse.
* @param height The height of the ellipse.
* @param lineWidth The line width of the ellipse.
* @returns an ellipse for {@code node}.
*/
export function renderEllipse(x: number | undefined, y: number | undefined, width: number, height: number, lineWidth: number): VNode {
return <ellipse
{...(x && y ? { transform: `translate(${x},${y})` } : {})}
Expand Down Expand Up @@ -257,6 +266,11 @@ export function renderKnGate(node: SNode, k: number, n: number): VNode {
);
}

/**
* Creates an Or-Gate for {@code node}.
* @param node The node that should be represented by an Or-Gate.
* @returns an Or-Gate for {@code node}.
*/
function createOrGate(node: SNode): string {
const leftX = 0;
const rightX = Math.max(node.size.width, 0);
Expand Down
3 changes: 2 additions & 1 deletion extension/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export namespace GenerateSVGsAction {
}
}

/** Send from client to server to start a cut set analysis with the start node given by the startId */
export interface CutSetAnalysisAction extends Action {
kind: typeof CutSetAnalysisAction.KIND;
startId: string
Expand All @@ -76,7 +77,7 @@ export namespace CutSetAnalysisAction {
}
}


/** Send from client to server to start a minimal cut set analysis with the start node given by the startId */
export interface MinimalCutSetAnalysisAction extends Action {
kind: typeof MinimalCutSetAnalysisAction.KIND;
startId: string
Expand Down
Loading

0 comments on commit a1b2800

Please sign in to comment.