<commit_before>/*
 * Copyright (C) 2013 The Conclusions Authors
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file without in compliance with the License. You may obtain a copy
 * of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package ru.rulex.conclusion;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

import ru.rulex.conclusion.ParserBuilders.*;
import ru.rulex.conclusion.delegate.ProxyUtils;
import ru.rulex.conclusion.execution.ParallelStrategy;

import java.lang.reflect.Array;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

import static ru.rulex.conclusion.execution.Callables.*;

/**
 * <pre>
 *                         <b> Classes hierarchy (single object oriented)</b>
 *                            __________________________________________                                  __________________
 *                           |   EventOrientedPhraseBuilder            |-------------------------------->|  AbstractPhrase |
 *                           |_________________________________________|                                 |_________________|
 *                                             |                                                      ___________|____________
 *                          ___________________|______________________                               |                       |
 *                         |    AbstractEventOrientedBuilderImpl     |
 *                         |_________________________________________|
 *                                             |
 *                                             |
 *         ____________________________________|_____________________________________________
 *        |                        |                                |                       |
 *        |        ________________|____________________            |            ___________|_____________________
 *        |       |   GuiceEventOrientedPhrasesBuilder |            |           |DaggerEventOrientedPhrasesBuilder|
 *        |       |____________________________________|            |           |_________________________________|
 *  ______|_______________________    ______________________________|_____________
 * | EventOrientedPhrasesBuilder |   | EventOrientedFactConsequencePhrasesBuilder|
 * |_____________________________|   |___________________________________________|
 *
 *                             <b> Classes hierarchy (collection oriented)</b>
 *                             ___________________________________________                            ___________________
 *                            |   AbstractIterableOrientedPhrasesBuilder |-------------------------->| IterablePhrases  |
 *                            |__________________________________________|                           |__________________|
 *                                                 |                                              ____________|_____________
 *                         ________________________|___________________                          |                         |
 *   _____________________|______________________________              |
 *  | AbstractConclusionCollectionExecutionPhrasesBuilder|             |
 *  |____________________________________________________|             |
 *                       |                                             |
 *                       |                                             |
 *                       |                                             |
 *                       |                       ______________________|____________________
 *                       |                      |AbstractImperativeExecutionPhrasesBuilder |
 *                       |                      |__________________________________________|
 *  _____________________|____________________                       |
 * |IterableValidationCollectionPhrasesBuilder|                      |
 * |__________________________________________|                      |
 *                                                    _______________|___________________
 *                                                   | ImperativeExecutionPhrasesBuilder|
 *                                                   |__________________________________|
 * </pre>
 */
public final class PhraseBuildersFacade
{

  private PhraseBuildersFacade()
  {
  }

  /**
   * {@code AbstractEventOrientedPhraseBuilder} class is a root of class
   * hierarchy define a base abstraction for single event oriented
   * PhraseBuilders
   * <p/>
   * This class extended for implementation specific behavior.
   * <p/>
   * <p/>
   * <b>Note:</b> {@code AbstractEventOrientedPhraseBuilder}'s class hierarchy
   * is a abstraction part of <b>pattern bridge </b>. Every subclass relies on a
   * set of abstract operations, where several implementations of the set of
   * abstract operations are possible. The implementation part (Bridge Pattern)
   * is a {@code AbstractPhrase} hierarchy
   * <p/>
   * <p/>
   * See wiki article on <a href=<a
   * href="http://en.wikipedia.org/wiki/Bridge_pattern"> Builder Design
   * Pattern</a>.
   */
  public static abstract class AbstractEventOrientedPhraseBuilder<T> {

    protected final AbstractPhrase<T, ?> phrase;

    protected ParallelStrategy<Boolean> pStrategy;

    private static final Logger logger = Logger.getLogger( AbstractEventOrientedPhraseBuilder.class );

    private AbstractEventOrientedPhraseBuilder( AbstractPhrase<T, ?> phrase,
                                              ParallelStrategy<Boolean> pStrategy )
    {
      this.phrase = phrase;
      this.pStrategy = pStrategy;
    }

    protected abstract AbstractPhrase<T, ?> getPhrase();

