1
votes

I want to create a fact of a template that looks like this:

; initial and final state of a single IF instance
    (deftemplate InitialAndFinalState
    (slot initial_state)
    (multislot final_state)
)

with the c++ interface, that I use in the following way:

void* ifs_template = EnvFindDeftemplate(clips_environment_,
  "InitialAndFinalState");
void* ifs_fact = EnvCreateFact(clips_environment_, ifs_template);
if (ifs_fact != NULL) {
  DATA_OBJECT field;
  field.type = FACT_ADDRESS;

  field.value = 
      addSpinWaveToClipsEnvironment(initial_state);
  EnvPutFactSlot(clips_environment_, ifs_fact, "initial_state", &field);

  void* multifield_ptr = EnvCreateMultifield(clips_environment_,
      final_state.size());

  for (unsigned int i = 0; i < final_state.size(); ++i) {
    SetMFType(multifield_ptr, i + 1, FACT_ADDRESS);
    SetMFValue(multifield_ptr, i + 1,
       addSpinWaveToClipsEnvironment(final_state[i]));//);
  }

  DATA_OBJECT final_states;
  SetDOBegin(final_states, 1);
  SetDOEnd(final_states, final_state.size());

  SetType(final_states, MULTIFIELD);
  SetValue(final_states, multifield_ptr);

  EnvPutFactSlot(clips_environment_, ifs_fact, "final_state", &final_states);

  EnvAssert(clips_environment_, ifs_fact);
}

The addSpinWaveToClipsEnviroment function will add a SpinWave fact or find the existing one and return a fact address.

