Esempio n. 1
0
        private void InitializeStrategy(Strategy strategy)
        {
            KeywordGenerator keywordStrategy = new KeywordGenerator();
            SymbolGenerator  symbolStrategy  = new SymbolGenerator();

            Strategy.StrategyType strategyType = strategy.GetStrategyType();
            int    numberOfActions             = 0;
            Action nextAction = new Action();

            byte[] endlineChar = BitConverter.GetBytes('\n');

            if (strategyType == Strategy.StrategyType.ValidFileOpen)
            {
                nextAction = strategy.GetNextAction();
                Action.ActionType type       = nextAction.Type;
                byte[]            actionData = nextAction.Data;

                using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(currentFilePath, FileMode.Create)))
                {
                    binaryWriter.Write(Convert.ToInt32(nextAction.Type));
                    binaryWriter.Write(actionData.Length);
                    binaryWriter.Write(actionData);

                    binaryWriter.Close();
                }
            }
            else
            {
                FileStream fileStream = File.Open(currentFilePath, FileMode.Create);
                using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
                {
                    while (numberOfActions < maxActions)
                    {
                        if (strategyType == Strategy.StrategyType.SymbolGenerator || strategyType == Strategy.StrategyType.KeywordGenerator)
                        {
                            int choice = random.Next(0, 2); // Choose between whether to insert a symbol or a keyword
                            if (choice == 0)
                            {
                                nextAction = keywordStrategy.GetNextAction();
                            }
                            else
                            {
                                nextAction = symbolStrategy.GetNextAction();
                            }
                        }
                        else
                        {
                            nextAction = strategy.GetNextAction();
                        }

                        Action.ActionType type       = nextAction.Type;
                        byte[]            actionData = nextAction.Data;


                        if (numberOfActions % 10 == 0) // To insert an endline character after every 100 insertions
                        {
                            binaryWriter.Write(Convert.ToInt32(Action.ActionType.InsertSymbol));
                            binaryWriter.Write(endlineChar.Length);
                            binaryWriter.Write(endlineChar);
                        }

                        binaryWriter.Write(Convert.ToInt32(nextAction.Type));
                        binaryWriter.Write(actionData.Length);
                        binaryWriter.Write(actionData);

                        numberOfActions++;
                    }

                    binaryWriter.Close();
                }
            }
        }
Esempio n. 2
0
        private void InitializeStrategy(Strategy strategy)
        {
            KeywordGenerator keywordStrategy = new KeywordGenerator();
            SymbolGenerator symbolStrategy = new SymbolGenerator();

            Strategy.StrategyType strategyType = strategy.GetStrategyType();
            int numberOfActions = 0;
            Action nextAction = new Action();
            byte[] endlineChar = BitConverter.GetBytes('\n');

            if (strategyType == Strategy.StrategyType.ValidFileOpen)
            {
                nextAction = strategy.GetNextAction();
                Action.ActionType type = nextAction.Type;
                byte[] actionData = nextAction.Data;

                using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(currentFilePath, FileMode.Create)))
                {
                    binaryWriter.Write(Convert.ToInt32(nextAction.Type));
                    binaryWriter.Write(actionData.Length);
                    binaryWriter.Write(actionData);

                    binaryWriter.Close();
                }
            }
            else
            {
                FileStream fileStream = File.Open(currentFilePath, FileMode.Create);
                using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
                {
                    while (numberOfActions < maxActions)
                    {
                        if (strategyType == Strategy.StrategyType.SymbolGenerator || strategyType == Strategy.StrategyType.KeywordGenerator)
                        {

                            int choice = random.Next(0, 2); // Choose between whether to insert a symbol or a keyword
                            if (choice == 0)
                                nextAction = keywordStrategy.GetNextAction();
                            else
                                nextAction = symbolStrategy.GetNextAction();
                        }
                        else
                            nextAction = strategy.GetNextAction();

                        Action.ActionType type = nextAction.Type;
                        byte[] actionData = nextAction.Data;

                        if (numberOfActions % 10 == 0) // To insert an endline character after every 100 insertions
                        {
                            binaryWriter.Write(Convert.ToInt32(Action.ActionType.InsertSymbol));
                            binaryWriter.Write(endlineChar.Length);
                            binaryWriter.Write(endlineChar);
                        }

                        binaryWriter.Write(Convert.ToInt32(nextAction.Type));
                        binaryWriter.Write(actionData.Length);
                        binaryWriter.Write(actionData);

                        numberOfActions++;
                    }

                    binaryWriter.Close();
                }
            }
        }