Example usage for org.apache.commons.lang3.mutable Mutable setValue

List of usage examples for org.apache.commons.lang3.mutable Mutable setValue

Introduction

In this page you can find the example usage for org.apache.commons.lang3.mutable Mutable setValue.

Prototype

void setValue(T value);

Source Link

Document

Sets the value of this mutable.

Usage

From source file:org.apache.vxquery.compiler.rewriter.rules.IntroduceCollectionRule.java

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    VXQueryOptimizationContext vxqueryContext = (VXQueryOptimizationContext) context;
    String collectionName = getCollectionName(opRef);

    if (collectionName != null) {
        // Build the new operator and update the query plan.
        int collectionId = vxqueryContext.newCollectionId();
        VXQueryCollectionDataSource ds = VXQueryCollectionDataSource.create(collectionId, collectionName,
                SequenceType.create(AnyItemType.INSTANCE, Quantifier.QUANT_STAR));
        if (ds != null) {
            ds.setTotalDataSources(vxqueryContext.getTotalDataSources());

            // Known to be true because of collection name.
            AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
            UnnestOperator unnest = (UnnestOperator) op;
            Mutable<ILogicalOperator> opRef2 = unnest.getInputs().get(0);
            AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
            AssignOperator assign = (AssignOperator) op2;

            DataSourceScanOperator opNew = new DataSourceScanOperator(assign.getVariables(), ds);
            opNew.getInputs().addAll(assign.getInputs());
            opRef2.setValue(opNew);
            return true;
        }/*w w  w . jav  a2s .c  o  m*/
    }
    return false;
}

From source file:org.apache.vxquery.compiler.rewriter.rules.PushAggregateIntoGroupbyRule.java

private boolean collectVarsBottomUp(Mutable<ILogicalOperator> opRef, IOptimizationContext context,
        Map<LogicalVariable, Integer> gbyListifyVarsCount, Map<LogicalVariable, GroupByOperator> gbyWithAgg,
        Map<LogicalVariable, Integer> gbyAggVarToPlanIndex,
        Map<ILogicalExpression, ILogicalExpression> aggregateExprToVarExpr) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    context.addToDontApplySet(this, op1);
    boolean change = false;
    for (Mutable<ILogicalOperator> child : op1.getInputs()) {
        if (collectVarsBottomUp(child, context, gbyListifyVarsCount, gbyWithAgg, gbyAggVarToPlanIndex,
                aggregateExprToVarExpr)) {
            change = true;//from w w  w .  j ava 2 s. c  o  m
        }
    }
    Set<LogicalVariable> used = new HashSet<>();
    VariableUtilities.getUsedVariables(op1, used);
    switch (op1.getOperatorTag()) {
    case ASSIGN:
    case SELECT: {
        boolean found = false;
        // Do some prefiltering: check if the Assign uses any gby vars.
        for (LogicalVariable v : used) {
            if (gbyListifyVarsCount.get(v) != null) {
                found = true;
                break;
            }
        }
        if (found) {
            if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
                AssignOperator assign = (AssignOperator) op1;
                for (Mutable<ILogicalExpression> exprRef : assign.getExpressions()) {
                    Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, gbyWithAgg,
                            aggregateExprToVarExpr, context);
                    if (p.first) {
                        change = true;
                        exprRef.setValue(p.second);
                    }
                }
            }
            if (op1.getOperatorTag() == LogicalOperatorTag.SELECT) {
                SelectOperator select = (SelectOperator) op1;
                Mutable<ILogicalExpression> exprRef = select.getCondition();
                Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, gbyWithAgg,
                        aggregateExprToVarExpr, context);
                if (p.first) {
                    change = true;
                    exprRef.setValue(p.second);
                }
            }
            used.clear();
            VariableUtilities.getUsedVariables(op1, used);
            // increment the count for the ones which are still used
            for (LogicalVariable v : used) {
                Integer m = gbyListifyVarsCount.get(v);
                if (m != null) {
                    gbyListifyVarsCount.put(v, m + 1);
                }
            }
        }
        break;
    }
    case SUBPLAN: {
        for (LogicalVariable v : used) {
            Integer m = gbyListifyVarsCount.get(v);
            if (m != null) {
                GroupByOperator gbyOp = gbyWithAgg.get(v);
                if (pushSubplanAsAggIntoGby(opRef, gbyOp, v, gbyListifyVarsCount, gbyWithAgg,
                        gbyAggVarToPlanIndex, context)) {
                    change = true;
                } else {
                    gbyListifyVarsCount.put(v, m + 1);
                }
            }
        }
        break;
    }
    case GROUP: {
        List<LogicalVariable> vars = collectOneVarPerAggFromGroupOp((GroupByOperator) op1);
        if (vars != null) {
            for (int i = 0; i < vars.size(); i++) {
                LogicalVariable v = vars.get(i);
                if (v != null) {
                    gbyListifyVarsCount.put(v, 0);
                    gbyAggVarToPlanIndex.put(v, i);
                    gbyWithAgg.put(v, (GroupByOperator) op1);
                }
            }
        }
        break;
    }
    default: {
        for (LogicalVariable v : used) {
            Integer m = gbyListifyVarsCount.get(v);
            if (m != null) {
                gbyListifyVarsCount.put(v, m + 1);
            }
        }
    }
    }
    return change;
}