    protected void setParallelStrategy( ParallelStrategy<Boolean> pStrategy )
    {
      this.pStrategy = pStrategy;
    }

    protected void setEvent( T event )
    {
      getPhrase().setEvent( event );
    }

    public Boolean sync( final T event )
    {
      try
      {
        return call( obtain( pStrategy.lift( createFunction( event ) ).apply( this ) ) );
      }
      catch ( Exception e )
      {
        logger.error( e.getMessage() );
      }
      return false;
    }

    public CheckedFuture<Boolean, PhraseExecutionException> async( final T event )
    {
      return pStrategy.lift( this.createFunction( event ) ).apply( this );
    }

    public Boolean sync( final T event, Runnable callback )
    {
      return null;
      //return sync( event, callback, MoreExecutors.sameThreadExecutor() );
    }

    protected <E extends AbstractEventOrientedPhraseBuilder<T>> ConclusionFunction<E, Boolean> createFunction(
            final T event )
    {
      return new ConclusionFunction<E, Boolean>()
      {
        @Override
        public Boolean apply( E arg )
        {
          setEvent( event );
          return getPhrase().evaluate();
        }
      };
    }
  }

  public static abstract class BaseEventOrientedPhraseBuilder<T> extends AbstractEventOrientedPhraseBuilder<T>
  {
    private BaseEventOrientedPhraseBuilder( ImmutableAbstractPhrase<T> phrase,
                                              ParallelStrategy<Boolean> pStrategy )
    {<commit_msg>Fix class diagram in class PhraseBuildersFacade<commit_after>/*
 * Copyright (C) 2013 The Conclusions Authors
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file without in compliance with the License. You may obtain a copy
 * of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package ru.rulex.conclusion;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

import ru.rulex.conclusion.ParserBuilders.*;
import ru.rulex.conclusion.delegate.ProxyUtils;
import ru.rulex.conclusion.execution.ParallelStrategy;

import java.lang.reflect.Array;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executor;

import org.apache.log4j.Logger;

import static ru.rulex.conclusion.execution.Callables.*;

/**
 * <pre>
 *                         <b> Classes hierarchy (single object oriented)</b>
 *                            __________________________________________                                  __________________
 *                           |   AbstractEventOrientedPhraseBuilder    |-------------------------------->|  AbstractPhrase |
 *                           |_________________________________________|                                 |_________________|
 *                                             |             |                                         ___________|____________
 *                          ___________________|_____________|_________                               |                       |
 *                         |    BaseEventOrientedPhraseBuilder       |
 *                         |_________________________________________|
 *                                             |             |
 *                                             |             |
 *         ____________________________________|_____________|________________________________
 *        |                        |                         |       |                       |
 *        |        ________________|____________________     |       |            ___________|_____________________
 *        |       |   GuiceEventOrientedPhrasesBuilder |     |       |           |    DaggerEventPhrasesBuilder    |
 *        |       |____________________________________|     |       |           |_________________________________|
 *  ______|_______________________ |  _______________________|_______|_____________
 * | EventOrientedPhrasesBuilder | | | EventOrientedFactConsequencePhrasesBuilder|
 * |_____________________________| | |___________________________________________|
 *               __________________|__________________       |
 *              | SimpleEventOrientedPhrasesBuilder  |       |
 *              |____________________________________|       |
 *                                                           |
 *                             <b> Classes hierarchy (collection oriented)</b>
 *                             ______________________________|____________                            ___________________
 *                            |   AbstractIterableOrientedPhrasesBuilder |-------------------------->| IterablePhrases  |
 *                            |__________________________________________|                           |__________________|
 *                                                 |         |                                     ____________|_____________
 *                         ________________________|_________|__________                          |                         |
 *   _____________________|______________________________    |         |
 *  | AbstractConclusionCollectionExecutionPhrasesBuilder|   |         |
 *  |____________________________________________________|   |         |
 *                       |                                   |         |
 *                       |                                   |         |
 *                       |                                   |         |
 *                       |                       ____________|_________|____________________
 *                       |                      |AbstractImperativeExecutionPhrasesBuilder |
 *                       |                      |__________________________________________|
 *  _____________________|____________________               |        |
 * |IterableValidationCollectionPhrasesBuilder|              |        |
 * |__________________________________________|              |        |
 *                                                    _______|________|___________________
 *                                                   | ImperativeExecutionPhrasesBuilder|
 *                                                   |__________________________________|
 *                                                           |
 *                                       ____________________|_______________________
 *                                      | AbstractMutableEventOrientedPhraseBuilder |
 *                                      |___________________________________________|
 *                                                        |
 *                                           _____________|__________________
 *                                          |DaggerMutableEventPhraseBuilder|
 * </pre>                                   |_______________________________|
 */
public final class PhraseBuildersFacade
{

  private PhraseBuildersFacade()
  {
  }

  /**
   * {@code AbstractEventOrientedPhraseBuilder} class is a root of class
   * hierarchy define a base abstraction for single event oriented
   * PhraseBuilders
   * <p/>
   * This class extended for implementation specific behavior.
   * <p/>
   * <p/>
   * <b>Note:</b> {@code AbstractEventOrientedPhraseBuilder}'s class hierarchy
   * is a abstraction part of <b>pattern bridge </b>. Every subclass relies on a
   * set of abstract operations, where several implementations of the set of
   * abstract operations are possible. The implementation part (Bridge Pattern)
   * is a {@code AbstractPhrase} hierarchy
   * <p/>
   * <p/>
   * See wiki article on <a href=<a
   * href="http://en.wikipedia.org/wiki/Bridge_pattern"> Builder Design
   * Pattern</a>.
   */
  public static abstract class AbstractEventOrientedPhraseBuilder<T> {

    protected final AbstractPhrase<T, ?> phrase;

    protected ParallelStrategy<Boolean> pStrategy;

    private static final Logger logger = Logger.getLogger( AbstractEventOrientedPhraseBuilder.class );

    private AbstractEventOrientedPhraseBuilder( AbstractPhrase<T, ?> phrase,
                                              ParallelStrategy<Boolean> pStrategy )
    {
      this.phrase = phrase;
      this.pStrategy = pStrategy;
    }

    protected abstract AbstractPhrase<T, ?> getPhrase();

    protected void setParallelStrategy( ParallelStrategy<Boolean> pStrategy )
    {
      this.pStrategy = pStrategy;
    }

    protected void setEvent( T event )
    {
      getPhrase().setEvent( event );
    }

    public Boolean sync( final T event )
    {
      try
      {
        return call( obtain( pStrategy.lift( createFunction( event ) ).apply( this ) ) );
      }
      catch ( Exception e )
      {
        logger.error( e.getMessage() );
      }
      return false;
    }

    public CheckedFuture<Boolean, PhraseExecutionException> async( final T event )
    {
      return pStrategy.lift( this.createFunction( event ) ).apply( this );
    }

    public Boolean sync( final T event, Runnable callback )
    {
      return sync( event, callback, MoreExecutors.sameThreadExecutor() );
    }

    public Boolean sync( final T event, Runnable callback, Executor callbackExecutor )
    {
      try
      {
        return call( obtain( pStrategy.lift( createFunction( event ) ).apply( this ), callback, callbackExecutor ) );
      } catch ( Exception ex )
      {
        logger.error( ex.getMessage() );
      }
      return false;
    }

    protected <E extends AbstractEventOrientedPhraseBuilder<T>> ConclusionFunction<E, Boolean> createFunction(
            final T event )
    {
      return new ConclusionFunction<E, Boolean>()
      {
        @Override
        public Boolean apply( E arg )
        {
          setEvent( event );
          return getPhrase().evaluate();
        }
      };
    }
  }

  public static abstract class BaseEventOrientedPhraseBuilder<T> extends AbstractEventOrientedPhraseBuilder<T>
  {
    private BaseEventOrientedPhraseBuilder( ImmutableAbstractPhrase<T> phrase,
                                              ParallelStrategy<Boolean> pStrategy )
    {
{'fraction_non_alphanumeric': 0.07755746622214424, 'fraction_numerical': 0.001052816283558519, 'mean_word_length': 1.6512637618235386, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 115, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2012404', 'n_tokens_mistral': 3616, 'n_tokens_neox': 3385, 'n_words': 776}