示例#1
0
        /// <summary>
        /// Constructs Xml using the <see cref="AlgorithmDefinition"/>.
        /// </summary>
        /// <param name="definition">The <see cref="AlgorithmDefinition"/>
        /// detailing the process and its properties.</param>
        /// <returns>An <see cref="XElement"/> representing the
        /// <see cref="AlgorithmDefinition"/>.</returns>
        public XElement Build(AlgorithmDefinition definition)
        {
            if (definition == null)
            {
                return(new XElement("algorithm"));
            }

            const string algElementName = "algorithm";
            XAttribute   nameAttr       = new XAttribute("name", definition.AlgorithmName);

            if (definition.Properties.Any() == false)
            {
                return(new XElement(algElementName, nameAttr));
            }

            ICollection <XElement> properties = new List <XElement>();

            foreach (Property property in definition.Properties)
            {
                XAttribute name        = new XAttribute("name", property.Name);
                XAttribute type        = new XAttribute("type", property.Type);
                XElement   value       = new XElement("value", property.Value);
                XElement   propertyXml = new XElement("property", name, type, value);
                properties.Add(propertyXml);
            }

            XElement propertiesXml = new XElement("properties", properties);

            return(new XElement(algElementName, nameAttr, propertiesXml));
        }
示例#2
0
        /// <summary>
        /// Creates the AlgorithmDefinition from the incoming type
        /// </summary>
        /// <param name="processType">The type to construct the definition for</param>
        /// <returns>An AlgorithmDefinition representing the incoming type</returns>
        private static AlgorithmDefinition _createDefinition(Type processType)
        {
            if (processType.IsSubclassOf(typeof(AlgorithmPlugin)) == false)
            {
                throw new ArgumentException(
                          string.Format("Type {0} is not a subclass of {1}", processType, typeof(AlgorithmPlugin)));
            }

            AlgorithmAttribute identifier = _getIdentifierAttribute(processType);

            if (identifier == null)
            {
                throw new ArgumentException("Supplied AlgorithmPlugin is not annotated correctly.");
            }

            IEnumerable <Property> properties = _gatherAlgorithmProperties(processType);
            AlgorithmDefinition    d          = new AlgorithmDefinition(identifier.PluginName, properties);

            if (identifier.ParameterObjectType != null)
            {
                d.ParameterObject = _activatePluginPropertiesObject(identifier.ParameterObjectType);
            }

            // Add the metadata if it has been provided.
            AlgorithmMetadataAttribute attr = _getMetadataAttribute(processType);

            if (attr != null)
            {
                d.DisplayName = attr.DisplayName;
                d.Description = attr.Description;
            }

            return(d);
        }
示例#3
0
        public void TestCreateDefinition_AnnotatedNoProperties()
        {
            AlgorithmDefinition d = PluginReflector.CreateDefinition(new AnnotatedPlugin());

            Assert.AreEqual("Plugin", d.AlgorithmName);
            Assert.AreEqual(0, d.Properties.Count);
        }
示例#4
0
        public void TestCreateDefinition_KnownSuperclass_NoParameters()
        {
            AlgorithmDefinition d = PluginReflector.CreateDefinition(typeof(AnnotatedPlugin));

            Assert.AreEqual("Plugin", d.AlgorithmName);
            Assert.AreEqual(0, d.Properties.Count);
        }
示例#5
0
        /// <summary>
        /// Decompiles an Xml node back into an <see cref="AlgorithmDefinition"/>.
        /// </summary>
        /// <param name="algorithmNode">The <see cref="XNode"/> representing the
        /// <see cref="AlgorithmDefinition"/>.</param>
        /// <returns>An <see cref="AlgorithmDefinition"/> object represented by
        /// the provided Xml.</returns>
        public AlgorithmDefinition DecompileAlgorithm(XNode algorithmNode)
        {
            if (algorithmNode.NodeType != System.Xml.XmlNodeType.Element)
            {
                throw new ArgumentException("An XElement is required");
            }

            object              paramObj = null;
            XElement            element  = (XElement)algorithmNode;
            XAttribute          nameAttr = element.Attribute("name");
            string              name     = nameAttr.Value;
            AlgorithmDefinition d        = new AlgorithmDefinition(name, new Property[] { });
            XNode propertyNode           = element.FirstNode;

            if (propertyNode != null && propertyNode.NodeType == System.Xml.XmlNodeType.Element)
            {
                if (_factories.ContainsKey(name) == false)
                {
                    throw new ArgumentException("Unknown process provided");
                }

                XElement propElement            = (XElement)propertyNode;
                IPipelineXmlInterpreter factory = _factories[name];
                d.ParameterObject = factory.CreateObject(propElement);
            }

            return(d);
        }
示例#6
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);
 }
示例#7
0
        public void TestConstructor_NullProperties()
        {
            AlgorithmDefinition d = new AlgorithmDefinition("Test", null);

            Assert.IsNotNull(d.Properties);
            Assert.AreEqual(0, d.Properties.Count);
        }
示例#8
0
        public void TestConstructor_EmptyProperties()
        {
            AlgorithmDefinition d = new AlgorithmDefinition("Test", new List <Property>());

            Assert.IsNotNull(d.Properties);
            Assert.AreEqual(0, d.Properties.Count);
        }
