LightJason - AgentSpeak(L++)
CRecursive.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.language.unify;
25 
26 import com.codepoetics.protonpack.StreamUtils;
33 
34 import java.util.List;
35 import java.util.Set;
36 import java.util.stream.Collectors;
37 import java.util.stream.Stream;
38 
39 
45 public final class CRecursive implements IUnifier.IAlgorithm
46 {
47 
48  @Override
49  public final <T extends ITerm> boolean unify( final Set<IVariable<?>> p_variables, final Stream<T> p_source, final Stream<T> p_target )
50  {
51  final List<T> l_target = p_target.collect( Collectors.toList() );
52  final List<T> l_source = p_source.collect( Collectors.toList() );
53 
54  if ( ( l_target.isEmpty() ) || ( l_source.isEmpty() ) )
55  return true;
56 
57  if ( l_target.size() != l_source.size() )
58  return false;
59 
60  return StreamUtils.zip(
61  l_source.stream(),
62  l_target.stream(),
63  ( s, t ) ->
64  {
65 
66  // if s and t are variable create a realocated variable for backtracking
67  if ( ( t instanceof IVariable<?> ) && ( s instanceof IVariable<?> ) )
68  {
69  p_variables.add(
70  ( (IVariable<?>) t ).mutex()
71  ? new CRelocateMutexVariable<>( t.fqnfunctor(), (IVariable<?>) s )
72  : new CRelocateVariable<>( t.fqnfunctor(), (IVariable<?>) s )
73  );
74  return true;
75  }
76 
77  // if target type is a variable set the value
78  if ( t instanceof IVariable<?> )
79  {
80  p_variables.add( t.<IVariable<Object>>term().set( s ) );
81  return true;
82  }
83 
84  // if both raw values -> equality check
85  if ( ( s instanceof IRawTerm<?> ) || ( t instanceof IRawTerm<?> ) )
86  return s.equals( t );
87 
88  // if a literal exists -> source and target literal must be equal with the functor -> recursive descent
89  if ( ( s instanceof ILiteral ) && ( t instanceof ILiteral ) )
90  {
91  final ILiteral l_sourceliteral = (ILiteral) s;
92  final ILiteral l_targetliteral = (ILiteral) t;
93 
94  if ( !l_sourceliteral.fqnfunctor().equals( l_targetliteral.fqnfunctor() ) )
95  return false;
96 
97  return this.unify( p_variables, l_sourceliteral.orderedvalues(), l_targetliteral.orderedvalues() );
98  }
99 
100  // otherwise false
101  return false;
102  }
103  ).allMatch( i -> i );
104  }
105 
106 }
Stream< ITerm > orderedvalues( @Nullable final IPath... p_path)
returns a stream over the ordered values in sequential ordering
interface of an unification algorithm
Definition: IUnifier.java:43
IPath fqnfunctor()
returns the full-qualified functor with path and name
final< T extends ITerm > boolean unify(final Set< IVariable<?>> p_variables, final Stream< T > p_source, final Stream< T > p_target)
unify algorithm
Definition: CRecursive.java:49