// choose.js BAYES STAR (c) coppola.ai const { Proposition, BackLink } = require("./predicate"); const { Storage } = require("./storage"); const { ConvertToQuantified, ExtractPremiseRoleMap, ConvertToProposition } = require("./ops"); function Combine(inputArray, k) { var result = []; function run(level, start, currentArray) { if (currentArray.length === k) { result.push(currentArray.slice()); return; } for (var i = start; i < inputArray.length; i++) { currentArray.push(inputArray[i]); run(level + 1, i + 1, currentArray); currentArray.pop(); } } run(0, 0, []); return result; } function ComputeChooseConfigurations(N, K) { var inputArray = Array.from({ length: N }, (_, i) => i); return Combine(inputArray, K); } function ExtractRolesFromIndices(roles, indices) { var result = [] const indexSet = new Set(indices) for (var i = 0; i < roles.length; i++) { if (indexSet.has(i)) { result.push(roles[i]) } } return result } function ComputeSearchKeys(proposition) { const num_roles = proposition.roles.length const configurations1 = ComputeChooseConfigurations(num_roles, 1) const configurations2 = ComputeChooseConfigurations(num_roles, 2) const configurations = [...configurations1, ...configurations2]; const roles = proposition.RoleNames() var result = [] for (const configuration of configurations) { const quantifiedRoles = ExtractRolesFromIndices(roles, configuration) const quantified = ConvertToQuantified(proposition, quantifiedRoles) const searchString = quantified.SearchString() result.push(searchString) } return result } async function ComputeBacklinks(storage, proposition) { const searchKeys = ComputeSearchKeys(proposition) var buffer = [] for (const searchKey of searchKeys) { const implications = await storage.FindPremises(searchKey) for (const implication of implications) { const extractedMapping = ExtractPremiseRoleMap(proposition, implication.roleMap) const quantifiedPremise = implication.premise const extractedProposition = ConvertToProposition(quantifiedPremise, extractedMapping) buffer.push(new BackLink(implication, extractedProposition)); } } return buffer } async function ComputeMatchingPremises(storage, premise) { return [] } module.exports = { ComputeSearchKeys, ComputeBacklinks, ComputeMatchingPremises }