示例#9
0
        public void RestorePipe_ValidXml()
        {
            GammaProperties p = new GammaProperties()
            {
                Gamma = 3
            };
            AlgorithmDefinition def = new AlgorithmDefinition("gamma", new Property[] { });

            def.ParameterObject = p;
            ProcessPluginRepository algRepo  = new ProcessPluginRepository();
            PipelineXmlRepository   pipeRepo = new PipelineXmlRepository();

            RegistryCache.Cache.Initialize(pipeRepo);
            RegistryCache.Cache.Initialize(algRepo);
            PipelineManager m     = new PipelineManager(pipeRepo, algRepo);
            var             xml   = m.SavePipeline(new PipelineDefinition(new AlgorithmDefinition[] { def }));
            var             pipes = m.RestorePipeline(xml);

            Assert.AreEqual(1, pipes.Count());

            AlgorithmDefinition d = pipes.First();

            Assert.AreEqual(def.AlgorithmName, d.AlgorithmName);
            Assert.IsNotNull(def.ParameterObject);
            Assert.AreEqual(typeof(GammaProperties), d.ParameterObject.GetType());
            Assert.AreEqual(3, ((GammaProperties)d.ParameterObject).Gamma);
        }
示例#10
0
        public void TestCreateDefinition_PluginWithParameterObject()
        {
            AlgorithmDefinition d = PluginReflector.CreateDefinition(typeof(AnnotatedPluginWithParameterObjectType));

            Assert.IsNotNull(d.ParameterObject);
            Assert.AreEqual(typeof(PropertiesObj), d.ParameterObject.GetType());
        }
示例#11
0
        public void TestCreateDefinition_PluginWithMetadata()
        {
            AlgorithmDefinition d = PluginReflector.CreateDefinition(typeof(AnnotatedPluginWithMetadata));

            Assert.AreEqual("Display Name", d.DisplayName);
            Assert.AreEqual("Description", d.Description);
        }
