/// <summary> /// Creates a copy of this <see cref="MatlabProperties"/> object. /// </summary> /// <returns>A new instance of the <see cref="MatlabProperties"/> /// class with the same values as the current instance.</returns> public object Clone() { MatlabProperties p = new MatlabProperties(); p.ScriptFile = ScriptFile; p.SerializedFile = SerializedFile; p.Parameters = Parameters; return(p); }
private void _populateEngine(MatlabEngine e, MatlabProperties p) { foreach (MatlabParameter parameter in p.Parameters) { Workspace w = parameter.Workspace == "Base" ? e.Base : e.Global; IParameterValue value = parameter.Value; value.Put(parameter.Name, w); } }
private void _execute(MatlabProperties p) { using (MatlabEngine engine = new MatlabEngine()) { _populateEngine(engine, p); _putInput(engine); var script = _extractScript(p); _runScript(engine, script); _setOutput(engine); } }
private IEnumerable <string> _extractScript(MatlabProperties p) { string scriptName = Path.GetFileName(p.ScriptFile); using (IDisposable tmp = new TemporaryFile(scriptName, p.SerializedFile)) { // Remove any blank lines from the script var script = File.ReadAllLines(p.ScriptFile); return(script.Where(x => string.IsNullOrEmpty(x) == false)); } }
/// <summary> /// Attempts to execute the script. If an exception occurs it is wrapped /// by an AlgorithmException /// </summary> /// <param name="p">The properties to execute the script with.</param> private void _tryExecute(MatlabProperties p) { try { _execute(p); } catch (Exception e) { string err = "Error running MatlabProcess. See inner exception."; throw new AlgorithmException(err, e); } }
/// <summary> /// Executes the algorithm represented by this <see cref="AlgorithmPlugin"/>. /// </summary> /// <param name="parameterObject">An object of the type provided by the /// <see cref="AlgorithmAttribute"/> describing the properties used by this /// <see cref="AlgorithmPlugin"/>.</param> /// <exception cref="AlgorithmException">an internal exception has occured. This /// is accessed through the inner exception property.</exception> public override void Run(object parameterObject) { if (parameterObject is MatlabProperties == false) { throw new AlgorithmException("Cannot execute Matlab process without MatlabProperties"); } MatlabProperties p = parameterObject as MatlabProperties; if (p.HasScript) { _execute(p); } else { throw new AlgorithmException("No script provided"); } }
/// <summary> /// Converts the provided Xml back into the appropriate parameter /// object for the algorithm /// </summary> /// <param name="parameterXml">The <see cref="XElement"/> describing the properties /// within the object.</param> /// <returns>The appropriate object used to describe the parameters /// for the process.</returns> public ICloneable CreateObject(XElement parameterXml) { MatlabProperties p = new MatlabProperties(); if (parameterXml == null) { return(p); } XAttribute pathAttr = parameterXml.Attribute("script-path"); if (pathAttr != null) { p.ScriptFile = pathAttr.Value; } XElement scriptCopyElement = parameterXml.Descendants("script-copy").FirstOrDefault(); if (scriptCopyElement != null && scriptCopyElement.FirstNode != null && scriptCopyElement.FirstNode.NodeType == System.Xml.XmlNodeType.CDATA) { string cdataStr = ((XCData)scriptCopyElement.FirstNode).Value; byte[] cdata = System.Convert.FromBase64String(cdataStr); p.SerializedFile = cdata; } foreach (var paramElement in parameterXml.Descendants("parameter")) { XAttribute typeAttr = paramElement.Attribute("type"); if (typeAttr == null) { continue; } MatlabParameter param = MatlabParameterFactory.Manufacture(typeAttr.Value); param.Restore(paramElement); p.Parameters.Add(param); } return(p); }
/// <summary> /// Converts the parameter object provided by the /// <see cref="AlgorithmDefinition"/> containing the properties /// to persist. /// </summary> /// <param name="parameterObject">The value of the algorithms /// parameter object</param> /// <returns>The <see cref="XElement"/> describing the properties /// within the object.</returns> public XElement CreateXml(ICloneable parameterObject) { if (parameterObject == null) { return(new XElement("properties")); } MatlabProperties p = parameterObject as MatlabProperties; string path = p.ScriptFile; string contentsAsStr = System.Convert.ToBase64String(p.SerializedFile); ICollection <XElement> paramElements = new List <XElement>(); foreach (MatlabParameter parameter in p.Parameters) { XElement paramXml = parameter.CreateXml(); paramElements.Add(paramXml); } return(new XElement("properties", new XAttribute("script-path", path), new XElement("script-copy", new XCData(contentsAsStr)), new XElement("parameters", paramElements))); }