<?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"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  targetNamespace="urn:getriebe:sequential-order-approval"
  expressionLanguage="https://tokenjanitor.github.io/getriebe/languages/expression/v1">
  <bpmn:itemDefinition id="OrderPositions" structureRef="xsd:string" isCollection="true" />
  <bpmn:itemDefinition id="ReviewVotes" structureRef="xsd:boolean" isCollection="true" />
  <bpmn:itemDefinition id="OrderPosition" structureRef="xsd:string" />
  <bpmn:itemDefinition id="ReviewVote" structureRef="xsd:boolean" />
  <bpmn:itemDefinition id="OrderAmount" structureRef="xsd:decimal" />
  <bpmn:resource id="purchasing-reviewer" name="Purchasing" />
  <bpmn:resource id="requester" name="Requester" />
  <bpmn:process id="sequentialOrderApproval" name="Order approval with sequential position reviews" isExecutable="true">
    <bpmn:dataObject id="orderPositions" itemSubjectRef="OrderPositions" isCollection="true" />
    <bpmn:dataObject id="reviewVotes" itemSubjectRef="ReviewVotes" isCollection="true" />
    <bpmn:dataObject id="orderNumber" itemSubjectRef="OrderPosition" />
    <bpmn:dataObject id="currency" itemSubjectRef="OrderPosition" />
    <bpmn:dataObject id="amount" itemSubjectRef="OrderAmount" />
    <bpmn:startEvent id="submitted" name="Order submitted" />
    <bpmn:exclusiveGateway id="reviewEntry" gatewayDirection="Converging" />
    <bpmn:userTask id="reviewPosition" name="Review order position">
      <bpmn:ioSpecification>
        <bpmn:dataInput id="orderPositionsInput" name="orderPositions" itemSubjectRef="OrderPositions" isCollection="true" />
        <bpmn:dataInput id="reviewAmount" name="amount" itemSubjectRef="OrderAmount" />
        <bpmn:dataInput id="reviewOrderNumber" name="orderNumber" itemSubjectRef="OrderPosition" />
        <bpmn:dataInput id="reviewCurrency" name="currency" itemSubjectRef="OrderPosition" />
        <bpmn:dataOutput id="reviewVotesOutput" name="reviewVotes" itemSubjectRef="ReviewVotes" isCollection="true" />
        <bpmn:inputSet>
          <bpmn:dataInputRefs>orderPositionsInput</bpmn:dataInputRefs>
          <bpmn:dataInputRefs>reviewAmount</bpmn:dataInputRefs>
          <bpmn:dataInputRefs>reviewOrderNumber</bpmn:dataInputRefs>
          <bpmn:dataInputRefs>reviewCurrency</bpmn:dataInputRefs>
        </bpmn:inputSet>
        <bpmn:outputSet>
          <bpmn:dataOutputRefs>reviewVotesOutput</bpmn:dataOutputRefs>
        </bpmn:outputSet>
      </bpmn:ioSpecification>
      <bpmn:dataInputAssociation>
        <bpmn:sourceRef>orderPositions</bpmn:sourceRef>
        <bpmn:targetRef>orderPositionsInput</bpmn:targetRef>
      </bpmn:dataInputAssociation>
      <bpmn:dataInputAssociation>
        <bpmn:sourceRef>amount</bpmn:sourceRef>
        <bpmn:targetRef>reviewAmount</bpmn:targetRef>
      </bpmn:dataInputAssociation>
      <bpmn:dataInputAssociation>
        <bpmn:sourceRef>orderNumber</bpmn:sourceRef>
        <bpmn:targetRef>reviewOrderNumber</bpmn:targetRef>
      </bpmn:dataInputAssociation>
      <bpmn:dataInputAssociation>
        <bpmn:sourceRef>currency</bpmn:sourceRef>
        <bpmn:targetRef>reviewCurrency</bpmn:targetRef>
      </bpmn:dataInputAssociation>
      <bpmn:dataOutputAssociation>
        <bpmn:sourceRef>reviewVotesOutput</bpmn:sourceRef>
        <bpmn:targetRef>reviewVotes</bpmn:targetRef>
      </bpmn:dataOutputAssociation>
      <bpmn:potentialOwner>
        <bpmn:resourceRef>purchasing-reviewer</bpmn:resourceRef>
      </bpmn:potentialOwner>
      <bpmn:multiInstanceLoopCharacteristics isSequential="true" behavior="All">
        <bpmn:loopDataInputRef>orderPositionsInput</bpmn:loopDataInputRef>
        <bpmn:loopDataOutputRef>reviewVotesOutput</bpmn:loopDataOutputRef>
        <bpmn:inputDataItem id="localOrderPosition" name="position" itemSubjectRef="OrderPosition" />
        <bpmn:outputDataItem id="localReviewVote" name="reviewVote" itemSubjectRef="ReviewVote" />
      </bpmn:multiInstanceLoopCharacteristics>
    </bpmn:userTask>
    <bpmn:exclusiveGateway id="reviewDecision" name="All positions accepted?" gatewayDirection="Diverging" default="requiresRevision" />
    <bpmn:userTask id="reviseOrder" name="Revise order">
      <bpmn:ioSpecification>
        <bpmn:dataInput id="previousAmount" name="amount" itemSubjectRef="OrderAmount" />
        <bpmn:dataOutput id="revisedAmount" name="amount" itemSubjectRef="OrderAmount" />
        <bpmn:inputSet><bpmn:dataInputRefs>previousAmount</bpmn:dataInputRefs></bpmn:inputSet>
        <bpmn:outputSet><bpmn:dataOutputRefs>revisedAmount</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:potentialOwner><bpmn:resourceRef>requester</bpmn:resourceRef></bpmn:potentialOwner>
    </bpmn:userTask>
    <bpmn:endEvent id="orderApproved" name="Order approved" />
    <bpmn:sequenceFlow id="initialSubmission" sourceRef="submitted" targetRef="reviewEntry" />
    <bpmn:sequenceFlow id="beginReview" sourceRef="reviewEntry" targetRef="reviewPosition" />
    <bpmn:sequenceFlow id="decideReview" sourceRef="reviewPosition" targetRef="reviewDecision" />
    <bpmn:sequenceFlow id="allApproved" sourceRef="reviewDecision" targetRef="orderApproved">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">all(reviewVotes)</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="requiresRevision" sourceRef="reviewDecision" targetRef="reviseOrder" />
    <bpmn:sequenceFlow id="resubmit" name="Resubmit" sourceRef="reviseOrder" targetRef="reviewEntry" />
  </bpmn:process>
  <bpmndi:BPMNDiagram id="sequentialOrderApprovalDiagram">
    <bpmndi:BPMNPlane id="sequentialOrderApprovalPlane" bpmnElement="sequentialOrderApproval">
      <bpmndi:BPMNShape id="submittedShape" bpmnElement="submitted"><dc:Bounds x="80" y="142" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewEntryShape" bpmnElement="reviewEntry" isMarkerVisible="true"><dc:Bounds x="165" y="135" width="50" height="50" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewPositionShape" bpmnElement="reviewPosition"><dc:Bounds x="265" y="120" width="144" height="80" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewDecisionShape" bpmnElement="reviewDecision" isMarkerVisible="true"><dc:Bounds x="455" y="135" width="50" height="50" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="orderApprovedShape" bpmnElement="orderApproved"><dc:Bounds x="585" y="142" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviseOrderShape" bpmnElement="reviseOrder"><dc:Bounds x="267" y="245" width="140" height="80" /></bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="initialSubmissionEdge" bpmnElement="initialSubmission"><di:waypoint x="116" y="160" /><di:waypoint x="165" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="beginReviewEdge" bpmnElement="beginReview"><di:waypoint x="215" y="160" /><di:waypoint x="265" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="decideReviewEdge" bpmnElement="decideReview"><di:waypoint x="409" y="160" /><di:waypoint x="455" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="allApprovedEdge" bpmnElement="allApproved"><di:waypoint x="505" y="160" /><di:waypoint x="585" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="requiresRevisionEdge" bpmnElement="requiresRevision"><di:waypoint x="480" y="185" /><di:waypoint x="480" y="285" /><di:waypoint x="407" y="285" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="resubmitEdge" bpmnElement="resubmit"><di:waypoint x="267" y="285" /><di:waypoint x="190" y="285" /><di:waypoint x="190" y="185" /></bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>
