From bada150bd628396b0c0d10591aebc103560513cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Radek=20Pu=C5=A1?= <pusradek@fit.cvut.cz>
Date: Thu, 12 Dec 2019 01:40:49 +0100
Subject: [PATCH] constant symbol, userID loading

---
 Core/Core/AI/NetworkLogic/Feeder.cs           | 18 ++++--
 .../ImportCSVLogic/ModelImporter.cs           |  6 +-
 .../ConstantSymbolOperations.cs               | 61 +++++++++++++++++++
 Core/Core/Controllers/ForecastController.cs   | 29 ++++++++-
 Core/Core/Controllers/LoginController.cs      |  2 -
 Core/Core/Models/Network.cs                   | 20 ++++++
 6 files changed, 126 insertions(+), 10 deletions(-)
 create mode 100644 Core/Core/BusinessLogic/TransactionDataOperations/ConstantSymbolOperations.cs

diff --git a/Core/Core/AI/NetworkLogic/Feeder.cs b/Core/Core/AI/NetworkLogic/Feeder.cs
index fa6f8da..00f567e 100644
--- a/Core/Core/AI/NetworkLogic/Feeder.cs
+++ b/Core/Core/AI/NetworkLogic/Feeder.cs
@@ -22,6 +22,7 @@ namespace Core.AI.NetworkLogic
         #endregion
 
         private readonly Model Context;
+        private readonly long UserID;
         private DateTime LowerBoundDate;
         List<NormalizationContainer> DataSet;
 
@@ -32,10 +33,15 @@ namespace Core.AI.NetworkLogic
         // seek / comparsion interval
         private const int DAYSTEP = 14;
 
-        public Feeder(Model context)
+        public Feeder(Model context, long userID)
         {
             Context = context;
-            LowerBoundDate = Context.Transactions.Min(t => t.Date);
+            UserID = userID;
+
+            LowerBoundDate = Context.Files
+                .Where(f => f.UserID == UserID)
+                .SelectMany(f => f.Transactions)
+                .Min(t => t.Date);
         }
 
 
