예제 #1
0
  public static void writeSession(MarshallerWriteContext context) throws IOException {
    // context.out.println( "... write session");
    ReteooWorkingMemory wm = (ReteooWorkingMemory) context.wm;
    wm.getAgenda().unstageActivations();

    final boolean multithread = wm.isPartitionManagersActive();
    // is multi-thread active?
    if (multithread) {
      context.writeBoolean(true);
      wm.stopPartitionManagers();
    } else {
      context.writeBoolean(false);
    }

    long time = 0;
    if (context.wm.getTimerService() instanceof PseudoClockScheduler) {
      time = context.clockTime;
    }
    context.writeLong(time);

    context.writeInt(wm.getFactHandleFactory().getId());
    context.writeLong(wm.getFactHandleFactory().getRecency());
    //// context.out.println( "FactHandleFactory int:" + wm.getFactHandleFactory().getId() + "
    // long:" + wm.getFactHandleFactory().getRecency() );

    context.writeLong(wm.getPropagationIdCounter());
    // context.out.println( "PropagationCounter long:" + wm.getPropagationIdCounter() );

    InternalFactHandle handle = context.wm.getInitialFactHandle();
    context.writeInt(handle.getId());
    context.writeLong(handle.getRecency());
    // context.out.println( "InitialFact int:" + handle.getId() + " long:" + handle.getRecency() );

    writeAgenda(context);

    writeInitialFactHandleRightTuples(context);
    for (WorkingMemoryEntryPoint wmep : wm.getEntryPoints().values()) {
      context.stream.writeShort(PersisterEnums.ENTRY_POINT);
      context.stream.writeUTF(wmep.getEntryPointId());
      writeFactHandles(context, ((NamedEntryPoint) wmep).getObjectStore());
    }
    context.stream.writeShort(PersisterEnums.END);
    writeInitialFactHandleLeftTuples(context);

    writePropagationContexts(context);

    writeActivations(context);

    writeActionQueue(context);

    writeTruthMaintenanceSystem(context);

    if (context.marshalProcessInstances && processMarshaller != null) {
      processMarshaller.writeProcessInstances(context);
    } else {
      context.stream.writeShort(PersisterEnums.END);
    }

    if (context.marshalWorkItems && processMarshaller != null) {
      processMarshaller.writeWorkItems(context);
    } else {
      context.stream.writeShort(PersisterEnums.END);
    }

    if (processMarshaller != null) {
      // this now just assigns the writer, it will not write out any timer information
      processMarshaller.writeProcessTimers(context);
    } else {
      context.stream.writeShort(PersisterEnums.END);
    }

    // Only works for JpaJDKTimerService
    writeTimers(context.wm.getTimerService().getTimerJobInstances(), context);

    if (multithread) {
      wm.startPartitionManagers();
    }

    // context.out.println( "--- write session --- END");
  }
  private static ProtobufMessages.KnowledgeSession serializeSession(MarshallerWriteContext context)
      throws IOException {
    ReteooWorkingMemory wm = (ReteooWorkingMemory) context.wm;
    wm.getAgenda().unstageActivations();

    ProtobufMessages.RuleData.Builder _ruleData = ProtobufMessages.RuleData.newBuilder();

    final boolean multithread = wm.isPartitionManagersActive();
    if (multithread) {
      wm.stopPartitionManagers();
    }

    long time = 0;
    if (context.wm.getTimerService() instanceof PseudoClockScheduler) {
      time = context.clockTime;
    }
    _ruleData.setLastId(wm.getFactHandleFactory().getId());
    _ruleData.setLastRecency(wm.getFactHandleFactory().getRecency());

    InternalFactHandle handle = context.wm.getInitialFactHandle();
    ProtobufMessages.FactHandle _ifh =
        ProtobufMessages.FactHandle.newBuilder()
            .setType(ProtobufMessages.FactHandle.HandleType.INITIAL_FACT)
            .setId(handle.getId())
            .setRecency(handle.getRecency())
            .build();
    _ruleData.setInitialFact(_ifh);

    writeAgenda(context, _ruleData);

    writeNodeMemories(context, _ruleData);

    for (WorkingMemoryEntryPoint wmep : wm.getEntryPoints().values()) {
      org.drools.marshalling.impl.ProtobufMessages.EntryPoint.Builder _epb =
          ProtobufMessages.EntryPoint.newBuilder();
      _epb.setEntryPointId(wmep.getEntryPointId());
      writeFactHandles(context, _epb, ((NamedEntryPoint) wmep).getObjectStore());
      _ruleData.addEntryPoint(_epb.build());
    }

    writeActionQueue(context, _ruleData);

    writeTruthMaintenanceSystem(context, _ruleData);

    ProtobufMessages.KnowledgeSession.Builder _session =
        ProtobufMessages.KnowledgeSession.newBuilder()
            .setMultithread(multithread)
            .setTime(time)
            .setRuleData(_ruleData.build());

    if (processMarshaller != null) {
      Builder _pdata = ProtobufMessages.ProcessData.newBuilder();
      if (context.marshalProcessInstances) {
        context.parameterObject = _pdata;
        processMarshaller.writeProcessInstances(context);
      }

      if (context.marshalWorkItems) {
        context.parameterObject = _pdata;
        processMarshaller.writeWorkItems(context);
      }

      // this now just assigns the writer, it will not write out any timer information
      context.parameterObject = _pdata;
      processMarshaller.writeProcessTimers(context);

      _session.setProcessData(_pdata.build());
    }

    Timers _timers = writeTimers(context.wm.getTimerService().getTimerJobInstances(), context);
    if (_timers != null) {
      _session.setTimers(_timers);
    }

    if (multithread) {
      wm.startPartitionManagers();
    }

    return _session.build();
  }
