<commit_before>/*
 *  JavaSMT is an API wrapper for a collection of SMT solvers.
 *  This file is part of JavaSMT.
 *
 *  Copyright (C) 2007-2016  <NAME>
 *  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.princess;

import static scala.collection.JavaConversions.asScalaBuffer;
import static scala.collection.JavaConversions.seqAsJavaList;

import ap.SimpleAPI;
import ap.SimpleAPI.ConstantLoc;
import ap.SimpleAPI.IntFunctionLoc;
import ap.SimpleAPI.IntValue;
import ap.SimpleAPI.ModelLocation;
import ap.SimpleAPI.ModelValue;
import ap.SimpleAPI.PartialModel;
import ap.SimpleAPI.PredicateLoc;
import ap.basetypes.IdealInt;
import ap.parser.IAtom;
import ap.parser.IExpression;
import ap.parser.IFunApp;
import ap.parser.ITerm;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.sosy_lab.java_smt.api.Formula;
import org.sosy_lab.java_smt.basicimpl.AbstractModel.CachingAbstractModel;
import org.sosy_lab.java_smt.basicimpl.FormulaCreator;
import scala.Option;
import scala.Tuple2;
import scala.collection.Iterator;

class PrincessModel
    extends CachingAbstractModel<IExpression, PrincessTermType, PrincessEnvironment> {
  private final PartialModel model;

  PrincessModel(
      PartialModel partialModel,
      FormulaCreator<IExpression, PrincessTermType, PrincessEnvironment, ?> creator) {
    super(creator);
    this.model = partialModel;
  }

  @Nullable
  @Override
  public Object evaluateImpl(IExpression f) {
    Option<ModelValue> out = model.evalExpression(f);
    if (out.isEmpty()) {
      return null;
    }
    ModelValue value = out.get();
    return getValue(value);
  }

  @Override
  protected ImmutableList<ValueAssignment> modelToList() {
    scala.collection.Map<ModelLocation, ModelValue> interpretation = model.interpretation();

    // first get the addresses of arrays
    Map<IdealInt, ITerm> arrays = getArrayAddresses(interpretation);

    // then iterate over the model and generate the assignments
    Builder<ValueAssignment> assignments = ImmutableSet.builder();
    Iterator<Tuple2<ModelLocation, ModelValue>> it2 = interpretation.iterator();
    while (it2.hasNext()) {
      Tuple2<ModelLocation, ModelValue> entry = it2.next();
      ValueAssignment assignment = getAssignment(entry._1, entry._2, arrays);
      if (assignment != null) {
        assignments.add(assignment);
      }
    }

    return assignments.build().asList();
  }

  /**
   * Collect array-models, we need them to replace identifiers later. Princess models arrays as
   * plain numeric "memory-addresses", and the model for an array-access at one of the addresses is
   * the array-content. Example: "arr[5]=123" is modeled as "{arr=0, select(0,5)=123}" or "{arr=0,
   * store(0,5,123)=0}", where "0" is the memory-address. The returned mapping contains the mapping
   * of "0" (=address) to "arr" (=identifier).
   */
  private Map<IdealInt, ITerm> getArrayAddresses(
      scala.collection.Map<ModelLocation, ModelValue> interpretation) {
    Map<IdealInt, ITerm> arrays = new HashMap<>();
    Iterator<Tuple2<ModelLocation, ModelValue>> it1 = interpretation.iterator();
    while (it1.hasNext()) {
      Tuple2<ModelLocation, ModelValue> entry = it1.next();
      if (entry._1 instanceof ConstantLoc) {
        ITerm maybeArray = IExpression.i(((ConstantLoc) entry._1).c());
        if (creator.getEnv().hasArrayType(maybeArray) && entry._2 instanceof IntValue) {
          arrays.put(((IntValue) entry._2).v(), maybeArray);
        }
      }
    }
    return arrays;
  }

  private @Nullable ValueAssignment getAssignment(
      ModelLocation key, ModelValue value, Map<IdealInt, ITerm> arrays) {
    Object fValue = getValue(value);
    if (key instanceof PredicateLoc) {
      Formula fKey =<commit_msg>simplification<commit_after>/*
 *  JavaSMT is an API wrapper for a collection of SMT solvers.
 *  This file is part of JavaSMT.
 *
 *  Copyright (C) 2007-2016  <NAME>
 *  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.princess;

import static scala.collection.JavaConversions.asJavaIterable;
import static scala.collection.JavaConversions.asScalaBuffer;
import static scala.collection.JavaConversions.seqAsJavaList;

import ap.SimpleAPI;
import ap.SimpleAPI.ConstantLoc;
import ap.SimpleAPI.IntFunctionLoc;
import ap.SimpleAPI.IntValue;
import ap.SimpleAPI.ModelLocation;
import ap.SimpleAPI.ModelValue;
import ap.SimpleAPI.PartialModel;
import ap.SimpleAPI.PredicateLoc;
import ap.basetypes.IdealInt;
import ap.parser.IAtom;
import ap.parser.IExpression;
import ap.parser.IFunApp;
import ap.parser.ITerm;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.sosy_lab.java_smt.api.Formula;
import org.sosy_lab.java_smt.basicimpl.AbstractModel.CachingAbstractModel;
import org.sosy_lab.java_smt.basicimpl.FormulaCreator;
import scala.Option;
import scala.Tuple2;
import scala.collection.Iterator;

class PrincessModel
    extends CachingAbstractModel<IExpression, PrincessTermType, PrincessEnvironment> {
  private final PartialModel model;

  PrincessModel(
      PartialModel partialModel,
      FormulaCreator<IExpression, PrincessTermType, PrincessEnvironment, ?> creator) {
    super(creator);
    this.model = partialModel;
  }

  @Nullable
  @Override
  public Object evaluateImpl(IExpression f) {
    Option<ModelValue> out = model.evalExpression(f);
    if (out.isEmpty()) {
      return null;
    }
    ModelValue value = out.get();
    return getValue(value);
  }

  @Override
  protected ImmutableList<ValueAssignment> modelToList() {
    scala.collection.Map<ModelLocation, ModelValue> interpretation = model.interpretation();

    // first get the addresses of arrays
    Map<IdealInt, ITerm> arrays = getArrayAddresses(interpretation);

    // then iterate over the model and generate the assignments
    Builder<ValueAssignment> assignments = ImmutableSet.builder();
    for (Tuple2<ModelLocation, ModelValue> entry : asJavaIterable(interpretation)) {
      ValueAssignment assignment = getAssignment(entry._1, entry._2, arrays);
      if (assignment != null) {
        assignments.add(assignment);
      }
    }

    return assignments.build().asList();
  }

  /**
   * Collect array-models, we need them to replace identifiers later. Princess models arrays as
   * plain numeric "memory-addresses", and the model for an array-access at one of the addresses is
   * the array-content. Example: "arr[5]=123" is modeled as "{arr=0, select(0,5)=123}" or "{arr=0,
   * store(0,5,123)=0}", where "0" is the memory-address. The returned mapping contains the mapping
   * of "0" (=address) to "arr" (=identifier).
   */
  private Map<IdealInt, ITerm> getArrayAddresses(
      scala.collection.Map<ModelLocation, ModelValue> interpretation) {
    Map<IdealInt, ITerm> arrays = new HashMap<>();
    for (Tuple2<ModelLocation, ModelValue> entry : asJavaIterable(interpretation)) {
      if (entry._1 instanceof ConstantLoc) {
        ITerm maybeArray = IExpression.i(((ConstantLoc) entry._1).c());
        if (creator.getEnv().hasArrayType(maybeArray) && entry._2 instanceof IntValue) {
          arrays.put(((IntValue) entry._2).v(), maybeArray);
        }
      }
    }
    return arrays;
  }

  private @Nullable ValueAssignment getAssignment(
      ModelLocation key, ModelValue value, Map<IdealInt, ITerm> arrays) {
    Object fValue = getValue(value);
    if (key instanceof PredicateLoc) {
      Formula fKey =
{'fraction_non_alphanumeric': 0.1013468756899978, 'fraction_numerical': 0.009715168911459484, 'mean_word_length': 4.179531160663236, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3954783', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2646, 'n_words': 739}