@@ -103,15 +109,17 @@ namespace Core.AI.NetworkLogic
         /// <returns></returns>
         private List<NormalizationContainer> GetChunkFromDB(DateTime minDate, DateTime maxDate)
         {
-            IQueryable<Transaction> Transactions = Context.Transactions
+            IQueryable<Transaction> transactions = Context.Files
+                .Where(f => f.UserID == UserID)
+                .SelectMany(f => f.Transactions)
                 .Include(t => t.SenderAccount)
                 .Include(t => t.ConstantSymbol)
                 .Where(t => t.Date >= minDate && t.Date < maxDate)
                 .Where(t => t.RepetitionID == null);
 
-            List<NormalizationContainer> containers = new List<NormalizationContainer>(Transactions.Count());
+            List<NormalizationContainer> containers = new List<NormalizationContainer>(transactions.Count());
 
-            foreach (Transaction transaction in Transactions)
+            foreach (Transaction transaction in transactions)
             {
                 if (Exclusions(transaction))
                     continue;
diff --git a/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs b/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs
index d2fd631..bb3ad46 100644
--- a/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs
+++ b/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs
@@ -1,4 +1,5 @@
-using Core.Data;
+using Core.BusinessLogic.TransactionContextOperations;
+using Core.Data;
 using Core.Models;
 using CsvHelper;
 using Microsoft.EntityFrameworkCore;
@@ -13,10 +14,12 @@ namespace Core.BusinessLogic.ImportCSVLogic
     public class ModelImporter : IModelImporter
     {
         private readonly Model model;
+        private readonly ConstantSymbolOperations ConstantSymbolOperations;
 
         public ModelImporter(Model model)
         {
             this.model = model;
+            ConstantSymbolOperations = new ConstantSymbolOperations(model);
         }
 
         public void Import(string filePath, string fileName, long userID)
@@ -119,6 +122,7 @@ namespace Core.BusinessLogic.ImportCSVLogic
                 return symbol;
 
             symbol = new ConstantSymbol(parsedValue);
+            ConstantSymbolOperations.Fill(symbol);
             constantSymbolsList.Add(symbol);
             return symbol;
         }
diff --git a/Core/Core/BusinessLogic/TransactionDataOperations/ConstantSymbolOperations.cs b/Core/Core/BusinessLogic/TransactionDataOperations/ConstantSymbolOperations.cs
new file mode 100644
index 0000000..93a6a9d
--- /dev/null
+++ b/Core/Core/BusinessLogic/TransactionDataOperations/ConstantSymbolOperations.cs
@@ -0,0 +1,61 @@
+using Core.Data;
+using Core.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Core.BusinessLogic.TransactionContextOperations
+{
+    public class ConstantSymbolOperations
+    {
+        private readonly Model Context;
+
+        public ConstantSymbolOperations(Model context)
+        {
+            Context = context;
+        }
+
+        // https://cs.wikipedia.org/wiki/Konstantn%C3%AD_symbol_(pen%C4%9B%C5%BEn%C3%AD_p%C5%99evod)
+        public void FillAll()
+        {
+            foreach (ConstantSymbol symbol in Context.ConstantSymbols)
+            {
+                symbol.ValidateThis();
+                if (symbol.Valid)
+                    DomesticInitialization(symbol);
+            }
+            Context.SaveChanges();
+        }
+
+        public ConstantSymbol Fill(ConstantSymbol symbol)
+        {
+            symbol.ValidateThis();
+            if (symbol.Valid)
+                DomesticInitialization(symbol);
+            return symbol;
+        }
+
+        private void DomesticInitialization(ConstantSymbol cs)
+        {
+            int parseID = (int)cs.ProvidedID;
+
+            //cca 6 numbers
+            cs.ReservedSymbol = Context.ReservedSymbols.FirstOrDefault(x => x.Code == parseID);
+            if (cs.ReservedSymbol != null)
+                return;
+
+            //numbers 0-9
+            cs.LastNumber = Context.LastNumbers.FirstOrDefault(x => x.Code == parseID % 10);
+
+            parseID /= 10;
+            cs.Kind = Context.Kinds.FirstOrDefault(x => x.Code == parseID % 10);
+
+            // 3-4 ciphers
+            cs.MinistryPredefined = Context.MinistryPredefined.FirstOrDefault(x => (x.Code == parseID || x.Code == (int)cs.ProvidedID));
+            cs.Valid = (cs.MinistryPredefined != null);
+
+            return;
+        }
+    }
+}
diff --git a/Core/Core/Controllers/ForecastController.cs b/Core/Core/Controllers/ForecastController.cs
index 0cf118e..248bcec 100644
--- a/Core/Core/Controllers/ForecastController.cs
+++ b/Core/Core/Controllers/ForecastController.cs
@@ -9,6 +9,8 @@ using Core.Models;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.EntityFrameworkCore;
+using NeuralNetwork.NeuralNetNamespace;
 
 namespace Core.Controllers
 {
@@ -38,10 +40,10 @@ namespace Core.Controllers
             if (!transactions.Any())
                 return null;
 
-            return GetWeekSums(transactions);
+            return GetWeekSums(transactions, userID);
         }
 
-        private TransactionSumsContainer GetWeekSums(IQueryable<Transaction> transactions)
+        private TransactionSumsContainer GetWeekSums(IQueryable<Transaction> transactions, long userID)
         {
             DateTime maxDate = transactions.Max(t => t.Date);
             DateTime lowerbound = maxDate.AddDays(-7);
@@ -55,6 +57,9 @@ namespace Core.Controllers
                 .Select(t => (double) t.Amount)
                 .ToList();
 
+            //NeuralNet ann = GetNetwork(userID);
+            //feed network
+
             int smallCount = intervalTransactions.Where(t => t.Amount < -2 && t.Amount >= -500).Count();
             int mediumCount = intervalTransactions.Where(t => t.Amount < -500 && t.Amount >= -5000).Count();
             int hugeCount = intervalTransactions.Where(t => t.Amount < -5000).Count();
@@ -62,6 +67,26 @@ namespace Core.Controllers
             return new TransactionSumsContainer(expenses, smallCount, mediumCount, hugeCount);
         }
 
+        private NeuralNet GetNetwork(long userID)
+        {
+            
+            long? networkID = Context.Users
+                .FirstOrDefault(u => u.ID == userID)
+                .NetworkID;
+
+            /*var networksQuery = Context.Networks
+                    .Include(network => network.Layer)
+                    .ThenInclude(layer => layer.Neuron)
+                    .ThenInclude(neuron => neuron.Weight);
+
+            Network network = networkID == null ? networksQuery.First() : (networksQuery.First(n => n.ID == networkID) ?? networksQuery.First());*/
+            Network network = networkID == null ? Context.Networks.First() : (Context.Networks.First(n => n.ID == networkID) ?? Context.Networks.First());
+
+            NeuralNet ann = new NeuralNet();
+            ann.Import(network.ToImportList());
+            return ann;
+        }
+
         // POST: api/Forecast
         [HttpPost]
         public void Post([FromBody] string value)
diff --git a/Core/Core/Controllers/LoginController.cs b/Core/Core/Controllers/LoginController.cs
index 23d97d7..a02457a 100644
--- a/Core/Core/Controllers/LoginController.cs
+++ b/Core/Core/Controllers/LoginController.cs
@@ -40,10 +40,8 @@ namespace Core.Controllers
                 Subject = new ClaimsIdentity(new Claim[] {
                     new Claim(JwtRegisteredClaimNames.Sub, username),
                     new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
-                    //new Claim(ClaimTypes.NameIdentifier, ID.ToString()),
                     new Claim("UserID", ID.ToString()),
                     new Claim("LoggedOn", DateTime.Now.ToString())
-                    //ClaimTypes.Role if needed role
                 }),
                 SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature),
                 Issuer = Settings.Site,
diff --git a/Core/Core/Models/Network.cs b/Core/Core/Models/Network.cs
index 6ef403d..a22adc1 100644
--- a/Core/Core/Models/Network.cs
+++ b/Core/Core/Models/Network.cs
@@ -13,6 +13,15 @@ namespace Core.Models
         public long ID { get; set; }
         public ICollection<Layer> Layer { get; set; }
         public ICollection<User> Users { get; set; }
+
+        public List<List<List<double>>> ToImportList()
+        {
+            List<List<List<double>>> list = new List<List<List<double>>>(Layer.Count);
+            foreach (Layer l in Layer)
+                list.Add(l.ToList());
+
+            return list;
+        }
     }
 
     public class Layer
@@ -20,6 +29,15 @@ namespace Core.Models
         [Key]
         public long ID { get; set; }
         public ICollection<Neuron> Neuron { get; set; }
+
+        public List<List<double>> ToList()
+        {
+            List<List<double>> list = new List<List<double>>(Neuron.Count);
+            foreach (Neuron n in Neuron)
+                list.Add(n.ToList());
+
+            return list;
+        }
     }
 
     public class Neuron
@@ -27,6 +45,8 @@ namespace Core.Models
         [Key]
         public long ID { get; set; }
         public ICollection<Weight> Weight { get; set; }
+
+        public List<double> ToList() => Weight.Select(w => w.Value).ToList();
     }
 
     public class Weight
-- 
GitLab