예제 #3
0
  public static ReteooStatefulSession readSession(
      ProtobufMessages.KnowledgeSession _session,
      ReteooStatefulSession session,
      DefaultAgenda agenda,
      MarshallerReaderContext context)
      throws IOException, ClassNotFoundException {
    GlobalResolver globalResolver = (GlobalResolver) context.env.get(EnvironmentName.GLOBALS);
    if (globalResolver != null) {
      session.setGlobalResolver(globalResolver);
    }

    if (session.getTimerService() instanceof PseudoClockScheduler) {
      PseudoClockScheduler clock = (PseudoClockScheduler) session.getTimerService();
      clock.advanceTime(_session.getTime(), TimeUnit.MILLISECONDS);
    }

    // RuleFlowGroups need to reference the session
    for (RuleFlowGroup group : agenda.getRuleFlowGroupsMap().values()) {
      ((RuleFlowGroupImpl) group).setWorkingMemory(session);
    }

    context.wm = session;

    // need to read node memories before reading the fact handles
    // because this data is required during fact propagation
    readNodeMemories(context, _session.getRuleData());

    List<PropagationContextImpl> pctxs = new ArrayList<PropagationContextImpl>();

    readInitialFactHandle(context, _session.getRuleData(), pctxs);

    for (ProtobufMessages.EntryPoint _ep : _session.getRuleData().getEntryPointList()) {
      SessionEntryPoint wmep = context.wm.getEntryPoints().get(_ep.getEntryPointId());
      readFactHandles(context, _ep, ((NamedEntryPoint) wmep).getObjectStore(), pctxs);
      readTruthMaintenanceSystem(context, wmep, _ep, pctxs);
    }

    cleanReaderContexts(pctxs);

    readActionQueue(context, _session.getRuleData());

    if (processMarshaller != null) {
      if (_session.hasProcessData()) {
        context.parameterObject = _session.getProcessData();
        processMarshaller.readProcessInstances(context);

        context.parameterObject = _session.getProcessData();
        processMarshaller.readWorkItems(context);

        // This actually does ALL timers, due to backwards compatability issues
        // It will read in old JBPM binaries, but always write to the new binary format.
        context.parameterObject = _session.getProcessData();
        processMarshaller.readProcessTimers(context);
      }
    } else {
      if (_session.hasProcessData()) {
        throw new IllegalStateException(
            "No process marshaller, unable to unmarshall process data.");
      }
    }

    if (_session.hasTimers()) {
      for (ProtobufMessages.Timers.Timer _timer : _session.getTimers().getTimerList()) {
        readTimer(context, _timer);
      }
    }

    // remove the activations filter
    agenda.setActivationsFilter(null);

    return session;
  }
