Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
   * 
   * http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.simpleworkflow.flow.worker;
 
 import java.util.List;
 import java.util.Map;
 
 
 class DecisionsHelper {
 
     //    private static final Log log = LogFactory.getLog(DecisionsHelper.class);
 
     static final int MAXIMUM_DECISIONS_PER_COMPLETION = 100;
 
     static final String FORCE_IMMEDIATE_DECISION_TIMER = "FORCE_IMMEDIATE_DECISION";
 
     private final DecisionTask task;
 
     private long idCounter;
 
     private final Map<LongStringactivitySchedulingEventIdToActivityId = new HashMap<LongString>();
 
     private final Map<LongStringsignalInitiatedEventIdToSignalId = new HashMap<LongString>();

    
Use access-order to ensure that decisions are emitted in order of their creation
 
             0.75f, true);
 
     private Throwable workflowFailureCause;
 
     private String workflowContextData;
 
 
     DecisionsHelper(DecisionTask task) {
         this. = task;
     }
 
         DecisionId decisionId = new DecisionId(.schedule.getActivityId());
        addDecision(decisionIdnew ActivityDecisionStateMachine(decisionIdschedule));
    }

    

Returns:
true if cancellation already happened as schedule event was found in the new decisions list
    boolean requestCancelActivityTask(String activityIdRunnable immediateCancellationCallback) {
        DecisionStateMachine decision = getDecision(new DecisionId(.activityId));
        decision.cancel(immediateCancellationCallback);
        return decision.isDone();
    }
    boolean handleActivityTaskClosed(String activityId) {
        DecisionStateMachine decision = getDecision(new DecisionId(.activityId));
        decision.handleCompletionEvent();
        return decision.isDone();
    }
    boolean handleActivityTaskScheduled(HistoryEvent event) {
        String activityId = attributes.getActivityId();
        .put(event.getEventId(), activityId);
        DecisionStateMachine decision = getDecision(new DecisionId(.activityId));
        decision.handleInitiatedEvent(event);
        return decision.isDone();
    }
    public boolean handleScheduleActivityTaskFailed(HistoryEvent event) {
        String activityId = attributes.getActivityId();
        DecisionStateMachine decision = getDecision(new DecisionId(.activityId));
        decision.handleInitiationFailedEvent(event);
        return decision.isDone();
    }
        String activityId = attributes.getActivityId();
        DecisionStateMachine decision = getDecision(new DecisionId(.activityId));
        decision.handleCancellationInitiatedEvent();
        return decision.isDone();
    }
    public boolean handleActivityTaskCanceled(HistoryEvent event) {
        String activityId = getActivityId(attributes);
        DecisionStateMachine decision = getDecision(new DecisionId(.activityId));
        decision.handleCancellationEvent();
        return decision.isDone();
    }
        String activityId = attributes.getActivityId();
        DecisionStateMachine decision = getDecision(new DecisionId(.activityId));
        decision.handleCancellationFailureEvent(event);
        return decision.isDone();
    }
        DecisionId decisionId = new DecisionId(.schedule.getWorkflowId());
        addDecision(decisionIdnew ChildWorkflowDecisionStateMachine(decisionIdschedule));
    }
        String workflowId = attributes.getWorkflowId();
        DecisionStateMachine decision = getDecision(new DecisionId(.workflowId));
        decision.handleInitiatedEvent(event);
    }
    public boolean handleStartChildWorkflowExecutionFailed(HistoryEvent event) {
        String workflowId = attributes.getWorkflowId();
        DecisionStateMachine decision = getDecision(new DecisionId(.workflowId));
        decision.handleInitiationFailedEvent(event);
        return decision.isDone();
    }

    

