LightJason - AgentSpeak(L++)
CMethodAction.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 package org.lightjason.agentspeak.action.binding;
25 
34 
35 import javax.annotation.Nonnegative;
36 import javax.annotation.Nonnull;
37 import javax.annotation.Nullable;
38 import java.io.IOException;
39 import java.io.ObjectInputStream;
40 import java.io.ObjectOutputStream;
41 import java.lang.invoke.MethodHandle;
42 import java.lang.invoke.MethodHandles;
43 import java.lang.reflect.Method;
44 import java.text.MessageFormat;
45 import java.util.List;
46 import java.util.Locale;
47 import java.util.Objects;
48 import java.util.stream.Collectors;
49 import java.util.stream.Stream;
50 
51 
56 public final class CMethodAction extends IBaseAction
57 {
61  private static final long serialVersionUID = -507236338411690842L;
65  private final IPath m_name;
69  private final int m_arguments;
73  private transient Method m_method;
77  private transient MethodHandle m_methodhandle;
78 
79 
86  public CMethodAction( @Nonnull final Method p_method ) throws IllegalAccessException
87  {
88  m_method = p_method;
89  m_arguments = m_method.getParameterCount();
90  m_name = CPath.from(
91  m_method.isAnnotationPresent( IAgentActionName.class ) && !m_method.getAnnotation( IAgentActionName.class ).name().isEmpty()
92  ? m_method.getAnnotation( IAgentActionName.class ).name().toLowerCase( Locale.ROOT )
93  : m_method.getName().toLowerCase( Locale.ROOT )
94  );
95  m_methodhandle = MethodHandles.lookup().unreflect( m_method );
96  }
97 
104  private void writeObject( final ObjectOutputStream p_stream ) throws IOException
105  {
106  p_stream.defaultWriteObject();
107 
108  // serialize method handle
109  p_stream.writeObject( m_method.getDeclaringClass() );
110  p_stream.writeUTF( m_method.getName() );
111  p_stream.writeObject( m_method.getParameterTypes() );
112  }
113 
123  @SuppressWarnings( "unchecked" )
124  private void readObject( final ObjectInputStream p_stream ) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException
125  {
126  p_stream.defaultReadObject();
127 
128  // deserialize method handle
129  m_method = ( (Class<?>) p_stream.readObject() ).getMethod( p_stream.readUTF(), (Class<?>[])p_stream.readObject() );
130  m_methodhandle = MethodHandles.lookup().unreflect( m_method );
131  }
132 
133  @Nonnull
134  @Override
135  public final IPath name()
136  {
137  return m_name;
138  }
139 
140  @Nonnegative
141  @Override
142  public final int minimalArgumentNumber()
143  {
144  return m_arguments;
145  }
146 
147  @Nonnull
148  @Override
149  public IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
150  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
151  )
152  {
153  try
154  {
155  return m_arguments == 0
156 
158  m_methodhandle.invoke( p_context.agent() ),
159  p_return
160  )
161 
163  m_methodhandle.invokeWithArguments(
164  Stream.concat(
165  Stream.of( p_context.agent() ),
166  p_argument.stream().map( ITerm::raw )
167  ).collect( Collectors.toList() )
168  ),
169  p_return
170  );
171  }
172  catch ( final Throwable l_throwable )
173  {
174  LOGGER.warning( MessageFormat.format( "binding method [{0}] throws error [{1}] in agent: ", m_name, l_throwable, p_context.agent() ) );
175  return CFuzzyValue.from( false );
176  }
177  }
178 
186  @Nonnull
187  private static IFuzzyValue<Boolean> returnvalues( @Nullable final Object p_result, @Nonnull final List<ITerm> p_return )
188  {
189  // void result of the execution
190  if ( ( Objects.isNull( p_result ) ) || ( void.class.equals( p_result.getClass() ) ) )
191  return CFuzzyValue.from( true );
192 
193  // otherwise object is returned
194  p_return.add( CRawTerm.from( p_result ) );
195  return CFuzzyValue.from( true );
196  }
197 }
void writeObject(final ObjectOutputStream p_stream)
serialize call
transient MethodHandle m_methodhandle
method handle
default implementation of an action
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
final int minimalArgumentNumber()
minimum number of arguments
static IPath from( @Nonnull final String p_string)
factor method to build path
Definition: CPath.java:166
class to create a path structure
Definition: CPath.java:53
void readObject(final ObjectInputStream p_stream)
deserializable call
CMethodAction( @Nonnull final Method p_method)
ctor
execution context with local data
Definition: IContext.java:42
final IPath name()
returns the name with path of the action
result for an immutable fuzzy value
IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
annotation to define an individual action name
< T > T raw()
cast to any raw value type
final boolean equals(final Object p_object)
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static IFuzzyValue< Boolean > returnvalues( @Nullable final Object p_result, @Nonnull final List< ITerm > p_return)
creates the returns values of the execution
term structure for simple datatypes
Definition: CRawTerm.java:45