예제 #4
0
  private static ProtobufMessages.KnowledgeSession serializeSession(MarshallerWriteContext context)
      throws IOException {
    StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) context.wm;

    try {
      wm.getLock().lock();
      for (WorkingMemoryEntryPoint ep : wm.getWorkingMemoryEntryPoints().values()) {
        if (ep instanceof NamedEntryPoint) {
          ((NamedEntryPoint) ep).lock();
        }
      }

      ((InternalAgenda) wm.getAgenda()).unstageActivations();

      evaluateRuleActivations(wm);

      ProtobufMessages.RuleData.Builder _ruleData = ProtobufMessages.RuleData.newBuilder();

      long time = 0;
      if (context.wm.getTimerService() instanceof PseudoClockScheduler) {
        time = context.clockTime;
      }
      _ruleData.setLastId(wm.getFactHandleFactory().getId());
      _ruleData.setLastRecency(wm.getFactHandleFactory().getRecency());

      InternalFactHandle handle = context.wm.getInitialFactHandle();
      if (handle != null) {
        // can be null for RETE, if fireAllRules has not yet been called
        ProtobufMessages.FactHandle _ifh =
            ProtobufMessages.FactHandle.newBuilder()
                .setType(ProtobufMessages.FactHandle.HandleType.INITIAL_FACT)
                .setId(handle.getId())
                .setRecency(handle.getRecency())
                .build();
        _ruleData.setInitialFact(_ifh);
      }

      writeAgenda(context, _ruleData);

      writeNodeMemories(context, _ruleData);

      for (EntryPoint wmep : wm.getWorkingMemoryEntryPoints().values()) {
        org.drools.core.marshalling.impl.ProtobufMessages.EntryPoint.Builder _epb =
            ProtobufMessages.EntryPoint.newBuilder();
        _epb.setEntryPointId(wmep.getEntryPointId());

        writeObjectTypeConfiguration(
            context,
            ((InternalWorkingMemoryEntryPoint) wmep).getObjectTypeConfigurationRegistry(),
            _epb);

        writeFactHandles(context, _epb, ((NamedEntryPoint) wmep).getObjectStore());

        writeTruthMaintenanceSystem(context, wmep, _epb);

        _ruleData.addEntryPoint(_epb.build());
      }

      writeActionQueue(context, _ruleData);

      ProtobufMessages.KnowledgeSession.Builder _session =
          ProtobufMessages.KnowledgeSession.newBuilder()
              .setMultithread(false)
              .setTime(time)
              .setRuleData(_ruleData.build());

      if (processMarshaller != null) {
        Builder _pdata = ProtobufMessages.ProcessData.newBuilder();
        if (context.marshalProcessInstances) {
          context.parameterObject = _pdata;
          processMarshaller.writeProcessInstances(context);
        }

        if (context.marshalWorkItems) {
          context.parameterObject = _pdata;
          processMarshaller.writeWorkItems(context);
        }

        // this now just assigns the writer, it will not write out any timer information
        context.parameterObject = _pdata;
        processMarshaller.writeProcessTimers(context);

        _session.setProcessData(_pdata.build());
      }

      Timers _timers =
          writeTimers(
              context.wm.getTimerService().getTimerJobInstances(context.wm.getId()), context);
      if (_timers != null) {
        _session.setTimers(_timers);
      }

      return _session.build();
    } finally {
      for (WorkingMemoryEntryPoint ep : wm.getWorkingMemoryEntryPoints().values()) {
        if (ep instanceof NamedEntryPoint) {
          ((NamedEntryPoint) ep).unlock();
        }
      }
      wm.getLock().unlock();
    }
  }
