/// <summary>
        /// Default constructor
        /// </summary>
        public LinkableComponentV1FluidEarth()
            : base(new Identity(new Describes("FluidEarthV1", "Converts a OpenMI V1 component implemented using FluidEarth V1 to OpenMI V2")),
                new ExternalType(typeof(LinkableComponentV1FluidEarth)),
                new ExternalType(typeof(IProxyEngine5)),
                true)
        {
            var v1ServerType = new ArgumentExternalType(GetArgumentIdentity(ArgsFE.ExternalTypeV1EngineServer), new ExternalType());

            Arguments.Add(v1ServerType);

            // 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);
        }
Exemplo n.º 2
0
            public static bool Parse( 
                XElement xLinkableComponent,
                IDocumentAccessor accessor,
                List<IReport> reports,
                out ExternalType component,
                out SupportedPlatforms platforms,
                out List<Argument> argumentsOmi)
            {
                Contract.Requires(reports != null, "reports != null");

                component = null;
                platforms = SupportedPlatforms.Unknown;
                argumentsOmi = new List<Argument>();

                var ns = xLinkableComponent.Name.Namespace;

                if (!Validates(xLinkableComponent, Xsd.ComponentV2, reports))
                    return false;

                var xArgs = xLinkableComponent.Elements(NamespaceOpenMIv2.GetName(XArguments)).SingleOrDefault();
                var xArguments = xArgs != null
                    ? xArgs.Elements(NamespaceOpenMIv2.GetName(XArgument))
                    : null;

                var xPlatforms = xLinkableComponent.Elements(NamespaceOpenMIv2.GetName(XPlatforms)).SingleOrDefault();

                var assembly = Utilities.Xml.GetAttribute(xLinkableComponent, XAssembly);
                var type = Utilities.Xml.GetAttribute(xLinkableComponent, XType);

                AssemblyName assemblyName;
                Uri codeBaseUri = null;

                if (!ExternalType.AssemblyNameGet(assembly, codeBaseUri, type, reports, accessor, out assemblyName))
                    return false;

                component = new ExternalType(accessor);
                component.Initialise(assemblyName, type);

                if (xArguments != null)
                    argumentsOmi.AddRange(xArguments
                        .Select(x => new Argument(x, accessor)));

                platforms = Platforms.Parse(xPlatforms, accessor);

                return true;
            }
        protected virtual ArgumentUri InitialiseArguments(ILinkableComponentVersion1 component1, List<Utilities.Standard1.Argument1> args1)
        {
            if (component1 == null)
            {
                var uri = new Uri(@"http://sourceforge.net/projects/fluidearth/");
                var persistence = new ArgumentUri(GetArgumentIdentity(ArgsV1Wrapper.Persistence), uri);

                // For Uri's have to use a valid Url to initialise, then change to relevant value
                Arguments.Add(persistence);
                Arguments.Add(new ArgumentExternalType(GetArgumentIdentity(ArgsV1Wrapper.EngineExternalType),
                    new ExternalType(DocumentAccessor)));

                if (_includeUpdateTimeInterval)
                    Arguments.Add(new ArgumentTimeInterval(GetArgumentIdentity(ArgsV1Wrapper.UpdateTimeInterval),
                        new TimeInterval()));

                if (args1 != null)
                    foreach (var a1 in args1)
                        Arguments.Add(new ArgumentStandard1(a1));

                return persistence;
            }

            ArgumentTimeHorizon = new Time(
                component1.TimeHorizon.Start.ModifiedJulianDay,
                component1.TimeHorizon.End.ModifiedJulianDay);

            ExternalType component1Type = new ExternalType(DocumentAccessor);
            component1Type.Initialise(component1.GetType());

            var uriP = new Uri(
                DocumentAccessor.Uri
                    .LocalPath
                    .Substring(0, DocumentAccessor.Uri.LocalPath.LastIndexOf('.')) + Caption + "Persist.xml");

            var uriPersistence = new ArgumentUri(GetArgumentIdentity(ArgsV1Wrapper.Persistence), uriP);

            uriPersistence.ValueAsString =
                new Uri(
                    DocumentAccessor.Uri
                        .LocalPath
                        .Substring(0, DocumentAccessor.Uri.LocalPath.LastIndexOf('.')) + Caption + "Persist.xml")
                .AbsoluteUri;

            Arguments.Add(uriPersistence);
            Arguments.Add(new ArgumentExternalType(GetArgumentIdentity(ArgsV1Wrapper.EngineExternalType),
                component1Type));

            if (_includeUpdateTimeInterval)
                Arguments.Add(new ArgumentTimeInterval(GetArgumentIdentity(ArgsV1Wrapper.UpdateTimeInterval),
                    new TimeInterval()));

            foreach (var a1 in args1)
                Arguments.Add(new ArgumentStandard1(a1));

            return uriPersistence;
        }
        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;
            }
        }
        /// <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 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;
        }