LightJason - AgentSpeak(L++)
language/instantiable/plan/statistic/CPlanStatistic.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 
25 package org.lightjason.agentspeak.language.instantiable.plan.statistic;
26 
27 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
31 
32 import javax.annotation.Nonnegative;
33 import javax.annotation.Nonnull;
34 import java.text.MessageFormat;
35 import java.util.concurrent.atomic.AtomicLong;
36 import java.util.stream.Stream;
37 
38 
42 public final class CPlanStatistic implements IPlanStatistic
43 {
47  private final IPlan m_plan;
51  private final AtomicLong m_successful = new AtomicLong();
55  private final AtomicLong m_fail = new AtomicLong();
56 
57 
63  private CPlanStatistic( @Nonnull final IPlan p_plan )
64  {
65  m_plan = p_plan;
66  }
67 
68  @Nonnull
69  @Override
70  public final IPlan plan()
71  {
72  return m_plan;
73  }
74 
75  @Override
76  @Nonnegative
77  public final long count()
78  {
79  return m_fail.get() + m_successful.get();
80  }
81 
82  @Override
83  @Nonnegative
84  public final long successful()
85  {
86  return m_successful.get();
87  }
88 
89  @Override
90  @Nonnegative
91  public final double successfulratio()
92  {
93  final double l_sum = m_successful.get() + m_fail.get();
94  return l_sum == 0
95  ? 0
96  : m_successful.get() / l_sum;
97  }
98 
99  @Override
100  @Nonnegative
101  public final long fail()
102  {
103  return m_fail.get();
104  }
105 
106  @Override
107  public final double failratio()
108  {
109  final double l_sum = m_successful.get() + m_fail.get();
110  return l_sum == 0
111  ? 0
112  : m_fail.get() / l_sum;
113  }
114 
115  @Nonnull
116  @Override
118  {
119  m_successful.incrementAndGet();
120  return this;
121  }
122 
123  @Nonnull
124  @Override
126  {
127  m_fail.incrementAndGet();
128  return this;
129  }
130 
131  @Nonnull
132  @Override
133  public final Stream<IVariable<?>> variables()
134  {
135  return Stream.of(
136  new CConstant<>( "PlanSuccessful", m_successful.get() ),
137  new CConstant<>( "PlanFail", m_fail.get() ),
138  new CConstant<>( "PlanRuns", m_successful.get() + m_fail.get() ),
139 
140  // execution ratio
141  new CConstant<>( "PlanSuccessfulRatio", this.successfulratio() ),
142  new CConstant<>( "PlanFailRatio", this.failratio() )
143  );
144  }
145 
146  @Override
147  public final int hashCode()
148  {
149  return m_plan.hashCode();
150  }
151 
152  @Override
153  @SuppressFBWarnings( "EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS" )
154  public final boolean equals( final Object p_object )
155  {
156  return ( ( p_object instanceof IPlanStatistic ) || ( p_object instanceof IPlan ) )
157  && ( this.hashCode() == p_object.hashCode() );
158  }
159 
160  @Override
161  public final String toString()
162  {
163  return MessageFormat.format( "successful [{0}], fail [{1}]: {2}", m_successful.get(), m_fail.get(), m_plan );
164  }
165 
172  @Nonnull
173  public static IPlanStatistic from( @Nonnull final IPlan p_plan )
174  {
175  return new CPlanStatistic( p_plan );
176  }
177 
178 
179  @Override
180  public final int compareTo( @Nonnull final IPlanStatistic p_other )
181  {
182  return Integer.compare( this.hashCode(), p_other.hashCode() );
183  }
184 }
final Stream< IVariable<?> > variables()
returns a stream with variables of the internal data