Exemplo n.º 1
0
  public static void writeAgenda(MarshallerWriteContext context) throws IOException {
    InternalWorkingMemory wm = context.wm;
    DefaultAgenda agenda = (DefaultAgenda) wm.getAgenda();

    context.writeInt(agenda.getDormantActivations());
    context.writeInt(agenda.getActiveActivations());

    Map<String, ActivationGroup> activationGroups = agenda.getActivationGroupsMap();

    AgendaGroup[] agendaGroups =
        (AgendaGroup[])
            agenda
                .getAgendaGroupsMap()
                .values()
                .toArray(new AgendaGroup[agenda.getAgendaGroupsMap().size()]);
    Arrays.sort(agendaGroups, AgendaGroupSorter.instance);

    for (AgendaGroup group : agendaGroups) {
      context.writeShort(PersisterEnums.AGENDA_GROUP);
      context.writeUTF(group.getName());
      context.writeBoolean(group.isActive());
    }
    context.writeShort(PersisterEnums.END);

    LinkedList<AgendaGroup> focusStack = agenda.getStackList();
    for (Iterator<AgendaGroup> it = focusStack.iterator(); it.hasNext(); ) {
      AgendaGroup group = it.next();
      context.writeShort(PersisterEnums.AGENDA_GROUP);
      context.writeUTF(group.getName());
    }
    context.writeShort(PersisterEnums.END);

    RuleFlowGroupImpl[] ruleFlowGroups =
        (RuleFlowGroupImpl[])
            agenda
                .getRuleFlowGroupsMap()
                .values()
                .toArray(new RuleFlowGroupImpl[agenda.getRuleFlowGroupsMap().size()]);
    Arrays.sort(ruleFlowGroups, RuleFlowGroupSorter.instance);

    for (RuleFlowGroupImpl group : ruleFlowGroups) {
      context.writeShort(PersisterEnums.RULE_FLOW_GROUP);
      // group.write( context );
      context.writeUTF(group.getName());
      context.writeBoolean(group.isActive());
      context.writeBoolean(group.isAutoDeactivate());
      Map<Long, String> nodeInstances = group.getNodeInstances();
      context.writeInt(nodeInstances.size());
      for (Map.Entry<Long, String> entry : nodeInstances.entrySet()) {
        context.writeLong(entry.getKey());
        context.writeUTF(entry.getValue());
      }
    }
    context.writeShort(PersisterEnums.END);
  }
Exemplo n.º 2
0
  public static void writeTrigger(Trigger trigger, MarshallerWriteContext outCtx)
      throws IOException {
    if (trigger instanceof CronTrigger) {
      outCtx.writeShort(PersisterEnums.CRON_TRIGGER);

      CronTrigger cronTrigger = (CronTrigger) trigger;
      outCtx.writeLong(cronTrigger.getStartTime().getTime());
      if (cronTrigger.getEndTime() != null) {
        outCtx.writeBoolean(true);
        outCtx.writeLong(cronTrigger.getEndTime().getTime());
      } else {
        outCtx.writeBoolean(false);
      }
      outCtx.writeInt(cronTrigger.getRepeatLimit());
      outCtx.writeInt(cronTrigger.getRepeatCount());
      outCtx.writeUTF(cronTrigger.getCronEx().getCronExpression());
      if (cronTrigger.getNextFireTime() != null) {
        outCtx.writeBoolean(true);
        outCtx.writeLong(cronTrigger.getNextFireTime().getTime());
      } else {
        outCtx.writeBoolean(false);
      }
      outCtx.writeObject(cronTrigger.getCalendarNames());
    } else if (trigger instanceof IntervalTrigger) {
      outCtx.writeShort(PersisterEnums.INT_TRIGGER);

      IntervalTrigger intTrigger = (IntervalTrigger) trigger;
      outCtx.writeLong(intTrigger.getStartTime().getTime());
      if (intTrigger.getEndTime() != null) {
        outCtx.writeBoolean(true);
        outCtx.writeLong(intTrigger.getEndTime().getTime());
      } else {
        outCtx.writeBoolean(false);
      }
      outCtx.writeInt(intTrigger.getRepeatLimit());
      outCtx.writeInt(intTrigger.getRepeatCount());
      if (intTrigger.getNextFireTime() != null) {
        outCtx.writeBoolean(true);
        outCtx.writeLong(intTrigger.getNextFireTime().getTime());
      } else {
        outCtx.writeBoolean(false);
      }
      outCtx.writeLong(intTrigger.getPeriod());
      outCtx.writeObject(intTrigger.getCalendarNames());
    } else if (trigger instanceof PointInTimeTrigger) {
      outCtx.writeShort(PersisterEnums.POINT_IN_TIME_TRIGGER);

      PointInTimeTrigger pinTrigger = (PointInTimeTrigger) trigger;

      outCtx.writeLong(pinTrigger.hasNextFireTime().getTime());
    }

    //        else if ( trigger instanceof DelayedTrigger ) {
    //
    //        } else if ( trigger instanceof PointInTimeTrigger ) {
    //
    //        } else if ( trigger instanceof CompositeMaxDurationTimer ) {
    //
    //        }
  }
