Program: 166717
Project: 166717
Buggy source:
static boolean dedSecondLayerVariableUnification(final Task task,final NAL nal){
  final Sentence taskSentence=task.sentence;
  if (taskSentence == null || taskSentence.isQuestion() || taskSentence.isQuest()) {
    return false;
  }
  Term first=taskSentence.content;
  if (!first.hasVar()) {
    return false;
  }
  if (!((first instanceof Disjunction || first instanceof Conjunction || first instanceof Equivalence|| first instanceof Implication))) {
    return false;
  }
  boolean unifiedAnything=false;
  int remainingUnifications=1;
  int maxUnificationAttempts=1;
  ArrayList<Term> terms_dependent=null;
  ArrayList<Term> terms_independent=null;
  HashMap<Term,Term> Values=null;
  HashMap<Term,Term> Values2=null;
  HashMap<Term,Term> Values3=null;
  HashMap<Term,Term> Values4=null;
  HashMap<Term,Term> smap=null;
  for (int k=0; k < maxUnificationAttempts; k++) {
    Concept secondConcept=nal.mem().sampleNextConcept();
    if (secondConcept == null) {
      break;
    }
    if (secondConcept.term.equals(first)) {
      continue;
    }
    Term secterm=secondConcept.term;
    Sentence second_belief=secondConcept.getBeliefRandomByConfidence();
    if (second_belief == null)     continue;
    TruthValue truthSecond=second_belief.truth;
    if (terms_dependent == null) {
      final int initialTermListSize=8;
      terms_dependent=new ArrayList<>(initialTermListSize);
      terms_independent=new ArrayList<>(initialTermListSize);
      Values=newVariableSubstitutionMap();
      Values2=newVariableSubstitutionMap();
      Values3=newVariableSubstitutionMap();
      Values4=newVariableSubstitutionMap();
      smap=newVariableSubstitutionMap();
    }
    terms_dependent.clear();
    terms_independent.clear();
    Term[] components_level1=((CompoundTerm)first).term;
    Term secterm_unwrap=unwrapNegation(secterm);
    for (    final Term T1 : components_level1) {
      Term T1_unwrap=unwrapNegation(T1);
      Values.clear();
      smap.clear();
      if (Variables.findSubstitute(Symbols.VAR_DEPENDENT,T1_unwrap,secterm_unwrap,Values,smap)) {
        CompoundTerm ctaskterm_subs=(CompoundTerm)first;
        ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values);
        Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
        if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
          terms_dependent.add(taskterm_subs);
        }
      }
      Values2.clear();
      smap.clear();
      if (Variables.findSubstitute(Symbols.VAR_INDEPENDENT,T1_unwrap,secterm_unwrap,Values2,smap)) {
        CompoundTerm ctaskterm_subs=(CompoundTerm)first;
        ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values2);
        Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
        if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
          terms_independent.add(taskterm_subs);
        }
      }
      if (!((T1_unwrap instanceof Implication) || (T1_unwrap instanceof Equivalence) || (T1_unwrap instanceof Conjunction)|| (T1_unwrap instanceof Disjunction))) {
        continue;
      }
      if (T1_unwrap instanceof CompoundTerm) {
        Term[] components_level2=((CompoundTerm)T1_unwrap).term;
        for (        final Term T2 : components_level2) {
          Term T2_unwrap=unwrapNegation(T2);
          Values3.clear();
          smap.clear();
          if (Variables.findSubstitute(Symbols.VAR_DEPENDENT,T2_unwrap,secterm_unwrap,Values3,smap)) {
            CompoundTerm ctaskterm_subs=(CompoundTerm)first;
            ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values3);
            Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
            if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
              terms_dependent.add(taskterm_subs);
            }
          }
          Values4.clear();
          smap.clear();
          if (Variables.findSubstitute(Symbols.VAR_INDEPENDENT,T2_unwrap,secterm_unwrap,Values4,smap)) {
            CompoundTerm ctaskterm_subs=(CompoundTerm)first;
            ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values4);
            Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
            if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
              terms_independent.add(taskterm_subs);
            }
          }
        }
      }
    }
    if (taskSentence.truth == null)     throw new RuntimeException("String_Node_Str" + taskSentence);
    Stamp ss=new Stamp(taskSentence.stamp,second_belief.stamp,nal.getTime());
    dedSecondLayerVariableUnificationTerms(nal,task,second_belief,ss,terms_dependent,anonymousAnalogy(taskSentence.truth,truthSecond),taskSentence.truth,truthSecond,false);
    dedSecondLayerVariableUnificationTerms(nal,task,second_belief,ss,terms_independent,deduction(taskSentence.truth,truthSecond),taskSentence.truth,truthSecond,true);
    final int termsIndependent=terms_independent.size();
    for (int i=0; i < termsIndependent; i++) {
      Term result=terms_independent.get(i);
      if (invalidSentenceContent(result)) {
        continue;
      }
      TruthValue truth=deduction(taskSentence.truth,truthSecond);
      char mark=Symbols.JUDGMENT_MARK;
      if (taskSentence.isGoal() || second_belief.isGoal()) {
        truth=TruthFunctions.abduction(taskSentence.truth,truthSecond);
        mark=Symbols.GOAL_MARK;
      }
      Stamp useEvidentalBase=new Stamp(taskSentence.stamp,second_belief.stamp,nal.getTime());
      Sentence newSentence=new Sentence(result,mark,truth,new Stamp(taskSentence.stamp,nal.getTime(),useEvidentalBase));
      BudgetValue budget=BudgetFunctions.compoundForward(truth,newSentence.content,nal);
      if (budget.aboveThreshold()) {
        Task newTask=new Task(newSentence,budget,task,null);
        Task dummy=new Task(second_belief,budget,task,null);
        nal.setCurrentBelief(taskSentence);
        nal.setCurrentTask(dummy);
        if (nal.derivedTask(newTask,false,false,task,second_belief)) {
          nal.mem().logic.DED_SECOND_LAYER_VARIABLE_UNIFICATION.commit();
          nal.emit(Events.ConceptUnification.class,newTask,first,secondConcept,second_belief);
          unifiedAnything=true;
        }
      }
    }
    remainingUnifications--;
    if (remainingUnifications == 0) {
      break;
    }
  }
  return unifiedAnything;
}

