public DialogVariablesSimulator Clone()
        {
            var nestedVariableSimulator = new DialogVariablesSimulator(Variables, entityVariablesNames);

            foreach (var variable in VariablesValues.Keys)
            {
                nestedVariableSimulator.VariablesValues[variable] = VariablesValues[variable];
            }
            nestedVariableSimulator.LastEntityMatches = LastEntityMatches;
            return(nestedVariableSimulator);
        }
Beispiel #2
0
        public void GenerateMappingUris(DialogVariablesSimulator dialogVariables, MappingUriGenerator.MappingUriConfig mappingUriConfig, IDictionary <string, IDictionary <string, IList <string> > > arraysOfAllowedValuesByEntityNameAndFederation)
        {
            EntityVariablesNotExplicitlySet = dialogVariables.ResetEntityVariablesNotExplicitlySet(MappingUriGenerator.GetEntityVariables(mappingUriConfig));
            bool redirectToLongTail;

            MappingUris = MappingUriGenerator.GenerateMappingURIs(dialogVariables, mappingUriConfig, arraysOfAllowedValuesByEntityNameAndFederation, out redirectToLongTail);
            if (redirectToLongTail)
            {
                Type = DialogNodeType.RedirectToLongTail;
            }
        }
 internal void AddIntent(MatchIntentAndEntities intent, DialogVariablesSimulator dialogVariables)
 {
     if (!Intents.ContainsKey(intent.Name))
     {
         Intents.Add(intent.Name, intent);
         dialogVariables.AddMatchIntentAndEntities(intent);
     }
     else
     {
         var otherIntent = Intents[intent.Name];
         LogMessage(intent.LineNumber, MessageType.DuplicateKey, "Two intent matching nodes found for intent \"" + intent.Name + "\" : line " + intent.LineNumber + " and line " + otherIntent.LineNumber);
     }
 }