void* DecayGenerator::addSpinWaveToClipsEnvironment(const SpinWave& spinwave) {
  std::vector<std::pair<std::string, int> > spin_qn_name_value_pairs;
  for (auto spin_like_qn = spinwave.spin_like_quantum_numbers_.begin();
      spin_like_qn != spinwave.spin_like_quantum_numbers_.end();
      ++spin_like_qn) {
    spin_qn_name_value_pairs.push_back(
        std::make_pair(spin_like_qn->first,
            addSpinQuantumNumberToClipsEnvironment(spin_like_qn->second)));
  }

  std::stringstream clips_query;
  std::stringstream values_part;
  values_part << "(explode$ \"";
  clips_query << "(find-spinwave-fact-list (explode$ \"";
  for (auto spin_like_qn = spin_qn_name_value_pairs.begin();
      spin_like_qn != spin_qn_name_value_pairs.end(); ++spin_like_qn) {
    clips_query << "\\\"" << spin_like_qn->first << "\\\" ";
    values_part << spin_like_qn->second << " ";
  }
  for (auto int_like_qn = spinwave.integer_like_quantum_numbers_.begin();
      int_like_qn != spinwave.integer_like_quantum_numbers_.end();
      ++int_like_qn) {
    clips_query << "\\\"" << int_like_qn->first << "\\\" ";
    values_part << int_like_qn->second << " ";
  }
  for (auto double_like_qn = spinwave.double_like_quantum_numbers_.begin();
      double_like_qn != spinwave.double_like_quantum_numbers_.end();
      ++double_like_qn) {
    clips_query << "\\\"" << double_like_qn->first << "\\\" ";
    values_part << double_like_qn->second << " ";
  }
  values_part << "\")";
  clips_query << "\") " << values_part.str() << ")";

  DATA_OBJECT found_spin_waves_facts;
  EnvEval(clips_environment_, clips_query.str().c_str(),
      &found_spin_waves_facts);

  void* spinwave_fact;
  if (0 < GetDOLength(found_spin_waves_facts)) {
    spinwave_fact = GetMFValue(GetValue(found_spin_waves_facts), 1);
  }
  else {
    void* spinwave_template = EnvFindDeftemplate(clips_environment_,
        "SpinWave");
    // set the facts
    spinwave_fact = EnvCreateFact(clips_environment_, spinwave_template);
    if (spinwave_fact != NULL) {

      unsigned int total_qn_count = spinwave.spin_like_quantum_numbers_.size()
          + spinwave.integer_like_quantum_numbers_.size()
          + spinwave.double_like_quantum_numbers_.size();

      void* qn_names_ptr = EnvCreateMultifield(clips_environment_,
          total_qn_count);

      void* qn_values_ptr = EnvCreateMultifield(clips_environment_,
          total_qn_count);

      unsigned int counter(1);
      for (auto spin_like_qn = spin_qn_name_value_pairs.begin();
          spin_like_qn != spin_qn_name_value_pairs.end(); ++spin_like_qn) {
        SetMFType(qn_names_ptr, counter, STRING);
        SetMFValue(qn_names_ptr, counter,
            EnvAddSymbol(clips_environment_, spin_like_qn->first.c_str()));
        SetMFType(qn_values_ptr, counter, INTEGER);
        SetMFValue(qn_values_ptr, counter,
            EnvAddLong(clips_environment_, spin_like_qn->second));
        ++counter;
      }
      for (auto int_like_qn = spinwave.integer_like_quantum_numbers_.begin();
          int_like_qn != spinwave.integer_like_quantum_numbers_.end();
          ++int_like_qn) {
        SetMFType(qn_names_ptr, counter, STRING);
        SetMFValue(qn_names_ptr, counter,
            EnvAddSymbol(clips_environment_, int_like_qn->first.c_str()));
        SetMFType(qn_values_ptr, counter, INTEGER);
        SetMFValue(qn_values_ptr, counter,
            EnvAddLong(clips_environment_, int_like_qn->second));
        ++counter;
      }
      for (auto double_like_qn = spinwave.double_like_quantum_numbers_.begin();
          double_like_qn != spinwave.double_like_quantum_numbers_.end();
          ++double_like_qn) {
        SetMFType(qn_names_ptr, counter, STRING);
        SetMFValue(qn_names_ptr, counter,
            EnvAddSymbol(clips_environment_, double_like_qn->first.c_str()));
        SetMFType(qn_values_ptr, counter, INTEGER);
        SetMFValue(qn_values_ptr, counter,
            EnvAddLong(clips_environment_, double_like_qn->second));
        ++counter;
      }

      DATA_OBJECT qn_names;
      DATA_OBJECT qn_values;

      SetType(qn_names, MULTIFIELD);
      SetValue(qn_names, qn_names_ptr);

      SetDOBegin(qn_names, 1);
      SetDOEnd(qn_names, total_qn_count);

      SetType(qn_values, MULTIFIELD);
      SetValue(qn_values, qn_values_ptr);

      SetDOBegin(qn_values, 1);
      SetDOEnd(qn_values, total_qn_count);

      EnvPutFactSlot(clips_environment_, spinwave_fact, "quantum_number_names",
          &qn_names);
      EnvPutFactSlot(clips_environment_, spinwave_fact, "quantum_number_values",
          &qn_values);

      EnvAssignFactSlotDefaults(clips_environment_, spinwave_fact);
      EnvAssert(clips_environment_, spinwave_fact);
    }
  }
  return spinwave_fact;
}

The problem is that the final_state multislot variable is only filled correctly in some cases, for example if I have 1, 3, 4, 6,... values inside but not 2 and 5! When I want to put two inside, then there is just some float close to 0 inside the multislot. If I use 5 then it crashes when asserting the fact. Seems like some memory allocation problem to me. I debugged the c++ code and it passes the EnvCreateMultifield function a proper value for the size. I dont really have an idea what could cause that. Any ideas?

1
The void* pointers have a certain odour. - Cheers and hth. - Alf
Yeah, but thats the clips interface... I dont really have a choice here - steve
This looks to be the same issue that you had previously here: stackoverflow.com/questions/34918259/…. Since the creation of the InitialAndFinalState fact requires potentially numerous calls to EnvEval and EnvAssert, I'd suggest wrapping the process with calls to EnvIncrementGCLocks/EnvDecrementGCLocks to see if that resolves your issue. - Gary Riley

1 Answers

0
votes

As Gary mentioned in the comment, wrapping that function with the

EnvIncrementGCLocks/EnvDecrementGCLocks

solves the problem.