public static void writeTruthMaintenanceSystem(
      MarshallerWriteContext context, EntryPoint wmep, ProtobufMessages.EntryPoint.Builder _epb)
      throws IOException {
    TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
    ObjectHashMap justifiedMap = tms.getEqualityKeyMap();

    if (!justifiedMap.isEmpty()) {
      EqualityKey[] keys = new EqualityKey[justifiedMap.size()];
      org.drools.core.util.Iterator it = justifiedMap.iterator();
      int i = 0;
      for (org.drools.core.util.ObjectHashMap.ObjectEntry entry =
              (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next();
          entry != null;
          entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
        EqualityKey key = (EqualityKey) entry.getKey();
        keys[i++] = key;
      }

      Arrays.sort(keys, EqualityKeySorter.instance);

      ProtobufMessages.TruthMaintenanceSystem.Builder _tms =
          ProtobufMessages.TruthMaintenanceSystem.newBuilder();

      // write the assert map of Equality keys
      for (EqualityKey key : keys) {
        ProtobufMessages.EqualityKey.Builder _key = ProtobufMessages.EqualityKey.newBuilder();
        _key.setStatus(key.getStatus());
        _key.setHandleId(key.getFactHandle().getId());

        if (key.size() > 1) {
          // add all the other key's if they exist
          FastIterator keyIter = key.fastIterator();
          for (DefaultFactHandle handle = key.getFirst().getNext();
              handle != null;
              handle = (DefaultFactHandle) keyIter.next(handle)) {
            _key.addOtherHandle(handle.getId());
          }
        }

        if (key.getBeliefSet() != null) {
          writeBeliefSet(context, key.getBeliefSet(), _key);
        }

        _tms.addKey(_key.build());
      }

      _epb.setTms(_tms.build());
    }
  }
  void unHashSinks(final FieldIndex fieldIndex) {
    final int index = fieldIndex.getIndex();
    // this is the list of sinks that need to be removed from the hashedSinkMap
    final List<HashKey> unhashedSinks = new ArrayList<HashKey>();

    final Iterator iter = this.hashedSinkMap.newIterator();
    ObjectHashMap.ObjectEntry entry = (ObjectHashMap.ObjectEntry) iter.next();

    while (entry != null) {
      final AlphaNode alphaNode = (AlphaNode) entry.getValue();
      final IndexableConstraint indexableConstraint =
          (IndexableConstraint) alphaNode.getConstraint();

      // only alpha nodes that have an Operator.EQUAL are in sinks, so only check if it is
      // the right field index
      if (index == indexableConstraint.getFieldExtractor().getIndex()) {
        final FieldValue value = indexableConstraint.getField();
        if (this.hashableSinks == null) {
          this.hashableSinks = new ObjectSinkNodeList();
        }
        this.hashableSinks.add(alphaNode);

        unhashedSinks.add(new HashKey(index, value, fieldIndex.getFieldExtractor()));
      }

      entry = (ObjectHashMap.ObjectEntry) iter.next();
    }

    for (HashKey hashKey : unhashedSinks) {
      this.hashedSinkMap.remove(hashKey);
    }

    if (this.hashedSinkMap.isEmpty()) {
      this.hashedSinkMap = null;
    }

    fieldIndex.setHashed(false);
  }
Esempio n. 3
0
  public static void writeTruthMaintenanceSystem(MarshallerWriteContext context)
      throws IOException {
    ObjectOutputStream stream = context.stream;

    ObjectHashMap assertMap = context.wm.getTruthMaintenanceSystem().getAssertMap();

    EqualityKey[] keys = new EqualityKey[assertMap.size()];
    org.drools.core.util.Iterator it = assertMap.iterator();
    int i = 0;
    for (org.drools.core.util.ObjectHashMap.ObjectEntry entry =
            (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next();
        entry != null;
        entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
      EqualityKey key = (EqualityKey) entry.getKey();
      keys[i++] = key;
    }

    Arrays.sort(keys, EqualityKeySorter.instance);

    // write the assert map of Equality keys
    for (EqualityKey key : keys) {
      stream.writeShort(PersisterEnums.EQUALITY_KEY);
      stream.writeInt(key.getStatus());
      InternalFactHandle handle = key.getFactHandle();
      stream.writeInt(handle.getId());
      // context.out.println( "EqualityKey int:" + key.getStatus() + " int:" + handle.getId() );
      if (key.getOtherFactHandle() != null && !key.getOtherFactHandle().isEmpty()) {
        for (InternalFactHandle handle2 : key.getOtherFactHandle()) {
          stream.writeShort(PersisterEnums.FACT_HANDLE);
          stream.writeInt(handle2.getId());
          // context.out.println( "OtherHandle int:" + handle2.getId() );
        }
      }
      stream.writeShort(PersisterEnums.END);
    }
    stream.writeShort(PersisterEnums.END);
  }
  public static void writeTruthMaintenanceSystem(
      MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _session)
      throws IOException {
    ObjectHashMap assertMap = context.wm.getTruthMaintenanceSystem().getAssertMap();
    ObjectHashMap justifiedMap = context.wm.getTruthMaintenanceSystem().getJustifiedMap();

    if (!assertMap.isEmpty() || !justifiedMap.isEmpty()) {
      EqualityKey[] keys = new EqualityKey[assertMap.size()];
      org.drools.core.util.Iterator it = assertMap.iterator();
      int i = 0;
      for (org.drools.core.util.ObjectHashMap.ObjectEntry entry =
              (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next();
          entry != null;
          entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
        EqualityKey key = (EqualityKey) entry.getKey();
        keys[i++] = key;
      }

      Arrays.sort(keys, EqualityKeySorter.instance);

      ProtobufMessages.TruthMaintenanceSystem.Builder _tms =
          ProtobufMessages.TruthMaintenanceSystem.newBuilder();

      // write the assert map of Equality keys
      for (EqualityKey key : keys) {
        ProtobufMessages.EqualityKey.Builder _key = ProtobufMessages.EqualityKey.newBuilder();
        _key.setStatus(key.getStatus());
        _key.setHandleId(key.getFactHandle().getId());
        if (key.getOtherFactHandle() != null && !key.getOtherFactHandle().isEmpty()) {
          for (InternalFactHandle handle : key.getOtherFactHandle()) {
            _key.addOtherHandle(handle.getId());
          }
        }
        _tms.addKey(_key.build());
      }

      it = justifiedMap.iterator();
      i = 0;
      for (org.drools.core.util.ObjectHashMap.ObjectEntry entry =
              (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next();
          entry != null;
          entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
        ProtobufMessages.Justification.Builder _justification =
            ProtobufMessages.Justification.newBuilder();
        _justification.setHandleId(((Integer) entry.getKey()).intValue());

        org.drools.core.util.LinkedList list = (org.drools.core.util.LinkedList) entry.getValue();
        for (LinkedListEntry node = (LinkedListEntry) list.getFirst();
            node != null;
            node = (LinkedListEntry) node.getNext()) {
          LogicalDependency dependency = (LogicalDependency) node.getObject();
          org.drools.spi.Activation activation = dependency.getJustifier();
          ProtobufMessages.Activation _activation =
              ProtobufMessages.Activation.newBuilder()
                  .setPackageName(activation.getRule().getPackage())
                  .setRuleName(activation.getRule().getName())
                  .setTuple(PersisterHelper.createTuple(activation.getTuple()))
                  .build();
          _justification.addActivation(_activation);
        }
        _tms.addJustification(_justification.build());
      }
      _session.setTms(_tms.build());
    }
  }