示例#1
0
        /// <summary>
        /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
        /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
        /// </summary>
        /// <remarks>If an error occurs during the execution the operation is aborted and the operation
        /// is pushed on the stack again.<br/>
        /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
        protected virtual void ProcessNextOperation(bool logOperations, CancellationToken cancellationToken)
        {
            IAtomicOperation atomicOperation = CurrentOperation as IAtomicOperation;
              OperationCollection operations = CurrentOperation as OperationCollection;
              if (atomicOperation != null && operations != null)
            throw new InvalidOperationException("Current operation is both atomic and an operation collection");

              if (atomicOperation != null) {
            if (logOperations)
              Log.LogMessage(string.Format("Performing atomic operation {0}", Utils.Name(atomicOperation)));
            PerformAtomicOperation(atomicOperation, cancellationToken);
              } else if (operations != null) {
            if (logOperations)
              Log.LogMessage("Expanding operation collection");
            ExecutionStack.AddRange(operations.Reverse());
            CurrentOperation = null;
              } else if (ExecutionStack.Count > 0) {
            if (logOperations)
              Log.LogMessage("Popping execution stack");
            CurrentOperation = ExecutionStack.Last();
            ExecutionStack.RemoveAt(ExecutionStack.Count - 1);
              } else {
            if (logOperations)
              Log.LogMessage("Nothing to do");
              }
              OperatorTrace.Regenerate(CurrentAtomicOperation);
        }
示例#2
0
        public override IOperation InstrumentedApply()
        {
            IOperation next = base.InstrumentedApply();

            IVRPEncoding solution = VRPToursParameter.ActualValue;

            generator.PermutationParameter.ActualName = VRPToursParameter.ActualName;
            IAtomicOperation op = this.ExecutionContext.CreateChildOperation(generator);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);

            foreach (IScope scope in this.ExecutionContext.Scope.SubScopes)
            {
                IVariable moveVariable = scope.Variables[
                    TranslocationMoveParameter.ActualName];

                if (moveVariable.Value is TranslocationMove &&
                    !(moveVariable.Value is AlbaTranslocationMove))
                {
                    TranslocationMove move = moveVariable.Value as TranslocationMove;
                    moveVariable.Value =
                        new AlbaTranslocationMove(
                            move.Index1, move.Index2, move.Index3, solution as AlbaEncoding);
                }
            }

            return(next);
        }
 private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
   if (e.Node != null) {
     IAtomicOperation op = e.Node.Tag as IAtomicOperation;
     if (op != null)
       MainFormManager.MainForm.ShowContent(op.Operator);
   }
 }
示例#4
0
 protected virtual void PerformAtomicOperation(IAtomicOperation operation, CancellationToken cancellationToken)
 {
     if (operation != null)
     {
         try {
             IOperation successor = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
             if (successor != null)
             {
                 OperatorTrace.RegisterParenthood(operation, successor);
                 ExecutionStack.Add(successor);
             }
             CurrentOperation = null;
         }
         catch (Exception ex) {
             if (ex is OperationCanceledException)
             {
                 throw ex;
             }
             else
             {
                 throw new OperatorExecutionException(operation.Operator, ex);
             }
         }
     }
 }
        private void ApplyInnerSelector()
        {
            // necessary for inner GenderSpecificSelector to execute all operations in OperationCollection
            Stack <IOperation> executionStack = new Stack <IOperation>();

            executionStack.Push(ExecutionContext.CreateChildOperation(Selector));
            while (executionStack.Count > 0)
            {
                CancellationToken.ThrowIfCancellationRequested();
                IOperation next = executionStack.Pop();
                if (next is OperationCollection)
                {
                    OperationCollection coll = (OperationCollection)next;
                    for (int i = coll.Count - 1; i >= 0; i--)
                    {
                        if (coll[i] != null)
                        {
                            executionStack.Push(coll[i]);
                        }
                    }
                }
                else if (next is IAtomicOperation)
                {
                    IAtomicOperation operation = (IAtomicOperation)next;
                    next = operation.Operator.Execute((IExecutionContext)operation, CancellationToken);
                    if (next != null)
                    {
                        executionStack.Push(next);
                    }
                }
            }
        }
        protected override PrinsEncoding Crossover(IRandom random, PrinsEncoding parent1, PrinsEncoding parent2)
        {
            if (parent1.Length != parent2.Length)
            {
                return(parent1.Clone() as PrinsEncoding);
            }

            //note - the inner crossover is called here and the result is converted to a prins representation
            //some refactoring should be done here in the future - the crossover operation should be called directly

            InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName;
            IAtomicOperation op = this.ExecutionContext.CreateOperation(
                InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);

            string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName;

            if (ExecutionContext.Scope.Variables.ContainsKey(childName))
            {
                Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation;
                ExecutionContext.Scope.Variables.Remove(childName);

                return(new PrinsEncoding(permutation, ProblemInstance));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 添加一个执行器到队列queuedInvocations并执行
        /// </summary>
        /// <param name="executionOperation">执行器</param>
        /// <param name="execution"></param>
        /// <param name="performAsync">是否异步</param>
        public virtual void PerformOperation(IAtomicOperation executionOperation, ExecutionEntity execution,
                                             bool performAsync)
        {
            AtomicOperationInvocation invocation = new AtomicOperationInvocation(executionOperation, execution, performAsync);

            queuedInvocations.Enqueue(invocation);//.Insert(0, invocation);
            PerformNext();
        }
        protected override void Manipulate(IRandom random, ZhuEncoding individual)
        {
            InnerManipulatorParameter.ActualValue.PermutationParameter.ActualName = VRPToursParameter.ActualName;

            IAtomicOperation op = this.ExecutionContext.CreateOperation(
                InnerManipulatorParameter.ActualValue, this.ExecutionContext.Scope);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);
        }
示例#9
0
        protected override void PerformMove()
        {
            IVRPMove move = VRPMoveParameter.ActualValue as IVRPMove;

            VRPMoveMaker moveMaker = move.GetMoveMaker();

            moveMaker.VRPMoveParameter.ActualName = VRPMoveParameter.Name;
            IAtomicOperation op = this.ExecutionContext.CreateOperation(moveMaker);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);
        }
