org.lightjason.agentspeak.action.buildin.agent.CPlanStatistic.java Source code

Java tutorial

Introduction

Here is the source code for org.lightjason.agentspeak.action.buildin.agent.CPlanStatistic.java

Source

/*
 * @cond LICENSE
 * ######################################################################################
 * # LGPL License                                                                       #
 * #                                                                                    #
 * # This file is part of the LightJason AgentSpeak(L++)                                #
 * # Copyright (c) 2015-16, LightJason (info@lightjason.org)                            #
 * # This program is free software: you can redistribute it and/or modify               #
 * # it under the terms of the GNU Lesser General Public License as                     #
 * # published by the Free Software Foundation, either version 3 of the                 #
 * # License, or (at your option) any later version.                                    #
 * #                                                                                    #
 * # This program is distributed in the hope that it will be useful,                    #
 * # but WITHOUT ANY WARRANTY; without even the implied warranty of                     #
 * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                      #
 * # GNU Lesser General Public License for more details.                                #
 * #                                                                                    #
 * # You should have received a copy of the GNU Lesser General Public License           #
 * # along with this program. If not, see http://www.gnu.org/licenses/                  #
 * ######################################################################################
 * @endcond
 */

package org.lightjason.agentspeak.action.buildin.agent;

import org.apache.commons.lang3.tuple.Triple;
import org.lightjason.agentspeak.action.buildin.IBuildinAction;
import org.lightjason.agentspeak.agent.IAgent;
import org.lightjason.agentspeak.language.CCommon;
import org.lightjason.agentspeak.language.CRawTerm;
import org.lightjason.agentspeak.language.ITerm;
import org.lightjason.agentspeak.language.execution.IContext;
import org.lightjason.agentspeak.language.execution.fuzzy.CFuzzyValue;
import org.lightjason.agentspeak.language.execution.fuzzy.IFuzzyValue;
import org.lightjason.agentspeak.language.instantiable.plan.IPlan;
import org.lightjason.agentspeak.language.instantiable.plan.trigger.ITrigger;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * action to get plan statistic.
 * The actions returns for each plan the statistic information,
 * for each plan argument, succesfull, fail and sum rate is returned,
 * the action fails if the plan does not exist within the plan-base
 *
 * @code [Successful1|Fail1|Sum1|Successful2|Fail2|Sum2] = agent/planstatistic( Plan1, Plan2 ); @endcode
 */
public final class CPlanStatistic extends IBuildinAction {

    @Override
    public final int minimalArgumentNumber() {
        return 1;
    }

    @Override
    public final IFuzzyValue<Boolean> execute(final IContext p_context, final boolean p_parallel,
            final List<ITerm> p_argument, final List<ITerm> p_return, final List<ITerm> p_annotation) {
        return CFuzzyValue.from(CCommon.flatcollection(p_argument)
                .allMatch(i -> CPlanStatistic.statistic(i.<IPlan>raw().getTrigger(), p_context.agent(), p_return)));
    }

    /**
     * creates the plan statistic
     *
     * @param p_trigger plan trigger
     * @param p_agent agent
     * @param p_return return arguments
     * @return successfull flag
     */
    private static boolean statistic(final ITrigger p_trigger, final IAgent<?> p_agent,
            final List<ITerm> p_return) {
        final Collection<Triple<IPlan, AtomicLong, AtomicLong>> l_plans = p_agent.plans().get(p_trigger);
        if (l_plans.isEmpty())
            return false;

        final long l_success = l_plans.parallelStream().mapToLong(i -> i.getMiddle().get()).sum();
        final long l_fail = l_plans.parallelStream().mapToLong(i -> i.getRight().get()).sum();

        p_return.add(CRawTerm.from(l_success));
        p_return.add(CRawTerm.from(l_fail));
        p_return.add(CRawTerm.from(l_success + l_fail));

        return true;
    }
}