import { Core, EdgeSingular, NodeSingular } from "cytoscape";
export const assignCredence = (cytoscape: Core, iterations: number) => {
const points = cytoscape.nodes(".point");
const negations = cytoscape.edges(".negation");
points.forEach((point) => {
point.data("credence", point.data("conviction"));
});
negations.forEach((negation) => {
negation.data("credence", negation.data("conviction"));
});
for (let i = 0; i < iterations; i++) {
points.forEach((point) => {
point.scratch("roundCredence", point.data("credence"));
});
negations.forEach((negation) => {
negation.scratch("roundCredence", negation.data("credence"));
});
negations.forEach((negation) => {
const source = negation.source().hasClass("point")
? negation.source()
: getNegationEdge(negation.source());
const target = negation.target().hasClass("point")
? negation.target()
: getNegationEdge(negation.target());
const relevance = negation.scratch("roundCredence");
attack(source, target, relevance);
attack(target, source, relevance);
});
}
cytoscape.style().update();
};
const attack = (
source: NodeSingular | EdgeSingular,
target: NodeSingular | EdgeSingular,
relevance: number
) => {
const attackSign = source.scratch("roundCredence") > 0 ? 1 : -1;
const attackPower = Math.min(
Math.abs(source.scratch("roundCredence")),
relevance
);
target.data(
"credence",
target.data("credence") - attackPower * attackSign
);
};
const getNegationEdge = (auxNode: NodeSingular) =>
auxNode.cy().getElementById(auxNode.data("edgeId")) as EdgeSingular;