private Assembly Compile(bool fireChanged)
        {
            var assembly = base.Compile();
            var types    = assembly.GetTypes();

            if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
            {
                throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine +
                                                           $"The problem definition must be a subclass of {nameof(CompiledProblemDefinition)} and implement {nameof(ILinearProblemDefinition)}.");
            }
            if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
            {
                throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine +
                                                           $"Only one subclass of {nameof(CompiledProblemDefinition)} is allowed.");
            }

            CompiledProblemDefinition inst;

            try {
                inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x =>
                                                                                        typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
            } catch (Exception e) {
                compiledProblemDefinition = null;
                throw new ProblemDefinitionScriptException(
                          "Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
            }

            try {
                inst.vars = new Variables(VariableStore);
                inst.Initialize();
            } catch (Exception e) {
                compiledProblemDefinition = null;
                throw new ProblemDefinitionScriptException(
                          "Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
            }

            try {
                compiledProblemDefinition = (ILinearProblemDefinition)inst;
                if (fireChanged)
                {
                    OnProblemDefinitionChanged();
                }
            } catch (Exception e) {
                compiledProblemDefinition = null;
                throw new ProblemDefinitionScriptException(
                          "Using the problem definition in the problem failed." + Environment.NewLine +
                          "Examine this error message carefully.", e);
            }

            codeChanged = false;
            return(assembly);
        }
 protected override void OnCodeChanged()
 {
     base.OnCodeChanged();
     compiledProblemDefinition = null;
     codeChanged = true;
 }
Exemple #3
0
 public override void Solve(ILinearProblemDefinition problemDefintion, ResultCollection results, CancellationToken cancellationToken)
 {
     timeLimit = TimeSpan.Zero;
     base.Solve(problemDefintion, results, cancellationToken);
 }