/// <summary> /// Register the collection of processors with automatic dependency resolution. /// May to throw an InvalidOperationException. /// </summary> /// <param name="collectionOfProcessors">Collection of the processors.</param> public void Register(IEnumerable <BaseTextProcessor> collectionOfProcessors) { Queue <BaseTextProcessor> registrationQueue = new Queue <BaseTextProcessor>(collectionOfProcessors); int previousCount = registrationQueue.Count; while (registrationQueue.Count > 0) { String insolubleInput = null; for (int i = 0; i < previousCount; ++i) { BaseTextProcessor processor = registrationQueue.Dequeue(); String registerDetails; RegisterState result = this.RegisterProcessor(processor, out registerDetails); if (result == RegisterState.OutputLayerNotUnique) { throw new InvalidOperationException(String.Format(@"Name ""{0}"" of output layer is not unique. Layer may be generated by one processor only!", registerDetails)); } if (result == RegisterState.InputLayerNotFound) { insolubleInput = registerDetails; registrationQueue.Enqueue(processor); } } if (previousCount == registrationQueue.Count) { throw new InvalidOperationException(String.Format(@"Collection of processors contains insoluble depending - ""{0}""", insolubleInput)); } previousCount = registrationQueue.Count; } }
private static string getHtml(string inputFileName) { // using will close input, as it is opened from a filename using (Stream inputStream = BaseTextProcessor.GetInputStream(inputFileName)) { StreamReader inputStreamReader = new StreamReader(inputStream); string html = inputStreamReader.ReadToEnd(); return(html); } }
public void RegisterMethod_With_ValidCollectionOfProcessors() { TextProcessingManager manager = new TextProcessingManager(); //create some processort with valid dependences BaseTextProcessor processor1 = new BaseTextProcessor("processor1", new String[] {}, new String[] { "layer1" }); BaseTextProcessor processor2 = new BaseTextProcessor("processor2", new String[] { "layer1", "layer3" }, new String[] { "layer2" }); BaseTextProcessor processor3 = new BaseTextProcessor("processor3", new String[] {}, new String[] { "layer3" }); //Collection of processors BaseTextProcessor[] validCollection = new BaseTextProcessor[] { processor2, processor1, processor3 }; manager.Register(validCollection); }
private static void save(StringBuilder outputHtml, string outputFileName) { Stream outputStream = BaseTextProcessor.GetOutputStream(outputFileName); try { StreamWriter outputStreamWriter = new StreamWriter(outputStream); outputStreamWriter.Write(outputHtml.ToString()); outputStreamWriter.Flush(); } finally { //if (args.Length > 2) // outputfilename, do not close the default OutputStream, as the console relies on that outputStream.Close(); } }
public void RegisterProcessorMethod_With_TwoProcessorsWithValidDependences() { TextProcessingManager manager = new TextProcessingManager(); BaseTextProcessor processor1 = new BaseTextProcessor("processor1", new String[] {}, new String[] { "layer1" }); BaseTextProcessor processor2 = new BaseTextProcessor("processo2", new String[] { "layer1" }, new String[] { "layer2" }); String details; var regResult = manager.RegisterProcessor(processor1, out details); if (regResult != TextProcessingManager.RegisterState.SuccessfullyRegistered) { Assert.Fail("Registration of the processor failed.", new Object[] { regResult, details }); } regResult = manager.RegisterProcessor(processor2, out details); Assert.AreEqual(regResult, TextProcessingManager.RegisterState.SuccessfullyRegistered, "Registration of the processor 2 failed. Details is : " + details); }
/// <summary> /// Method, which perform registration processor and its layers. /// Stable, this method guarantees the indexes processors previously recorded. /// </summary> /// <param name="processor">The processor for registration.</param> /// <param name="stateDetails">Result details.</param> /// <returns>Registration result state</returns> public RegisterState RegisterProcessor(BaseTextProcessor processor, out String stateDetails) { stateDetails = processor.Name; int proc_index = 0; if (!_processorNameTable.TryGetValue(processor.Name, out proc_index)) { String[] input = processor.InputLayerNames; List <List <int> > inputRegisterCache = new List <List <int> >(); int len = input.Length; for (int i = 0; i < len; ++i) { int layerIndex = 0; if (!TryGetLayerIndex(input[i], out layerIndex)) { len = i; break; } if (layerIndex == _processorByInputLayer.Count) { _processorByInputLayer.Add(new List <int>()); } List <int> procByInput = _processorByInputLayer[layerIndex]; inputRegisterCache.Add(procByInput); procByInput.Add(_processorList.Count); } if (len != input.Length) //очистка в случае неудачи { for (int i = 0; i < inputRegisterCache.Count; ++i) { inputRegisterCache[i].RemoveAt(inputRegisterCache[i].Count - 1); } stateDetails = input[len]; return(RegisterState.InputLayerNotFound); } String[] output = processor.OutputLayerNames; len = output.Length; for (int i = 0; i < len; ++i) { if (_layerNameTable.ContainsKey(output[i])) { len = i; break; } _layerNameTable.Add(output[i], _processorByOutputLayer.Count); _processorByOutputLayer.Add(_processorList.Count); } if (len != output.Length) //очистка в случае неудачи { for (int i = 0; i < inputRegisterCache.Count; ++i) { inputRegisterCache[i].RemoveAt(inputRegisterCache[i].Count - 1); } for (int i = 0; i < len; ++i) { _layerNameTable.Remove(output[i]); } _processorByOutputLayer.RemoveRange(_processorByOutputLayer.Count - len, len); stateDetails = output[len]; return(RegisterState.OutputLayerNotUnique); } _processorList.Add(processor); return(RegisterState.SuccessfullyRegistered); } else { return(RegisterState.AlreadyRegistered); } }