public void Deserialize(EntityProps entity_props, JObject serialization_obj)
        {
            // Dispatch to the correct function
            var final_type = entity_props.GetType();
            var method     = GetType()
                             .GetMethods()
                             .Where(mi => mi.Name == "Deserialize")
                             .Select(mi => new { MethodInfo = mi, Parameters = mi.GetParameters() })
                             .Where(info => info.Parameters.Length == 2 && info.Parameters[0].ParameterType == final_type && info.Parameters[1].ParameterType == typeof(JObject))
                             .Select(info => info.MethodInfo)
                             .Single();

            method.Invoke(this, new object[] { entity_props, serialization_obj });
        }
Ejemplo n.º 2
0
        public static bool PerformInternalTransfer(Account activeAccount, EntityProps entities)
        {
            try
            {
                var accountToDeduct = _context.Accounts
                                      .FirstOrDefault(a => a.Id == activeAccount.Id);
                var accountToAdd = _context.Accounts
                                   .FirstOrDefault(a => a.Id == entities.Account.Id);

                accountToDeduct.Balance -= entities.MoneyAmount;
                accountToAdd.Balance    += entities.MoneyAmount;

                _context.SaveChanges();

                return(true);
            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
 public ExchangeRateDialog(LuisResult luisResult)
 {
     _entityProps = new EntityAssigner().AssignEntities(luisResult);
 }
 public TransactionsQueryDialog(LuisResult luisResult = null)
 {
     _entityProps = new EntityAssigner().AssignEntities(luisResult);
 }
Ejemplo n.º 5
0
        public EntityProps AssignEntities(LuisResult luisResult)
        {
            var entityProps = new EntityProps();

            //Resolve LUIS entities
            foreach (var entity in luisResult.Entities)
            {
                //handle encycplopedia seperately due to multiple recommendations per encyclopedia entity
                if (entity.Type.Contains("builtin.encyclopedia") && string.IsNullOrEmpty(entityProps.Encyclopedia))
                {
                    entityProps.Encyclopedia = entity.Entity;
                    continue;
                }

                //handle multiple datetime resolution variants
                if (entity.Type.Contains("builtin.datetimeV2"))
                {
                    //dance around the fire. VS doesn't know what types the objects have, need to give it a hand...
                    var getTheListObj = entity.Resolution["values"] as List <object>;
                    var resolvedDict  = getTheListObj[0] as Dictionary <string, object>;

                    //assign string values
                    var      outDateTimex = resolvedDict["timex"].ToString();
                    var      outDateType  = resolvedDict["type"].ToString();
                    DateTime outDateStart = new DateTime();
                    DateTime outDateEnd   = new DateTime();


                    //parse datetime values
                    switch (outDateType)
                    {
                    case "date":
                        DateTime.TryParse(resolvedDict["value"].ToString(), out outDateStart);
                        break;

                    case "daterange":
                    case "datetimerange":
                        DateTime.TryParse(resolvedDict["start"].ToString(), out outDateStart);
                        DateTime.TryParse(resolvedDict["end"].ToString(), out outDateEnd);
                        break;
                    }


                    //set property to parsed values
                    entityProps.DateRange = new DateRange
                    {
                        Timex = outDateTimex,
                        Type  = outDateType,
                        Start = outDateStart,
                        End   = outDateEnd
                    };
                    continue;
                }

                switch (entity.Type)
                {
                case "builtin.percentage":
                    float.TryParse(entity.Resolution["value"].ToString().Replace("%", ""), out var parsedPercentage);
                    entityProps.Percentage = parsedPercentage / 100f;
                    break;

                case "builtin.currency":
                    float.TryParse(entity.Resolution["value"]?.ToString(), out var parsedCurrency);
                    entityProps.MoneyAmount = entityProps.MoneyAmount == 0 ? parsedCurrency : entityProps.MoneyAmount;

                    entityProps.MoneyCurrency.Add(
                        entity.Resolution["unit"]?
                        .ToString()
                        .ToLower()
                        );
                    break;

                //comparison operators.
                //TODO: Check for multiple operators (e.g. <=)
                case "comparisonOperator::equal":
                    entityProps.ComparisonOperator = "equal";
                    break;

                case "comparisonOperator::lessThan":
                    entityProps.ComparisonOperator = "lessThan";
                    break;

                case "comparisonOperator::moreThan":
                    entityProps.ComparisonOperator = "moreThan";
                    break;

                //custom ordinals
                case "ordinalTense::last":
                    entityProps.OrdinalTense = "last";
                    break;

                case "ordinalTense::first":
                    entityProps.OrdinalTense = "first";
                    break;

                case "account":
                    var trimmedEntity = entity.Score >= 0.7 ? entity.Entity.ToLower().Replace("account", "").Trim() : null;

                    if (string.IsNullOrEmpty(trimmedEntity))
                    {
                        break;
                    }

                    var accounts = AccountDataController.Accounts.ToList();
                    foreach (var account in accounts)
                    {
                        if (string.Equals(account.Name, trimmedEntity, StringComparison.CurrentCultureIgnoreCase) ||
                            string.Equals(account.Type, trimmedEntity, StringComparison.CurrentCultureIgnoreCase))
                        {
                            entityProps.Account = account;
                            break;
                        }
                    }
                    break;
                }
            }

            return(entityProps);
        }
Ejemplo n.º 6
0
 public AccountQueryDialog(LuisResult result)
 {
     _entityProps = new EntityAssigner().AssignEntities(result);
 }
Ejemplo n.º 7
0
 public TransferDialog(LuisResult luisResult)
 {
     _entityProps   = new EntityAssigner().AssignEntities(luisResult);
     _targetAccount = _entityProps.Account;
     _amount        = _entityProps.MoneyAmount;
 }
Ejemplo n.º 8
0
        public static IEnumerable <Transaction> GetTransactionsFromEntities(Account account, EntityProps entities)
        {
            //TODO: Come up with a cleaner way of checking that enough info has been supplied
            if (entities.DateRange == null &&
                entities.MoneyAmount == 0 &&
                string.IsNullOrEmpty(entities.Encyclopedia) &&
                string.IsNullOrEmpty(entities.OrdinalTense))
            {
                entities = null;
            }

            var sql = new QueryStringBuilder().TransactionBuilder(account, entities);

            return(_context.Transactions.SqlQuery(sql)
                   .OrderBy(t => t.DateTime));
        }
        //TODO: Use StringBuilder instead of string concatenation
        public string TransactionBuilder(Account account, EntityProps entities)
        {
            //if no usable entities supplied by user, return latest 5 transaction query string
            if (entities == null)
            {
                return("SELECT TOP 5 * FROM Transactions WHERE DateTime = (SELECT max(DateTime) FROM Transactions)");
            }

            //start building query string
            string query = "SELECT ";

            //if query contains the word 'last' or 'first'
            if (!string.IsNullOrEmpty(entities.OrdinalTense))
            {
                //ordianl tense (last/first N transactions)
                //TODO: add support for 'last 2/4/6/etc.'
                switch (entities.OrdinalTense)
                {
                case "first":     //todo: add support for 'first'
                case "last":
                    //if user specified recepient name, fetch latest transaction to that recepient
                    if (!string.IsNullOrEmpty(entities.Encyclopedia))
                    {
                        query += $"* FROM Transactions WHERE DateTime IN (SELECT max(DateTime) FROM Transactions WHERE RecepientName LIKE '%{entities.Encyclopedia}%') AND RecepientName LIKE '%{entities.Encyclopedia}%'";
                    }
                    else
                    {
                        query += "TOP 1 * FROM Transactions WHERE DateTime = (SELECT max(DateTime) FROM Transactions)";
                    }

                    //further additions to the query will not change the result. Return the query and done.
                    return(query);
                }
            }
            else if (entities.DateRange != null)
            {
                query += "* FROM Transactions WHERE ";

                //dates
                //TODO: check collisions if OrdinalTense is set as well
                switch (entities.DateRange.Type)
                {
                case "daterange":
                case "datetimerange":
                    query +=
                        $"DateTime >= '{entities.DateRange.Start:yyyy-MM-dd}' AND DateTime <= '{entities.DateRange.End:yyyy-MM-dd}' AND ";
                    break;

                case "date":
                    query += $"DateTime = '{entities.DateRange.Start:yyyy-MM-dd}' AND ";
                    break;
                }
            }
            else
            {
                query += "* FROM Transactions WHERE ";
            }

            //recepient
            //DONE: Support partial names
            query += string.IsNullOrEmpty(entities.Encyclopedia) ? "" : $"RecepientName LIKE '%{entities.Encyclopedia}%' AND ";

            //currency (amount of transaction)
            if (entities.MoneyAmount != 0)
            {
                query += "Amount ";

                //check if user specified if transaction amount is 'over' or 'under' certain number
                switch (entities.ComparisonOperator)
                {
                case "lessThan":
                    query += "< ";
                    break;

                case "moreThan":
                    query += "> ";
                    break;

                default:
                    query += "= ";
                    break;
                }

                query += entities.MoneyAmount;
            }

            //return transactions that mach account id
            query += query.EndsWith("AND ") ? $"AccountId = {account.Id}" : $" AND AccountId = {account.Id}";

            //check if sql query ends with 'AND '
            return(query);
        }