Esempio n. 1
0
 /// <summary>
 /// This method returns a language-specific service.
 ///
 /// It provides a point of extensibility for a language implementation
 /// to offer more functionality than the standard engine members discussed here.
 /// </summary>
 public TService GetService <TService>(params object[] args) where TService : class
 {
     if (typeof(TService) == typeof(TokenCategorizer))
     {
         TokenizerService service = _language.GetService <TokenizerService>(ArrayUtils.Insert((object)_language, args));
         return((service != null) ? (TService)(object)new TokenCategorizer(service) : null);
     }
     if (typeof(TService) == typeof(ExceptionOperations))
     {
         ExceptionOperations service = _language.GetService <ExceptionOperations>();
         return((service != null) ? (TService)(object)service : (TService)(object)new ExceptionOperations(_language));
     }
     return(_language.GetService <TService>(args));
 }
        public void GetGenericValueReturnsCorrectValue()
        {
            // Arrange
            var mockLogManager = new Mock <ILogManager>();
            var testObject     = new Audit {
                InvoiceId = 5
            };

            // Act
            var sut      = new TokenizerService(mockLogManager.Object);
            var response = sut.GetValue <int>(testObject, "InvoiceId");

            // Assert
            response.Content.Should().BeOfType(typeof(int));
            response.Content.Should().Be(5);
        }
Esempio n. 3
0
        /// <summary>
        /// Reads a text from user input, tokenizes it, produces a custom output
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //retrieve the logging instance
            NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

            Logger.Info("Application started");
            Console.WriteLine("#### Text Tokenizer - Willkommen ####");

            //retrieve the associated service
            ITokenizerService _TokenizerService = new TokenizerService();

            // iterate logic until user stops execution
            do
            {
                Console.Write("Enter some text: ");
                string text = Console.ReadLine();

                IEnumerable <string> result = null;

                try {
                    //retrieve result from the associated Service
                    result = _TokenizerService.TokenizeAndConvert(text);
                }
                catch (Exception ex)
                {
                    Logger.Fatal(ex.Message);       //Log Exception
                    throw;
                }

                //just forward to console
                foreach (var item in result)
                {
                    Console.WriteLine(item);
                }

                // get the user input for every iteration, allowing to exit at will
                Console.Write("Continue [y|n]?");
                text = Console.ReadLine();
                if (text.Equals("n"))
                {
                    // exit the method.
                    break;
                }
            }while (true);
        }
        public void GetValuesReturnsCorrectValues()
        {
            // Arrange
            var mockLogManager = new Mock <ILogManager>();
            var testObject     = new Audit {
                Id = 2, InvoiceId = 5
            };
            var testFields = new List <string> {
                "InvoiceId", "Id"
            };

            // Act
            var sut      = new TokenizerService(mockLogManager.Object);
            var response = sut.GetValues(testObject, testFields);

            // Assert
            response.Content["Id"].Should().Be(2);
            response.Content["InvoiceId"].Should().Be(5);
        }
        /// <summary>
        /// This method returns a language-specific service.
        ///
        /// It provides a point of extensibility for a language implementation
        /// to offer more functionality than the standard engine members discussed here.
        ///
        /// Commonly available services include:
        ///     TokenCategorizer
        ///         Provides standardized tokenization of source code
        ///     ExceptionOperations
        ///         Provides formatting of exception objects.
        ///     DocumentationProvidera
        ///         Provides documentation for live object.
        /// </summary>
        public TService GetService <TService>(params object[] args) where TService : class
        {
            if (typeof(TService) == typeof(TokenCategorizer))
            {
                TokenizerService service = LanguageContext.GetService <TokenizerService>(ArrayUtils.Insert((object)LanguageContext, args));
                return((service != null) ? (TService)(object)new TokenCategorizer(service) : null);
            }

            if (typeof(TService) == typeof(ExceptionOperations))
            {
                ExceptionOperations service = LanguageContext.GetService <ExceptionOperations>();
                return((service != null) ? (TService)(object)service : (TService)(object)new ExceptionOperations(LanguageContext));
            }

            if (typeof(TService) == typeof(DocumentationOperations))
            {
                DocumentationProvider service = LanguageContext.GetService <DocumentationProvider>(args);
                return((service != null) ? (TService)(object)new DocumentationOperations(service) : null);
            }

            return(LanguageContext.GetService <TService>(args));
        }
Esempio n. 6
0
 internal TokenCategorizer(TokenizerService tokenizer)
 {
     Assert.NotNull(tokenizer);
     _tokenizer = tokenizer;
 }
Esempio n. 7
0
        private async Task Tokenize(Submission submission, TokenizerService tokenizerService, string accessToken)
        {
            // check if all files are tokenized - if so, abort
            var fullyTokenized = !submission.Files.Any(f => string.IsNullOrEmpty(f.TokenizedContent) && !string.IsNullOrEmpty(f.Content));

            if (fullyTokenized)
            {
                return;
            }

            var client = new HttpClient();

            client.BaseAddress = new Uri(tokenizerService.BaseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                accessToken);

            var payload = new Model.TokenizerRequest
            {
                Title       = submission.Assignment.Title,
                Assignments = new List <Model.Assignment>()
            };

            var restAssignment = new Model.Assignment
            {
                FirstName = submission.FirstName,
                LastName  = submission.LastName,
                Files     = new List <Model.File>()
            };

            foreach (var file in submission.Files.Where(f => string.IsNullOrEmpty(f.TokenizedContent)))
            {
                restAssignment.Files.Add(new Model.File
                {
                    FileName     = file.FileName,
                    Base64Source = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(file.Content))
                });
            }

            payload.Assignments.Add(restAssignment);

            var response = await client.PostAsJsonAsync(tokenizerService.RequestPath, payload);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var studResult = (await response.Content.ReadAsAsync <Model.TokenizerResult>())
                                 ?.StudentResults
                                 ?.FirstOrDefault(s => s.FirstName.Equals(submission.FirstName) && s.LastName.Equals(submission.LastName));

                if (studResult != null)
                {
                    foreach (var file in submission.Files.Where(f => string.IsNullOrEmpty(f.TokenizedContent)))
                    {
                        file.TokenizedContent =
                            studResult.Files.FirstOrDefault(f => f.FileName.Equals(file.FileName)).Tokens.ToString();
                    }
                }
            }

            await this.submissionManager.SaveFilesAsync(submission.Files.ToList());
        }