<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:getriebe:examples" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="https://tokenjanitor.github.io/getriebe/languages/expression/v1">
  <bpmn:itemDefinition id="DecimalValue" structureRef="xsd:decimal" />
  <bpmn:itemDefinition id="BooleanValue" structureRef="xsd:boolean" />
  <bpmn:itemDefinition id="AccountId" structureRef="xsd:string" />
  <bpmn:resource id="reviewer" name="Reviewer" />
  <bpmn:resource id="requester" name="Requester" />
  <bpmn:resource id="senior-reviewer" name="Senior reviewer" />
  <bpmn:process id="orderApprovalWithDeadline" name="Order approval with deadline" isExecutable="true">
    <bpmn:dataObject id="amount" itemSubjectRef="DecimalValue" />
    <bpmn:dataObject id="approved" itemSubjectRef="BooleanValue" />
    <bpmn:dataObject id="withdraw" itemSubjectRef="BooleanValue" />
    <bpmn:dataObject id="requesterId" itemSubjectRef="AccountId" />
    <bpmn:startEvent id="startOrder" name="Order submitted" />
    <bpmn:exclusiveGateway id="reviewEntry" name="Ready for review" gatewayDirection="Converging" />
    <bpmn:userTask id="reviewOrder" name="Review order">
      <bpmn:ioSpecification>
        <bpmn:dataInput id="reviewAmount" name="amount" itemSubjectRef="DecimalValue" />
        <bpmn:dataOutput id="reviewApproval" name="approved" itemSubjectRef="BooleanValue" />
        <bpmn:inputSet>
          <bpmn:dataInputRefs>reviewAmount</bpmn:dataInputRefs>
        </bpmn:inputSet>
        <bpmn:outputSet>
          <bpmn:dataOutputRefs>reviewApproval</bpmn:dataOutputRefs>
        </bpmn:outputSet>
      </bpmn:ioSpecification>
      <bpmn:dataInputAssociation>
        <bpmn:sourceRef>amount</bpmn:sourceRef>
        <bpmn:targetRef>reviewAmount</bpmn:targetRef>
      </bpmn:dataInputAssociation>
      <bpmn:dataOutputAssociation>
        <bpmn:sourceRef>reviewApproval</bpmn:sourceRef>
        <bpmn:targetRef>approved</bpmn:targetRef>
      </bpmn:dataOutputAssociation>
      <bpmn:potentialOwner>
        <bpmn:resourceRef>reviewer</bpmn:resourceRef>
      </bpmn:potentialOwner>
    </bpmn:userTask>
    <bpmn:boundaryEvent id="reviewDeadline" name="Review deadline" attachedToRef="reviewOrder" cancelActivity="true">
      <bpmn:timerEventDefinition>
        <bpmn:timeDuration xsi:type="bpmn:tFormalExpression" language="https://tokenjanitor.github.io/getriebe/languages/expression/v1">"PT48H"</bpmn:timeDuration>
      </bpmn:timerEventDefinition>
    </bpmn:boundaryEvent>
    <bpmn:userTask id="seniorReview" name="Senior review">
      <bpmn:ioSpecification>
        <bpmn:dataInput id="seniorAmount" name="amount" itemSubjectRef="DecimalValue" />
        <bpmn:dataOutput id="seniorApproval" name="approved" itemSubjectRef="BooleanValue" />
        <bpmn:inputSet><bpmn:dataInputRefs>seniorAmount</bpmn:dataInputRefs></bpmn:inputSet>
        <bpmn:outputSet><bpmn:dataOutputRefs>seniorApproval</bpmn:dataOutputRefs></bpmn:outputSet>
      </bpmn:ioSpecification>
      <bpmn:dataInputAssociation><bpmn:sourceRef>amount</bpmn:sourceRef><bpmn:targetRef>seniorAmount</bpmn:targetRef></bpmn:dataInputAssociation>
      <bpmn:dataOutputAssociation><bpmn:sourceRef>seniorApproval</bpmn:sourceRef><bpmn:targetRef>approved</bpmn:targetRef></bpmn:dataOutputAssociation>
      <bpmn:potentialOwner><bpmn:resourceRef>senior-reviewer</bpmn:resourceRef></bpmn:potentialOwner>
    </bpmn:userTask>
    <bpmn:exclusiveGateway id="reviewResult" name="Review result" gatewayDirection="Converging" />
    <bpmn:exclusiveGateway id="reviewChoice" name="Order approved?" gatewayDirection="Diverging" default="needsChanges" />
    <bpmn:userTask id="reviseOrder" name="Revise or withdraw">
      <bpmn:ioSpecification>
        <bpmn:dataInput id="previousAmount" name="amount" itemSubjectRef="DecimalValue" />
        <bpmn:dataOutput id="revisedAmount" name="amount" itemSubjectRef="DecimalValue" />
        <bpmn:dataOutput id="withdrawOrder" name="withdraw" itemSubjectRef="BooleanValue" />
        <bpmn:inputSet>
          <bpmn:dataInputRefs>previousAmount</bpmn:dataInputRefs>
        </bpmn:inputSet>
        <bpmn:outputSet>
          <bpmn:dataOutputRefs>revisedAmount</bpmn:dataOutputRefs>
          <bpmn:dataOutputRefs>withdrawOrder</bpmn:dataOutputRefs>
        </bpmn:outputSet>
      </bpmn:ioSpecification>
      <bpmn:dataInputAssociation>
        <bpmn:sourceRef>amount</bpmn:sourceRef>
        <bpmn:targetRef>previousAmount</bpmn:targetRef>
      </bpmn:dataInputAssociation>
      <bpmn:dataOutputAssociation>
        <bpmn:sourceRef>revisedAmount</bpmn:sourceRef>
        <bpmn:targetRef>amount</bpmn:targetRef>
      </bpmn:dataOutputAssociation>
      <bpmn:dataOutputAssociation>
        <bpmn:sourceRef>withdrawOrder</bpmn:sourceRef>
        <bpmn:targetRef>withdraw</bpmn:targetRef>
      </bpmn:dataOutputAssociation>
      <bpmn:humanPerformer>
        <bpmn:resourceAssignmentExpression>
          <bpmn:formalExpression>requesterId</bpmn:formalExpression>
        </bpmn:resourceAssignmentExpression>
      </bpmn:humanPerformer>
    </bpmn:userTask>
    <bpmn:exclusiveGateway id="reviewJoin" name="Review resolved" gatewayDirection="Converging" />
    <bpmn:exclusiveGateway id="repeatChoice" name="Review again?" gatewayDirection="Diverging" default="decideOutcome" />
    <bpmn:exclusiveGateway id="orderOutcome" name="Order outcome" gatewayDirection="Diverging" default="finishWithdrawn" />
    <bpmn:endEvent id="orderApproved" name="Order approved" />
    <bpmn:endEvent id="orderWithdrawn" name="Order withdrawn" />
    <bpmn:sequenceFlow id="submitOrder" sourceRef="startOrder" targetRef="reviewEntry" />
    <bpmn:sequenceFlow id="assignReview" sourceRef="reviewEntry" targetRef="reviewOrder" />
    <bpmn:sequenceFlow id="normalResult" sourceRef="reviewOrder" targetRef="reviewResult" />
    <bpmn:sequenceFlow id="escalateReview" sourceRef="reviewDeadline" targetRef="seniorReview" />
    <bpmn:sequenceFlow id="seniorResult" sourceRef="seniorReview" targetRef="reviewResult" />
    <bpmn:sequenceFlow id="decideReview" sourceRef="reviewResult" targetRef="reviewChoice" />
    <bpmn:sequenceFlow id="approvedReview" sourceRef="reviewChoice" targetRef="reviewJoin" name="Approved">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">approved</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="needsChanges" sourceRef="reviewChoice" targetRef="reviseOrder" name="Needs changes" />
    <bpmn:sequenceFlow id="finishRevision" sourceRef="reviseOrder" targetRef="reviewJoin" />
    <bpmn:sequenceFlow id="routeReview" sourceRef="reviewJoin" targetRef="repeatChoice" />
    <bpmn:sequenceFlow id="resubmitOrder" sourceRef="repeatChoice" targetRef="reviewEntry" name="Resubmit order">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">not(approved) and not(withdraw)</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="decideOutcome" sourceRef="repeatChoice" targetRef="orderOutcome" />
    <bpmn:sequenceFlow id="finishApproved" sourceRef="orderOutcome" targetRef="orderApproved" name="Approved">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">approved</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="finishWithdrawn" sourceRef="orderOutcome" targetRef="orderWithdrawn" name="Withdrawn" />
  </bpmn:process>
  <bpmndi:BPMNDiagram id="orderApprovalWithDeadlineDiagram">
    <bpmndi:BPMNPlane id="orderApprovalWithDeadlinePlane" bpmnElement="orderApprovalWithDeadline">
      <bpmndi:BPMNShape id="startOrderShape" bpmnElement="startOrder">
        <dc:Bounds x="70" y="160" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewEntryShape" bpmnElement="reviewEntry" isMarkerVisible="true">
        <dc:Bounds x="150" y="153" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewOrderShape" bpmnElement="reviewOrder">
        <dc:Bounds x="245" y="138" width="146" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewDeadlineShape" bpmnElement="reviewDeadline">
        <dc:Bounds x="331" y="200" width="36" height="36" />
        <bpmndi:BPMNLabel><dc:Bounds x="244" y="235" width="100" height="16" /></bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="seniorReviewShape" bpmnElement="seniorReview">
        <dc:Bounds x="268" y="286" width="162" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewResultShape" bpmnElement="reviewResult" isMarkerVisible="true">
        <dc:Bounds x="470" y="153" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="escalateReviewEdge" bpmnElement="escalateReview">
        <di:waypoint x="349" y="236" /><di:waypoint x="349" y="286" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="seniorResultEdge" bpmnElement="seniorResult">
        <di:waypoint x="430" y="326" /><di:waypoint x="495" y="326" /><di:waypoint x="495" y="203" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="reviewChoiceShape" bpmnElement="reviewChoice" isMarkerVisible="true">
        <dc:Bounds x="620" y="153" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviseOrderShape" bpmnElement="reviseOrder">
        <dc:Bounds x="745" y="266" width="160" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewJoinShape" bpmnElement="reviewJoin" isMarkerVisible="true">
        <dc:Bounds x="962" y="153" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="repeatChoiceShape" bpmnElement="repeatChoice" isMarkerVisible="true">
        <dc:Bounds x="1098" y="153" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="orderOutcomeShape" bpmnElement="orderOutcome" isMarkerVisible="true">
        <dc:Bounds x="1240" y="153" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="orderApprovedShape" bpmnElement="orderApproved">
        <dc:Bounds x="1370" y="119" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="orderWithdrawnShape" bpmnElement="orderWithdrawn">
        <dc:Bounds x="1370" y="246" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="submitOrderEdge" bpmnElement="submitOrder">
        <di:waypoint x="106" y="178" />
        <di:waypoint x="150" y="178" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="assignReviewEdge" bpmnElement="assignReview">
        <di:waypoint x="200" y="178" />
        <di:waypoint x="245" y="178" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="normalResultEdge" bpmnElement="normalResult">
        <di:waypoint x="391" y="178" /><di:waypoint x="470" y="178" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="decideReviewEdge" bpmnElement="decideReview">
        <di:waypoint x="520" y="178" /><di:waypoint x="620" y="178" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="approvedReviewEdge" bpmnElement="approvedReview">
        <di:waypoint x="670" y="178" />
        <di:waypoint x="962" y="178" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="780" y="155" width="70" height="16" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="needsChangesEdge" bpmnElement="needsChanges">
        <di:waypoint x="645" y="203" />
        <di:waypoint x="645" y="306" />
        <di:waypoint x="745" y="306" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="653" y="231" width="90" height="16" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="finishRevisionEdge" bpmnElement="finishRevision">
        <di:waypoint x="905" y="306" />
        <di:waypoint x="987" y="306" />
        <di:waypoint x="987" y="203" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="routeReviewEdge" bpmnElement="routeReview">
        <di:waypoint x="1012" y="178" />
        <di:waypoint x="1098" y="178" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="resubmitOrderEdge" bpmnElement="resubmitOrder">
        <di:waypoint x="1123" y="153" />
        <di:waypoint x="1123" y="78" />
        <di:waypoint x="175" y="78" />
        <di:waypoint x="175" y="153" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="690" y="54" width="100" height="16" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="decideOutcomeEdge" bpmnElement="decideOutcome">
        <di:waypoint x="1148" y="178" />
        <di:waypoint x="1240" y="178" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="finishApprovedEdge" bpmnElement="finishApproved">
        <di:waypoint x="1290" y="178" />
        <di:waypoint x="1330" y="178" />
        <di:waypoint x="1330" y="137" />
        <di:waypoint x="1370" y="137" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1306" y="99" width="64" height="16" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="finishWithdrawnEdge" bpmnElement="finishWithdrawn">
        <di:waypoint x="1265" y="203" />
        <di:waypoint x="1265" y="264" />
        <di:waypoint x="1370" y="264" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1280" y="240" width="70" height="16" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>