示例#10
0
 public virtual bool IsApplicableForOperation(IAtomicOperation operation)
 {
     foreach (var identifier in OperationIdentifier)
     {
         if (operation.CanonicalName.Equals(identifier))
         {
             return(true);
         }
     }
     return(false);
 }
    public override IOperation InstrumentedApply() {
      IOperation next = base.InstrumentedApply();

      IVRPEncoding solution = VRPToursParameter.ActualValue;

      PermutationMoveOperatorParameter.PermutationParameter.ActualName = VRPToursParameter.ActualName;
      IAtomicOperation op = this.ExecutionContext.CreateChildOperation(PermutationMoveOperatorParameter);
      op.Operator.Execute((IExecutionContext)op, CancellationToken);

      return next;
    }
示例#12
0
 public static void PerformAction(object sender, IAtomicOperation action)
 {
     if (IsInDocumentTree(sender))
     {
         (action as DocAtomicOperation).PrepareForHistory();
         History.Instance.Execute(action);
     }
     else
     {
         action.Do();
     }
 }
示例#13
0
        protected override void PerformMove()
        {
            IVariable moveVariable = this.ExecutionContext.Scope.Variables[
                TranslocationMoveParameter.ActualName];
            TranslocationMove move = moveVariable.Value as TranslocationMove;

            VRPToursParameter.ActualValue = move.Permutation as AlbaEncoding;

            moveMaker.PermutationParameter.ActualName = VRPToursParameter.ActualName;
            IAtomicOperation op = this.ExecutionContext.CreateChildOperation(moveMaker);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);
        }
示例#14
0
        //helper method to evaluate an updated individual
        protected void UpdateEvaluation(IVRPEncoding updatedTours)
        {
            IVRPEvaluator evaluator = ProblemInstance.MoveEvaluator;

            try {
                this.ExecutionContext.Scope.Variables.Add(new Variable(evaluator.VRPToursParameter.ActualName,
                                                                       updatedTours));

                IAtomicOperation op = this.ExecutionContext.CreateChildOperation(evaluator);
                op.Operator.Execute((IExecutionContext)op, CancellationToken);
            }
            finally {
                this.ExecutionContext.Scope.Variables.Remove(evaluator.VRPToursParameter.ActualName);
            }
        }
示例#15
0
        private void PerformUndo(IAtomicOperation action)
        {
            CheckNotInAction();

            DocLogger.WriteLineVerbose("Undo: " + action.ToString());

            try
            {
                currentlyInAction = true;

                action.Undo();
            }
            finally
            {
                currentlyInAction = false;
            }
        }
        private int AddStackOperations(TreeNodeCollection nodes, IEnumerable <IOperation> operations)
        {
            int count = 0;

            foreach (IOperation op in operations)
            {
                if (op is IAtomicOperation)
                {
                    IAtomicOperation atom = op as IAtomicOperation;
                    TreeNode         node = nodes.Add(atom.Operator.Name ?? atom.Operator.ItemName);
                    node.Tag         = atom;
                    node.ToolTipText = string.Format("{0}{1}{1}{2}",
                                                     Utils.TypeName(atom.Operator), Environment.NewLine,
                                                     Utils.Wrap(atom.Operator.Description ?? atom.Operator.ItemDescription, 60));
                    if (atom.Operator.Breakpoint)
                    {
                        node.ForeColor          = Color.Red;
                        node.ImageIndex         = 2;
                        node.SelectedImageIndex = 2;
                    }
                    else
                    {
                        node.ImageIndex         = 0;
                        node.SelectedImageIndex = 0;
                    }
                    count++;
                }
                else if (op is OperationCollection)
                {
                    OperationCollection ops  = op as OperationCollection;
                    TreeNode            node = nodes.Add(
                        string.Format("{0} {2}Operation{1}",
                                      ops.Count,
                                      ops.Count == 1 ? string.Empty : "s",
                                      ops.Parallel ? "Parallel " : string.Empty
                                      ));
                    node.Tag                = op;
                    node.ToolTipText        = Utils.TypeName(ops);
                    node.ImageIndex         = 1;
                    node.SelectedImageIndex = 1;
                    count += AddStackOperations(node.Nodes, ops);
                }
            }
            return(count);
        }