Returns:
true if cancellation already happened as schedule event was found in the new decisions list
    boolean requestCancelExternalWorkflowExecution(boolean childWorkflow,
            RequestCancelExternalWorkflowExecutionDecisionAttributes requestRunnable immediateCancellationCallback) {
        decision.cancel(immediateCancellationCallback);
        return decision.isDone();
    }
        String workflowId = attributes.getWorkflowId();
        DecisionStateMachine decision = getDecision(new DecisionId(.workflowId));
        decision.handleCancellationInitiatedEvent();
    }
        decision.handleCancellationFailureEvent(event);
    }
        DecisionId decisionId = new DecisionId(.signal.getControl());
        addDecision(decisionIdnew SignalDecisionStateMachine(decisionIdsignal));
    }
    void cancelSignalExternalWorkflowExecution(String signalIdRunnable immediateCancellationCallback) {
        DecisionStateMachine decision = getDecision(new DecisionId(.signalId));
        decision.cancel(immediateCancellationCallback);
    }
        String signalId = attributes.getControl();
        .put(event.getEventId(), signalId);
        DecisionStateMachine decision = getDecision(new DecisionId(.signalId));
        decision.handleInitiatedEvent(event);
    }
    public boolean handleSignalExternalWorkflowExecutionFailed(String signalId) {
        DecisionStateMachine decision = getDecision(new DecisionId(.signalId));
        decision.handleCompletionEvent();
        return decision.isDone();
    }
    public boolean handleExternalWorkflowExecutionSignaled(String signalId) {
        DecisionStateMachine decision = getDecision(new DecisionId(.signalId));
        decision.handleCompletionEvent();
        return decision.isDone();
    }
    void startTimer(StartTimerDecisionAttributes requestObject createTimerUserContext) {
        String timerId = request.getTimerId();
        DecisionId decisionId = new DecisionId(.timerId);
        addDecision(decisionIdnew TimerDecisionStateMachine(decisionIdrequest));
    }
    boolean cancelTimer(String timerIdRunnable immediateCancellationCallback) {
        DecisionStateMachine decision = getDecision(new DecisionId(.timerId));
        decision.cancel(immediateCancellationCallback);
        return decision.isDone();
    }
    public void handleChildWorkflowExecutionStarted(HistoryEvent event) {
        String workflowId = attributes.getWorkflowExecution().getWorkflowId();
        DecisionStateMachine decision = getDecision(new DecisionId(.workflowId));
        decision.handleStartedEvent(event);
    }
    boolean handleChildWorkflowExecutionClosed(String workflowId) {
        DecisionStateMachine decision = getDecision(new DecisionId(.workflowId));
        decision.handleCompletionEvent();
        return decision.isDone();
    }
    }
    public boolean handleChildWorkflowExecutionCanceled(String workflowId) {
        DecisionStateMachine decision = getDecision(new DecisionId(.workflowId));
        decision.handleCancellationEvent();
        return decision.isDone();
    }
    boolean handleTimerClosed(String timerId) {
        DecisionStateMachine decision = getDecision(new DecisionId(.timerId));
        decision.handleCompletionEvent();
        return decision.isDone();
    }
    boolean handleTimerStarted(HistoryEvent event) {
        TimerStartedEventAttributes attributes = event.getTimerStartedEventAttributes();
        DecisionStateMachine decision = getDecision(new DecisionId(.attributes.getTimerId()));
        decision.handleInitiatedEvent(event);
        return decision.isDone();
    }
    public boolean handleStartTimerFailed(HistoryEvent event) {
        DecisionStateMachine decision = getDecision(new DecisionId(.attributes.getTimerId()));
        decision.handleInitiationFailedEvent(event);
        return decision.isDone();
    }
    boolean handleTimerCanceled(HistoryEvent event) {
        TimerCanceledEventAttributes attributes = event.getTimerCanceledEventAttributes();
        DecisionStateMachine decision = getDecision(new DecisionId(.attributes.getTimerId()));
        decision.handleCancellationEvent();
        return decision.isDone();
    }
    boolean handleCancelTimerFailed(HistoryEvent event) {
        DecisionStateMachine decision = getDecision(new DecisionId(.attributes.getTimerId()));
        decision.handleCancellationFailureEvent(event);
        return decision.isDone();
    }
    void completeWorkflowExecution(String output) {
        Decision decision = new Decision();
        complete.setResult(output);
        decision.setCompleteWorkflowExecutionDecisionAttributes(complete);
        DecisionId decisionId = new DecisionId(.null);
        addDecision(decisionIdnew CompleteWorkflowStateMachine(decisionIddecision));
    }
        attributes.setWorkflowTypeVersion(continueParameters.getWorkflowTypeVersion());
        ChildPolicy childPolicy = continueParameters.getChildPolicy();
        if (childPolicy != null) {
            attributes.setChildPolicy(childPolicy);
        }
        attributes.setInput(continueParameters.getInput());
        attributes.setExecutionStartToCloseTimeout(FlowHelpers.secondsToDuration(continueParameters.getExecutionStartToCloseTimeoutSeconds()));
        attributes.setTaskStartToCloseTimeout(FlowHelpers.secondsToDuration(continueParameters.getTaskStartToCloseTimeoutSeconds()));
        attributes.setTaskPriority(FlowHelpers.taskPriorityToString(continueParameters.getTaskPriority()));
        List<StringtagList = continueParameters.getTagList();
        if (tagList != null) {
            attributes.setTagList(tagList);
        }
        String taskList = continueParameters.getTaskList();
        if (taskList != null && !taskList.isEmpty()) {
            attributes.setTaskList(new TaskList().withName(taskList));
        }
        Decision decision = new Decision();
        decision.setContinueAsNewWorkflowExecutionDecisionAttributes(attributes);
        DecisionId decisionId = new DecisionId(.null);
        addDecision(decisionIdnew CompleteWorkflowStateMachine(decisionIddecision));
    }
    void failWorkflowExecution(Throwable e) {
        Decision decision = new Decision();
        decision.setFailWorkflowExecutionDecisionAttributes(fail);
        DecisionId decisionId = new DecisionId(.null);
        addDecision(decisionIdnew CompleteWorkflowStateMachine(decisionIddecision));
         = e;
    }
        // To make sure that failure goes through do not make any other decisions
        .clear();
        this.failWorkflowExecution(e);
    }
        DecisionStateMachine decision = getDecision(new DecisionId(.null));
        decision.handleInitiationFailedEvent(event);
    }
        DecisionStateMachine decision = getDecision(new DecisionId(.null));
        decision.handleInitiationFailedEvent(event);
    }
        DecisionStateMachine decision = getDecision(new DecisionId(.null));
        decision.handleInitiationFailedEvent(event);
    }
        DecisionStateMachine decision = getDecision(new DecisionId(.null));
        decision.handleInitiationFailedEvent(event);
    }

    

