24 package org.lightjason.agentspeak.language;
26 import com.google.common.hash.Hasher;
27 import com.google.common.hash.Hashing;
28 import com.rits.cloning.Cloner;
29 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
30 import org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputStream;
31 import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
32 import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream;
33 import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
34 import org.apache.commons.compress.utils.IOUtils;
35 import org.apache.commons.io.output.NullOutputStream;
36 import org.apache.commons.lang3.tuple.ImmutablePair;
37 import org.apache.commons.lang3.tuple.Pair;
49 import javax.annotation.Nonnull;
50 import javax.annotation.Nullable;
51 import java.io.ByteArrayInputStream;
52 import java.io.DataOutputStream;
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.io.OutputStream;
56 import java.nio.charset.StandardCharsets;
57 import java.util.Arrays;
58 import java.util.Collection;
59 import java.util.Collections;
60 import java.util.List;
61 import java.util.Locale;
63 import java.util.Objects;
65 import java.util.stream.Collectors;
66 import java.util.stream.IntStream;
67 import java.util.stream.Stream;
78 private static final Cloner
CLONER =
new Cloner();
99 p_unifiedvariables.parallel().forEach( i -> p_context.instancevariables().get( i.fqnfunctor() ).
set( i.raw() ) );
114 final Set<IVariable<?>> l_variables = p_instance.variables().parallel().map( i -> i.shallowcopy() ).collect( Collectors.toSet() );
117 p_agent.variablebuilder().apply( p_agent, p_instance )
119 .peek( l_variables::remove )
120 .forEach( l_variables::add );
122 return new CContext( p_agent, p_instance, Collections.unmodifiableSet( l_variables ) );
140 if ( !( p_source.literal().emptyValues() == p_target.literal().emptyValues() ) )
141 return new ImmutablePair<>(
false, Collections.emptySet() );
144 final Set<IVariable<?>> l_variables = p_unifier.unify( p_source.literal(), p_target.literal().deepcopy().<
ILiteral>raw() );
147 return l_variables.size() ==
CCommon.variablefrequency( p_target.literal() ).size()
148 ?
new ImmutablePair<>(
true, l_variables )
149 :
new ImmutablePair<>(
false, Collections.emptySet() );
164 return new ImmutablePair<>(
166 p_planstatistic.plan().instantiate(
168 Stream.concat( p_variables.stream(), p_planstatistic.variables() )
184 @SuppressWarnings(
"varargs" )
185 public static <T> Stream<T> streamconcat( @Nonnull final Stream<T>... p_streams )
187 return Arrays.stream( p_streams ).reduce( Stream::concat ).orElseGet( Stream::empty );
199 return Collections.unmodifiableMap(
202 .map( i -> (IVariable<?>) i )
203 .collect( Collectors.toMap( i -> i, i -> 1, Integer::sum ) )
214 @SuppressWarnings(
"unchecked" )
215 public static <T>
boolean rawvalueAssignableTo( @Nonnull final T p_value, @Nonnull final Class<?>... p_class )
218 return ( (IVariable<?>) p_value ).valueassignableto( p_class );
220 return ( (IRawTerm<?>) p_value ).valueassignableto( p_class );
222 return Arrays.stream( p_class ).anyMatch( i -> i.isAssignableFrom( p_value.getClass() ) );
236 return p_terms.stream().map( i ->
replaceFromContext( p_context, i ) ).collect( Collectors.toList() );
253 final IVariable<?> l_variable = p_context.instancevariables().get( p_term.fqnfunctor() );
254 if ( Objects.nonNull( l_variable ) )
271 public static Stream<ITerm>
flatten( @Nonnull
final Collection<? extends ITerm> p_terms )
284 public static Stream<ITerm>
flatten( @Nonnull
final Stream<? extends ITerm> p_terms )
308 @SuppressWarnings(
"unchecked" )
309 private static Stream<
ITerm> flattenstream( @Nonnull final Stream<?> p_stream )
311 return p_stream.flatMap( i ->
313 final Object l_value = i instanceof
ITerm ? ( (
ITerm) i ).
raw() : i;
314 return l_value instanceof Collection<?>
328 return Hashing.sipHash24().newHasher();
339 @SuppressWarnings(
"unchecked" )
340 public static <T> T deepclone( @Nullable final T p_object )
342 return Objects.isNull( p_object ) ? null :
CLONER.deepClone( p_object );
358 public static double levenshtein( @Nonnull
final String p_first, @Nonnull
final String p_second,
final double p_insertweight,
359 final double p_replaceweight,
final double p_deleteweight )
362 double[] l_cost = IntStream.range( 0, p_first.length() + 1 ).mapToDouble( i -> i ).toArray();
363 double[] l_newcost =
new double[l_cost.length];
365 for (
int j = 1; j < p_second.length() + 1; j++ )
370 for (
int i = 1; i < l_cost.length; i++ )
372 l_cost[i - 1] + ( p_first.charAt( i - 1 ) == p_second.charAt( j - 1 ) ? 0 : p_replaceweight ),
373 l_newcost[i - 1] + p_deleteweight,
374 l_cost[i] + p_insertweight
377 final double[] l_swap = l_cost;
382 return l_cost[p_first.length()];
394 public static double min(
final double p_first,
final double p_second,
final double p_third )
396 return Math.min( Math.min( p_first, p_second ), p_third );
408 public static double ncd( @Nonnull
final ECompression p_compression, @Nonnull
final String p_first, @Nonnull
final String p_second )
410 if ( p_first.equals( p_second ) )
413 final double l_first =
compress( p_compression, p_first );
414 final double l_second =
compress( p_compression, p_second );
415 return (
compress( p_compression, p_first + p_second ) - Math.min( l_first, l_second ) ) / Math.max( l_first, l_second );
429 final DataOutputStream l_counting =
new DataOutputStream(
new NullOutputStream() );
432 final InputStream l_input =
new ByteArrayInputStream( p_input.getBytes( StandardCharsets.UTF_8 ) );
433 final OutputStream l_compress = p_compression.get( l_counting )
436 IOUtils.copy( l_input, l_compress );
438 catch (
final IOException l_exception )
443 return l_counting.size();
461 private static final Set<String> ALGORITHMS = Collections.unmodifiableSet(
463 .map( i -> i.name().toUpperCase( Locale.ROOT ) )
464 .collect( Collectors.toSet() )
475 public final OutputStream
get( @Nonnull
final DataOutputStream p_datastream )
throws IOException
479 case BZIP :
return new BZip2CompressorOutputStream( p_datastream );
481 case GZIP :
return new GzipCompressorOutputStream( p_datastream );
483 case DEFLATE :
return new DeflateCompressorOutputStream( p_datastream );
485 case PACK200 :
return new Pack200CompressorOutputStream( p_datastream );
487 case XZ :
return new XZCompressorOutputStream( p_datastream );
503 return ECompression.valueOf( p_value.toUpperCase( Locale.ROOT ) );
513 public static boolean exist( @Nonnull
final String p_value )
515 return ALGORITHMS.contains( p_value.toUpperCase( Locale.ROOT ) );
static double min(final double p_first, final double p_second, final double p_third)
returns the minimum of three elemens
illegal argument exception
interface for (instantiable) plans and logical-rules
static ECompression from( @Nonnull final String p_value)
returns a compression value
static boolean exist( @Nonnull final String p_value)
checks if a compression exists
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
interface of an unification algorithm
static Stream< ITerm > flattenrecursive( @Nonnull final Stream< ITerm > p_input)
recursive stream of term values
static final Cloner CLONER
cloner
execution context with local data
static Pair< Boolean, Set< IVariable<?> > > unifytrigger( @Nonnull final IUnifier p_unifier, @Nonnull final ITrigger p_source, @Nonnull final ITrigger p_target)
unifies trigger and creates the set of variables
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
static List< ITerm > replaceFromContext( @Nonnull final IContext p_context, @Nonnull final Collection<? extends ITerm > p_terms)
replace variables with context variables
static Hasher getTermHashing()
returns the hasing function for term data
static Map< IVariable<?>, Integer > variablefrequency( @Nonnull final ILiteral p_literal)
consts the variables within a literal
static IContext updatecontext( @Nonnull final IContext p_context, @Nonnull final Stream< IVariable<?>> p_unifiedvariables)
updates within an instance context all variables of the stream
static Pair< IPlanStatistic, IContext > instantiateplan( @Nonnull final IPlanStatistic p_planstatistic, @Nonnull final IAgent<?> p_agent, @Nonnull final Set< IVariable<?>> p_variables)
instantiate a plan with context and plan-specific variables
static ITerm replaceFromContext( @Nonnull final IContext p_context, @Nonnull final ITerm p_term)
replace variable with context variable other values will be passed without context access ...
static Stream< ITerm > flattenstream( @Nonnull final Stream<?> p_stream)
< T > T raw()
cast to any raw value type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
class for any helper calls
CCommon()
private ctor - avoid instantiation
static double ncd( @Nonnull final ECompression p_compression, @Nonnull final String p_first, @Nonnull final String p_second)
normalized-compression-distance
static double levenshtein( @Nonnull final String p_first, @Nonnull final String p_second, final double p_insertweight, final double p_replaceweight, final double p_deleteweight)
calculates the levenshtein distance
static IContext instantiate( @Nonnull final IInstantiable p_instance, @Nonnull final IAgent<?> p_agent, @Nonnull final Stream< IVariable<?>> p_variable)
creates the instantiate execution context with default variables
term structure for simple datatypes
static double compress( @Nonnull final ECompression p_compression, @Nonnull final String p_input)
compression algorithm
static Stream< ITerm > flatten( @Nonnull final Stream<? extends ITerm > p_terms)
flat term-in-term stream into a straight term list