collateLUISFiles: async function()

in packages/Ludown/lib/parseFileContents.js [261:391]


    collateLUISFiles: async function (parsedLUISList) {
        if (parsedLUISList.length === 0) return undefined;
        let FinalLUISJSON = parsedLUISList[0].LUISJsonStructure;
        parsedLUISList.splice(0, 1);
        parsedLUISList.forEach(function (blob) {
            blob = blob.LUISJsonStructure;
            mergeResults(blob, FinalLUISJSON, LUISObjNameEnum.INTENT);
            mergeResults(blob, FinalLUISJSON, LUISObjNameEnum.ENTITIES);
            mergeResults_closedlists(blob, FinalLUISJSON, LUISObjNameEnum.CLOSEDLISTS);
            mergeResults(blob, FinalLUISJSON, LUISObjNameEnum.UTTERANCE);
            mergeResults(blob, FinalLUISJSON, LUISObjNameEnum.PATTERNS);
            mergeResults(blob, FinalLUISJSON, LUISObjNameEnum.PATTERNANYENTITY);

            // do we have regex entities here?
            if (blob.regex_entities.length > 0) {
                blob.regex_entities.forEach(function (regexEntity) {
                    // do we have the same entity in final?
                    let entityExistsInFinal = (FinalLUISJSON.regex_entities || []).find(item => item.name == regexEntity.name);
                    if (entityExistsInFinal === undefined) {
                        FinalLUISJSON.regex_entities.push(regexEntity);
                    } else {
                        // verify that the pattern is the same
                        if (entityExistsInFinal.regexPattern !== regexEntity.regexPattern) {
                            throw (new exception(retCode.errorCode.INVALID_REGEX_ENTITY, `[ERROR]: RegEx entity : ${regexEntity.name} has inconsistent pattern definitions. \n 1. ${regexEntity.regexPattern} \n 2. ${entityExistsInFinal.regexPattern}`));
                        }
                        // merge roles
                        if (entityExistsInFinal.roles.length > 0) {
                            (regexEntity.roles || []).forEach(function (role) {
                                if (!entityExistsInFinal.roles.includes(role))
                                    entityExistsInFinal.roles.push(role);
                            })
                        }
                    }
                })
            }

            // do we have prebuiltEntities here?
            if (blob.prebuiltEntities.length > 0) {
                blob.prebuiltEntities.forEach(function (prebuiltEntity) {
                    let prebuiltTypeExists = false;
                    for (let fIndex in FinalLUISJSON.prebuiltEntities) {
                        if (prebuiltEntity.name === FinalLUISJSON.prebuiltEntities[fIndex].name) {
                            // do we have all the roles? if not, merge the roles
                            prebuiltEntity.roles.forEach(function (role) {
                                if (!FinalLUISJSON.prebuiltEntities[fIndex].roles.includes(role)) {
                                    FinalLUISJSON.prebuiltEntities[fIndex].roles.push(role);
                                }
                            });
                            prebuiltTypeExists = true;
                            break;
                        }
                    }
                    if (!prebuiltTypeExists) {
                        FinalLUISJSON.prebuiltEntities.push(prebuiltEntity);
                    }
                });
            }
            // do we have model_features?
            if (blob.model_features.length > 0) {
                blob.model_features.forEach(function (modelFeature) {
                    let modelFeatureInMaster = helpers.filterMatch(FinalLUISJSON.model_features, 'name', modelFeature.name);
                    if (modelFeatureInMaster.length === 0) {
                        FinalLUISJSON.model_features.push(modelFeature);
                    } else {
                        if (modelFeatureInMaster[0].mode !== modelFeature.mode) {
                            // error.
                            throw (new exception(retCode.errorCode.INVALID_INPUT, '[ERROR]: Phrase list : "' + modelFeature.name + '" has conflicting definitions. One marked interchangeable and another not interchangeable'));
                        } else {
                            modelFeature.words.split(',').forEach(function (word) {
                                if (!modelFeatureInMaster[0].words.includes(word)) modelFeatureInMaster[0].words += "," + word;
                            })
                        }
                    }
                });
            }

            // do we have composites? collate them correctly
            (blob.composites || []).forEach(composite => {
                let compositeInMaster = helpers.filterMatch(FinalLUISJSON.composites, 'name', composite.name);
                if (compositeInMaster.length === 0) {
                    FinalLUISJSON.composites.push(composite);
                } else {
                    if (JSON.stringify(composite.children.sort()) !== JSON.stringify(compositeInMaster[0].children.sort())) {
                        throw (new exception(retCode.errorCode.INVALID_COMPOSITE_ENTITY, `[ERROR]: Composite entity: ${composite.name} has multiple definition with different children. \n 1. ${compositeInMaster[0].children.join(', ')}\n 2. ${composite.children.join(', ')}`));
                    } else {
                        // merge roles
                        (composite.roles || []).forEach(blobRole => {
                            if (!compositeInMaster[0].roles.includes(blobRole)) {
                                compositeInMaster[0].roles.push(blobRole);
                            }
                        })
                    }
                }
            });

            // do we have pattern.any entities here? 
            (blob.patternAnyEntities || []).forEach(patternAny => {
                let paIdx = -1;
                let patternAnyInMaster = FinalLUISJSON.patternAnyEntities.find((item, idx) => {
                    if (item.name === patternAny.name) {
                        paIdx = idx;
                        return true;
                    }
                    return false;
                });
                // verify that this patternAny entity does not exist as any other type
                let simpleEntityInMaster = FinalLUISJSON.entities.find(item => item.name == patternAny.name);
                let compositeInMaster = FinalLUISJSON.composites.find(item => item.name == patternAny.name);
                let listEntityInMaster = FinalLUISJSON.closedLists.find(item => item.name == patternAny.name);
                let regexEntityInMaster = FinalLUISJSON.regex_entities.find(item => item.name == patternAny.name);
                let prebuiltInMaster = FinalLUISJSON.prebuiltEntities.find(item => item.name == patternAny.name);
                if (!simpleEntityInMaster && 
                    !compositeInMaster &&
                    !listEntityInMaster &&
                    !regexEntityInMaster &&
                    !prebuiltInMaster) {
                    if (patternAnyInMaster) {
                        (patternAny.roles || []).forEach(role => !patternAnyInMaster.roles.includes(role) ? patternAnyInMaster.roles.push(role) : undefined);
                    } else {
                            FinalLUISJSON.patternAnyEntities.push(patternAny);
                    }
                } else {
                    // remove the pattern.any from master if another entity type has this name.
                    if (patternAnyInMaster) {
                        if (paIdx !== -1) FinalLUISJSON.patternAnyEntities.splice(paIdx, 1);
                    }
                }
            })
        });
        return FinalLUISJSON;
    },