Returns:
false means that cancel failed, true that CancelWorkflowExecution was created.
    void cancelWorkflowExecution() {
        Decision decision = new Decision();
        cancel.setDetails(null);
        decision.setCancelWorkflowExecutionDecisionAttributes(cancel);
        DecisionId decisionId = new DecisionId(.null);
        addDecision(decisionIdnew CompleteWorkflowStateMachine(decisionIddecision));
    }
        List<Decisionresult = new ArrayList<Decision>( + 1);
        for (DecisionStateMachine decisionStateMachine : .values()) {
            Decision decision = decisionStateMachine.getDecision();
            if (decision != null) {
                result.add(decision);
            }
        }
        // Include FORCE_IMMEDIATE_DECISION timer only if there are more then 100 events
        int size = result.size();
        if (size >  && !isCompletionEvent(result.get( - 2))) {
            result = result.subList(0,  - 1);
            StartTimerDecisionAttributes attributes = new StartTimerDecisionAttributes();
            attributes.setStartToFireTimeout("0");
            attributes.setTimerId();
            Decision d = new Decision();
            d.setStartTimerDecisionAttributes(attributes);
            d.setDecisionType(..toString());
            result.add(d);
        }
        return result;
    }
    private boolean isCompletionEvent(Decision decision) {
        DecisionType type = DecisionType.fromValue(decision.getDecisionType());
        switch (type) {
        case :
        case :
        case :
        case :
            return true;
        default:
            return false;
        }
    }
    public void handleDecisionTaskStartedEvent() {
        int count = 0;
        Iterator<DecisionStateMachineiterator = .values().iterator();
        DecisionStateMachine next = null;
        DecisionStateMachine decisionStateMachine = getNextDecision(iterator);
        while (decisionStateMachine != null) {
            next = getNextDecision(iterator);
            if (++count ==  && next != null && !isCompletionEvent(next.getDecision())) {
                break;
            }
            decisionStateMachine.handleDecisionTaskStartedEvent();
            decisionStateMachine = next;
        }
        if (next != null && count < ) {
            next.handleDecisionTaskStartedEvent();
        }
    }
        DecisionStateMachine result = null;
        while (result == null && iterator.hasNext()) {
            result = iterator.next();
            if (result.getDecision() == null) {
                result = null;
            }
        }
        return result;
    }
    public String toString() {
        return WorkflowExecutionUtils.prettyPrintDecisions(getDecisions());
    }
    boolean isWorkflowFailed() {
        return  != null;
    }
        return ;
    }
        return ;
    }
    void setWorkflowContextData(String workflowState) {
        this. = workflowState;
    }

    

Returns:
new workflow state or null if it didn't change since the last decision completion
        if ( == null
            return ;
        }
        return null;
    }
    void handleDecisionCompletion(DecisionTaskCompletedEventAttributes decisionTaskCompletedEventAttributes) {
         = decisionTaskCompletedEventAttributes.getExecutionContext();
    }
    DecisionTask getTask() {
        return ;
    }
        Long sourceId = attributes.getScheduledEventId();
        return .get(sourceId);
    }
        Long sourceId = attributes.getScheduledEventId();
        return .get(sourceId);
    }
        Long sourceId = attributes.getScheduledEventId();
        return .get(sourceId);
    }
        Long sourceId = attributes.getScheduledEventId();
        return .get(sourceId);
    }
        return .get(initiatedEventId);
    }
        String reason;
        String details;
        if (failure instanceof WorkflowException) {
            WorkflowException f = (WorkflowExceptionfailure;
            reason = f.getReason();
            details = f.getDetails();
        }
        else {
            reason = failure.getMessage();
            StringWriter sw = new StringWriter();
            failure.printStackTrace(new PrintWriter(sw));
            details = sw.toString();
        }
        result.setReason(WorkflowExecutionUtils.truncateReason(reason));
        result.setDetails(WorkflowExecutionUtils.truncateDetails(details));
        return result;
    }
    void addDecision(DecisionId decisionIdDecisionStateMachine decision) {
        .put(decisionIddecision);
    }
    private DecisionStateMachine getDecision(DecisionId decisionId) {
        DecisionStateMachine result = .get(decisionId);
        if (result == null) {
            throw new IllegalArgumentException("Unknown " + decisionId + ". The possible causes are "
                    + "nondeterministic workflow definition code or incompatible change in the workflow definition.");
        }
        return result;
    }
    public String getNextId() {
        return String.valueOf(++);
    }
New to GrepCode? Check out our FAQ X