示例#12
0
        public void TestCanActivate_NoParameterlessConstructor()
        {
            AlgorithmActivator  activator = new AlgorithmActivator(new TestRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("BadConstructor", null);
            bool canActivate = activator.CanActivate(d);

            Assert.IsFalse(canActivate);
        }
示例#13
0
        public void TestCanActivate_NotCached()
        {
            AlgorithmActivator  activator = new AlgorithmActivator(new TestRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("unknown", new Property[] { });
            bool canActivate = activator.CanActivate(d);

            Assert.IsFalse(canActivate);
        }
 internal async Task AddAlgorithm(AlgorithmDefinition algorithm)
 {
     if (!SelectedAlgorithms?.Contains(algorithm) ?? false)
     {
         SelectedAlgorithms.Add(algorithm);
     }
     await SelectedAlgorithmsChanged.InvokeAsync(SelectedAlgorithms).ConfigureAwait(false);
 }
示例#15
0
        public void TestCanActivate_ValidDefinition()
        {
            AlgorithmActivator  activator = new AlgorithmActivator(new TestRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("Test",
                                                                    new Property[] { new Property("Test", typeof(double)) });
            bool canActivate = activator.CanActivate(d);

            Assert.IsTrue(canActivate);
        }
示例#16
0
        /// <summary>
        /// Initializes this <see cref="IPluginRegistry"/> from the loaded
        /// <see cref="Assembly"/>.
        /// </summary>
        /// <param name="assembly">The <see cref="Assembly"/> specified within
        /// the Windows registry containing plugins.</param>
        public void Initialize(Assembly assembly)
        {
            var validTypes = assembly.GetTypes().Where(_isValidType);

            foreach (var type in validTypes)
            {
                AlgorithmDefinition definition = PluginReflector.CreateDefinition(type);
                _pluginCache.Add(definition, type);
            }
        }
示例#17
0
        public void TestDecompileAlgorithm_NoProperties()
        {
            JobXmlDecompiler decompiler = new JobXmlDecompiler();
            XElement         xml        = new XElement("algorithm",
                                                       new XAttribute("name", "gamma"));
            AlgorithmDefinition a = decompiler.DecompileAlgorithm(xml);

            Assert.AreEqual("gamma", a.AlgorithmName);
            Assert.AreEqual(0, a.Properties.Count);
        }
        public void TestBuild_UnknownAlgorithm()
        {
            IDictionary <string, IPipelineXmlInterpreter> factories = new Dictionary <string, IPipelineXmlInterpreter>();

            factories.Add("Unknown", new DudInterpreter());
            PipelinePersistanceProcess p = new PipelinePersistanceProcess(factories);
            AlgorithmDefinition        d = new AlgorithmDefinition("Test", new Property[] { });

            d.ParameterObject = new Cloneable();
            p.Build(d);
        }
示例#19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AlgorithmViewModel"/>
        /// class.
        /// </summary>
        /// <param name="definition">The <see cref="AlgorithmDefinition"/> this
        /// view-model provides presentation logic for.</param>
        /// <exception cref="ArgumentNullException">definition is null.</exception>
        public AlgorithmViewModel(AlgorithmDefinition definition)
        {
            if (definition == null)
            {
                throw new ArgumentNullException("definition");
            }

            Definition  = definition;
            _removeCmd  = new RelayCommand(_remove, _canRemove);
            IsRemovable = true;
        }
示例#20
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);
        }
 internal void ToggleAlgorithm(bool add, AlgorithmDefinition algorithmDefinition)
 {
     if (add)
     {
         _ = AddAlgorithm(algorithmDefinition);
     }
     else
     {
         _ = RemoveAlgorithm(algorithmDefinition);
     }
 }
示例#22
0
 /// <summary>
 /// Manufactures an appropriate <see cref="AlgorithmPlugin"/> given the
 /// provided <see cref="AlgorithmDefinition"/>.
 /// </summary>
 /// <param name="algorithm">The <see cref="AlgorithmDefinition"/>
 /// descriving the algorithm to create.</param>
 /// <returns>An instance of <see cref="AlgorithmPlugin"/> represented
 /// by the given definition, or null if an algorithm cannot be built
 /// with the provided definition.</returns>
 public AlgorithmPlugin Manufacture(AlgorithmDefinition algorithm)
 {
     try
     {
         return(_activator.Activate(algorithm));
     }
     catch
     {
         return(null);
     }
 }
示例#23
0
 /// <summary>
 /// Performs the visiting logic against an <see cref="XNode"/> representing
 /// an <see cref="AlgorithmDefinition"/>.
 /// </summary>
 /// <param name="xml">The <see cref="XNode"/> containing the algorithm
 /// information.</param>
 public void VisitAlgorithm(XNode xml)
 {
     try
     {
         AlgorithmDefinition d = _decompiler.DecompileAlgorithm(xml);
         Algorithms.Add(d);
     }
     catch (Exception e)
     {
         throw new XmlDecompilationException(e.Message, e);
     }
 }
示例#24
0
        public void TestCreateDefinition_AnnotatedOneProperty()
        {
            AlgorithmDefinition d = PluginReflector.CreateDefinition(new AnnotatedPluginWithProperty());

            Assert.AreEqual("Plugin", d.AlgorithmName);
            Assert.AreEqual(1, d.Properties.Count);

            Property p = d.Properties.First();

            Assert.AreEqual("Value", p.Name);
            Assert.AreEqual(typeof(double), p.Type);
        }
示例#25
0
        public void TestBuild_NoProperties()
        {
            string              algorithmName = "Test";
            JobBuilderProcess   builder       = new JobBuilderProcess();
            AlgorithmDefinition d             = new AlgorithmDefinition(algorithmName, null);
            XElement            xml           = builder.Build(d);

            Assert.AreEqual("algorithm", xml.Name);
            Assert.IsNotNull(xml.Attribute("name"));
            Assert.AreEqual(algorithmName, xml.Attribute("name").Value);
            Assert.IsFalse(xml.Descendants("properties").Any());
        }
示例#26
0
        public void TestCreatePipeline_ExceptionCreatingPlugin()
        {
            IPluginFactory      f   = new BadFactory();
            PipelineDefinition  d   = new PipelineDefinition();
            AlgorithmDefinition def = new AlgorithmDefinition("Test", new Property[] { });

            d.Add(def);
            PluginPipelineFactory p = new PluginPipelineFactory(f);
            Pipeline pipe           = p.CreatePipeline(d);

            Assert.IsNotNull(pipe);
            Assert.AreEqual(0, pipe.Count);
        }
示例#27
0
        public void TestConstructor_ValidArgs()
        {
            Property p = new Property("TestProperty", typeof(int));
            IEnumerable <Property> properties = new List <Property> {
                p
            };
            string definitionName = "Test";
            AlgorithmDefinition d = new AlgorithmDefinition(definitionName, properties);

            Assert.AreEqual(definitionName, d.AlgorithmName);
            Assert.AreEqual(1, d.Properties.Count);
            Assert.IsTrue(d.Properties.Contains(p));
        }
示例#28
0
        /// <summary>
        /// Resolves the qualified name of the plugin from its registered
        /// identifier.
        /// </summary>
        /// <param name="id">The registered identifier of the plugin.</param>
        /// <returns>The fully qualified plugin name.</returns>
        public string NameForIdentifier(string id)
        {
            AlgorithmDefinition match = _pluginCache.Keys.FirstOrDefault(x => x.AlgorithmName == id);

            if (match == null)
            {
                return(string.Empty);
            }
            else
            {
                return(match.DisplayName);
            }
        }
示例#29
0
        public void TestCreateDefinition_KnownSuperclass_OneParameter()
        {
            AlgorithmDefinition d = PluginReflector.CreateDefinition(typeof(AnnotatedPluginWithProperty));

            Assert.AreEqual("Plugin", d.AlgorithmName);
            Assert.AreEqual(1, d.Properties.Count);

            Property p = d.Properties.First();

            Assert.AreEqual("Value", p.Name);
            Assert.AreEqual(typeof(double), p.Type);
            Assert.AreEqual(3d, p.Value);
        }
示例#30
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);
        }