private static ProcessActivityTreeOptions AttachCancellationToken(ProcessActivityTreeOptions result, CancellationToken cancellationToken)
        {
            ProcessActivityTreeOptions clone = result.Clone();

            clone.CancellationToken = cancellationToken;
            return(clone);
        }
        public static ProcessActivityTreeOptions GetFinishCachingSubtreeOptions(ProcessActivityTreeOptions originalOptions)
        {
            ProcessActivityTreeOptions result;

            if (originalOptions.CreateEmptyBindings)
            {
                Fx.Assert(!originalOptions.SkipRootConfigurationValidation, "If we ever add code that uses this combination of options, " +
                          "we need a new predefined setting on ProcessActivityTreeOptions.");
                result = ProcessActivityTreeOptions.FinishCachingSubtreeOptionsWithCreateEmptyBindings;
            }
            else
            {
                if (originalOptions.SkipRootConfigurationValidation)
                {
                    result = ProcessActivityTreeOptions.SkipRootFinishCachingSubtreeOptions;
                }
                else
                {
                    result = ProcessActivityTreeOptions.FinishCachingSubtreeOptionsWithoutCreateEmptyBindings;
                }
            }

            if (originalOptions.CancellationToken == CancellationToken.None)
            {
                return(result);
            }
            else
            {
                return(AttachCancellationToken(result, originalOptions.CancellationToken));
            }
        }
        public static ProcessActivityTreeOptions GetValidationOptions(ValidationSettings settings)
        {
            ProcessActivityTreeOptions result = null;

            if (settings.SkipValidatingRootConfiguration && settings.SingleLevel)
            {
                result = ProcessActivityTreeOptions.SingleLevelSkipRootConfigurationValidationOptions;
            }
            else if (settings.SkipValidatingRootConfiguration)
            {
                result = ProcessActivityTreeOptions.SkipRootConfigurationValidationOptions;
            }
            else if (settings.SingleLevel)
            {
                result = ProcessActivityTreeOptions.SingleLevelValidationOptions;
            }
            else if (settings.PrepareForRuntime)
            {
                Fx.Assert(!settings.SkipValidatingRootConfiguration && !settings.SingleLevel && !settings.OnlyUseAdditionalConstraints, "PrepareForRuntime cannot be set at the same time any of the three is set.");
                result = ProcessActivityTreeOptions.ValidationAndPrepareForRuntimeOptions;
            }
            else
            {
                result = ProcessActivityTreeOptions.ValidationOptions;
            }
            if (settings.CancellationToken == CancellationToken.None)
            {
                return(result);
            }
            else
            {
                return(AttachCancellationToken(result, settings.CancellationToken));
            }
        }
 internal ValidationContext(ActivityUtilities.ChildActivity owner, ActivityUtilities.ActivityCallStack parentChain, ProcessActivityTreeOptions options, LocationReferenceEnvironment environment)
 {
     this.owner = owner;
     this.parentChain = parentChain;
     this.options = options;
     this.environment = environment;
 }
 public static ProcessActivityTreeOptions GetFinishCachingSubtreeOptions(ProcessActivityTreeOptions originalOptions)
 {
     if (originalOptions.CreateEmptyBindings)
     {
         return(FinishCachingSubtreeOptionsWithCreateEmptyBindings);
     }
     return(FinishCachingSubtreeOptionsWithoutCreateEmptyBindings);
 }
 public static ProcessActivityTreeOptions GetFinishCachingSubtreeOptions(ProcessActivityTreeOptions originalOptions)
 {
     if (originalOptions.CreateEmptyBindings)
     {
         return FinishCachingSubtreeOptionsWithCreateEmptyBindings;
     }
     return FinishCachingSubtreeOptionsWithoutCreateEmptyBindings;
 }
 static ProcessActivityTreeOptions AttachCancellationToken(ProcessActivityTreeOptions result, CancellationToken cancellationToken)
 {
     ProcessActivityTreeOptions clone = result.Clone();
     clone.CancellationToken = cancellationToken;
     return clone;
 }
        public static ProcessActivityTreeOptions GetFinishCachingSubtreeOptions(ProcessActivityTreeOptions originalOptions)
        {
            ProcessActivityTreeOptions result;
            if (originalOptions.CreateEmptyBindings)
            {
                Fx.Assert(!originalOptions.SkipRootConfigurationValidation, "If we ever add code that uses this combination of options, " +
                    "we need a new predefined setting on ProcessActivityTreeOptions.");
                result = ProcessActivityTreeOptions.FinishCachingSubtreeOptionsWithCreateEmptyBindings;
            }
            else
            {
                if (originalOptions.SkipRootConfigurationValidation)
                {
                    result = ProcessActivityTreeOptions.SkipRootFinishCachingSubtreeOptions;
                }
                else
                {
                    result = ProcessActivityTreeOptions.FinishCachingSubtreeOptionsWithoutCreateEmptyBindings;
                }
            }

            if (originalOptions.CancellationToken == CancellationToken.None)
            {
                return result;
            }
            else
            {
                return AttachCancellationToken(result, originalOptions.CancellationToken);
            }
        }
 private static bool ShouldShortcut(Activity activity, ProcessActivityTreeOptions options)
 {
     return((options.SkipIfCached && options.IsRuntimeReadyOptions) && activity.IsRuntimeReady);
 }
 public static void CacheRootMetadata(Activity activity, LocationReferenceEnvironment hostEnvironment, ProcessActivityTreeOptions options, ProcessActivityCallback callback, ref IList <ValidationError> validationErrors)
 {
     if (!ShouldShortcut(activity, options))
     {
         lock (activity.ThisLock)
         {
             if (!ShouldShortcut(activity, options))
             {
                 if (activity.HasBeenAssociatedWithAnInstance)
                 {
                     throw FxTrace.Exception.AsError(new InvalidOperationException(System.Activities.SR.RootActivityAlreadyAssociatedWithInstance(activity.DisplayName)));
                 }
                 activity.InitializeAsRoot(hostEnvironment);
                 ProcessActivityTreeCore(new ChildActivity(activity, true), null, options, callback, ref validationErrors);
                 if (!ActivityValidationServices.HasErrors(validationErrors) && options.IsRuntimeReadyOptions)
                 {
                     activity.SetRuntimeReady();
                 }
             }
         }
     }
 }
        private static void ProcessActivityTreeCore(ChildActivity currentActivity, ActivityCallStack parentChain, ProcessActivityTreeOptions options, ProcessActivityCallback callback, ref IList <ValidationError> validationErrors)
        {
            ChildActivity         empty = ChildActivity.Empty;
            Stack <ChildActivity> activitiesRemaining = null;

            if (parentChain == null)
            {
                parentChain = new ActivityCallStack();
            }
            if (options.OnlyVisitSingleLevel)
            {
                ProcessActivity(currentActivity, ref empty, ref activitiesRemaining, parentChain, ref validationErrors, options, callback);
            }
            else
            {
                while (!currentActivity.Equals(ChildActivity.Empty))
                {
                    if (object.ReferenceEquals(currentActivity.Activity, popActivity))
                    {
                        parentChain.Pop().Activity.SetCached();
                    }
                    else
                    {
                        SetupForProcessing(popActivity, true, ref empty, ref activitiesRemaining);
                        ProcessActivity(currentActivity, ref empty, ref activitiesRemaining, parentChain, ref validationErrors, options, callback);
                        parentChain.Push(currentActivity);
                    }
                    currentActivity = empty;
                    if ((activitiesRemaining != null) && (activitiesRemaining.Count > 0))
                    {
                        empty = activitiesRemaining.Pop();
                    }
                    else
                    {
                        empty = ChildActivity.Empty;
                    }
                }
            }
        }
        private static void ProcessActivity(ChildActivity childActivity, ref ChildActivity nextActivity, ref Stack <ChildActivity> activitiesRemaining, ActivityCallStack parentChain, ref IList <ValidationError> validationErrors, ProcessActivityTreeOptions options, ProcessActivityCallback callback)
        {
            Activity                element            = childActivity.Activity;
            IList <Constraint>      runtimeConstraints = element.RuntimeConstraints;
            IList <ValidationError> list2 = null;

            if (!element.HasStartedCachingMetadata)
            {
                element.MemberOf.AddMember(element);
                element.InternalCacheMetadata(options.CreateEmptyBindings, ref list2);
                ActivityValidationServices.ValidateArguments(element, element.Parent == null, ref list2);
                ActivityLocationReferenceEnvironment environment  = null;
                ActivityLocationReferenceEnvironment environment2 = new ActivityLocationReferenceEnvironment(element.HostEnvironment)
                {
                    InternalRoot = element
                };
                int nextEnvironmentId = 0;
                ProcessChildren(element, element.Children, ActivityCollectionType.Public, true, ref nextActivity, ref activitiesRemaining, ref list2);
                ProcessChildren(element, element.ImportedChildren, ActivityCollectionType.Imports, true, ref nextActivity, ref activitiesRemaining, ref list2);
                ProcessChildren(element, element.ImplementationChildren, ActivityCollectionType.Implementation, !options.SkipPrivateChildren, ref nextActivity, ref activitiesRemaining, ref list2);
                ProcessArguments(element, element.RuntimeArguments, true, ref environment2, ref nextEnvironmentId, ref nextActivity, ref activitiesRemaining, ref list2);
                ProcessVariables(element, element.RuntimeVariables, ActivityCollectionType.Public, true, ref environment, ref nextEnvironmentId, ref nextActivity, ref activitiesRemaining, ref list2);
                ProcessVariables(element, element.ImplementationVariables, ActivityCollectionType.Implementation, !options.SkipPrivateChildren, ref environment2, ref nextEnvironmentId, ref nextActivity, ref activitiesRemaining, ref list2);
                if (element.HandlerOf != null)
                {
                    for (int i = 0; i < element.HandlerOf.RuntimeDelegateArguments.Count; i++)
                    {
                        RuntimeDelegateArgument argument      = element.HandlerOf.RuntimeDelegateArguments[i];
                        DelegateArgument        boundArgument = argument.BoundArgument;
                        if ((boundArgument != null) && boundArgument.InitializeRelationship(element, ref list2))
                        {
                            boundArgument.Id = nextEnvironmentId;
                            nextEnvironmentId++;
                        }
                    }
                }
                if (environment == null)
                {
                    element.PublicEnvironment = new ActivityLocationReferenceEnvironment(element.GetParentEnvironment());
                }
                else
                {
                    if (environment.Parent == null)
                    {
                        environment.InternalRoot = element;
                    }
                    element.PublicEnvironment = environment;
                }
                element.ImplementationEnvironment = environment2;
                ProcessDelegates(element, element.Delegates, ActivityCollectionType.Public, true, ref nextActivity, ref activitiesRemaining, ref list2);
                ProcessDelegates(element, element.ImportedDelegates, ActivityCollectionType.Imports, true, ref nextActivity, ref activitiesRemaining, ref list2);
                ProcessDelegates(element, element.ImplementationDelegates, ActivityCollectionType.Implementation, !options.SkipPrivateChildren, ref nextActivity, ref activitiesRemaining, ref list2);
                if (callback != null)
                {
                    callback(childActivity, parentChain);
                }
                if (list2 != null)
                {
                    Activity activity2;
                    if (validationErrors == null)
                    {
                        validationErrors = new List <ValidationError>();
                    }
                    string str = ActivityValidationServices.GenerateValidationErrorPrefix(childActivity.Activity, parentChain, out activity2);
                    for (int j = 0; j < list2.Count; j++)
                    {
                        ValidationError item = list2[j];
                        item.Source = activity2;
                        item.Id     = activity2.Id;
                        if (!string.IsNullOrEmpty(str))
                        {
                            item.Message = str + item.Message;
                        }
                        validationErrors.Add(item);
                    }
                    list2 = null;
                }
                if (options.StoreTempViolations && (validationErrors != null))
                {
                    childActivity.Activity.SetTempValidationErrorCollection(validationErrors);
                    validationErrors = null;
                }
            }
            else
            {
                SetupForProcessing(element.Children, true, ref nextActivity, ref activitiesRemaining);
                SetupForProcessing(element.ImportedChildren, false, ref nextActivity, ref activitiesRemaining);
                SetupForProcessing(element.RuntimeArguments, ref nextActivity, ref activitiesRemaining);
                SetupForProcessing(element.RuntimeVariables, ref nextActivity, ref activitiesRemaining);
                SetupForProcessing(element.Delegates, true, ref nextActivity, ref activitiesRemaining);
                SetupForProcessing(element.ImportedDelegates, false, ref nextActivity, ref activitiesRemaining);
                if (!options.SkipPrivateChildren)
                {
                    SetupForProcessing(element.ImplementationChildren, true, ref nextActivity, ref activitiesRemaining);
                    SetupForProcessing(element.ImplementationDelegates, true, ref nextActivity, ref activitiesRemaining);
                    SetupForProcessing(element.ImplementationVariables, ref nextActivity, ref activitiesRemaining);
                }
                if ((callback != null) && !options.OnlyCallCallbackForDeclarations)
                {
                    callback(childActivity, parentChain);
                }
                if (childActivity.Activity.HasTempViolations && !options.StoreTempViolations)
                {
                    childActivity.Activity.TransferTempValidationErrors(ref validationErrors);
                }
            }
            if ((!options.SkipConstraints && parentChain.WillExecute) && (childActivity.CanBeExecuted && (runtimeConstraints.Count > 0)))
            {
                ActivityValidationServices.RunConstraints(childActivity, parentChain, runtimeConstraints, options, false, ref validationErrors);
            }
        }
        public static void FinishCachingSubtree(ChildActivity subtreeRoot, ActivityCallStack parentChain, ProcessActivityTreeOptions options, ProcessActivityCallback callback)
        {
            IList <ValidationError> validationErrors = null;

            ProcessActivityTreeCore(subtreeRoot, parentChain, ProcessActivityTreeOptions.GetFinishCachingSubtreeOptions(options), callback, ref validationErrors);
        }
 internal ValidationResults InternalValidate()
 {
     this.options = ProcessActivityTreeOptions.GetValidationOptions(this.settings);
     if (this.settings.OnlyUseAdditionalConstraints)
     {
         IList<ValidationError> validationErrors = null;
         ActivityUtilities.CacheRootMetadata(this.rootToValidate, null, this.options, new ActivityUtilities.ProcessActivityCallback(this.ValidateElement), ref validationErrors);
     }
     else
     {
         ActivityUtilities.CacheRootMetadata(this.rootToValidate, null, this.options, new ActivityUtilities.ProcessActivityCallback(this.ValidateElement), ref this.errors);
     }
     return new ValidationResults(this.errors);
 }
 internal static void RunConstraints(ActivityUtilities.ChildActivity childActivity, ActivityUtilities.ActivityCallStack parentChain, IList<Constraint> constraints, ProcessActivityTreeOptions options, bool suppressGetChildrenViolations, ref IList<ValidationError> validationErrors)
 {
     if (constraints != null)
     {
         Activity activity = childActivity.Activity;
         LocationReferenceEnvironment parentEnvironment = activity.GetParentEnvironment();
         Dictionary<string, object> inputs = new Dictionary<string, object>(2);
         for (int i = 0; i < constraints.Count; i++)
         {
             Constraint workflow = constraints[i];
             if (workflow != null)
             {
                 object obj2;
                 inputs["ToValidate"] = activity;
                 ValidationContext context = new ValidationContext(childActivity, parentChain, options, parentEnvironment);
                 inputs["ToValidateContext"] = context;
                 IDictionary<string, object> dictionary2 = null;
                 try
                 {
                     dictionary2 = WorkflowInvoker.Invoke(workflow, inputs);
                 }
                 catch (Exception exception)
                 {
                     if (Fx.IsFatal(exception))
                     {
                         throw;
                     }
                     ValidationError data = new ValidationError(System.Activities.SR.InternalConstraintException(workflow.DisplayName, activity.GetType().FullName, activity.DisplayName, exception.ToString()), false) {
                         Source = activity,
                         Id = activity.Id
                     };
                     ActivityUtilities.Add<ValidationError>(ref validationErrors, data);
                 }
                 if ((dictionary2 != null) && dictionary2.TryGetValue("ViolationList", out obj2))
                 {
                     IList<ValidationError> list = (IList<ValidationError>) obj2;
                     if (list.Count > 0)
                     {
                         Activity activity2;
                         if (validationErrors == null)
                         {
                             validationErrors = new List<ValidationError>();
                         }
                         string str = GenerateValidationErrorPrefix(childActivity.Activity, parentChain, out activity2);
                         for (int j = 0; j < list.Count; j++)
                         {
                             ValidationError item = list[j];
                             item.Source = activity2;
                             item.Id = activity2.Id;
                             if (!string.IsNullOrEmpty(str))
                             {
                                 item.Message = str + item.Message;
                             }
                             validationErrors.Add(item);
                         }
                     }
                 }
                 if (!suppressGetChildrenViolations)
                 {
                     context.AddGetChildrenErrors(ref validationErrors);
                 }
             }
         }
     }
 }
 internal static List<Activity> GetChildren(ActivityUtilities.ChildActivity root, ActivityUtilities.ActivityCallStack parentChain, ProcessActivityTreeOptions options)
 {
     ActivityUtilities.FinishCachingSubtree(root, parentChain, options);
     List<Activity> list = new List<Activity>();
     foreach (Activity activity in WorkflowInspectionServices.GetActivities(root.Activity))
     {
         list.Add(activity);
     }
     for (int i = 0; i < list.Count; i++)
     {
         foreach (Activity activity2 in WorkflowInspectionServices.GetActivities(list[i]))
         {
             list.Add(activity2);
         }
     }
     return list;
 }