Example #1
0
        /// <summary>
        /// Load an optional object from the repository directory.
        /// Returns false iff no stream was found for the object, iff result is set to null.
        /// Throws if loading fails for any other reason.
        /// </summary>
        public static bool LoadModelOrNull <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, string dir, params object[] extra)
            where TRes : class
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(rep, nameof(rep));
            var ent = rep.OpenEntryOrNull(dir, ModelStreamName);

            if (ent != null)
            {
                using (ent)
                {
                    // Provide the repository, entry, and directory name to the loadable class ctor.
                    env.Assert(ent.Stream.Position == 0);
                    LoadModel <TRes, TSig>(env, out result, rep, ent, dir, extra);
                    return(true);
                }
            }

            if ((ent = rep.OpenEntryOrNull(dir, NameBinary)) != null)
            {
                using (ent)
                {
                    env.Assert(ent.Stream.Position == 0);
                    LoadModel <TRes, TSig>(env, out result, ent.Stream, extra);
                    return(true);
                }
            }

            result = null;
            return(false);
        }
Example #2
0
        /// <summary>
        /// Try to load from the given repository entry using the default loader(s) specified in the header.
        /// Returns false iff the default loader(s) could not be bound to a compatible loadable class.
        /// </summary>
        private static bool TryLoadModel <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, Repository.Entry ent, string dir, params object[] extra)
            where TRes : class
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(rep, nameof(rep));
            long fp = ent.Stream.Position;

            using (var ctx = new ModelLoadContext(rep, ent, dir))
            {
                env.Assert(fp == ctx.FpMin);
                if (ctx.TryLoadModelCore <TRes, TSig>(env, out result, extra))
                {
                    return(true);
                }
            }

            // TryLoadModelCore should rewind on failure.
            Contracts.Assert(fp == ent.Stream.Position);
            return(false);
        }
Example #3
0
 /// <summary>
 /// Load from the given repository entry using the default loader(s) specified in the header.
 /// </summary>
 public static void LoadModel <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, Repository.Entry ent, string dir, params object[] extra)
     where TRes : class
 {
     Contracts.CheckValue(env, nameof(env));
     env.CheckValue(rep, nameof(rep));
     if (!TryLoadModel <TRes, TSig>(env, out result, rep, ent, dir, extra))
     {
         throw env.ExceptDecode("Couldn't load model: '{0}'", dir);
     }
 }
Example #4
0
 /// <summary>
 /// Load an object from the repository directory.
 /// </summary>
 public static void LoadModel <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, string dir, params object[] extra)
     where TRes : class
 {
     Contracts.CheckValue(env, nameof(env));
     env.CheckValue(rep, nameof(rep));
     if (!LoadModelOrNull <TRes, TSig>(env, out result, rep, dir, extra))
     {
         throw env.ExceptDecode("Corrupt model file");
     }
     env.AssertValue(result);
 }