public void SetDefaultArguments()
        {
            // UnitTests use shadow copying, so have to go this route
            var uriExecuting = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            var fileExecuting = new FileInfo(uriExecuting.LocalPath);

            var enginePath = Path.Combine(fileExecuting.DirectoryName, "FluidEarth2_Sdk_V1Wrappers.dll");

            var engineType = new ExternalType();
            engineType.Initialise(enginePath, "FluidEarth2.Sdk.V1Wrappers.Engine5");

            ArgumentParametersNativeDll = new ParametersNativeDll(engineType,
                ParametersNativeDll.Interface.FluidEarth2_Sdk_Interfaces_IEngineTime,
                null, false);

            var serverExe = Arguments
                .Where(a => a.Caption == "FluidEarth_SDK.ArgFile.ServerExe"
                    | a.Caption == "OpenWEB_SDK.ArgFile.ServerExe")
                .SingleOrDefault();

            if (serverExe != null)
            {
                var serverType = Arguments
                    .Where(a => a.Caption == "FluidEarth_SDK.Arg.ServerType"
                        || a.Caption == "OpenWEB_SDK.Arg.ServerType")
                    .SingleOrDefault();

                string typeName = string.Empty;

                // Guess that implemented followed recommended naming conventions and structure so Engine exists

                if (serverType != null)
                    typeName = serverType
                        .ValueAsString
                        .Substring(0, serverType.ValueAsString.LastIndexOf(".")) + ".Engine";

                var nativeEngineWrapperType = new ExternalType();
                nativeEngineWrapperType.Initialise(
                    serverExe.ValueAsString,
                    typeName);

                try
                {
                    Type type;
                    if (nativeEngineWrapperType.CreateInstance(out type) == null)
                        nativeEngineWrapperType.TypeName = string.Empty;
                }
                catch (Exception)
                {
                    nativeEngineWrapperType.TypeName = string.Empty;
                }

                if (nativeEngineWrapperType.TypeName == string.Empty)
                {
                    // try again with unmodified name
                    typeName = serverType.ValueAsString;
                    nativeEngineWrapperType.Initialise(serverExe.ValueAsString, typeName);

                    try
                    {
                        Type type;
                        if (nativeEngineWrapperType.CreateInstance(out type) == null)
                            nativeEngineWrapperType.TypeName = string.Empty;
                    }
                    catch (Exception)
                    {
                        nativeEngineWrapperType.TypeName = string.Empty;
                    }
                }

                ArgumentEngineExternalType = nativeEngineWrapperType;
            }
        }
            public static bool Instantiate(
                ExternalType componentType,
                SupportedPlatforms platforms,
                List<IReport> reports,
                out IBaseLinkableComponent component)
            {
                Contract.Requires(componentType != null, "componentType != null");
                Contract.Requires(reports != null, "reports != null");

                component = null;

                Type type;

                try
                {
                    component = componentType
                        .CreateInstance(out type) as IBaseLinkableComponent;
                }
                catch (System.Exception e)
                {
                    reports.Add(Report.Error(Report.ResourceIds.Instantiation,
                        "Cannot instantiate IBaseLinkableComponent", e.Message));
                    return false;
                }

                if (component == null)
                {
                    reports.Add(Report.Error(Report.ResourceIds.Instantiation,
                        "Cannot instantiate IBaseLinkableComponent", type.ToString()));
                    return false;
                }

                return true;
            }
        /// <summary>
        /// Intercept base initialise call to update references to IEngine server and native DLL.
        /// </summary>
        /// <param name="reinitialising">True if component has been initialised before</param>
        protected override void DoInitialise(bool reinitialising)
        {
            base.DoInitialise(reinitialising);

            var serverExe = Arguments
                .Where(a => a.Caption == "FluidEarth_SDK.ArgFile.ServerExe"
                    | a.Caption == "OpenWEB_SDK.ArgFile.ServerExe")
                .SingleOrDefault();

            if (serverExe != null)
            {
                var serverType = Arguments
                    .Where(a => a.Caption == "FluidEarth_SDK.Arg.ServerType"
                        || a.Caption == "OpenWEB_SDK.Arg.ServerType")
                    .SingleOrDefault();

                string typeName = string.Empty;

                // Guess that implemented followed recommended naming conventions and structure so Engine exists

                if (serverType != null)
                    typeName = serverType
                        .ValueAsString
                        .Substring(0, serverType.ValueAsString.LastIndexOf(".")) + ".Engine";

                var nativeEngineWrapperType = new ExternalType();
                nativeEngineWrapperType.Initialise(
                    serverExe.ValueAsString,
                    typeName);

                try
                {
                    Type type;
                    if (nativeEngineWrapperType.CreateInstance(out type) == null)
                        nativeEngineWrapperType.TypeName = string.Empty;
                }
                catch (Exception)
                {
                    nativeEngineWrapperType.TypeName = string.Empty;
                }

                if (nativeEngineWrapperType.TypeName == string.Empty)
                {
                    // try again with unmodified name
                    typeName = serverType.ValueAsString;
                    nativeEngineWrapperType.Initialise(serverExe.ValueAsString, typeName);

                    try
                    {
                        Type type;
                        if (nativeEngineWrapperType.CreateInstance(out type) == null)
                            nativeEngineWrapperType.TypeName = string.Empty;
                    }
                    catch (Exception)
                    {
                        nativeEngineWrapperType.TypeName = string.Empty;
                    }
                }

                ArgumentExternalTypeV1EngineServer = nativeEngineWrapperType;
            }
        }
            public static LinkableComponentOpenMIV1Wrapper ImportComponent(Component1Type v1Type, XElement xLinkableComponentV1, IDocumentAccessor accessor)
            {
                var assembly = Utilities.Xml.GetAttribute(xLinkableComponentV1, "Assembly");
                var type = Utilities.Xml.GetAttribute(xLinkableComponentV1, "Type");
                var assemblyUri = new Uri(accessor.Uri, assembly);

                string path;

                if (!FluidEarth2.Sdk.Utilities.UriIsFilePath(assemblyUri, out path) || !File.Exists(path))
                    throw new Exception("OMI file Assembly attribute cannot be resolved to an existing file, correct OMI file and retry. Used: "
                        + assembly);

                var componentTypeV1 = new ExternalType(accessor);
                componentTypeV1.Initialise(assemblyUri.LocalPath, type);

                Type tComponentV1;
                var component1 = componentTypeV1.CreateInstance(out tComponentV1) as ILinkableComponent1;

                if (component1 == null)
                    throw new Exception("Cannot instantiate " + componentTypeV1.ToString());

                var ns1 = Omi.Component.NamespaceOpenMIv1;

                var xArguments = xLinkableComponentV1
                    .Element(ns1.GetName("Arguments"));

                var args1 = Arguments1(xArguments, ns1, accessor.Uri);

                component1.Initialize(args1.ToArray());

                switch (v1Type)
                {
                    case Component1Type.OpenMIGeneric:
                        return new LinkableComponentOpenMIV1Wrapper(component1, componentTypeV1.DocumentAccessor, args1.ToList());
                    case Component1Type.FluidEarthEngine5:
                        return new LinkableComponentFluidEarthV1Wrapper(component1, componentTypeV1.DocumentAccessor, args1.ToList());
                    default:
                        throw new NotImplementedException(v1Type.ToString());
                }
            }
            public static LinkableComponentOpenMIV1Wrapper CreateWrappedComponent(ExternalType component, IEnumerable<Argument1> arguments)
            {
                Type tComponentV1;
                ILinkableComponent1 component1
                    = component.CreateInstance(out tComponentV1)
                    as ILinkableComponent1;

                if (component1 == null)
                    throw new Exception("Cannot instantiate " + component.ToString());

                component1.Initialize(arguments.ToArray());

                bool isFluidEarthV1 = tComponentV1.BaseType != null &&
                    tComponentV1.BaseType.AssemblyQualifiedName.Contains("FluidEarth.Sdk.LinkableComponent");

                return isFluidEarthV1
                    ? new LinkableComponentFluidEarthV1Wrapper(component1, component.DocumentAccessor, arguments.ToList())
                    : new LinkableComponentOpenMIV1Wrapper(component1, component.DocumentAccessor, arguments.ToList());
            }
        public void Initialise(string initialisingXml, IDocumentAccessor accessor)
        {
            try
            {
                Trace.WriteIf(_traceEngine, string.Format("{0}: {1}\r\n",
                    DateTime.Now.ToString("u"), "Initialise:\r\n" + initialisingXml));

                if (_engine != null)
                    throw new Exception("Engine already initialised on server");

                _initialisingXml = XElement.Parse(initialisingXml);

                var arguments = Persistence.Arguments
                    .Parse(_initialisingXml, accessor);

                var diagnosticsIdentity = BaseComponentWithEngine.GetArgumentIdentity(
                    BaseComponentWithEngine.ArgsWithEngine.Diagnostics);

                var argDiagnostics = arguments
                    .Where(a => a.Id == diagnosticsIdentity.Id)
                    .Single()
                    as ArgumentParametersDiagnosticsEngine;

                var diagnostics = argDiagnostics.Parameters;

                if (diagnostics.LaunchDebugger)
                    Debugger.Launch();

                var assemblyLoaders = arguments
                    .Where(a => a.Value is IAssemblyLoader)
                    .Select(a => a.Value as IAssemblyLoader);

                foreach (var l in assemblyLoaders)
                    _assemblyLoader.Add(l);

                var xmlType = _initialisingXml
                    .Elements("ExternalType")
                    .Single();

                var externalType = new ExternalType();
                externalType.Initialise(xmlType, accessor);

                object engine = externalType.CreateInstance(out _engineType);

                _engine = engine as IEngine;

                Contract.Requires(_engine != null,
                    "\"{0}\" is not a \"{1}\"", engine.GetType(), typeof(IEngine));

                _engine = Utilities.Diagnostics.AddDiagnostics(
                    diagnostics, _engine,
                    out _diagnosticsStream, true);

                _engine.Initialise(initialisingXml);
            }
            catch (System.Exception e)
            {
                EngineMethodCatch(e);
            }
        }
        /// <summary>
        /// Create an instance of OpenMI.Standard.ILinkableComponent
        /// </summary>
        /// <param name="uriOmiV1">Uri of OMI XML</param>
        /// <param name="component1">Instance of OpenMI.Standard.ILinkableComponent</param>
        /// <param name="reports">Details about method success</param>
        /// <returns>True if successful</returns>
        public static bool InstantiateComponent1(Uri uriOmiV1, out OpenMI.Standard.ILinkableComponent component1, List<IReport> reports)
        {
            component1 = null;
            XElement xLinkableComponent;
            List<Utilities.Standard1.Argument1> args1;

            if (!UpdateArgumentsFromOmiV1(uriOmiV1, out xLinkableComponent, out args1, reports))
                return false;

            var assembly = Utilities.Xml.GetAttribute(xLinkableComponent, "Assembly");
            var type = Utilities.Xml.GetAttribute(xLinkableComponent, "Type");

            var omiFile = new DocumentExistingFile(uriOmiV1);
            var assemblyUri = new Uri(uriOmiV1, assembly);

            var typeV1 = new ExternalType(omiFile);
            typeV1.Initialise(assemblyUri.LocalPath, type);

            Type tComponentV1;
            component1 = typeV1.CreateInstance(out tComponentV1) as OpenMI.Standard.ILinkableComponent;

            if (component1 == null)
            {
                reports.Add(Report.Error(Report.ResourceIds.Instantiation, tComponentV1.ToString(), string.Empty));
                return false;
            }

            return true;
        }