Beispiel #4
0
        // Mapping URIs generation

        public static string[] GenerateMappingURIs(DialogVariablesSimulator dialogVariablesSimulator, MappingUriConfig mappingUriConfig, IDictionary <string, IDictionary <string, IList <string> > > arraysOfAllowedValuesByEntityNameAndFederation, out bool redirectToLongTail)
        {
            IList <string> federationGroups = null;

            if (arraysOfAllowedValuesByEntityNameAndFederation != null)
            {
                federationGroups = arraysOfAllowedValuesByEntityNameAndFederation.Values.First().Keys.ToList();
            }

            // Redirect to long tail ?
            var redirectToLongTailVariableName = mappingUriConfig == MappingUriConfig.Insurance ? Insurance_RedirectToLongTailVariable : Savings_RedirectToLongTailVariable;
            var redirectToLongTailValue        = dialogVariablesSimulator.TryGetVariableValue(redirectToLongTailVariableName);

            redirectToLongTail = redirectToLongTailValue == "yes";
            if (redirectToLongTail)
            {
                return(null);
            }

            // Deduce subdomain from entity name
            string[][] deduceVariableValues = mappingUriConfig == MappingUriConfig.Insurance ? Insurance_DeduceVariableValues : Savings_DeduceVariableValues;
            foreach (var deduceVariableStrings in deduceVariableValues)
            {
                var inspectedVariable = deduceVariableStrings[0];
                var searchPattern     = deduceVariableStrings[1];
                var targetVariable    = deduceVariableStrings[2];
                var targetValue       = deduceVariableStrings[3];

                var inspectedValue = dialogVariablesSimulator.TryGetVariableValue(inspectedVariable);
                if (inspectedValue != null && inspectedValue.Contains(searchPattern))
                {
                    dialogVariablesSimulator.SetVariableValue(targetVariable, targetValue, DialogNodeType.FatHeadAnswers);
                }
            }

            // Generate mapping URIs
            string[][]       mappingUriSegments = mappingUriConfig == MappingUriConfig.Insurance ? Insurance_MappingUriSegments : Savings_MappingUriSegments;
            IList <string>[] mappingUriValues   = new IList <string> [mappingUriSegments.Length];
            for (int i = 0; i < mappingUriSegments.Length; i++)
            {
                mappingUriValues[i] = dialogVariablesSimulator.TryGetVariableValues(mappingUriSegments[i][1]);
                // Generate mapping URIs for all federation groups if needed
                if (mappingUriValues[i] == null && mappingUriSegments[i][1] == "federationGroup")
                {
                    mappingUriValues[i] = federationGroups;
                }
                // Filter only valid entity values
                if (mappingUriValues[i] != null)
                {
                    DialogVariable variable = null;
                    if (dialogVariablesSimulator.Variables.TryGetValue(mappingUriSegments[i][1], out variable))
                    {
                        var entity = dialogVariablesSimulator.TryGetEntityFromVariable(variable);
                        if (entity != null)
                        {
                            var valuesCount = mappingUriValues[i].Count;
                            for (int j = 0; j < valuesCount; j++)
                            {
                                var entityValueName = mappingUriValues[i][j];
                                var entityValue     = entity.TryGetEntityValueFromName(entityValueName);
                                if (entityValue == null)
                                {
                                    mappingUriValues[i].RemoveAt(j);
                                    j--;
                                    valuesCount--;
                                }
                            }
                            if (valuesCount == 0)
                            {
                                mappingUriValues[i] = null;
                            }
                        }
                    }
                }
            }
            var indexes = new int[mappingUriSegments.Length];
            int count   = 0;

            for (int i = 0; i < mappingUriSegments.Length; i++)
            {
                if (mappingUriValues[i] != null)
                {
                    if (count == 0)
                    {
                        count = mappingUriValues[i].Count;
                    }
                    else
                    {
                        count *= mappingUriValues[i].Count;
                    }
                }
                else
                {
                    indexes[i] = -1;
                }
            }
            var result = new string[count];

            for (int cnt = 0; cnt < count; cnt++)
            {
                string federationGroup = null;
                for (int i = 0; i < mappingUriSegments.Length; i++)
                {
                    if (indexes[i] >= 0)
                    {
                        // Check if this combination of entity values is allowed for the federation group
                        if (mappingUriSegments[i][1] == "federationGroup")
                        {
                            federationGroup = mappingUriValues[i][indexes[i]];
                        }
                        else if (federationGroup != null)
                        {
                            var entityName = mappingUriSegments[i][0].ToUpper();
                            IDictionary <string, IList <string> > arraysOfAllowedValuesByFederation = null;
                            if (arraysOfAllowedValuesByEntityNameAndFederation.TryGetValue(entityName, out arraysOfAllowedValuesByFederation))
                            {
                                var arrayOfAllowedValues = arraysOfAllowedValuesByFederation[federationGroup];
                                if (!arrayOfAllowedValues.Contains(mappingUriValues[i][indexes[i]]))
                                {
                                    // Mapping URI not allowed, because one of its entity value is not supported for the current federation group
                                    // => exit the loop
                                    // Delete the corresponding URI
                                    result[cnt] = "";
                                    cnt        -= 1;
                                    count      -= 1;
                                    break;
                                }
                            }
                        }

                        result[cnt] += "/" + mappingUriSegments[i][0] + "/" + mappingUriValues[i][indexes[i]];
                    }
                }
                if (cnt < (count - 1))
                {
                    int idx = 0;
                    for (;;)
                    {
                        if (indexes[idx] < 0)
                        {
                            idx++;
                            continue;
                        }
                        if (indexes[idx] < (mappingUriValues[idx].Count - 1))
                        {
                            indexes[idx]++;
                            break;
                        }
                        else
                        {
                            indexes[idx] = 0;
                            idx++;
                        }
                    }
                }
            }

            var compactResult = new List <string>();

            foreach (var uri in result)
            {
                if (uri != null)
                {
                    compactResult.Add(uri);
                }
            }
            return(compactResult.ToArray());
        }
        // Mapping URIs generation

        public static string[] GenerateMappingURIs(DialogVariablesSimulator dialogVariablesSimulator, MappingUriConfig mappingUriConfig, IDictionary <string, IDictionary <string, IList <string> > > arraysOfAllowedValuesByEntityNameAndFederation, out bool redirectToLongTail, XDocument XmlDocument = null, DialogNode node = null)
        {
            IList <string> federationGroups = null;

            if (arraysOfAllowedValuesByEntityNameAndFederation != null)
            {
                federationGroups = arraysOfAllowedValuesByEntityNameAndFederation.Values.First().Keys.ToList();
            }

            // Redirect to long tail ?
            var redirectToLongTailVariableName = mappingUriConfig == MappingUriConfig.Insurance ? Insurance_RedirectToLongTailVariable : Savings_RedirectToLongTailVariable;
            var redirectToLongTailValue        = dialogVariablesSimulator.TryGetVariableValue(redirectToLongTailVariableName);

            redirectToLongTail = redirectToLongTailValue == "yes";
            if (redirectToLongTail)
            {
                return(null);
            }

            // Deduce subdomain from entity name
            string[][] deduceVariableValues = mappingUriConfig == MappingUriConfig.Insurance ? Insurance_DeduceVariableValues : Savings_DeduceVariableValues;
            foreach (var deduceVariableStrings in deduceVariableValues)
            {
                var inspectedVariable = deduceVariableStrings[0];
                var searchPattern     = deduceVariableStrings[1];
                var targetVariable    = deduceVariableStrings[2];
                var targetValue       = deduceVariableStrings[3];

                var inspectedValue = dialogVariablesSimulator.TryGetVariableValue(inspectedVariable);
                if (inspectedValue != null && inspectedValue.Contains(searchPattern))
                {
                    dialogVariablesSimulator.SetVariableValue(targetVariable, targetValue, DialogNodeType.FatHeadAnswers);
                }
            }

            // Generate mapping URIs
            string[][]       mappingUriSegments = mappingUriConfig == MappingUriConfig.Insurance ? Insurance_MappingUriSegments : Savings_MappingUriSegments;
            IList <string>[] mappingUriValues   = new IList <string> [mappingUriSegments.Length];
            for (int i = 0; i < mappingUriSegments.Length; i++)
            {
                mappingUriValues[i] = dialogVariablesSimulator.TryGetVariableValues(mappingUriSegments[i][1]);
                // Generate mapping URIs for all federation groups if needed
                if (mappingUriValues[i] == null && mappingUriSegments[i][1] == "federationGroup")
                {
                    mappingUriValues[i] = federationGroups;
                }
                // Filter only valid entity values
                if (mappingUriValues[i] != null)
                {
                    DialogVariable variable = null;
                    if (dialogVariablesSimulator.Variables.TryGetValue(mappingUriSegments[i][1], out variable))
                    {
                        var entity = dialogVariablesSimulator.TryGetEntityFromVariable(variable);

                        if (entity != null)
                        {
                            var valuesCount = mappingUriValues[i].Count;
                            for (int j = 0; j < valuesCount; j++)
                            {
                                var entityValueName = mappingUriValues[i][j];
                                var entityValue     = entity.TryGetEntityValueFromName(entityValueName);

                                if (entityValue == null)
                                {
                                    // Force in case it is a SET_TO operator with a disambiguationQuestion
                                    if (node != null)
                                    {
                                        if (node.ParentNode != null)
                                        {
                                            if (node.ParentNode.ParentNode != null)
                                            {
                                                if (node.ParentNode.ParentNode.ParentNode != null)
                                                {
                                                    var DisambiguationQuestion = node.ParentNode.ParentNode.ParentNode;
                                                    if (DisambiguationQuestion.Type == DialogNodeType.DisambiguationQuestion)
                                                    {
                                                        string setToEntityType          = mappingUriSegments[i][1];
                                                        var    condition                = node.ParentNode;
                                                        var    listEntityTypeCondition  = StringUtils.ExtractFromString(condition.ToString(), ":", "=");
                                                        var    listEntityValueCondition = StringUtils.ExtractFromString(condition.ToString(), "'", "'");

                                                        for (int a = 0; a < listEntityTypeCondition.Count; a++)
                                                        {
                                                            if (setToEntityType == listEntityTypeCondition[a])
                                                            {
                                                                entityValue = entity.TryGetEntityValueFromName(listEntityValueCondition[a]);
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (entityValue == null)
                                    {
                                        // mappingUriValues[i].RemoveAt(j);
                                        //j--;
                                        //valuesCount--;
                                    }
                                }
                            }
                            if (valuesCount == 0)
                            {
                                mappingUriValues[i] = null;
                            }
                        }
                    }
                }
            }
            var indexes = new int[mappingUriSegments.Length];
            int count   = 0;

            for (int i = 0; i < mappingUriSegments.Length; i++)
            {
                if (mappingUriValues[i] != null)
                {
                    if (count == 0)
                    {
                        count = mappingUriValues[i].Count;
                    }
                    else
                    {
                        count *= mappingUriValues[i].Count;
                    }
                }
                else
                {
                    indexes[i] = -1;
                }
            }
            var result = new string[count];

            for (int cnt = 0; cnt < count; cnt++)
            {
                string federationGroup = null;
                for (int i = 0; i < mappingUriSegments.Length; i++)
                {
                    if (indexes[i] >= 0)
                    {
                        // Check if this combination of entity values is allowed for the federation group
                        if (mappingUriSegments[i][1] == "federationGroup")
                        {
                            federationGroup = mappingUriValues[i][indexes[i]];
                        }
                        else if (federationGroup != null)
                        {
                            var entityName = mappingUriSegments[i][0].ToUpper();
                            IDictionary <string, IList <string> > arraysOfAllowedValuesByFederation = null;
                            if (arraysOfAllowedValuesByEntityNameAndFederation.TryGetValue(entityName, out arraysOfAllowedValuesByFederation))
                            {
                                var arrayOfAllowedValues = arraysOfAllowedValuesByFederation[federationGroup];
                                if (!arrayOfAllowedValues.Contains(mappingUriValues[i][indexes[i]]))
                                {
                                    // Mapping URI not allowed, because one of its entity value is not supported for the current federation group
                                    // => exit the loop
                                    // Delete the corresponding URI
                                    result[cnt] = "";
                                    cnt        -= 1;
                                    count      -= 1;
                                    break;
                                }
                            }
                        }

                        result[cnt] += "/" + mappingUriSegments[i][0] + "/" + mappingUriValues[i][indexes[i]];
                    }
                }
                if (cnt < (count - 1))
                {
                    int idx = 0;
                    for (; ;)
                    {
                        if (indexes[idx] < 0)
                        {
                            idx++;
                            continue;
                        }
                        if (indexes[idx] < (mappingUriValues[idx].Count - 1))
                        {
                            indexes[idx]++;
                            break;
                        }
                        else
                        {
                            indexes[idx] = 0;
                            idx++;
                        }
                    }
                }
            }

            var compactResult = new List <string>();

            foreach (var uri in result)
            {
                if (mappingUriConfig == MappingUriConfig.Insurance)
                {
                    var uriTemp = uri;
                    foreach (string federation in Insurance_List_Federation)
                    {
                        uriTemp = "/federationGroup/" + federation + uri;
                        bool restrictionOnProduct = false;
                        foreach (var tuple in Insurance_FederationNotAllowedEntities)
                        {
                            // If the URI is about this product
                            if (uriTemp.Contains("/" + tuple.Key))
                            {
                                restrictionOnProduct = true;
                                // Chek if the actual uri has the forbidden Federations
                                bool inside = false;
                                foreach (var fede in tuple.Value)
                                {
                                    if (uriTemp.Contains("/federationGroup/" + fede + "/"))
                                    {
                                        inside = true;
                                    }
                                }
                                if (!inside)
                                {
                                    if (uri != null)
                                    {
                                        compactResult.Add(uriTemp);
                                    }
                                }
                            }
                        }
                        if (!restrictionOnProduct)
                        {
                            if (uri != null)
                            {
                                compactResult.Add(uriTemp);
                            }
                        }
                    }
                }
                else
                {
                    if (uri != null)
                    {
                        compactResult.Add(uri);
                    }
                }
            }
            return(compactResult.ToArray());
        }
        internal void LinkDialogVariableConditionToDialogVariableAndEntityValue(DialogNode dialogNode, DialogVariableCondition variableCondition, DialogVariablesSimulator dialogVariables)
        {
            if (String.IsNullOrEmpty(variableCondition.VariableName))
            {
                return;
            }

            DialogVariable variable    = null;
            EntityValue    entityValue = null;

            if (!Variables.ContainsKey(variableCondition.VariableName))
            {
                LogMessage(dialogNode.LineNumber, MessageType.InvalidReference, "Variable condition references undefined variable name : \"" + variableCondition.VariableName + "\"");
            }
            else
            {
                variable = Variables[variableCondition.VariableName];
                variable.AddDialogNodeReference(dialogNode, VariableReferenceType.Read);

                if (variableCondition.Comparison != ConditionComparison.HasValue && !String.IsNullOrEmpty(variableCondition.Value))
                {
                    var entity = dialogVariables.TryGetEntityFromVariable(variable);
                    if (entity != null)
                    {
                        entityValue = entity.TryGetEntityValueFromName(variableCondition.Value);
                        if (entityValue == null)
                        {
                            var message = "Variable condition references undefined entity value name \"" + variableCondition.Value + "\" for entity " + entity.Name;
                            var suggestedEntityValue = entity.SuggestEntityValueFromName(variableCondition.Value, Entities.Values);
                            if (suggestedEntityValue != null)
                            {
                                if (suggestedEntityValue.Entity == entity)
                                {
                                    message += ", you may want to replace this name with \"" + suggestedEntityValue.Name + "\" (defined line " + suggestedEntityValue.LineNumber + ")";
                                }
                                else
                                {
                                    message += ", you may want to move this condition under another entity \"" + suggestedEntityValue.Entity.Name + "\" with value \"" + suggestedEntityValue.Name + "\" (defined line " + suggestedEntityValue.LineNumber + ")";
                                }
                            }
                            LogMessage(dialogNode.LineNumber, MessageType.InvalidReference, message);
                        }
                        else
                        {
                            entityValue.AddDialogNodeReference(dialogNode);
                        }
                    }
                }
            }
            variableCondition.SetVariableAndEntityValue(variable, entityValue);
        }