예제 #1
0
        /// <summary>
        /// Performs the processing procedure against an input
        /// </summary>
        /// <param name="pipeline">The processing pipeline</param>
        /// <param name="input">The input to be processed</param>
        /// <returns>The output from the pipeline, or null if the process
        /// is cancelled</returns>
        private Image _processInput(Pipeline.Pipeline pipeline, JobInput input)
        {
            Image theInput = input.Input;

            foreach (PipelineEntry entry in pipeline)
            {
                lock ( _cancelPadlock )
                {
                    if (_cancel)
                    {
                        return(null);
                    }
                }

                AlgorithmPlugin plugin = entry.Process;
                plugin.Input = theInput;
                plugin.Run(entry.ProcessInput);
                theInput = plugin.Output ?? plugin.Input;
            }

            InputProcessedArgs e = new InputProcessedArgs(input.Identifier, (Image)theInput.Clone());

            _ticket.OnInputProcessed(e);
            return(theInput);
        }
예제 #2
0
 public void TestActivate_Interpreter_Exception()
 {
     AlgorithmActivator  activator = new AlgorithmActivator(new InterpreterRegistrar());
     AlgorithmDefinition d         = new AlgorithmDefinition("Test",
                                                             new Property[] { new Property("throw", typeof(double)) });
     AlgorithmPlugin p = activator.Activate(d);
 }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PipelineEntry"/>
        /// class.
        /// </summary>
        /// <param name="plugin">The <see cref="AlgorithmPlugin"/> to be
        /// executed.</param>
        /// <exception cref="ArgumentNullException">plugin is null.</exception>
        public PipelineEntry(AlgorithmPlugin plugin)
        {
            if (plugin == null)
            {
                throw new ArgumentNullException("plugin");
            }

            Process = plugin;
        }
예제 #4
0
        public void TestManufacture_InvalidDefintion()
        {
            ProcessPluginRepository r = new ProcessPluginRepository();

            RegistryCache.Cache.Initialize(r);
            RegistryFactory factory = new RegistryFactory(r);
            AlgorithmPlugin p       = factory.Manufacture(null);

            Assert.IsNull(p);
        }
예제 #5
0
        public void TestManufacture_ValidDefinition()
        {
            AlgorithmDefinition     d = new AlgorithmDefinition("gamma", new Property[] {});
            ProcessPluginRepository r = new ProcessPluginRepository();

            RegistryCache.Cache.Initialize(r);
            RegistryFactory factory = new RegistryFactory(r);
            AlgorithmPlugin p       = factory.Manufacture(d);

            Assert.IsNotNull(p);
        }