示例#17
0
        private void PerformAdd(IAtomicOperation action)
        {
            CheckCurrentAction();

            if (Object.ReferenceEquals(action, null))
            {
                throw new Exception("Can't add null action");
            }

            while (currentAction < actions.Count - 1)
            {
                actions.Remove(actions.Last());
            }

            actions.Add(action);
            currentAction++;

            CheckCurrentAction();
        }
示例#18
0
        public static void PerformAction(object sender, EventArgs e)
        {
            IAtomicOperation action = (e as IAtomicOperations).CreateActions(sender as IDocLeaf).First();

            if (IsInDocumentTree(sender))
            {
                if (History.Instance.MarkDirty != null)
                {
                    History.Instance.MarkDirty(sender, e);
                }

                (action as DocAtomicOperation).PrepareForHistory();
                History.Instance.Execute(action);
            }
            else
            {
                action.Do();
            }
        }
示例#19
0
        public void Execute(IAtomicOperation action)
        {
            CheckThreadId();

            Debug.Assert(action.IsValidForHistory);

            CheckNotInAction();

            if (HasTransaction())
            {
                PerformDo(action);
                transaction.Add(action);
            }
            else
            {
                PerformDo(action);
                PerformAdd(action);
                PerformNotifications();
            }
        }
示例#20
0
        public virtual void RegisterParenthood(IAtomicOperation parent, IOperation children)
        {
            if (!isEnabled)
            {
                return;
            }
            OperationCollection operations = children as OperationCollection;

            if (operations != null)
            {
                foreach (var op in operations)
                {
                    RegisterParenthood(parent, op);
                }
            }
            IAtomicOperation atomicOperation = children as IAtomicOperation;

            if (atomicOperation != null && atomicOperation.Operator != null && !parents.ContainsKey(atomicOperation))
            {
                parents[atomicOperation] = parent;
            }
        }
示例#21
0
        public virtual void Regenerate(IAtomicOperation operation)
        {
            if (!isEnabled)
            {
                Reset();
                return;
            }
            if (operation == null)
            {
                return;
            }
            Stack <IOperator> trace = new Stack <IOperator>();

            while (operation != null)
            {
                trace.Push(operation.Operator);
                IAtomicOperation parent = null;
                parents.TryGetValue(operation, out parent);
                operation = parent;
            }
            ReplaceAll(trace);
        }
 /// <summary>
 /// 添加一个异步执行的执行器到队列queuedInvocations 并执行
 /// </summary>
 /// <param name="executionOperation">执行器</param>
 /// <param name="execution"></param>
 public virtual void PerformOperationAsync(IAtomicOperation executionOperation, ExecutionEntity execution)
 {
     PerformOperation(executionOperation, execution, true);
 }
示例#23
0
 protected virtual void PerformAtomicOperation(IAtomicOperation operation, CancellationToken cancellationToken) {
   if (operation != null) {
     try {
       IOperation successor = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
       if (successor != null) {
         OperatorTrace.RegisterParenthood(operation, successor);
         ExecutionStack.Add(successor);
       }
       CurrentOperation = null;
     }
     catch (Exception ex) {
       if (ex is OperationCanceledException) throw ex;
       else throw new OperatorExecutionException(operation.Operator, ex);
     }
   }
 }
示例#24
0
 public static string Name(IAtomicOperation operation)
 {
     return(string.IsNullOrEmpty(operation.Operator.Name) ? operation.Operator.ItemName : operation.Operator.Name);
 }
示例#25
0
 public void Add(IAtomicOperation action)
 {
     actions.Add(action);
 }
示例#26
0
 public static string Name(IAtomicOperation operation) {
   return string.IsNullOrEmpty(operation.Operator.Name) ? operation.Operator.ItemName : operation.Operator.Name;
 }
 /// <summary>
 /// 添加一个同步执行的执行器到队列queuedInvocations并执行
 /// </summary>
 /// <param name="executionOperation"></param>
 /// <param name="execution"></param>
 public virtual void PerformOperation(IAtomicOperation executionOperation, ExecutionEntity execution)
 {
     PerformOperation(executionOperation, execution, false);
 }
 protected internal virtual void Init(IAtomicOperation operation, ExecutionEntity execution, bool performAsync)
 {
     this.operation    = operation;
     this.execution    = execution;
     this.performAsync = performAsync;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="operation">执行器</param>
 /// <param name="execution">ExecutionEntity</param>
 /// <param name="performAsync">是否异步</param>
 public AtomicOperationInvocation(IAtomicOperation operation, ExecutionEntity execution, bool performAsync)
 {
     Init(operation, execution, performAsync);
 }
示例#30
0
 public void Append(IAtomicOperation operation)
 {
     this.state.AppendedOperations.Add(operation);
 }