@npmstuff/argdown-core
Version:
A pluggable parser for the Argdown argumentation syntax
135 lines • 6.32 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatementSelectionPlugin = exports.StatementSelectionMode = void 0;
const ArgdownPluginError_1 = require("../ArgdownPluginError");
const utils_1 = require("../utils");
const model_1 = require("../model/model");
const selectionUtils_1 = require("./selectionUtils");
const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep"));
var StatementSelectionMode;
(function (StatementSelectionMode) {
StatementSelectionMode["ALL"] = "all";
StatementSelectionMode["TOP_LEVEL"] = "top-level";
StatementSelectionMode["WITH_TITLE"] = "with-title";
StatementSelectionMode["WITH_RELATIONS"] = "with-relations";
StatementSelectionMode["NOT_USED_IN_ARGUMENT"] = "not-used-in-argument";
StatementSelectionMode["WITH_MORE_THAN_ONE_RELATION"] = "with-more-than-one-relation";
})(StatementSelectionMode = exports.StatementSelectionMode || (exports.StatementSelectionMode = {}));
const defaultSettings = {
excludeDisconnected: true,
statementSelectionMode: StatementSelectionMode.WITH_TITLE
};
class StatementSelectionPlugin {
constructor(config) {
this.name = "StatementSelectionPlugin";
this.getSettings = (request) => {
if ((0, utils_1.isObject)(request.selection)) {
return request.selection;
}
else {
request.selection = {};
return request.selection;
}
};
this.prepare = (request, response) => {
(0, ArgdownPluginError_1.checkResponseFields)(this, response, [
"statements",
"arguments",
"relations"
]);
(0, utils_1.mergeDefaults)(this.getSettings(request), this.defaults);
};
this.run = (request, response) => {
(0, ArgdownPluginError_1.checkResponseFields)(this, response, ["selection"]);
const settings = this.getSettings(request);
const selectedStatementsMap = (0, utils_1.reduceToMap)(response.selection.statements, curr => curr.title);
const selectedArgumentsMap = (0, utils_1.reduceToMap)(response.selection.arguments, curr => curr.title);
response.selection.statements = response.selection.statements.filter(isStatementSelected(settings, selectedStatementsMap, selectedArgumentsMap));
};
this.defaults = (0, lodash_defaultsdeep_1.default)({}, config, defaultSettings);
}
}
exports.StatementSelectionPlugin = StatementSelectionPlugin;
const untitledPattern = /^Untitled/;
const isStatementSelected = (settings, selectedStatements, selectedArguments) => (equivalenceClass) => {
if (!settings.ignoreIsInMap &&
equivalenceClass.data &&
equivalenceClass.data.isInMap === true) {
return true;
}
if (settings.includeStatements &&
settings.includeStatements.indexOf(equivalenceClass.title) !== -1) {
return true;
}
const withRelations = equivalenceClass.relations.length > 0 &&
undefined !==
equivalenceClass.relations.find(r => (0, selectionUtils_1.otherRelationMemberIsInSelection)(r, equivalenceClass, selectedStatements, selectedArguments));
const usedInArgument = equivalenceClass.members.find(isUsedInSelectedArgument(selectedArguments));
let inSelection = false;
switch (settings.statementSelectionMode) {
case StatementSelectionMode.ALL:
inSelection = true;
break;
case StatementSelectionMode.WITH_TITLE:
inSelection =
(!usedInArgument && withRelations) ||
!untitledPattern.exec(equivalenceClass.title);
break;
case StatementSelectionMode.TOP_LEVEL:
inSelection =
(!usedInArgument && withRelations) ||
!!equivalenceClass.isUsedAsTopLevelStatement;
break;
case StatementSelectionMode.WITH_RELATIONS:
inSelection = withRelations;
break;
case StatementSelectionMode.NOT_USED_IN_ARGUMENT:
inSelection = !usedInArgument;
break;
case StatementSelectionMode.WITH_MORE_THAN_ONE_RELATION:
const nrOfRelationPartners = equivalenceClass.relations.reduce((acc, r) => {
return countOtherRelationMembersInSelection(acc, r, equivalenceClass, selectedStatements, selectedArguments);
}, 0);
inSelection =
withRelations && (!usedInArgument || nrOfRelationPartners > 1);
break;
}
return ((!settings.excludeDisconnected || (usedInArgument || withRelations)) &&
inSelection);
};
const isUsedInSelectedArgument = (selectedArguments) => (statement) => {
if ((0, model_1.isPCSStatement)(statement) &&
statement.role !== model_1.StatementRole.INTERMEDIARY_CONCLUSION) {
return selectedArguments.get(statement.argumentTitle) !== undefined;
}
return false;
};
const countOtherRelationMembersInSelection = (currentCount, relation, relationMember, selectedStatements, selectedArguments) => {
const other = relation.from === relationMember ? relation.to : relation.from;
if (other.type === model_1.ArgdownTypes.EQUIVALENCE_CLASS) {
if (selectedStatements.get(relationMember.title) === undefined) {
return currentCount;
}
let role = model_1.StatementRole.MAIN_CONCLUSION;
if (relation.to === other) {
role = model_1.StatementRole.PREMISE;
}
return other.members.reduce((acc, s) => s.role === role &&
selectedArguments.get(s.argumentTitle) !== undefined
? acc + 1
: acc, currentCount);
}
else if (other.type === model_1.ArgdownTypes.ARGUMENT &&
selectedArguments.get(other.title) !== undefined) {
return currentCount + 1;
}
else if (other.type === model_1.ArgdownTypes.INFERENCE &&
selectedArguments.get(other.argumentTitle) !== undefined) {
return currentCount + 1;
}
return currentCount;
};
//# sourceMappingURL=StatementSelectionPlugin.js.map
;