/// <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)); }
/// <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); }
public void TestCreateDefinition_AnnotatedNoProperties() { AlgorithmDefinition d = PluginReflector.CreateDefinition(new AnnotatedPlugin()); Assert.AreEqual("Plugin", d.AlgorithmName); Assert.AreEqual(0, d.Properties.Count); }
public void TestCreateDefinition_KnownSuperclass_NoParameters() { AlgorithmDefinition d = PluginReflector.CreateDefinition(typeof(AnnotatedPlugin)); Assert.AreEqual("Plugin", d.AlgorithmName); Assert.AreEqual(0, d.Properties.Count); }
/// <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); }
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); }
public void TestConstructor_NullProperties() { AlgorithmDefinition d = new AlgorithmDefinition("Test", null); Assert.IsNotNull(d.Properties); Assert.AreEqual(0, d.Properties.Count); }
public void TestConstructor_EmptyProperties() { AlgorithmDefinition d = new AlgorithmDefinition("Test", new List <Property>()); Assert.IsNotNull(d.Properties); Assert.AreEqual(0, d.Properties.Count); }
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); }
public void TestCreateDefinition_PluginWithParameterObject() { AlgorithmDefinition d = PluginReflector.CreateDefinition(typeof(AnnotatedPluginWithParameterObjectType)); Assert.IsNotNull(d.ParameterObject); Assert.AreEqual(typeof(PropertiesObj), d.ParameterObject.GetType()); }
public void TestCreateDefinition_PluginWithMetadata() { AlgorithmDefinition d = PluginReflector.CreateDefinition(typeof(AnnotatedPluginWithMetadata)); Assert.AreEqual("Display Name", d.DisplayName); Assert.AreEqual("Description", d.Description); }
public void TestCanActivate_NoParameterlessConstructor() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("BadConstructor", null); bool canActivate = activator.CanActivate(d); Assert.IsFalse(canActivate); }
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); }
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); }
/// <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); } }
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); }
/// <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; }
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); } }
/// <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); } }
/// <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); } }
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); }
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()); }
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); }
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)); }
/// <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); } }
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); }
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); }