Fixed version (ground truth):
static boolean dedSecondLayerVariableUnification(final Task task,final NAL nal){
  final Sentence taskSentence=task.sentence;
  if (taskSentence == null || taskSentence.isQuestion() || taskSentence.isQuest()) {
    return false;
  }
  Term first=taskSentence.content;
  if (!first.hasVar()) {
    return false;
  }
  if (!((first instanceof Disjunction || first instanceof Conjunction || first instanceof Equivalence|| first instanceof Implication))) {
    return false;
  }
  boolean unifiedAnything=false;
  int remainingUnifications=1;
  int maxUnificationAttempts=1;
  ArrayList<Term> terms_dependent=null;
  ArrayList<Term> terms_independent=null;
  HashMap<Term,Term> Values=null;
  HashMap<Term,Term> Values2=null;
  HashMap<Term,Term> Values3=null;
  HashMap<Term,Term> Values4=null;
  HashMap<Term,Term> smap=null;
  for (int k=0; k < maxUnificationAttempts; k++) {
    Concept secondConcept=nal.mem().sampleNextConcept();
    if (secondConcept == null) {
      break;
    }
    if (secondConcept.term.equals(first)) {
      continue;
    }
    Term secterm=secondConcept.term;
    Sentence second_belief=secondConcept.getBeliefRandomByConfidence();
    if (second_belief == null)     continue;
    TruthValue truthSecond=second_belief.truth;
    if (terms_dependent == null) {
      final int initialTermListSize=8;
      terms_dependent=new ArrayList<>(initialTermListSize);
      terms_independent=new ArrayList<>(initialTermListSize);
      Values=newVariableSubstitutionMap();
      Values2=newVariableSubstitutionMap();
      Values3=newVariableSubstitutionMap();
      Values4=newVariableSubstitutionMap();
      smap=newVariableSubstitutionMap();
    }
    terms_dependent.clear();
    terms_independent.clear();
    Term[] components_level1=((CompoundTerm)first).term;
    Term secterm_unwrap=unwrapNegation(secterm);
    for (    final Term T1 : components_level1) {
      Term T1_unwrap=unwrapNegation(T1);
      Values.clear();
      smap.clear();
      if (Variables.findSubstitute(Symbols.VAR_DEPENDENT,T1_unwrap,secterm_unwrap,Values,smap)) {
        CompoundTerm ctaskterm_subs=(CompoundTerm)first;
        ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values);
        Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
        if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
          terms_dependent.add(taskterm_subs);
        }
      }
      Values2.clear();
      smap.clear();
      if (Variables.findSubstitute(Symbols.VAR_INDEPENDENT,T1_unwrap,secterm_unwrap,Values2,smap)) {
        CompoundTerm ctaskterm_subs=(CompoundTerm)first;
        ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values2);
        Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
        if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
          terms_independent.add(taskterm_subs);
        }
      }
      if (!((T1_unwrap instanceof Implication) || (T1_unwrap instanceof Equivalence) || (T1_unwrap instanceof Conjunction)|| (T1_unwrap instanceof Disjunction))) {
        continue;
      }
      if (T1_unwrap instanceof CompoundTerm) {
        Term[] components_level2=((CompoundTerm)T1_unwrap).term;
        for (        final Term T2 : components_level2) {
          Term T2_unwrap=unwrapNegation(T2);
          Values3.clear();
          smap.clear();
          if (Variables.findSubstitute(Symbols.VAR_DEPENDENT,T2_unwrap,secterm_unwrap,Values3,smap)) {
            CompoundTerm ctaskterm_subs=(CompoundTerm)first;
            ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values3);
            Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
            if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
              terms_dependent.add(taskterm_subs);
            }
          }
          Values4.clear();
          smap.clear();
          if (Variables.findSubstitute(Symbols.VAR_INDEPENDENT,T2_unwrap,secterm_unwrap,Values4,smap)) {
            CompoundTerm ctaskterm_subs=(CompoundTerm)first;
            ctaskterm_subs=ctaskterm_subs.applySubstituteToCompound(Values4);
            Term taskterm_subs=reduceUntilLayer2(ctaskterm_subs,secterm,nal.mem());
            if (taskterm_subs != null && !(Variables.indepVarUsedInvalid(taskterm_subs))) {
              terms_independent.add(taskterm_subs);
            }
          }
        }
      }
    }
    if (taskSentence.truth == null)     throw new RuntimeException("String_Node_Str" + taskSentence);
    Stamp ss=new Stamp(taskSentence.stamp,second_belief.stamp,nal.getTime());
    dedSecondLayerVariableUnificationTerms(nal,task,second_belief,ss,terms_dependent,anonymousAnalogy(taskSentence.truth,truthSecond),taskSentence.truth,truthSecond,false);
    dedSecondLayerVariableUnificationTerms(nal,task,second_belief,ss,terms_independent,deduction(taskSentence.truth,truthSecond),taskSentence.truth,truthSecond,true);
    final int termsIndependent=terms_independent.size();
    for (int i=0; i < termsIndependent; i++) {
      Term result=terms_independent.get(i);
      if (Sentence.invalidSentenceTerm(result)) {
        continue;
      }
      TruthValue truth=deduction(taskSentence.truth,truthSecond);
      char mark=Symbols.JUDGMENT_MARK;
      if (taskSentence.isGoal() || second_belief.isGoal()) {
        truth=TruthFunctions.abduction(taskSentence.truth,truthSecond);
        mark=Symbols.GOAL_MARK;
      }
      Stamp useEvidentalBase=new Stamp(taskSentence.stamp,second_belief.stamp,nal.getTime());
      Sentence newSentence=new Sentence(result,mark,truth,new Stamp(taskSentence.stamp,nal.getTime(),useEvidentalBase));
      BudgetValue budget=BudgetFunctions.compoundForward(truth,newSentence.content,nal);
      if (budget.aboveThreshold()) {
        Task newTask=new Task(newSentence,budget,task,null);
        Task dummy=new Task(second_belief,budget,task,null);
        nal.setCurrentBelief(taskSentence);
        nal.setCurrentTask(dummy);
        if (nal.derivedTask(newTask,false,false,task,second_belief)) {
          nal.mem().logic.DED_SECOND_LAYER_VARIABLE_UNIFICATION.commit();
          nal.emit(Events.ConceptUnification.class,newTask,first,secondConcept,second_belief);
          unifiedAnything=true;
        }
      }
    }
    remainingUnifications--;
    if (remainingUnifications == 0) {
      break;
    }
  }
  return unifiedAnything;
}