Search in sources :

Example 1 with LifeCyclePhase

use of org.kie.kogito.process.workitem.LifeCyclePhase in project kogito-runtimes by kiegroup.

the class BaseHumanTaskLifeCycle method transitionTo.

@Override
public Map<String, Object> transitionTo(KogitoWorkItem workItem, KogitoWorkItemManager manager, Transition<Map<String, Object>> transition) {
    logger.debug("Transition method invoked for work item {} to transition to {}, currently in phase {} and status {}", workItem.getStringId(), transition.phase(), workItem.getPhaseId(), workItem.getPhaseStatus());
    HumanTaskWorkItemImpl humanTaskWorkItem = (HumanTaskWorkItemImpl) workItem;
    LifeCyclePhase targetPhase = phases.get(transition.phase());
    if (targetPhase == null) {
        logger.debug("Target life cycle phase '{}' does not exist in {}", transition.phase(), this.getClass().getSimpleName());
        throw new InvalidLifeCyclePhaseException(transition.phase());
    }
    LifeCyclePhase currentPhase = phases.get(humanTaskWorkItem.getPhaseId());
    if (!targetPhase.canTransition(currentPhase)) {
        logger.debug("Target life cycle phase '{}' cannot transition from current state '{}'", targetPhase.id(), currentPhase.id());
        throw new InvalidTransitionException("Cannot transition from " + humanTaskWorkItem.getPhaseId() + " to " + targetPhase.id());
    }
    if (!targetPhase.id().equals(Active.ID) && !targetPhase.id().equals(Abort.ID) && !humanTaskWorkItem.enforce(transition.policies().toArray(new Policy[transition.policies().size()]))) {
        throw new NotAuthorizedException("User is not authorized to access task instance with id " + humanTaskWorkItem.getStringId());
    }
    humanTaskWorkItem.setPhaseId(targetPhase.id());
    humanTaskWorkItem.setPhaseStatus(targetPhase.status());
    targetPhase.apply(humanTaskWorkItem, transition);
    if (transition.data() != null) {
        logger.debug("Updating data for phase {} and work item {}", targetPhase.id(), humanTaskWorkItem.getStringId());
        humanTaskWorkItem.getResults().putAll(transition.data());
    }
    logger.debug("Transition for work item {} to {} done, currently in phase {} and status {}", workItem.getStringId(), transition.phase(), workItem.getPhaseId(), workItem.getPhaseStatus());
    if (targetPhase.isTerminating()) {
        logger.debug("Target life cycle phase '{}' is terminiating, completing work item {}", targetPhase.id(), humanTaskWorkItem.getStringId());
        // since target life cycle phase is terminating completing work item
        ((InternalKogitoWorkItemManager) manager).internalCompleteWorkItem(humanTaskWorkItem);
    }
    return data(humanTaskWorkItem);
}
Also used : InvalidTransitionException(org.kie.kogito.process.workitem.InvalidTransitionException) Policy(org.kie.kogito.process.workitem.Policy) InvalidLifeCyclePhaseException(org.kie.kogito.process.workitem.InvalidLifeCyclePhaseException) InternalKogitoWorkItemManager(org.kie.kogito.process.workitems.InternalKogitoWorkItemManager) NotAuthorizedException(org.kie.kogito.process.workitem.NotAuthorizedException) LifeCyclePhase(org.kie.kogito.process.workitem.LifeCyclePhase)

Example 2 with LifeCyclePhase

use of org.kie.kogito.process.workitem.LifeCyclePhase in project kogito-runtimes by kiegroup.

the class PredictionAwareHumanTaskLifeCycle method transitionTo.

@Override
public Map<String, Object> transitionTo(KogitoWorkItem workItem, KogitoWorkItemManager manager, Transition<Map<String, Object>> transition) {
    LifeCyclePhase targetPhase = phaseById(transition.phase());
    if (targetPhase == null) {
        logger.debug("Target life cycle phase '{}' does not exist in {}", transition.phase(), this.getClass().getSimpleName());
        throw new InvalidLifeCyclePhaseException(transition.phase());
    }
    HumanTaskWorkItemImpl humanTaskWorkItem = (HumanTaskWorkItemImpl) workItem;
    if (targetPhase.id().equals(Active.ID)) {
        PredictionOutcome outcome = predictionService.predict(workItem, workItem.getParameters());
        logger.debug("Prediction service returned confidence level {} for work item {}", outcome.getConfidenceLevel(), humanTaskWorkItem.getStringId());
        if (outcome.isCertain()) {
            humanTaskWorkItem.getResults().putAll(outcome.getData());
            logger.debug("Prediction service is certain (confidence level {}) on the outputs, completing work item {}", outcome.getConfidenceLevel(), humanTaskWorkItem.getStringId());
            ((InternalKogitoWorkItemManager) manager).internalCompleteWorkItem(humanTaskWorkItem);
            return outcome.getData();
        } else if (outcome.isPresent()) {
            logger.debug("Prediction service is NOT certain (confidence level {}) on the outputs, setting recommended outputs on work item {}", outcome.getConfidenceLevel(), humanTaskWorkItem.getStringId());
            humanTaskWorkItem.getResults().putAll(outcome.getData());
        }
    }
    // prediction service does work only on activating tasks
    Map<String, Object> data = super.transitionTo(workItem, manager, transition);
    if (targetPhase.id().equals(Complete.ID)) {
        // upon actual transition train the data if it's completion phase
        predictionService.train(humanTaskWorkItem, workItem.getParameters(), data);
    }
    return data;
}
Also used : InvalidLifeCyclePhaseException(org.kie.kogito.process.workitem.InvalidLifeCyclePhaseException) InternalKogitoWorkItemManager(org.kie.kogito.process.workitems.InternalKogitoWorkItemManager) HumanTaskWorkItemImpl(org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemImpl) LifeCyclePhase(org.kie.kogito.process.workitem.LifeCyclePhase)

Aggregations

InvalidLifeCyclePhaseException (org.kie.kogito.process.workitem.InvalidLifeCyclePhaseException)2 LifeCyclePhase (org.kie.kogito.process.workitem.LifeCyclePhase)2 InternalKogitoWorkItemManager (org.kie.kogito.process.workitems.InternalKogitoWorkItemManager)2 HumanTaskWorkItemImpl (org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemImpl)1 InvalidTransitionException (org.kie.kogito.process.workitem.InvalidTransitionException)1 NotAuthorizedException (org.kie.kogito.process.workitem.NotAuthorizedException)1 Policy (org.kie.kogito.process.workitem.Policy)1