/*
 *  JavaSMT is an API wrapper for a collection of SMT solvers.
 *  This file is part of JavaSMT.
 *
 *  Copyright (C) 2007-2016  Dirk Beyer
 *  All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  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 org.sosy_lab.java_smt.solvers.z3;

import com.google.common.collect.Iterables;
import com.google.common.primitives.Longs;
import com.microsoft.z3.Native;

import org.sosy_lab.java_smt.basicimpl.AbstractBooleanFormulaManager;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

class Z3BooleanFormulaManager extends AbstractBooleanFormulaManager<Long, Long, Long, Long> {

  private final long z3context;
  private final long z3true;
  private final long z3false;

  Z3BooleanFormulaManager(Z3FormulaCreator creator) {
    super(creator);
    z3context = creator.getEnv();
    z3true = Native.mkTrue(z3context);
    Native.incRef(z3context, z3true);
    z3false = Native.mkFalse(z3context);
    Native.incRef(z3context, z3false);
  }

  @Override
  protected Long makeVariableImpl(String varName) {
    long type = getFormulaCreator().getBoolType();
    return getFormulaCreator().makeVariable(type, varName);
  }

  @Override
  protected Long makeBooleanImpl(boolean pValue) {
    if (pValue) {
      return Native.mkTrue(z3context);
    } else {
      return Native.mkFalse(z3context);
    }
  }

  @Override
  protected Long not(Long pParam) {
    return Native.mkNot(z3context, pParam);
  }

  @Override
  protected Long and(Long pParam1, Long pParam2) {
    if (isTrue(pParam1)) {
      return pParam2;
    } else if (isTrue(pParam2)) {
      return pParam1;
    } else if (isFalse(pParam1)) {
      return z3false;
    } else if (isFalse(pParam2)) {
      return z3false;
    } else if (Native.isEqAst(z3context, pParam1, pParam2)) {
      return pParam1;
    }
    return Native.mkAnd(z3context, 2, new long[] {pParam1, pParam2});
  }

  @Override
  protected Long or(Long pParam1, Long pParam2) {
    if (isTrue(pParam1)) {
      return z3true;
    } else if (isTrue(pParam2)) {
      return z3true;
    } else if (isFalse(pParam1)) {
      return pParam2;
    } else if (isFalse(pParam2)) {
      return pParam1;
    } else if (Native.isEqAst(z3context, pParam1, pParam2)) {
      return pParam1;
    }
    return Native.mkOr(z3context, 2, new long[] {pParam1, pParam2});
  }

  @Override
  protected Long orImpl(Collection<Long> params) {
    Set<Long> filtered = new LinkedHashSet<>(params.size());
    for (Long param : params) {
      if (isFalse(param)) {
        // ignore
      } else if (isTrue(param)) {
        return z3true;
      } else {
        // heuristic: identical terms might have identical pointers, filter duplicates.
        filtered.add(param);
      }
    }

    switch (filtered.size()) {
      case 0:
        return z3false;
      case 1:
        return Iterables.getOnlyElement(filtered);
      default:
        return Native.mkOr(z3context, filtered.size(), Longs.toArray(filtered));
    }
  }

  @Override
  protected Long andImpl(Collection<Long> params) {
    Set<Long> filtered = new LinkedHashSet<>(params.size());
    for (Long param : params) {
      if (isFalse(param)) {
        return z3false;
      } else if (isTrue(param)) {
        // ignore
      } else {
        // heuristic: identical terms might have identical pointers, filter duplicates.
        filtered.add(param);
      }
    }

    switch (filtered.size()) {
      case 0:
        return z3true;
      case 1:
        return Iterables.getOnlyElement(filtered);
      default:
        return Native.mkAnd(z3context, filtered.size(), Longs.toArray(filtered));
    }
  }

  @Override
  protected Long xor(Long pParam1, Long pParam2) {
    if (isFalse(pParam1)) {
      return pParam2;
    } else if (isFalse(pParam2)) {
      return pParam1;
    }
    return Native.mkXor(z3context, pParam1, pParam2);
  }

  @Override
  protected Long equivalence(Long pBits1, Long pBits2) {
    return Native.mkEq(z3context, pBits1, pBits2);
  }

  @Override
  protected Long implication(Long pBits1, Long pBits2) {
    return Native.mkImplies(z3context, pBits1, pBits2);
  }

  @Override
  protected boolean isTrue(Long pParam) {
    return Native.isEqAst(z3context, pParam, z3true);
  }

  @Override
  protected boolean isFalse(Long pParam) {
    return Native.isEqAst(z3context, pParam, z3false);
  }

  @Override
  protected Long ifThenElse(Long pCond, Long pF1, Long pF2) {
    return Native.mkIte(z3context, pCond, pF1, pF2);
  }
}

@@ -779,52 +779,8 @@
 3;%0A%0A
-import com.google.common.collect.Iterables;%0A
 impo
@@ -954,62 +954,8 @@
 ion;
-%0Aimport java.util.LinkedHashSet;%0Aimport java.util.Set;
 %0A%0Acl
@@ -2827,504 +2827,8 @@
 ) %7B%0A
-    Set%3CLong%3E filtered = new LinkedHashSet%3C%3E(params.size());%0A    for (Long param : params) %7B%0A      if (isFalse(param)) %7B%0A        // ignore%0A      %7D else if (isTrue(param)) %7B%0A        return z3true;%0A      %7D else %7B%0A        // heuristic: identical terms might have identical pointers, filter duplicates.%0A        filtered.add(param);%0A      %7D%0A    %7D%0A%0A    switch (filtered.size()) %7B%0A      case 0:%0A        return z3false;%0A      case 1:%0A        return Iterables.getOnlyElement(filtered);%0A      default:%0A    
     
@@ -2853,32 +2853,30 @@
 (z3context, 
-filtered
+params
 .size(), Lon
@@ -2886,33 +2886,25 @@
 toArray(
-filtered));%0A    %7D
+params));
 %0A  %7D%0A%0A  
@@ -2969,504 +2969,8 @@
 ) %7B%0A
-    Set%3CLong%3E filtered = new LinkedHashSet%3C%3E(params.size());%0A    for (Long param : params) %7B%0A      if (isFalse(param)) %7B%0A        return z3false;%0A      %7D else if (isTrue(param)) %7B%0A        // ignore%0A      %7D else %7B%0A        // heuristic: identical terms might have identical pointers, filter duplicates.%0A        filtered.add(param);%0A      %7D%0A    %7D%0A%0A    switch (filtered.size()) %7B%0A      case 0:%0A        return z3true;%0A      case 1:%0A        return Iterables.getOnlyElement(filtered);%0A      default:%0A    
     
@@ -3000,24 +3000,22 @@
 ontext, 
-filtered
+params
 .size(),
@@ -3033,25 +3033,17 @@
 ray(
-filtered));%0A    %7D
+params));
 %0A  %7D
@@ -3111,121 +3111,8 @@
 ) %7B%0A
-    if (isFalse(pParam1)) %7B%0A      return pParam2;%0A    %7D else if (isFalse(pParam2)) %7B%0A      return pParam1;%0A    %7D%0A
     

Revert "Z3-wrapper: adding simplification for AND/OR with multiple params"