diff --git a/src/analysis/fallbackDeclaration.js b/src/analysis/fallbackDeclaration.js index 717960e..1ebe692 100644 --- a/src/analysis/fallbackDeclaration.js +++ b/src/analysis/fallbackDeclaration.js @@ -20,8 +20,7 @@ export const fallbackNodeType: NodeType = Object.freeze({ export const fallbackEdgeType: EdgeType = Object.freeze({ forwardName: "points to", backwardName: "is pointed to by", - defaultForwardWeight: 1, - defaultBackwardWeight: 1, + defaultWeight: {forwards: 1, backwards: 1}, prefix: EdgeAddress.empty, description: "The fallback EdgeType for edges which don't have any other type", diff --git a/src/analysis/types.js b/src/analysis/types.js index f046088..c7258de 100644 --- a/src/analysis/types.js +++ b/src/analysis/types.js @@ -1,6 +1,7 @@ // @flow import {type NodeAddressT, type EdgeAddressT} from "../core/graph"; +import {type EdgeWeight} from "./weights"; /** * This module defines `NodeType`s and `EdgeType`s, both of which are @@ -86,15 +87,10 @@ export type EdgeType = {| // is "referenced by" +backwardName: string, - // The default weight for the forward direction of this edge. + // The default forwards and backwards weights for this edge. // We use `1` as a default value ("of normal importance"). // The weights have linear importance, i.e. 2 is twice as important as 1. - +defaultForwardWeight: number, - - // The default weight for the backward direction of this edge. - // We use `1` as a default value ("of normal importance"). - // The weights have linear importance, i.e. 2 is twice as important as 1. - +defaultBackwardWeight: number, + +defaultWeight: EdgeWeight, // The address that will be used to test whether an edge is a member // of this EdgeType. A given edge `e` is a member of the type `t` if diff --git a/src/analysis/weights.js b/src/analysis/weights.js index 5a828aa..a2e3053 100644 --- a/src/analysis/weights.js +++ b/src/analysis/weights.js @@ -5,12 +5,26 @@ import {type NodeAddressT, type EdgeAddressT} from "../core/graph"; import type {EdgeType, NodeType} from "./types"; import type {PluginDeclaration} from "./pluginDeclaration"; -export type WeightedNodeType = {|+type: NodeType, +weight: number|}; +/** + * Represents the weight for a particular Node (or NodeType). + * Weight 1 is the default value and signifies normal importance. + * Weights are linear, so 2 is twice as important as 1. + */ +export type NodeWeight = number; + +/** + * Represents the forwards and backwards weights for a particular Edge (or + * EdgeType). + * Weight 1 is the default value and signifies normal importance. + * Weights are linear, so 2 is twice as important as 1. + */ +export type EdgeWeight = {|+forwards: number, +backwards: number|}; + +export type WeightedNodeType = {|+type: NodeType, +weight: NodeWeight|}; export type WeightedEdgeType = {| +type: EdgeType, - +forwardWeight: number, - +backwardWeight: number, + +weight: EdgeWeight, |}; export type WeightedTypes = {| @@ -31,8 +45,7 @@ export function defaultWeightedNodeType(type: NodeType): WeightedNodeType { export function defaultWeightedEdgeType(type: EdgeType): WeightedEdgeType { return { type, - forwardWeight: type.defaultForwardWeight, - backwardWeight: type.defaultBackwardWeight, + weight: type.defaultWeight, }; } diff --git a/src/analysis/weights.test.js b/src/analysis/weights.test.js index 92c08fc..669e381 100644 --- a/src/analysis/weights.test.js +++ b/src/analysis/weights.test.js @@ -24,8 +24,8 @@ describe("analysis/weights", () => { describe("defaultWeightedEdgeType", () => { it("sets default weights as specified in the type", () => { const wet = defaultWeightedEdgeType(assemblesEdgeType); - expect(wet.forwardWeight).toEqual(wet.type.defaultForwardWeight); - expect(wet.backwardWeight).toEqual(wet.type.defaultBackwardWeight); + expect(wet.weight.forwards).toEqual(wet.type.defaultWeight.forwards); + expect(wet.weight.backwards).toEqual(wet.type.defaultWeight.backwards); }); }); describe("defaultWeightsForDeclaration", () => { diff --git a/src/analysis/weightsToEdgeEvaluator.js b/src/analysis/weightsToEdgeEvaluator.js index 21319c7..85af74e 100644 --- a/src/analysis/weightsToEdgeEvaluator.js +++ b/src/analysis/weightsToEdgeEvaluator.js @@ -15,8 +15,8 @@ export function weightsToEdgeEvaluator( nodeTrie.add(type.prefix, weight); } const edgeTrie = new EdgeTrie(); - for (const {type, forwardWeight, backwardWeight} of weights.edges.values()) { - edgeTrie.add(type.prefix, {forwardWeight, backwardWeight}); + for (const {type, weight} of weights.edges.values()) { + edgeTrie.add(type.prefix, weight); } function nodeWeight(n: NodeAddressT): number { @@ -28,10 +28,10 @@ export function weightsToEdgeEvaluator( return function evaluator(edge: Edge) { const srcWeight = nodeWeight(edge.src); const dstWeight = nodeWeight(edge.dst); - const {forwardWeight, backwardWeight} = edgeTrie.getLast(edge.address); + const {forwards, backwards} = edgeTrie.getLast(edge.address); return { - toWeight: dstWeight * forwardWeight, - froWeight: srcWeight * backwardWeight, + toWeight: dstWeight * forwards, + froWeight: srcWeight * backwards, }; }; } diff --git a/src/analysis/weightsToEdgeEvaluator.test.js b/src/analysis/weightsToEdgeEvaluator.test.js index b45926b..ed909ea 100644 --- a/src/analysis/weightsToEdgeEvaluator.test.js +++ b/src/analysis/weightsToEdgeEvaluator.test.js @@ -43,13 +43,17 @@ describe("analysis/weightsToEdgeEvaluator", () => { const nodesMap = new Map(nodes.map((x) => [x.type.prefix, x])); const edges = [ { - forwardWeight: NullUtil.orElse(assemblesForward, 1), - backwardWeight: NullUtil.orElse(assemblesBackward, 1), + weight: { + forwards: NullUtil.orElse(assemblesForward, 1), + backwards: NullUtil.orElse(assemblesBackward, 1), + }, type: assemblesEdgeType, }, { - forwardWeight: NullUtil.orElse(baseForward, 1), - backwardWeight: NullUtil.orElse(baseBackward, 1), + weight: { + forwards: NullUtil.orElse(baseForward, 1), + backwards: NullUtil.orElse(baseBackward, 1), + }, type: fallbackEdgeType, }, ]; diff --git a/src/cli/pagerank.test.js b/src/cli/pagerank.test.js index 0fcc8e7..5e54ffe 100644 --- a/src/cli/pagerank.test.js +++ b/src/cli/pagerank.test.js @@ -206,8 +206,10 @@ describe("cli/pagerank", () => { const edgeType: EdgeType = { forwardName: "bars", backwardName: "barred by", - defaultForwardWeight: 5, - defaultBackwardWeight: 3, + defaultWeight: { + forwards: 5, + backwards: 3, + }, prefix: EdgeAddress.fromParts(["hom"]), description: "an example edge type", }; @@ -268,14 +270,9 @@ describe("cli/pagerank", () => { expect(weightedNodeType.type).toEqual(nodeType); } for (const edgeType of declaration.edgeTypes) { - const weightedEdgeType = NullUtil.get(ws.edges.get(edgeType.prefix)); - expect(weightedEdgeType.forwardWeight).toEqual( - edgeType.defaultForwardWeight - ); - expect(weightedEdgeType.backwardWeight).toEqual( - edgeType.defaultBackwardWeight - ); - expect(weightedEdgeType.type).toEqual(edgeType); + const {weight, type} = NullUtil.get(ws.edges.get(edgeType.prefix)); + expect(weight).toEqual(edgeType.defaultWeight); + expect(type).toEqual(edgeType); } } }); diff --git a/src/core/attribution/graphToMarkovChain.js b/src/core/attribution/graphToMarkovChain.js index d5ee40e..8b7ba2e 100644 --- a/src/core/attribution/graphToMarkovChain.js +++ b/src/core/attribution/graphToMarkovChain.js @@ -97,6 +97,8 @@ export type OrderedSparseMarkovChain = {| +chain: SparseMarkovChain, |}; +// TODO(@decentralion): Rename these fields to `forwards` and `backwards` to +// deduplicate with the EdgeWeight type defined by analysis/weights export type EdgeWeight = {| +toWeight: number, // weight from src to dst +froWeight: number, // weight from dst to src diff --git a/src/explorer/pagerankTable/Aggregation.test.js b/src/explorer/pagerankTable/Aggregation.test.js index b6aca4a..c7a6b05 100644 --- a/src/explorer/pagerankTable/Aggregation.test.js +++ b/src/explorer/pagerankTable/Aggregation.test.js @@ -151,8 +151,7 @@ describe("explorer/pagerankTable/Aggregation", () => { const edgeType: EdgeType = { forwardName: "marsellus", backwardName: "wallace", - defaultForwardWeight: 1, - defaultBackwardWeight: 1, + defaultWeight: {forwards: 1, backwards: 1}, prefix: EdgeAddress.fromParts(["look", "like"]), description: "Connects example nodes for testing purposes.", }; diff --git a/src/explorer/pagerankTable/aggregate.test.js b/src/explorer/pagerankTable/aggregate.test.js index 57eb180..a92618d 100644 --- a/src/explorer/pagerankTable/aggregate.test.js +++ b/src/explorer/pagerankTable/aggregate.test.js @@ -63,24 +63,21 @@ describe("explorer/pagerankTable/aggregate", () => { foo: { forwardName: "foos", backwardName: "foo'd by", - defaultForwardWeight: 1, - defaultBackwardWeight: 1, + defaultWeight: {forwards: 1, backwards: 1}, prefix: EdgeAddress.fromParts(["foo"]), description: "Connects example foo edges.", }, bar: { forwardName: "bars", backwardName: "bar'd by", - defaultForwardWeight: 1, - defaultBackwardWeight: 1, + defaultWeight: {forwards: 1, backwards: 1}, prefix: EdgeAddress.fromParts(["bar"]), description: "Connects example bar edges.", }, empty: { forwardName: "empty", backwardName: "emptied-by", - defaultForwardWeight: 1, - defaultBackwardWeight: 1, + defaultWeight: {forwards: 1, backwards: 1}, prefix: EdgeAddress.empty, description: "Connects arbitrary edges.", }, diff --git a/src/explorer/weights/EdgeTypeConfig.js b/src/explorer/weights/EdgeTypeConfig.js index c2d36b4..1b51de7 100644 --- a/src/explorer/weights/EdgeTypeConfig.js +++ b/src/explorer/weights/EdgeTypeConfig.js @@ -10,27 +10,30 @@ export class EdgeTypeConfig extends React.Component<{ +onChange: (WeightedEdgeType) => void, }> { render() { + const {weight, type} = this.props.weightedType; + const {forwards, backwards} = weight; + const {forwardName, backwardName, description} = type; return (