24 package org.lightjason.agentspeak.language;
26 import com.google.common.base.Charsets;
27 import com.google.common.collect.ImmutableListMultimap;
28 import com.google.common.collect.ImmutableMultimap;
29 import com.google.common.collect.LinkedListMultimap;
30 import com.google.common.collect.Multimap;
31 import com.google.common.hash.Hasher;
43 import javax.annotation.Nonnull;
44 import javax.annotation.Nullable;
45 import java.io.ByteArrayInputStream;
46 import java.io.InputStream;
47 import java.nio.charset.Charset;
48 import java.text.MessageFormat;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.List;
54 import java.util.Objects;
55 import java.util.stream.Collectors;
56 import java.util.stream.Stream;
77 private static final String
AT =
"@";
81 private final ImmutableMultimap<IPath, ITerm>
m_values;
97 private final boolean m_at;
117 public CLiteral(
final boolean p_at,
final boolean p_negated, @Nonnull
final IPath p_functor, @Nonnull
final Collection<ITerm> p_values )
120 m_negated = p_negated;
122 m_functor =
new CPath( p_functor );
125 if ( !p_values.isEmpty() )
127 final Multimap<IPath, ITerm> l_values = LinkedListMultimap.create();
128 p_values.forEach( i -> l_values.put( i.fqnfunctor(), i ) );
129 m_values = ImmutableListMultimap.copyOf( l_values );
130 m_orderedvalues = Collections.unmodifiableList(
new ArrayList<>( p_values ) );
134 m_orderedvalues = Collections.emptyList();
135 m_values = ImmutableListMultimap.of();
139 final Hasher l_hasher =
CCommon.getTermHashing();
140 l_hasher.putInt( m_functor.hashCode() );
141 l_hasher.putBoolean( m_negated );
142 l_hasher.putBoolean( m_at );
143 m_orderedvalues.forEach( i -> l_hasher.putInt( i.hashCode() ) );
144 m_hash = l_hasher.hash().asInt();
147 final Hasher l_valuehasher =
CCommon.getTermHashing();
148 m_orderedvalues.forEach( i -> l_valuehasher.putInt( i.structurehash() ) );
149 l_valuehasher.putBoolean( m_negated );
150 l_valuehasher.putString( p_functor.path(), Charsets.UTF_8 );
151 m_structurehash = l_valuehasher.hash().asInt();
161 public static ILiteral from( @Nonnull
final String p_functor, @Nullable
final ITerm... p_values )
165 ( Objects.isNull( p_values ) ) || ( p_values.length == 0 )
166 ? Collections.emptySet()
167 : Arrays.stream( p_values ).collect( Collectors.toList() )
178 public static ILiteral from( @Nonnull
final String p_functor, @Nonnull
final Collection<ITerm> p_values )
181 p_functor.contains( AT ), p_functor.contains( NEGATION ),
CPath.
from( p_functor.replace( AT,
"" ).replace( NEGATION,
"" ) ),
193 public static ILiteral from( @Nonnull
final String p_functor, @Nonnull
final Stream<ITerm> p_values )
195 return from( p_functor, p_values.collect( Collectors.toList() ) );
207 return from(
false,
false, p_functor, p_values );
217 public static ILiteral from( @Nonnull
final IPath p_functor, @Nonnull
final Stream<ITerm> p_values )
219 return from(
false,
false, p_functor, p_values );
231 public static ILiteral from(
final boolean p_at,
final boolean p_negated, @Nonnull
final IPath p_functor, @Nullable
final ITerm... p_values )
233 return from( p_at, p_negated, p_functor, ( Objects.isNull( p_values ) ) || ( p_values.length == 0 ) ? Stream.empty() : Arrays.stream( p_values ) );
245 public static ILiteral from(
final boolean p_at,
final boolean p_negated, @Nonnull
final IPath p_functor, @Nonnull
final Stream<ITerm> p_values )
247 return new CLiteral( p_at, p_negated, p_functor, p_values.collect( Collectors.toList() ) );
259 public static ILiteral parse( @Nonnull
final String p_literal )
throws Exception
261 return new CParser().
parse(
new ByteArrayInputStream( p_literal.getBytes( Charset.forName(
"UTF-8" ) ) ) ).literal();
266 public final Stream<ITerm>
values( @Nullable
final IPath... p_path )
268 return ( Objects.isNull( p_path ) ) || ( p_path.length < 1 )
269 ? m_values.values().stream()
271 ? m_values.asMap().get( p_path[0] ).stream()
272 : m_values.asMap().get( p_path[0] ).stream()
273 .filter( i -> i instanceof
ILiteral )
274 .flatMap( i -> ( (ILiteral) i ).
values( Arrays.copyOfRange( p_path, 1, p_path.length ) ) );
281 return ( Objects.isNull( p_path ) ) || ( p_path.length < 1 )
282 ? m_orderedvalues.stream().sequential()
284 ? m_orderedvalues.stream()
285 .filter( i -> i.fqnfunctor().equals( p_path[0] ) ).sequential()
286 : m_orderedvalues.stream()
287 .filter( i -> i.fqnfunctor().equals( p_path[0] ) )
288 .filter( i -> i instanceof
ILiteral )
290 .filter( Objects::nonNull )
291 .flatMap( i -> i.orderedvalues( Arrays.copyOfRange( p_path, 1, p_path.length ) ) );
297 return m_values.isEmpty();
337 final IVariable<?> l_variable = p_context.instancevariables().get( i.fqnfunctor() );
338 return ( Objects.isNull( l_variable ) ) || ( l_variable.allocated() ) ?
CRawTerm.
from( l_variable ) : l_variable;
341 return ( (ILiteral) i ).unify( p_context );
344 .collect( Collectors.toList() )
361 final IVariable<?> l_variable = p_context.instancevariables().get( i.fqnfunctor() );
362 return Objects.isNull( l_variable )
367 return ( (ILiteral) i ).unify( p_context );
370 .collect( Collectors.toList() )
378 return m_functor.
suffix();
385 return m_functor.
subpath( 0, m_functor.
size() - 1 );
397 @SuppressWarnings(
"unchecked" )
410 public final boolean equals(
final Object p_object )
412 return ( p_object instanceof
ILiteral ) && ( this.
hashCode() == p_object.hashCode() );
419 return ( Objects.isNull( p_prefix ) ) || ( p_prefix.length == 0 )
451 return Integer.compare( this.
hashCode(), p_literal.hashCode() );
458 return ( Objects.isNull( p_prefix ) ) || ( p_prefix.length == 0 )
463 m_values.values().stream().map( i -> i.deepcopy() ).collect( Collectors.toList() )
469 m_values.values().stream().map( i -> i.deepcopy() ).collect( Collectors.toList() )
479 m_values.values().stream().map( i -> i.deepcopy() ).collect( Collectors.toList() )
504 l_visitor.visit( this.
parser( p_stream ).literal_type() );
511 return TypeLexer.class;
517 return TypeParser.class;
CLiteral(final boolean p_at, final boolean p_negated, @Nonnull final IPath p_functor, @Nonnull final Collection< ITerm > p_values)
ctor
final boolean hasAt()
returns if the literal has an @ prefix
final IPath m_functor
literals functor
IPath subpath(final int p_fromindex)
creates a path of the start index until the end
final boolean m_negated
negated option
final String functor()
returns the functor without path
final ImmutableMultimap< IPath, ITerm > m_values
literal values
final boolean m_at
@ prefix is set
static final String NEGATION
negation symbol
final boolean negated()
getter of the literal for the negation
final ILiteral unify( @Nonnull final IContext p_context)
unifies variables if exists
static IPath from( @Nonnull final String p_string)
factor method to build path
parser for complex-datatypes
final synchronized ITerm deepcopysuffix()
class to create a path structure
static ILiteral from(final boolean p_at, final boolean p_negated, @Nonnull final IPath p_functor, @Nonnull final Stream< ITerm > p_values)
factory
static ILiteral from( @Nonnull final String p_functor, @Nonnull final Collection< ITerm > p_values)
factory
final Stream< ITerm > orderedvalues( @Nullable final IPath... p_path)
returns a stream over the ordered values in sequential ordering
final Class< TypeParser > parserclass()
final ILiteral allocate( @Nonnull final IContext p_context)
allocate all variables with the current context
final List< ITerm > m_orderedvalues
literal values as list
final int structurehash()
returns a hash value which defines a hash ove rthe structure
AntLR error lister to catch parser and lexer errors with individual error handling and language trans...
static final ITerm EMPTY
empty raw term
execution context with local data
static ILiteral from( @Nonnull final IPath p_functor, @Nullable final ITerm... p_values)
factory
final Stream< ITerm > values( @Nullable final IPath... p_path)
Stream< String > stream()
stream over elements
final< T > T raw()
cast to any raw value type
final int m_structurehash
hash of the structure
final Class< TypeLexer > lexerclass()
final int compareTo( @Nonnull final ILiteral p_literal)
static ILiteral from( @Nonnull final IPath p_functor, @Nonnull final Stream< ITerm > p_values)
factory
final int m_hash
hash code
final IPath functorpath()
returns the path of the functor
final boolean emptyValues()
check for empty values
final synchronized ITerm deepcopy( @Nullable final IPath... p_prefix)
final boolean hasVariable()
checks if the literal has variables
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
final P parser( @Nonnull final InputStream p_stream)
returns a parser component
static final String AT
at symbol
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
final IPath fqnfunctor()
returns the full-qualified functor with path and name
class for any helper calls
static ILiteral from(final boolean p_at, final boolean p_negated, @Nonnull final IPath p_functor, @Nullable final ITerm... p_values)
factory
static ILiteral parse( @Nonnull final String p_literal)
factory
static ILiteral from( @Nonnull final String p_functor, @Nonnull final Stream< ITerm > p_values)
stream factory
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
final ILiteral shallowcopy( @Nullable final IPath... p_prefix)
String suffix()
returns the last part of the path
final boolean equals(final Object p_object)
boolean hasVariable()
checks if the literal has variables
final ILiteral shallowcopysuffix()
static final long serialVersionUID
serial id
int size()
returns the number of path elements
final IASTVisitorType parse( @Nonnull final InputStream p_stream)
interface of a type parser
term structure for simple datatypes