예제 #6
0
        public void TestActivate_NoProperties()
        {
            AlgorithmActivator  activator = new AlgorithmActivator(new TestRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("Test", new Property[] {});
            AlgorithmPlugin     p         = activator.Activate(d);

            Assert.IsNotNull(p);
            Assert.AreEqual(typeof(TestPlugin), p.GetType());

            TestPlugin test = p as TestPlugin;

            Assert.AreEqual(1, test.Test);
        }
예제 #7
0
 /// <summary>
 /// Checks to see if the plugin requests to set its properties through
 /// interpretation
 /// </summary>
 /// <param name="definition">The definition containing the property values</param>
 /// <param name="plugin">The plugin instance to set the values against</param>
 private void _interpreterExecution(AlgorithmDefinition definition, AlgorithmPlugin plugin)
 {
     if (plugin is IPropertyInterpreter)
     {
         try
         {
             (plugin as IPropertyInterpreter).Interpret(definition.Properties);
         }
         catch (Exception e)
         {
             throw new ActivationException(e.Message, e);
         }
     }
 }
예제 #8
0
        public void TestActivate_Interpreter_NoException()
        {
            AlgorithmActivator  activator = new AlgorithmActivator(new InterpreterRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("Test",
                                                                    new Property[] { new Property("Test", typeof(double)) });
            AlgorithmPlugin p = activator.Activate(d);

            Assert.IsNotNull(p);
            Assert.AreEqual(typeof(TestPluginInterpreter), p.GetType());

            TestPluginInterpreter t = p as TestPluginInterpreter;

            Assert.IsTrue(t.DidInterpret);
        }
예제 #9
0
        public void TestActivate_OneProperty_UnknownType()
        {
            AlgorithmActivator  activator = new AlgorithmActivator(new TestRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("Test",
                                                                    new Property[] { new Property("Test", typeof(string)) });
            AlgorithmPlugin p = activator.Activate(d);

            Assert.IsNotNull(p);
            Assert.AreEqual(typeof(TestPlugin), p.GetType());

            TestPlugin t = p as TestPlugin;

            Assert.AreEqual(1, t.Test);
        }
예제 #10
0
        /// <summary>
        /// Adds an algorithm to the builder, returning an indicating whether
        /// a plugin has been resolved.
        /// </summary>
        /// <param name="algorithm">The <see cref="AlgorithmDefinition"/>
        /// describing the algorithm.</param>
        /// <returns><c>true</c> if the definition has been successfully converted
        /// into an object; <c>false</c> otherwise.</returns>
        public bool AddAlgorithm(AlgorithmDefinition algorithm)
        {
            AlgorithmPlugin plugin = _factory.Manufacture(algorithm);

            if (plugin == null)
            {
                return(false);
            }
            else
            {
                _plugins.Add(plugin);
                return(true);
            }
        }
예제 #11
0
        /// <summary>
        /// Actives the algorithm provided in the definition.
        /// </summary>
        /// <param name="definition">The <see cref="AlgorithmDefinition"/>
        /// to restore back into an object.</param>
        /// <returns>The <see cref="AlgorithmPlugin"/> represented by the
        /// <see cref="AlgorithmDefinition"/>.</returns>
        /// <exception cref="ActivationException">the provided definition cannot
        /// be converted back into an object.</exception>
        public AlgorithmPlugin Activate(AlgorithmDefinition definition)
        {
            if (CanActivate(definition) == false)
            {
                throw new ActivationException("Cannot activate provided definition.");
            }

            Type            type   = _registrar.FetchType(definition.AlgorithmName);
            AlgorithmPlugin plugin = Activator.CreateInstance(type) as AlgorithmPlugin;

            _reflectiveSetProperties(definition, type, plugin);
            _interpreterExecution(definition, plugin);

            return(plugin);
        }
예제 #12
0
        public void TestActivate_OneProperty_KnownType()
        {
            double              value     = 0d;
            AlgorithmActivator  activator = new AlgorithmActivator(new TestRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("Test",
                                                                    new Property[] { new Property("Test", typeof(double))
                                                                                     {
                                                                                         Value = value
                                                                                     } });
            AlgorithmPlugin p = activator.Activate(d);

            Assert.IsNotNull(p);
            Assert.AreEqual(typeof(TestPlugin), p.GetType());

            TestPlugin t = p as TestPlugin;

            Assert.AreEqual(value, t.Test);
        }
예제 #13
0
        /// <summary>
        /// Attempts to create the plugin with the specified definition
        /// </summary>
        /// <param name="def">The definition provided by the client</param>
        /// <returns>The PipelineEntry represented by the definition,
        /// or null if an error occurs</returns>
        private PipelineEntry _tryCreateEntry(AlgorithmDefinition def)
        {
            PipelineEntry entry = null;

            try
            {
                AlgorithmPlugin p = _factory.Manufacture(def);
                if (p != null)
                {
                    entry = new PipelineEntry(p);
                    entry.ProcessInput = (ICloneable)def.ParameterObject.Clone();
                }
            }
            catch
            {
                // Todo logging
            }

            return(entry);
        }
예제 #14
0
        /// <summary>
        /// Sets the values of all annotated properties using reflection.
        /// </summary>
        /// <param name="definition">The algorithm definition containing the properties.</param>
        /// <param name="type">The underlying Type object of the plugin</param>
        /// <param name="plugin">The instance of the plugin to set the values against</param>
        private void _reflectiveSetProperties(AlgorithmDefinition definition, Type type, AlgorithmPlugin plugin)
        {
            // Use Linq to quickly grab all attributed properties.
            var attributedProperties =
                from p in type.GetProperties()
                let attr = p.GetCustomAttributes(false).OfType <AlgorithmPropertyAttribute>().FirstOrDefault()
                           where attr != null
                           select new
            {
                Property    = p,
                DefinedName = attr.VariableIdentifier
            };

            foreach (var attributedProperty in attributedProperties)
            {
                PropertyInfo property = attributedProperty.Property;
                Property     p        = definition.Properties
                                        .FirstOrDefault(x => x.Name == attributedProperty.DefinedName);
                if (p != null && property.PropertyType == p.Type)
                {
                    property.SetValue(plugin, p.Value);
                }
            }
        }