Exemplo n.º 3
0
  public static void writeLeftTuple(
      LeftTuple leftTuple, MarshallerWriteContext context, boolean recurse) throws IOException {
    ObjectOutputStream stream = context.stream;
    InternalRuleBase ruleBase = context.ruleBase;
    InternalWorkingMemory wm = context.wm;

    LeftTupleSink sink = leftTuple.getLeftTupleSink();

    switch (sink.getType()) {
      case NodeTypeEnums.JoinNode:
        {
          // context.out.println( "JoinNode" );
          for (LeftTuple childLeftTuple = leftTuple.getFirstChild();
              childLeftTuple != null;
              childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) {
            stream.writeShort(PersisterEnums.RIGHT_TUPLE);
            int childSinkId = childLeftTuple.getLeftTupleSink().getId();
            stream.writeInt(childSinkId);
            stream.writeInt(childLeftTuple.getRightParent().getFactHandle().getId());
            // context.out.println( "RightTuple int:" + childLeftTuple.getLeftTupleSink().getId() +
            // " int:" + childLeftTuple.getRightParent().getFactHandle().getId() );
            writeLeftTuple(childLeftTuple, context, recurse);
          }
          stream.writeShort(PersisterEnums.END);
          // context.out.println( "JoinNode   ---   END" );
          break;
        }
      case NodeTypeEnums.QueryRiaFixerNode:
      case NodeTypeEnums.EvalConditionNode:
        {
          // context.out.println( ".... EvalConditionNode" );
          for (LeftTuple childLeftTuple = leftTuple.getFirstChild();
              childLeftTuple != null;
              childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) {
            stream.writeShort(PersisterEnums.LEFT_TUPLE);
            stream.writeInt(childLeftTuple.getLeftTupleSink().getId());
            writeLeftTuple(childLeftTuple, context, recurse);
          }
          stream.writeShort(PersisterEnums.END);
          // context.out.println( "---- EvalConditionNode   ---   END" );
          break;
        }
      case NodeTypeEnums.NotNode:
      case NodeTypeEnums.ForallNotNode:
        {
          if (leftTuple.getBlocker() == null) {
            // is not blocked so has children
            stream.writeShort(PersisterEnums.LEFT_TUPLE_NOT_BLOCKED);

            for (LeftTuple childLeftTuple = leftTuple.getFirstChild();
                childLeftTuple != null;
                childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) {
              stream.writeShort(PersisterEnums.LEFT_TUPLE);
              stream.writeInt(childLeftTuple.getLeftTupleSink().getId());
              writeLeftTuple(childLeftTuple, context, recurse);
            }
            stream.writeShort(PersisterEnums.END);

          } else {
            stream.writeShort(PersisterEnums.LEFT_TUPLE_BLOCKED);
            stream.writeInt(leftTuple.getBlocker().getFactHandle().getId());
          }
          break;
        }
      case NodeTypeEnums.ExistsNode:
        {
          if (leftTuple.getBlocker() == null) {
            // is blocked so has children
            stream.writeShort(PersisterEnums.LEFT_TUPLE_NOT_BLOCKED);
          } else {
            stream.writeShort(PersisterEnums.LEFT_TUPLE_BLOCKED);
            stream.writeInt(leftTuple.getBlocker().getFactHandle().getId());

            for (LeftTuple childLeftTuple = leftTuple.getFirstChild();
                childLeftTuple != null;
                childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) {
              stream.writeShort(PersisterEnums.LEFT_TUPLE);
              stream.writeInt(childLeftTuple.getLeftTupleSink().getId());
              writeLeftTuple(childLeftTuple, context, recurse);
            }
            stream.writeShort(PersisterEnums.END);
          }
          break;
        }
      case NodeTypeEnums.AccumulateNode:
        {
          // context.out.println( ".... AccumulateNode" );
          // accumulate nodes generate new facts on-demand and need special procedures when
          // serializing to persistent storage
          AccumulateMemory memory = (AccumulateMemory) context.wm.getNodeMemory((BetaNode) sink);
          AccumulateContext accctx = (AccumulateContext) leftTuple.getObject();
          // first we serialize the generated fact handle
          writeFactHandle(
              context,
              stream,
              context.objectMarshallingStrategyStore,
              accctx.result.getFactHandle());
          // then we serialize the associated accumulation context
          stream.writeObject(accctx.context);
          // then we serialize the boolean propagated flag
          stream.writeBoolean(accctx.propagated);

          // then we serialize all the propagated tuples
          for (LeftTuple childLeftTuple = leftTuple.getFirstChild();
              childLeftTuple != null;
              childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) {
            if (leftTuple.getLeftTupleSink().getId() == childLeftTuple.getLeftTupleSink().getId()) {
              // this is a matching record, so, associate the right tuples
              // context.out.println( "RightTuple(match) int:" +
              // childLeftTuple.getLeftTupleSink().getId() + " int:" +
              // childLeftTuple.getRightParent().getFactHandle().getId() );
              stream.writeShort(PersisterEnums.RIGHT_TUPLE);
              stream.writeInt(childLeftTuple.getRightParent().getFactHandle().getId());
            } else {
              // this is a propagation record
              // context.out.println( "RightTuple(propagation) int:" +
              // childLeftTuple.getLeftTupleSink().getId() + " int:" +
              // childLeftTuple.getRightParent().getFactHandle().getId() );
              stream.writeShort(PersisterEnums.LEFT_TUPLE);
              int sinkId = childLeftTuple.getLeftTupleSink().getId();
              stream.writeInt(sinkId);
              writeLeftTuple(childLeftTuple, context, recurse);
            }
          }
          stream.writeShort(PersisterEnums.END);
          // context.out.println( "---- AccumulateNode   ---   END" );
          break;
        }
      case NodeTypeEnums.RightInputAdaterNode:
        {
          // context.out.println( ".... RightInputAdapterNode" );
          // RIANs generate new fact handles on-demand to wrap tuples and need special procedures
          // when serializing to persistent storage
          ObjectHashMap memory = (ObjectHashMap) context.wm.getNodeMemory((NodeMemory) sink);
          InternalFactHandle ifh = (InternalFactHandle) memory.get(leftTuple);
          // first we serialize the generated fact handle ID
          // context.out.println( "FactHandle id:"+ifh.getId() );
          stream.writeInt(ifh.getId());
          stream.writeLong(ifh.getRecency());

          writeRightTuples(ifh, context);

          stream.writeShort(PersisterEnums.END);
          // context.out.println( "---- RightInputAdapterNode   ---   END" );
          break;
        }
      case NodeTypeEnums.FromNode:
        {
          // context.out.println( ".... FromNode" );
          // FNs generate new fact handles on-demand to wrap objects and need special procedures
          // when serializing to persistent storage
          FromMemory memory = (FromMemory) context.wm.getNodeMemory((NodeMemory) sink);

          Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getObject();
          for (RightTuple rightTuples : matches.values()) {
            // first we serialize the generated fact handle ID
            stream.writeShort(PersisterEnums.FACT_HANDLE);
            writeFactHandle(
                context,
                stream,
                context.objectMarshallingStrategyStore,
                rightTuples.getFactHandle());
            writeRightTuples(rightTuples.getFactHandle(), context);
          }
          stream.writeShort(PersisterEnums.END);
          for (LeftTuple childLeftTuple = leftTuple.getFirstChild();
              childLeftTuple != null;
              childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) {
            stream.writeShort(PersisterEnums.RIGHT_TUPLE);
            stream.writeInt(childLeftTuple.getLeftTupleSink().getId());
            stream.writeInt(childLeftTuple.getRightParent().getFactHandle().getId());
            // context.out.println( "RightTuple int:" + childLeftTuple.getLeftTupleSink().getId() +
            // " int:" + childLeftTuple.getRightParent().getFactHandle().getId() );
            writeLeftTuple(childLeftTuple, context, recurse);
          }
          stream.writeShort(PersisterEnums.END);
          // context.out.println( "---- FromNode   ---   END" );
          break;
        }
      case NodeTypeEnums.UnificationNode:
        {
          // context.out.println( ".... UnificationNode" );

          QueryElementNode node = (QueryElementNode) sink;
          boolean isOpen = node.isOpenQuery();

          context.writeBoolean(isOpen);
          if (isOpen) {
            InternalFactHandle factHandle = (InternalFactHandle) leftTuple.getObject();
            DroolsQuery query = (DroolsQuery) factHandle.getObject();

            // context.out.println( "factHandle:" +  factHandle );

            factHandle.setObject(null);
            writeFactHandle(context, stream, context.objectMarshallingStrategyStore, 0, factHandle);
            factHandle.setObject(query);
            writeLeftTuples(context, new InternalFactHandle[] {factHandle});
          } else {
            for (LeftTuple childLeftTuple = leftTuple.getFirstChild();
                childLeftTuple != null;
                childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) {
              stream.writeShort(PersisterEnums.LEFT_TUPLE);
              stream.writeInt(childLeftTuple.getLeftTupleSink().getId());
              InternalFactHandle factHandle = childLeftTuple.getLastHandle();
              writeFactHandle(
                  context, stream, context.objectMarshallingStrategyStore, 1, factHandle);
              writeLeftTuple(childLeftTuple, context, recurse);
            }
            stream.writeShort(PersisterEnums.END);
          }
          // context.out.println( "---- EvalConditionNode   ---   END" );
          break;
        }
      case NodeTypeEnums.RuleTerminalNode:
        {
          // context.out.println( "RuleTerminalNode" );
          int pos = context.terminalTupleMap.size();
          context.terminalTupleMap.put(leftTuple, pos);
          break;
        }
      case NodeTypeEnums.QueryTerminalNode:
        {
          // context.out.println( ".... QueryTerminalNode" );
          //                LeftTuple entry = leftTuple;
          //
          //                // find the DroolsQuery object
          //                while ( entry.getParent() != null ) {
          //                    entry = entry.getParent();
          //                }
          //
          //                // Now output all the child tuples in the caller network
          //                DroolsQuery query = (DroolsQuery) entry.getLastHandle().getObject();
          //                if ( query.getQueryResultCollector() instanceof
          // UnificationNodeViewChangedEventListener ) {
          //                    context.writeBoolean( true );
          //                    UnificationNodeViewChangedEventListener collector =
          // (UnificationNodeViewChangedEventListener) query.getQueryResultCollector();
          //                    leftTuple = collector.getLeftTuple();
          //
          context.writeBoolean(true);
          RightTuple rightTuple = (RightTuple) leftTuple.getObject();
          // context.out.println( "rightTuple:" +  rightTuple.getFactHandle() );
          writeFactHandle(
              context,
              stream,
              context.objectMarshallingStrategyStore,
              1,
              rightTuple.getFactHandle());

          for (LeftTuple childLeftTuple = rightTuple.firstChild;
              childLeftTuple != null;
              childLeftTuple = (LeftTuple) childLeftTuple.getRightParentNext()) {
            stream.writeShort(PersisterEnums.LEFT_TUPLE);
            stream.writeInt(childLeftTuple.getLeftTupleSink().getId());
            writeLeftTuple(childLeftTuple, context, recurse);
          }

          //                    for ( LeftTuple childLeftTuple = leftTuple.getFirstChild();
          // childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()
          // ) {
          //                        stream.writeShort( PersisterEnums.LEFT_TUPLE );
          //                        stream.writeInt( childLeftTuple.getLeftTupleSink().getId() );
          //                        writeFactHandle( context,
          //                                         stream,
          //                                         context.objectMarshallingStrategyStore,
          //                                         1,
          //                                         childLeftTuple.getLastHandle() );
          //                        writeLeftTuple( childLeftTuple,
          //                                        context,
          //                                        recurse );
          //                    }
          //                } else {
          //                    context.writeBoolean( false );
          //                }
          stream.writeShort(PersisterEnums.END);
          // context.out.println( "---- QueryTerminalNode   ---   END" );
          break;
        }
    }
  }
Exemplo n.º 4
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");
  }