Exemple #1
0
  /**
   * TMS will be automatically enabled when the first logical insert happens.
   *
   * <p>We will take all the already asserted objects of the same type and initialize the equality
   * map.
   *
   * @param object the logically inserted object.
   * @param conf the type's configuration.
   */
  private void enableTMS(Object object, ObjectTypeConf conf) {

    final Rete source = this.ruleBase.getRete();
    final ClassObjectType cot = new ClassObjectType(object.getClass());
    final Map<ObjectType, ObjectTypeNode> map = source.getObjectTypeNodes(EntryPoint.DEFAULT);
    final ObjectTypeNode node = map.get(cot);
    final ObjectHashSet memory = ((ObjectTypeNodeMemory) this.wm.getNodeMemory(node)).memory;

    // All objects of this type that are already there were certainly stated,
    // since this method call happens at the first logical insert, for any given type.
    org.drools.core.util.Iterator it = memory.iterator();

    for (Object obj = it.next(); obj != null; obj = it.next()) {

      org.drools.core.util.ObjectHashSet.ObjectEntry holder =
          (org.drools.core.util.ObjectHashSet.ObjectEntry) obj;

      InternalFactHandle handle = (InternalFactHandle) holder.getValue();

      if (handle != null) {
        EqualityKey key = createEqualityKey(handle);
        key.setStatus(EqualityKey.STATED);
        this.wm.getTruthMaintenanceSystem().put(key);
      }
    }

    // Enable TMS for this type.
    conf.enableTMS();
  }
  public static void readTruthMaintenanceSystem(MarshallerReaderContext context)
      throws IOException {
    ObjectInputStream stream = context.stream;

    TruthMaintenanceSystem tms = context.wm.getTruthMaintenanceSystem();
    while (stream.readShort() == PersisterEnums.EQUALITY_KEY) {
      int status = stream.readInt();
      int factHandleId = stream.readInt();
      InternalFactHandle handle = (InternalFactHandle) context.handles.get(factHandleId);

      // ObjectTypeConf state is not marshalled, so it needs to be re-determined
      ObjectTypeConf typeConf =
          context
              .wm
              .getObjectTypeConfigurationRegistry()
              .getObjectTypeConf(context.wm.getEntryPoint(), handle.getObject());
      if (!typeConf.isTMSEnabled()) {
        typeConf.enableTMS();
      }

      EqualityKey key = new EqualityKey(handle, status);
      handle.setEqualityKey(key);
      while (stream.readShort() == PersisterEnums.FACT_HANDLE) {
        factHandleId = stream.readInt();
        handle = (InternalFactHandle) context.handles.get(factHandleId);
        key.addFactHandle(handle);
        handle.setEqualityKey(key);
      }
      tms.put(key);
    }
  }