in parsers/LU/JS/packages/lu/src/parser/lufile/parseFileContents.js [1512:1566]
const handleComposite = function(parsedContent, entityName, entityType, entityRoles, range, inlineChildRequired, isEntityTypeDefinition) {
// remove simple entity definitions for composites but carry forward roles.
// Find this entity if it exists in the simple entity collection
let simpleEntityExists = (parsedContent.LUISJsonStructure.entities || []).find(item => item.name == entityName);
if (simpleEntityExists !== undefined) {
// take and add any roles into the roles list
(simpleEntityExists.roles || []).forEach(role => !entityRoles.includes(role) ? entityRoles.push(role) : undefined);
// remove this simple entity definition
for (var idx = 0; idx < parsedContent.LUISJsonStructure.entities.length; idx++) {
if (parsedContent.LUISJsonStructure.entities[idx].name === simpleEntityExists.name) {
parsedContent.LUISJsonStructure.entities.splice(idx, 1);
}
}
}
// handle composite entity definition
// drop [] and trim
let childDefinition = entityType.trim().replace('[', '').replace(']', '').trim();
if (childDefinition.length === 0 && inlineChildRequired) {
let errorMsg = `Composite entity: ${entityName} is missing child entity definitions. Child entities are denoted via [entity1, entity2] notation.`;
let error = BuildDiagnostic({
message: errorMsg,
range: range
})
throw (new exception(retCode.errorCode.INVALID_COMPOSITE_ENTITY, error.toString(), [error]));
}
// split the children based on ',' or ';' delimiter. Trim each child to remove white spaces.
let compositeChildren = childDefinition !== "" ? childDefinition.split(new RegExp(/[,;]/g)).map(item => item.trim()) : [];
// add this composite entity if it does not exist
let compositeEntity = (parsedContent.LUISJsonStructure.composites || []).find(item => item.name == entityName);
if (compositeEntity === undefined) {
// add new composite entity
parsedContent.LUISJsonStructure.composites.push(new helperClass.compositeEntity(entityName, compositeChildren, entityRoles));
// remove composite that might have been tagged as a simple entity due to inline entity definition in an utterance
parsedContent.LUISJsonStructure.entities = (parsedContent.LUISJsonStructure.entities || []).filter(entity => entity.name != entityName);
} else {
if (isEntityTypeDefinition) {
if (compositeEntity.children.length !== 0 && JSON.stringify(compositeChildren.sort()) !== JSON.stringify(compositeEntity.children.sort())) {
let errorMsg = `Composite entity: ${entityName} has multiple definition with different children. \n 1. ${compositeChildren.join(', ')}\n 2. ${compositeEntity.children.join(', ')}`;
let error = BuildDiagnostic({
message: errorMsg,
range: range
})
throw (new exception(retCode.errorCode.INVALID_COMPOSITE_ENTITY, error.toString(), [error]));
}
}
// update roles
// update children
compositeChildren.forEach(item => compositeEntity.children.push(item));
addItemOrRoleIfNotPresent(parsedContent.LUISJsonStructure, LUISObjNameEnum.COMPOSITES, compositeEntity.name, entityRoles);
}
};