internal DialogVariable[] ResetEntityVariablesNotExplicitlySet(IEnumerable <string> entityVariableNames)
        {
            IList <DialogVariable> result = new List <DialogVariable>();

            foreach (var entityVariableName in entityVariableNames)
            {
                DialogVariable entityVariable = null;
                Variables.TryGetValue(entityVariableName, out entityVariable);
                if (entityVariable != null)
                {
                    if (!VariablesValues.ContainsKey(entityVariable) ||
                        VariablesValues[entityVariable].Count == 0)
                    {
                        result.Add(entityVariable);
                    }
                    else if (VariablesValues[entityVariable].Count == 1)
                    {
                        var value = VariablesValues[entityVariable][0];
                        if (String.IsNullOrEmpty(value))
                        {
                            result.Add(entityVariable);
                        }
                        else if (value.StartsWith("$("))
                        {
                            VariablesValues.Remove(entityVariable);
                            result.Add(entityVariable);
                        }
                    }
                }
            }
            return(result.ToArray());
        }
Exemple #2
0
 public void SetVariableAndEntityValue(DialogVariable variable, EntityValue entityValue)
 {
     Variable = variable;
     if (entityValue != null)
     {
         Type        = DialogConditionType.EntityValueCondition;
         Entity      = entityValue.Entity;
         EntityValue = entityValue;
     }
 }
        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);
        }
 public Entity TryGetEntityFromVariable(DialogVariable variable)
 {
     if (LastEntityMatches != null)
     {
         foreach (var entityMatch in LastEntityMatches)
         {
             if (entityMatch != null && (entityMatch.EntityVariable1 == variable || entityMatch.EntityVariable2 == variable))
             {
                 return(entityMatch.Entity);
             }
         }
     }
     return(null);
 }
Exemple #5
0
 private static void WriteVariable(XmlWriter xw, DialogVariable variable)
 {
     if (variable.DialogNodeReferences != null && variable.DialogNodeReferences.Count > 0)
     {
         xw.WriteStartElement("Variable");
         xw.WriteAttributeString("Name", variable.Name);
         xw.WriteAttributeString("Type", variable.Type.ToString());
         if (!String.IsNullOrEmpty(variable.InitValue))
         {
             xw.WriteAttributeString("InitValue", variable.InitValue);
         }
         xw.WriteEndElement();
     }
 }
Exemple #6
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());
        }
 public DialogVariableAssignment(DialogVariable variable, DialogVariableOperator @operator, string value) :
     this(variable.Name, @operator, value)
 {
     Variable = variable;
 }
        // 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 AddVariable(DialogVariable variable)
 {
     Variables.Add(variable.Name, variable);
 }