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.
This commit is contained in:
parent
02b072699e
commit
6561a61a63
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue