﻿<?xml version="1.0" encoding="utf-8" ?>

<WIModel>
	<!-- ID of the Work Item used as the Recycle Bin -->
	<RecycleBin id="301" areapath="Test_1\RecycleBin"/>

	<!-- HIERARCHY RULES -->
	<WIHierarchy>
		<!--WORK ITEM OF TYPE: CHANGE REQUEST-->
		<WorkItem type="Change Request">
			<!--Allowed Work Item Type under this one-->
			<Allowed>
				<!--min="0" means 0 or more-->
				<WorkItem type="Change Request" min="0"/>
				<WorkItem type="Bug"			min="0"/>
				<WorkItem type="Task"			min="0"/>
			</Allowed>
		</WorkItem>

		<!--WORK ITEM OF TYPE: BUG-->
		<WorkItem type="Bug">
			<Allowed>
				<WorkItem type="Change Request"	min="0"/>
				<!--min="1" max="1" means there must be just one-->
				<WorkItem type="Task"			min="1" max="1"/>
			</Allowed>
		</WorkItem>

		<!--WORK ITEM OF TYPE: TASK-->
		<WorkItem type="Task">
			<Allowed>
				<!--min="0" max="1" means it's optional, but only one allowed-->
				<WorkItem type="Review"			min="0" max="1"/>
				<WorkItem type="Issue"			min="0" max="1"/>
				<WorkItem type="Bug"			min="0"/>
			</Allowed>
		</WorkItem>

		<!--WORK ITEM OF TYPE: ISSUE-->
		<WorkItem type="Issue">
			<Allowed>
				<WorkItem type="Change Request"	min="0" max="1">
					<!--If the Exception rules are satisfied, the owner element can't be allowed-->
					<Exceptions>
						<WorkItem type="Bug" min="1"/>
					</Exceptions>
				</WorkItem>
				<WorkItem type="Bug"			min="0" max="1">
					<Exceptions>
						<WorkItem type="Change Request" min="1"/>
					</Exceptions>
				</WorkItem>
			</Allowed>
		</WorkItem>
	</WIHierarchy>

	<NetworkModels>
		<!--Declaration of the model "project"-->
		<NetworkModel name="Project">
			<!--This section is for Work Item of type "Change Request" and "Bug"-->
			<!--Note the '$' is serving as delimiter in case of multiple types-->
			<WorkItem type="Change Request$Bug">
				<!--List of the many states to analyse to make the Network Model works-->
				<States>
					<!--The "Children" trigger means a change in a child will leads to the 
						execution of this state check-->
					<!--Possible triggers are "Children", "Parent" and "Current"-->
					
					<!--Here we want to Auto Activate Change Requests or Bugs, this is triggered by a child change-->
					<!--Whenever a child Change Request, Bug, or Task become Active, if the Work Item in analysing
						is either in Proposed, Resolved or Closed state, it becomes Active-->
					<State name="Auto Activate" trigger="Children">
						<!--List of the fields with their possible values to compare with the work item in analysing
							to validate this State-->
						<Fields>
							<!--If the current Work Item is either a Change Request or a Bug, and its State field
								is either Proposed, Resolved or Closed, then we can analyse it-->
							<Field name="State" current="Proposed"/>
						</Fields>

						<!--All the rules state here must be satisfied for the State execution to be valid-->
						<Rules>
							<!--The "OneChild" rule means at least one child must match the request criterias-->
							<Rule name="OneChild">
								<!--We're looking for a child of type Change Request or Task, and its state must be
									Active for the rule to be satisfied-->
								<WorkItem type ="Change Request$Bug$Task">
									<State>
										<Field name="State" current="Active"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<!--If all rules were satisfied, when we can make the following change in the Work Item in
							analysing-->
						<Actions>
							<!--Name of the action, that will be store on the Work Item History-->
							<Action name="Activate the Work Item">
								<!--We make a state change to Active-->
								<Field name="State" newvalue="Active"/>
							</Action>
						</Actions>
					</State>

					<State name="Reactivate" trigger="Children">
						<!--List of the fields with their possible values to compare with the work item in analysing
							to validate this State-->
						<Fields>
							<!--If the current Work Item is either a Change Request or a Bug, and its State field
								is either Proposed, Resolved or Closed, then we can analyse it-->
							<Field name="State" current="Resolved$Closed"/>
						</Fields>

						<!--At least one of the rules state here must be satisfied for the State execution to be valid-->
						<Rules>
							<Rule name="OneChild">
								<WorkItem type ="Change Request$Bug$Task">
									<State>
										<Field name="State" previous="" current="Proposed"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<!--If all rules were satisfied, when we can make the following change in the Work Item in
							analysing-->
						<Actions>
							<!--Name of the action, that will be store on the Work Item History-->
							<Action name="Reactivate the Work Item">
								<!--We make a state change to Active-->
								<Field name="State" newvalue="Active"/>
							</Action>
						</Actions>
					</State>

					<!--All the children must be Resolved/Closed (for CR or Bug) and Closed (for Task) for the
						CR/Bug to be resolved-->
					<State name="Turn to Resolved" trigger="Children">
						<Fields>
							<Field name="State" current="Proposed$Active"/>
						</Fields>

						<Rules>
							<Rule name="AllChildren">
								<WorkItem type ="Change Request$Bug">
									<State>
										<Field name="State" current="Resolved$Closed"/>
									</State>
								</WorkItem>
								<WorkItem type ="Task">
									<State>
										<Field name="State" current="Closed"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Resolved the Work Item">
								<Field name="State" newvalue="Resolved"/>
							</Action>
						</Actions>
					</State>

					<State name="Turn to Proposed" trigger="Children">
						<Fields>
							<Field name="State" current="Active$Resolved$Closed"/>
						</Fields>

						<Rules>
							<Rule name="AllChildren">
								<WorkItem type ="Change Request$Bug$Task">
									<State>
										<Field name="State" current="Proposed"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Turn back to proposed">
								<Field name="State" newvalue="Proposed"/>
							</Action>
						</Actions>
					</State>

					<State name="Rejected" trigger="Children">
						<Fields>
							<Field name="State" current="Proposed"/>
						</Fields>

						<Rules>
							<Rule name="AllChildren">
								<WorkItem type ="Task">
									<State>
										<Field name="State" current="Closed"/>
										<Field name="Reason" current="Rejected"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Rejected">
								<Field name="State" newvalue="Closed"/>
							</Action>
						</Actions>
					</State>

				</States>
			</WorkItem>
		</NetworkModel>
		
		<!--Declaration of the model "BlockedDev"-->
		<NetworkModel name="BlockDev">
			<WorkItem type="Issue">
				<States>
					<State name="Activate - Resolution starting" trigger="Children">
						<Fields>
							<Field name="State" current="Proposed$Resolved$Closed"/>
						</Fields>

						<Rules>
							<Rule name="OneChild">
								<WorkItem type="Change Request$Bug">
									<State>
										<Field name="State" previous="Proposed$Resolved$Closed" current="Active"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Resolution is starting">
								<Field name="State" newvalue="Active"/>
							</Action>
						</Actions>
					</State>
					
					<State name="Resolved - Resolution done" trigger="Children">
						<Fields>
							<Field name="State" current="Active"/>
						</Fields>

						<Rules>
							<Rule name="OneChild">
								<WorkItem type="Change Request$Bug">
									<State>
										<Field name="State" current="Resolved"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Resolution done and waiting validation.">
								<Field name="State" newvalue="Resolved"/>
							</Action>
						</Actions>
					</State>
					
					<State name="Activate - Task still blocked!" trigger="Parent">
						<Fields>
							<Field name="State" current="Resolved$Closed"/>
						</Fields>

						<Rules>
							<Rule name="Parent">
								<WorkItem type="Task">
									<State>
										<Field name="State" current="Active"/>
										<Field name="Blocked" previous="No" current="Yes"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Resolution not acceptable, issue still active!">
								<Field name="State" newvalue="Active"/>
							</Action>
						</Actions>
					</State>
				</States>
			</WorkItem>
			<WorkItem type="Task">
				<States>
					<State name="Unblocked by Issue resolution" trigger="Children">
						<Fields>
							<Field name="Blocked" current="Yes"/>
						</Fields>

						<Rules>
							<Rule name="OneChild">
								<WorkItem type="Issue">
									<State>
										<Field name="State" previous="Active" current="Resolved"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Unblocking the task due to issue resolution.">
								<Field name="Blocked" newvalue="No"/>
							</Action>
						</Actions>
					</State>
				</States>
			</WorkItem>
			<WorkItem type="Change Request$Bug">
				<States>
					<State name="Resolution closed" trigger="Parent">
						<Fields>
							<Field name="State" current="Resolved"/>
						</Fields>

						<Rules>
							<Rule name="Parent">
								<WorkItem type="Issue">
									<State>
										<Field name="State" previous="Active$Resolved" current="Closed"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Closing the resolution">
								<Field name="State" newvalue="Closed"/>
							</Action>
						</Actions>
					</State>
				</States>
			</WorkItem>
		</NetworkModel>
		
		<!--Declaration of the model "BlockedDev"-->
		<NetworkModel name="TaskDev">
			<WorkItem type="Task">
				<States>
					<State name="Reactivate, Review failed" trigger="Children">
						<Fields>
							<Field name="State" current="Review Pending before Check-in$Review Pending, Check-in done$Closed"/>
						</Fields>

						<Rules>
							<Rule name="OneChild">
								<WorkItem type="Review">
									<State>
										<Field name="State" previous="Active$Resolved$Closed" current="Proposed"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Reactivate task due to review fail">
								<Field name="State" newvalue="Active"/>
							</Action>
						</Actions>
					</State>

					<State name="To Pending Check-in" trigger="Children">
						<Fields>
							<Field name="State" current="Review Pending before Check-in"/>
						</Fields>

						<Rules>
							<Rule name="OneChild">
								<WorkItem type="Review">
									<State>
										<Field name="State" previous="Active" current="Resolved$Closed"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Ready to Check-in">
								<Field name="State" newvalue="Pending Check-in"/>
							</Action>
						</Actions>
					</State>

					<State name="Reactivated for minor changes" trigger="Children">
						<Fields>
							<Field name="State" current="Review Pending, Check-in done"/>
						</Fields>

						<Rules>
							<Rule name="OneChild">
								<WorkItem type="Review">
									<State>
										<Field name="State" previous="Active" current="Resolved"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Reactivated for minor changes">
								<Field name="State" newvalue="Active"/>
							</Action>
						</Actions>
					</State>

					<State name="Closed due to review close" trigger="Children">
						<Fields>
							<Field name="State" current="Review Pending, Check-in done"/>
						</Fields>

						<Rules>
							<Rule name="OneChild">
								<WorkItem type="Review">
									<State>
										<Field name="State" previous="Active$Resolved" current="Closed"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Closed by review completed successfully">
								<Field name="State" newvalue="Closed"/>
							</Action>
						</Actions>
					</State>
				</States>
			</WorkItem>
			
			<WorkItem type="Review">
				<States>
					<State name="Activate Review" trigger="Parent">
						<Fields>
							<Field name="State" current="Proposed"/>
						</Fields>

						<Rules>
							<Rule name="Parent">
								<WorkItem type="Task">
									<State>
										<Field name="State" previous="Active" current="Review Pending before Check-in$Review Pending, Check-in done"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Review can start now.">
								<Field name="State" newvalue="Active"/>
							</Action>
						</Actions>
					</State>

					<State name="Minor changes checked-in" trigger="Parent">
						<Fields>
							<Field name="State" current="Resolved"/>
						</Fields>

						<Rules>
							<Rule name="Parent">
								<WorkItem type="Task">
									<State>
										<Field name="State" previous="Active" current="Review Pending, Check-in done"/>
									</State>
								</WorkItem>
							</Rule>
						</Rules>

						<Actions>
							<Action name="Minor changes checked-in, review closed.">
								<Field name="State" newvalue="Closed"/>
							</Action>
						</Actions>
					</State>
				</States>
			</WorkItem>
		</NetworkModel>
	</NetworkModels>
</WIModel>