File: RuleDefinitions.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (127 lines) | stat: -rw-r--r-- 4,850 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// ---------------------------------------------------------------------------
// Copyright (C) 2006 Microsoft Corporation All Rights Reserved
// ---------------------------------------------------------------------------

#define CODE_ANALYSIS
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Workflow.ComponentModel;

namespace System.Workflow.Activities.Rules
{
    #region class RuleDefinitions

    public sealed class RuleDefinitions : IWorkflowChangeDiff
    {

        [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        public static readonly DependencyProperty RuleDefinitionsProperty = DependencyProperty.RegisterAttached("RuleDefinitions", typeof(RuleDefinitions), typeof(RuleDefinitions), new PropertyMetadata(null, DependencyPropertyOptions.Metadata, new GetValueOverride(OnGetRuleConditions), null, new Attribute[] { new DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }));

        private RuleConditionCollection conditions;
        private RuleSetCollection ruleSets;
        private bool runtimeInitialized;
        [NonSerialized]
        private object syncLock = new object();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RuleConditionCollection Conditions
        {
            get
            {
                if (this.conditions == null)
                    this.conditions = new RuleConditionCollection();
                return conditions;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RuleSetCollection RuleSets
        {
            get
            {
                if (this.ruleSets == null)
                    this.ruleSets = new RuleSetCollection();
                return this.ruleSets;
            }
        }

        internal static object OnGetRuleConditions(DependencyObject dependencyObject)
        {
            if (dependencyObject == null)
                throw new ArgumentNullException("dependencyObject");

            RuleDefinitions rules = dependencyObject.GetValueBase(RuleDefinitions.RuleDefinitionsProperty) as RuleDefinitions;
            if (rules != null)
                return rules;

            Activity rootActivity = dependencyObject as Activity;
            if (rootActivity.Parent == null)
            {
                rules = ConditionHelper.GetRuleDefinitionsFromManifest(rootActivity.GetType());
                if (rules != null)
                    dependencyObject.SetValue(RuleDefinitions.RuleDefinitionsProperty, rules);
            }
            return rules;
        }

        internal void OnRuntimeInitialized()
        {
            lock (syncLock)
            {
                if (runtimeInitialized)
                    return;
                Conditions.OnRuntimeInitialized();
                RuleSets.OnRuntimeInitialized();
                runtimeInitialized = true;
            }
        }

        #region IWorkflowChangeDiff Members

        public IList<WorkflowChangeAction> Diff(object originalDefinition, object changedDefinition)
        {
            RuleDefinitions originalRules = originalDefinition as RuleDefinitions;
            RuleDefinitions changedRules = changedDefinition as RuleDefinitions;
            if ((originalRules == null) || (changedRules == null))
                return new List<WorkflowChangeAction>();

            IList<WorkflowChangeAction> cdiff = Conditions.Diff(originalRules.Conditions, changedRules.Conditions);
            IList<WorkflowChangeAction> rdiff = RuleSets.Diff(originalRules.RuleSets, changedRules.RuleSets);

            // quick optimization -- if no condition changes, simply return the ruleset changes
            if (cdiff.Count == 0)
                return rdiff;

            // merge ruleset changes into condition changes
            for (int i = 0; i < rdiff.Count; ++i)
            {
                cdiff.Add(rdiff[i]);
            }
            return cdiff;
        }
        #endregion

        internal RuleDefinitions Clone()
        {
            RuleDefinitions newRuleDefinitions = new RuleDefinitions();

            if (this.ruleSets != null)
            {
                newRuleDefinitions.ruleSets = new RuleSetCollection();
                foreach (RuleSet r in this.ruleSets)
                    newRuleDefinitions.ruleSets.Add(r.Clone());
            }

            if (this.conditions != null)
            {
                newRuleDefinitions.conditions = new RuleConditionCollection();
                foreach (RuleCondition r in this.conditions)
                    newRuleDefinitions.conditions.Add(r.Clone());
            }

            return newRuleDefinitions;
        }
    }
    #endregion
}