From 6561a61a633ffc8e386be52ca63574fecf844524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Tue, 28 Jan 2020 18:08:22 -0800 Subject: [PATCH] Generate Weights from PluginDeclarations (#1595) This adds a simple method, `weightsForDeclaration`, which generates weights from a plugin declaration. This is a small but important piece of #1557, as it allows us to create appropriate Weights cleanly from the plugin. Test plan: Unit tests added; `yarn test` passes. --- src/analysis/pluginDeclaration.js | 14 +++++++ src/analysis/pluginDeclaration.test.js | 55 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/analysis/pluginDeclaration.test.js diff --git a/src/analysis/pluginDeclaration.js b/src/analysis/pluginDeclaration.js index e73deb1..7811787 100644 --- a/src/analysis/pluginDeclaration.js +++ b/src/analysis/pluginDeclaration.js @@ -2,6 +2,8 @@ import {type NodeAddressT, type EdgeAddressT} from "../core/graph"; import type {EdgeType, NodeType, NodeAndEdgeTypes} from "./types"; +import * as Weights from "../core/weights"; +import {type Weights as WeightsT} from "../core/weights"; // TODO(@decentralion): Maybe merge this file with analysis/types @@ -28,3 +30,15 @@ export function combineTypes( const edgeTypes = [].concat(...decs.map((x) => x.edgeTypes)); return {nodeTypes, edgeTypes}; } + +export function weightsForDeclaration(dec: PluginDeclaration): WeightsT { + const weights = Weights.empty(); + const {nodeTypes, edgeTypes} = dec; + for (const {prefix, defaultWeight} of nodeTypes) { + weights.nodeWeights.set(prefix, defaultWeight); + } + for (const {prefix, defaultWeight} of edgeTypes) { + weights.edgeWeights.set(prefix, defaultWeight); + } + return weights; +} diff --git a/src/analysis/pluginDeclaration.test.js b/src/analysis/pluginDeclaration.test.js new file mode 100644 index 0000000..e3428e9 --- /dev/null +++ b/src/analysis/pluginDeclaration.test.js @@ -0,0 +1,55 @@ +// @flow + +import deepFreeze from "deep-freeze"; +import {NodeAddress, EdgeAddress} from "../core/graph"; +import {type NodeType, type EdgeType} from "./types"; +import { + weightsForDeclaration, + type PluginDeclaration, +} from "./pluginDeclaration"; +import * as Weights from "../core/weights"; + +describe("analysis/pluginDeclaration", () => { + const nodeType: NodeType = deepFreeze({ + name: "node", + pluralName: "nodes", + prefix: NodeAddress.fromParts(["node"]), + defaultWeight: 2, + description: "a type", + }); + const edgeType: EdgeType = deepFreeze({ + forwardName: "points", + backwardName: "is pointed to", + prefix: EdgeAddress.fromParts(["edge"]), + defaultWeight: {forwards: 2, backwards: 3}, + description: "a type", + }); + const emptyDeclaration: PluginDeclaration = deepFreeze({ + name: "empty", + nodePrefix: NodeAddress.empty, + edgePrefix: EdgeAddress.empty, + nodeTypes: [], + edgeTypes: [], + userTypes: [], + }); + const nonEmptyDeclaration: PluginDeclaration = deepFreeze({ + name: "non-empty", + nodePrefix: NodeAddress.empty, + edgePrefix: EdgeAddress.empty, + nodeTypes: [nodeType], + edgeTypes: [edgeType], + userTypes: [], + }); + describe("weightsForDeclaration", () => { + it("works for an empty declaration", () => { + expect(weightsForDeclaration(emptyDeclaration)).toEqual(Weights.empty()); + }); + it("works for a non-empty declaration", () => { + const expected = Weights.empty(); + expected.nodeWeights.set(nodeType.prefix, nodeType.defaultWeight); + expected.edgeWeights.set(edgeType.prefix, edgeType.defaultWeight); + const actual = weightsForDeclaration(nonEmptyDeclaration); + expect(expected).toEqual(actual); + }); + }); +});