예제 #5
0
 public static void writeWorkItem(MarshallerWriteContext context, WorkItem workItem) {
   processMarshaller.writeWorkItem(context, workItem);
 }
  public static ReteooStatefulSession readSession(
      ReteooStatefulSession session,
      DefaultAgenda agenda,
      long time,
      boolean multithread,
      MarshallerReaderContext context)
      throws IOException, ClassNotFoundException {
    if (session.getTimerService() instanceof PseudoClockScheduler) {
      PseudoClockScheduler clock = (PseudoClockScheduler) session.getTimerService();
      clock.advanceTime(time, TimeUnit.MILLISECONDS);
    }

    // RuleFlowGroups need to reference the session
    for (RuleFlowGroup group : agenda.getRuleFlowGroupsMap().values()) {
      ((RuleFlowGroupImpl) group).setWorkingMemory(session);
    }

    context.wm = session;

    context.handles.put(
        context.wm.getInitialFactHandle().getId(), context.wm.getInitialFactHandle());

    if (context.stream.readBoolean()) {
      InternalFactHandle initialFactHandle = context.wm.getInitialFactHandle();
      int sinkId = context.stream.readInt();
      ObjectTypeNode initialFactNode = (ObjectTypeNode) context.sinks.get(sinkId);
      if (initialFactNode == null) {
        // ------ START RANT ------
        // The following code is as bad as it looks, but since I was so far
        // unable to convince Mark that creating OTNs on demand is really bad,
        // I have to continue doing it :)
        EntryPointNode defaultEPNode =
            context.ruleBase.getRete().getEntryPointNode(EntryPoint.DEFAULT);
        BuildContext buildContext =
            new BuildContext(
                context.ruleBase, context.ruleBase.getReteooBuilder().getIdGenerator());
        buildContext.setPartitionId(RuleBasePartitionId.MAIN_PARTITION);
        buildContext.setObjectTypeNodeMemoryEnabled(true);
        initialFactNode =
            new ObjectTypeNode(
                sinkId, defaultEPNode, ClassObjectType.InitialFact_ObjectType, buildContext);
        // isn't contention something everybody loves?
        context.ruleBase.lock();
        try {
          // Yeah, I know, because one session is being deserialized, we go and lock all of them...
          initialFactNode.attach(buildContext);
        } finally {
          context.ruleBase.unlock();
        }
        // ------- END RANT -----
      }
      ObjectHashSet initialFactMemory = (ObjectHashSet) context.wm.getNodeMemory(initialFactNode);

      initialFactMemory.add(initialFactHandle);
      readRightTuples(initialFactHandle, context);
    }
    while (context.readShort() == PersisterEnums.ENTRY_POINT) {
      String entryPointId = context.stream.readUTF();
      WorkingMemoryEntryPoint wmep = context.wm.getEntryPoints().get(entryPointId);
      readFactHandles(context, ((NamedEntryPoint) wmep).getObjectStore());
    }
    InternalFactHandle handle = context.wm.getInitialFactHandle();
    while (context.stream.readShort() == PersisterEnums.LEFT_TUPLE) {
      LeftTupleSink sink = (LeftTupleSink) context.sinks.get(context.stream.readInt());
      LeftTuple leftTuple = sink.createLeftTuple(handle, sink, true);
      readLeftTuple(leftTuple, context);
    }

    readPropagationContexts(context);

    readActivations(context);

    readActionQueue(context);

    readTruthMaintenanceSystem(context);

    if (processMarshaller != null) {
      processMarshaller.readProcessInstances(context);
    } else {
      short type = context.stream.readShort();
      if (PersisterEnums.END != type) {
        throw new IllegalStateException(
            "No process marshaller, unable to unmarshall type: " + type);
      }
    }

    if (processMarshaller != null) {
      processMarshaller.readWorkItems(context);
    } else {
      short type = context.stream.readShort();
      if (PersisterEnums.END != type) {
        throw new IllegalStateException(
            "No process marshaller, unable to unmarshall type: " + type);
      }
    }

    if (processMarshaller != null) {
      // This actually does ALL timers, due to backwards compatability issues
      // It will read in old JBPM binaries, but always write to the new binary format.
      processMarshaller.readProcessTimers(context);
    } else {
      short type = context.stream.readShort();
      if (PersisterEnums.END != type) {
        throw new IllegalStateException(
            "No process marshaller, unable to unmarshall type: " + type);
      }
    }

    // no legacy jBPM timers, so handle locally
    while (context.readShort() == PersisterEnums.DEFAULT_TIMER) {
      InputMarshaller.readTimer(context);
    }

    if (multithread) {
      session.startPartitionManagers();
    }

    return session;
  }