From source file:org.apache.vxquery.compiler.rewriter.rules.PushAggregateIntoGroupbyRule.java

/**
 * @param expr/*  w w  w .j a v  a 2  s.c om*/
 * @param aggVars
 * @param gbyWithAgg
 * @param context
 * @return a pair whose first member is a boolean which is true iff
 *         something was changed in the expression tree rooted at expr. The
 *         second member is the result of transforming expr.
 * @throws AlgebricksException
 */

private Pair<Boolean, ILogicalExpression> extractAggFunctionsFromExpression(Mutable<ILogicalExpression> exprRef,
        Map<LogicalVariable, GroupByOperator> gbyWithAgg,
        Map<ILogicalExpression, ILogicalExpression> aggregateExprToVarExpr, IOptimizationContext context)
        throws AlgebricksException {
    ILogicalExpression expr = exprRef.getValue();
    switch (expr.getExpressionTag()) {
    case FUNCTION_CALL: {
        AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expr;
        Function functionInfo = (Function) fce.getFunctionInfo();
        FunctionIdentifier fi = null;
        if (functionInfo.hasAggregateEvaluatorFactory()) {
            fi = functionInfo.getFunctionIdentifier();
        } //FunctionIdentifier fi = functionInfo.getFunctionIdentifier();
        if (fi != null) {
            ILogicalExpression a1 = fce.getArguments().get(0).getValue();
            if (a1.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
                LogicalVariable argVar = ((VariableReferenceExpression) a1).getVariableReference();
                GroupByOperator gbyOp = gbyWithAgg.get(argVar);

                if (gbyOp != null) {
                    if (!aggregateExprToVarExpr.containsKey(expr)) {
                        LogicalVariable newVar = context.newVar();
                        AggregateFunctionCallExpression aggFun = new AggregateFunctionCallExpression(
                                functionInfo, false, fce.getArguments());
                        rewriteGroupByAggregate(argVar, gbyOp, aggFun, newVar, context);
                        ILogicalExpression newVarExpr = new VariableReferenceExpression(newVar);
                        aggregateExprToVarExpr.put(expr, newVarExpr);
                        return new Pair<Boolean, ILogicalExpression>(Boolean.TRUE, newVarExpr);
                    } else {
                        ILogicalExpression varExpr = aggregateExprToVarExpr.get(expr);
                        return new Pair<Boolean, ILogicalExpression>(Boolean.TRUE, varExpr);
                    }
                }
            }
        }

        boolean change = false;
        for (Mutable<ILogicalExpression> a : fce.getArguments()) {
            Pair<Boolean, ILogicalExpression> aggArg = extractAggFunctionsFromExpression(a, gbyWithAgg,
                    aggregateExprToVarExpr, context);
            if (aggArg.first.booleanValue()) {
                a.setValue(aggArg.second);
                change = true;
            }
        }
        return new Pair<Boolean, ILogicalExpression>(change, fce);

    }
    case VARIABLE:
    case CONSTANT: {
        return new Pair<Boolean, ILogicalExpression>(Boolean.FALSE, expr);
    }
    default: {
        throw new IllegalArgumentException();
    }
    }
}

From source file:org.apache.vxquery.compiler.rewriter.rules.PushAggregateIntoGroupbyRule.java

private boolean pushSubplanAsAggIntoGby(Mutable<ILogicalOperator> subplanOpRef, GroupByOperator gbyOp,
        LogicalVariable varFromGroupAgg, Map<LogicalVariable, Integer> gbyAggVars,
        Map<LogicalVariable, GroupByOperator> gbyWithAgg, Map<LogicalVariable, Integer> gbyAggVarToPlanIndex,
        IOptimizationContext context) throws AlgebricksException {
    SubplanOperator subplan = (SubplanOperator) subplanOpRef.getValue();
    // only free var can be varFromGroupAgg
    HashSet<LogicalVariable> freeVars = new HashSet<LogicalVariable>();
    OperatorPropertiesUtil.getFreeVariablesInSubplans(subplan, freeVars);
    for (LogicalVariable vFree : freeVars) {
        if (!vFree.equals(varFromGroupAgg)) {
            return false;
        }/*from  w  w  w.j  ava2s  . co  m*/
    }

    List<ILogicalPlan> plans = subplan.getNestedPlans();
    if (plans.size() > 1) {
        return false;
    }
    ILogicalPlan p = plans.get(0);
    if (p.getRoots().size() > 1) {
        return false;
    }
    Mutable<ILogicalOperator> opRef = p.getRoots().get(0);
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    AggregateOperator aggInSubplanOp = (AggregateOperator) op;
    LogicalVariable unnestVar = null;
    boolean pushableNestedSubplan = false;
    while (op.getInputs().size() == 1) {
        opRef = op.getInputs().get(0);
        op = (AbstractLogicalOperator) opRef.getValue();
        switch (op.getOperatorTag()) {
        case ASSIGN: {
            break;
        }
        case UNNEST: {
            UnnestOperator unnest = (UnnestOperator) op;
            if (unnest.getPositionalVariable() != null) {
                // TODO currently subplan with both accumulating and running aggregate is not supported.
                return false;
            }
            ILogicalExpression expr = unnest.getExpressionRef().getValue();
            if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                return false;
            }
            AbstractFunctionCallExpression fun = (AbstractFunctionCallExpression) expr;
            if (fun.getFunctionIdentifier() != BuiltinOperators.ITERATE.getFunctionIdentifier()) {
                return false;
            }
            ILogicalExpression arg0 = fun.getArguments().get(0).getValue();
            if (arg0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
                return false;
            }
            VariableReferenceExpression varExpr = (VariableReferenceExpression) arg0;
            if (!varExpr.getVariableReference().equals(varFromGroupAgg)) {
                return false;
            }
            opRef = op.getInputs().get(0);
            op = (AbstractLogicalOperator) opRef.getValue();
            if (op.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
                return false;
            }
            pushableNestedSubplan = true;
            unnestVar = unnest.getVariable();
            break;
        }
        default: {
            return false;
        }
        }
    }
    if (pushableNestedSubplan) {
        for (int i = 0; i < gbyOp.getNestedPlans().size(); i++) {
            Mutable<ILogicalOperator> gbyAggRef = gbyOp.getNestedPlans().get(i).getRoots().get(0);
            AggregateOperator gbyAgg = (AggregateOperator) gbyAggRef.getValue();
            Mutable<ILogicalOperator> gbyAggChildRef = gbyAgg.getInputs().get(0);
            LogicalVariable listifyVar = findListifiedVariable(gbyAgg, varFromGroupAgg);
            if (listifyVar == null) {
                continue;
            }
            OperatorManipulationUtil.substituteVarRec(aggInSubplanOp, unnestVar, listifyVar, true, context);
            gbyAgg.getVariables().addAll(aggInSubplanOp.getVariables());
            gbyAgg.getExpressions().addAll(aggInSubplanOp.getExpressions());
            for (LogicalVariable v : aggInSubplanOp.getVariables()) {
                gbyWithAgg.put(v, gbyOp);
                gbyAggVars.put(v, 0);
                gbyAggVarToPlanIndex.put(v, i);
            }

            Mutable<ILogicalOperator> opRef1InSubplan = aggInSubplanOp.getInputs().get(0);
            if (opRef1InSubplan.getValue().getInputs().size() > 0) {
                Mutable<ILogicalOperator> opRef2InSubplan = opRef1InSubplan.getValue().getInputs().get(0);
                AbstractLogicalOperator op2InSubplan = (AbstractLogicalOperator) opRef2InSubplan.getValue();
                if (op2InSubplan.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
                    List<Mutable<ILogicalOperator>> gbyInpList = gbyAgg.getInputs();
                    gbyInpList.clear();
                    gbyInpList.add(opRef1InSubplan);
                    while (true) {
                        opRef2InSubplan = opRef1InSubplan.getValue().getInputs().get(0);
                        op2InSubplan = (AbstractLogicalOperator) opRef2InSubplan.getValue();
                        if (op2InSubplan.getOperatorTag() == LogicalOperatorTag.UNNEST) {
                            List<Mutable<ILogicalOperator>> opInpList = opRef1InSubplan.getValue().getInputs();
                            opInpList.clear();
                            opInpList.add(gbyAggChildRef);
                            break;
                        }
                        opRef1InSubplan = opRef2InSubplan;
                        if (opRef1InSubplan.getValue().getInputs().size() == 0) {
                            throw new IllegalStateException(
                                    "PushAggregateIntoGroupbyRule: could not find UNNEST.");
                        }
                    }
                }
            }
            subplanOpRef.setValue(subplan.getInputs().get(0).getValue());
            OperatorPropertiesUtil.typeOpRec(gbyAggRef, context);
        }
        return true;
    } else {
        return false;
    }
}

From source file:org.apache.vxquery.compiler.rewriter.rules.PushChildIntoDataScanRule.java

protected boolean processOperator(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    ;//www . jav  a2  s . com
    if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnest = (UnnestOperator) op1;

    AbstractLogicalOperator op2 = (AbstractLogicalOperator) unnest.getInputs().get(0).getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
        return false;
    }
    DataSourceScanOperator datascan = (DataSourceScanOperator) op2;

    if (!usedVariables.contains(datascan.getVariables())) {
        // Find all child functions.
        VXQueryCollectionDataSource ds = (VXQueryCollectionDataSource) datascan.getDataSource();
        if (!updateDataSource(ds, unnest.getExpressionRef())) {
            return false;
        }

        // Replace unnest with noop assign. Keeps variable chain.
        Mutable<ILogicalExpression> varExp = ExpressionToolbox.findVariableExpression(unnest.getExpressionRef(),
                datascan.getVariables().get(0));
        AssignOperator noOp = new AssignOperator(unnest.getVariable(), varExp);
        noOp.getInputs().addAll(unnest.getInputs());
        opRef.setValue(noOp);
        return true;
    }
    return false;
}

From source file:org.apache.vxquery.compiler.rewriter.rules.PushFunctionsOntoEqJoinBranches.java

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();

    if (op.getOperatorTag() != LogicalOperatorTag.INNERJOIN) {
        return false;
    }/*from w w  w  .  j  a va 2  s . c  om*/
    AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) op;

    ILogicalExpression expr = join.getCondition().getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression fexp = (AbstractFunctionCallExpression) expr;
    FunctionIdentifier fi = fexp.getFunctionIdentifier();
    if (!(fi.equals(AlgebricksBuiltinFunctions.AND) || fi.equals(AlgebricksBuiltinFunctions.EQ))) {
        return false;
    }
    boolean modified = false;
    List<Mutable<ILogicalExpression>> functionList = new ArrayList<Mutable<ILogicalExpression>>();
    List<Mutable<ILogicalExpression>> variableList = new ArrayList<Mutable<ILogicalExpression>>();
    functionList.clear();
    ExpressionToolbox.findAllFunctionExpressions(join.getCondition(), AlgebricksBuiltinFunctions.EQ,
            functionList);
    Collection<LogicalVariable> producedVariables = new ArrayList<LogicalVariable>();
    for (Mutable<ILogicalExpression> searchM : functionList) {
        ILogicalExpression search = searchM.getValue();
        if (search.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
            continue;
        }
        AbstractFunctionCallExpression searchExp = (AbstractFunctionCallExpression) search;
        // Go through all argument for EQ.
        for (Mutable<ILogicalExpression> expressionM : searchExp.getArguments()) {
            // Push on to branch when possible.
            for (Mutable<ILogicalOperator> branch : join.getInputs()) {
                producedVariables.clear();
                getProducedVariablesInDescendantsAndSelf(branch.getValue(), producedVariables);
                variableList.clear();
                ExpressionToolbox.findVariableExpressions(expressionM, variableList);
                boolean found = true;
                for (Mutable<ILogicalExpression> searchVariableM : variableList) {
                    VariableReferenceExpression vre = (VariableReferenceExpression) searchVariableM.getValue();
                    if (!producedVariables.contains(vre.getVariableReference())) {
                        found = false;
                    }
                }
                if (found) {
                    // push down
                    LogicalVariable assignVariable = context.newVar();
                    AssignOperator aOp = new AssignOperator(assignVariable,
                            new MutableObject<ILogicalExpression>(expressionM.getValue()));
                    aOp.getInputs().add(new MutableObject<ILogicalOperator>(branch.getValue()));
                    branch.setValue(aOp);
                    aOp.recomputeSchema();

                    expressionM.setValue(new VariableReferenceExpression(assignVariable));
                    modified = true;
                }
            }
        }
    }
    return modified;
}

From source file:org.apache.vxquery.compiler.rewriter.rules.RemoveUnusedUnnestIterateRule.java

protected boolean processOperator(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }/*  ww w  .ja v  a2 s .co  m*/
    UnnestOperator unnest = (UnnestOperator) op;

    // Check to see if the expression is a function and iterate.
    ILogicalExpression logicalExpressionUnnest1 = (ILogicalExpression) unnest.getExpressionRef().getValue();
    if (logicalExpressionUnnest1.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression functionCallUnnest1 = (AbstractFunctionCallExpression) logicalExpressionUnnest1;
    if (!functionCallUnnest1.getFunctionIdentifier().equals(BuiltinOperators.ITERATE.getFunctionIdentifier())) {
        return false;
    }

    // Check to see if the expression is a variable.
    Mutable<ILogicalExpression> logicalExpressionUnnestRef2 = functionCallUnnest1.getArguments().get(0);
    ILogicalExpression logicalExpressionUnnest2 = (ILogicalExpression) logicalExpressionUnnestRef2.getValue();
    if (logicalExpressionUnnest2.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    VariableReferenceExpression vre2 = (VariableReferenceExpression) logicalExpressionUnnest2;
    LogicalVariable unnestInput = vre2.getVariableReference();

    // Check if the input is an DATASCAN or UNNEST operator..
    Mutable<ILogicalOperator> opRef2 = unnest.getInputs().get(0);
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
    if (op2.getOperatorTag() == LogicalOperatorTag.UNNEST
            || op2.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
        AbstractScanOperator aso = (AbstractScanOperator) op2;
        if (aso.getVariables().size() == 1 && aso.getVariables().contains(unnestInput)) {
            // Add in a noop.
            AssignOperator assign = new AssignOperator(unnest.getVariable(), logicalExpressionUnnestRef2);
            assign.getInputs().addAll(unnest.getInputs());
            opRef.setValue(assign);
            return true;
        }
    }
    return false;
}

From source file:org.mitre.mpf.wfm.camel.JobCompleteProcessorImpl.java

@Override
public void wfmProcess(Exchange exchange) throws WfmProcessingException {
    Long jobId = exchange.getIn().getHeader(MpfHeaders.JOB_ID, Long.class);
    assert jobId != null : String.format("The header '%s' (value=%s) was not set or is not a Long.",
            MpfHeaders.JOB_ID, exchange.getIn().getHeader(MpfHeaders.JOB_ID));

    if (jobId == Long.MIN_VALUE) {
        // If we receive a very large negative Job ID, it means an exception was encountered during processing of a job,
        // and none of the provided error handling logic could fix it. Further processing should not be performed.
        log.warn(//from  ww  w.j  a va 2  s .c  o  m
                "[Job {}:*:*] An error prevents a job from completing successfully. Please review the logs for additional information.",
                jobId);
    } else {
        String statusString = exchange.getIn().getHeader(MpfHeaders.JOB_STATUS, String.class);
        Mutable<JobStatus> jobStatus = new MutableObject<>(JobStatus.parse(statusString, JobStatus.UNKNOWN));

        markJobStatus(jobId, jobStatus.getValue());

        try {
            markJobStatus(jobId, JobStatus.BUILDING_OUTPUT_OBJECT);

            // NOTE: jobStatus is mutable - it __may__ be modified in createOutputObject!
            createOutputObject(jobId, jobStatus);
        } catch (Exception exception) {
            log.warn("Failed to create the output object for Job {} due to an exception.", jobId, exception);
            jobStatus.setValue(JobStatus.ERROR);
        }

        markJobStatus(jobId, jobStatus.getValue());

        try {
            callback(jobId);
        } catch (Exception exception) {
            log.warn("Failed to make callback (if appropriate) for Job #{}.", jobId);
        }

        // Tear down the job.
        try {
            destroy(jobId);
        } catch (Exception exception) {
            log.warn(
                    "Failed to clean up Job {} due to an exception. Data for this job will remain in the transient data store, but the status of the job has not been affected by this failure.",
                    jobId, exception);
        }

        JobCompleteNotification jobCompleteNotification = new JobCompleteNotification(
                exchange.getIn().getHeader(MpfHeaders.JOB_ID, Long.class));

        for (NotificationConsumer<JobCompleteNotification> consumer : consumers) {
            if (!jobCompleteNotification.isConsumed()) {
                try {
                    consumer.onNotification(this, new JobCompleteNotification(
                            exchange.getIn().getHeader(MpfHeaders.JOB_ID, Long.class)));
                } catch (Exception exception) {
                    log.warn("Completion consumer '{}' threw an exception.", consumer, exception);
                }
            }
        }

        AtmosphereController.broadcast(new JobStatusMessage(jobId, 100, jobStatus.getValue(), new Date()));
        jobProgressStore.setJobProgress(jobId, 100.0f);
        log.info("[Job {}:*:*] Job complete!", jobId);
    }
}

From source file:org.mitre.mpf.wfm.camel.JobCompleteProcessorImpl.java

public void createOutputObject(long jobId, Mutable<JobStatus> jobStatus) throws WfmProcessingException {
    TransientJob transientJob = redis.getJob(jobId);
    JobRequest jobRequest = jobRequestDao.findById(jobId);

    if (transientJob.isCancelled()) {
        jobStatus.setValue(JobStatus.CANCELLED);
    }/*from ww  w . j  a va 2 s  . c  o m*/

    JsonOutputObject jsonOutputObject = new JsonOutputObject(jobRequest.getId(), UUID.randomUUID().toString(),
            transientJob == null ? null : jsonUtils.convert(transientJob.getPipeline()),
            transientJob.getPriority(), propertiesUtil.getSiteId(), transientJob.getExternalId(),
            jobRequest.getTimeReceived().toString(), jobRequest.getTimeCompleted().toString(),
            jobStatus.getValue().toString());

    // Build detection output object....
    int mediaIndex = 0;
    for (TransientMedia transientMedia : transientJob.getMedia()) {
        StringBuilder stateKeyBuilder = new StringBuilder("+");

        JsonMediaOutputObject mediaOutputObject = new JsonMediaOutputObject(transientMedia.getId(),
                transientMedia.getUri(), transientMedia.getType(), transientMedia.getLength(),
                transientMedia.getSha256(), transientMedia.getMessage(),
                transientMedia.isFailed() ? "ERROR" : "COMPLETE");

        mediaOutputObject.getMediaMetadata().putAll(transientMedia.getMetadata());
        mediaOutputObject.getOverriddenProperties().putAll(transientMedia.getMediaSpecificProperties());

        MarkupResult markupResult = markupResultDao.findByJobIdAndMediaIndex(jobId, mediaIndex);
        if (markupResult != null) {
            mediaOutputObject.setMarkupResult(
                    new JsonMarkupOutputObject(markupResult.getId(), markupResult.getMarkupUri(),
                            markupResult.getMarkupStatus().name(), markupResult.getMessage()));
        }

        if (transientJob.getPipeline() != null) {
            for (int stageIndex = 0; stageIndex < transientJob.getPipeline().getStages().size(); stageIndex++) {
                TransientStage transientStage = transientJob.getPipeline().getStages().get(stageIndex);
                for (int actionIndex = 0; actionIndex < transientStage.getActions().size(); actionIndex++) {
                    TransientAction transientAction = transientStage.getActions().get(actionIndex);
                    String stateKey = String.format("%s#%s", stateKeyBuilder.toString(),
                            transientAction.getName());

                    for (DetectionProcessingError detectionProcessingError : redis.getDetectionProcessingErrors(
                            jobId, transientMedia.getId(), stageIndex, actionIndex)) {
                        JsonDetectionProcessingError jsonDetectionProcessingError = new JsonDetectionProcessingError(
                                detectionProcessingError.getStartOffset(),
                                detectionProcessingError.getEndOffset(), detectionProcessingError.getError());
                        if (!mediaOutputObject.getDetectionProcessingErrors().containsKey(stateKey)) {
                            mediaOutputObject.getDetectionProcessingErrors().put(stateKey,
                                    new TreeSet<JsonDetectionProcessingError>());
                        }
                        mediaOutputObject.getDetectionProcessingErrors().get(stateKey)
                                .add(jsonDetectionProcessingError);
                        if (!StringUtils.equalsIgnoreCase(mediaOutputObject.getStatus(), "COMPLETE")) {
                            mediaOutputObject.setStatus("INCOMPLETE");
                            if (StringUtils.equalsIgnoreCase(jsonOutputObject.getStatus(), "COMPLETE")) {
                                jsonOutputObject.setStatus("INCOMPLETE");
                            }
                        }
                    }

                    Collection<Track> tracks = redis.getTracks(jobId, transientMedia.getId(), stageIndex,
                            actionIndex);
                    if (tracks.size() == 0) {
                        // Always include detection actions in the output object, even if they do not generate any results.
                        if (!mediaOutputObject.getTracks().containsKey(stateKey)) {
                            mediaOutputObject.getTracks().put(stateKey, new TreeSet<JsonTrackOutputObject>());
                        }
                    } else {
                        for (Track track : tracks) {
                            JsonTrackOutputObject jsonTrackOutputObject = new JsonTrackOutputObject(
                                    TextUtils.getTrackUuid(transientMedia.getSha256(),
                                            track.getExemplar().getMediaOffsetFrame(),
                                            track.getExemplar().getX(), track.getExemplar().getY(),
                                            track.getExemplar().getWidth(), track.getExemplar().getHeight(),
                                            track.getType()),
                                    track.getStartOffsetFrameInclusive(), track.getEndOffsetFrameInclusive(),
                                    track.getStartOffsetTimeInclusive(), track.getEndOffsetTimeInclusive(),
                                    track.getType(), stateKey);

                            jsonTrackOutputObject.setExemplar(new JsonDetectionOutputObject(
                                    track.getExemplar().getX(), track.getExemplar().getY(),
                                    track.getExemplar().getWidth(), track.getExemplar().getHeight(),
                                    track.getExemplar().getConfidence(),
                                    track.getExemplar().getDetectionProperties(),
                                    track.getExemplar().getMediaOffsetFrame(),
                                    track.getExemplar().getMediaOffsetTime(),
                                    track.getExemplar().getArtifactExtractionStatus().name(),
                                    track.getExemplar().getArtifactPath()));
                            for (Detection detection : track.getDetections()) {
                                jsonTrackOutputObject.getDetections()
                                        .add(new JsonDetectionOutputObject(detection.getX(), detection.getY(),
                                                detection.getWidth(), detection.getHeight(),
                                                detection.getConfidence(), detection.getDetectionProperties(),
                                                detection.getMediaOffsetFrame(), detection.getMediaOffsetTime(),
                                                detection.getArtifactExtractionStatus().name(),
                                                detection.getArtifactPath()));

                            }

                            if (!mediaOutputObject.getTracks().containsKey(stateKey)) {
                                mediaOutputObject.getTracks().put(stateKey,
                                        new TreeSet<JsonTrackOutputObject>());
                            }
                            mediaOutputObject.getTracks().get(stateKey).add(jsonTrackOutputObject);
                        }
                    }

                    if (actionIndex == transientStage.getActions().size() - 1) {
                        stateKeyBuilder.append("#").append(transientAction.getName());
                    }
                }
            }
        }
        jsonOutputObject.getMedia().add(mediaOutputObject);
        mediaIndex++;
    }

    try {
        File outputFile = propertiesUtil.createDetectionOutputObjectFile(jobId);
        jsonUtils.serialize(jsonOutputObject, outputFile);
        jobRequest.setOutputObjectPath(outputFile.getAbsolutePath());
        jobRequestDao.persist(jobRequest);
    } catch (IOException | WfmProcessingException wpe) {
        log.error("Failed to create the JSON detection output object for '{}' due to an exception.", jobId,
                wpe);
    }

    try {
        jmsUtils.destroyCancellationRoutes(jobId);
    } catch (Exception exception) {
        log.warn(
                "Failed to destroy the cancellation routes associated with {}. If this job is resubmitted, it will likely not complete again!",
                jobId, exception);
    }

}