示例#1
0
  public static void writeActivation(
      MarshallerWriteContext context,
      LeftTuple leftTuple,
      AgendaItem agendaItem,
      RuleTerminalNode ruleTerminalNode)
      throws IOException {
    ObjectOutputStream stream = context.stream;

    stream.writeLong(agendaItem.getActivationNumber());

    stream.writeInt(context.terminalTupleMap.get(leftTuple));

    stream.writeInt(agendaItem.getSalience());

    Rule rule = agendaItem.getRule();
    stream.writeUTF(rule.getPackage());
    stream.writeUTF(rule.getName());

    // context.out.println( "Rule " + rule.getPackage() + "." + rule.getName() );

    // context.out.println( "AgendaItem long:" +
    // agendaItem.getPropagationContext().getPropagationNumber() );
    stream.writeLong(agendaItem.getPropagationContext().getPropagationNumber());

    if (agendaItem.getActivationGroupNode() != null) {
      stream.writeBoolean(true);
      // context.out.println( "ActivationGroup bool:" + true );
      stream.writeUTF(agendaItem.getActivationGroupNode().getActivationGroup().getName());
      // context.out.println( "ActivationGroup string:" +
      // agendaItem.getActivationGroupNode().getActivationGroup().getName() );
    } else {
      stream.writeBoolean(false);
      // context.out.println( "ActivationGroup bool:" + false );
    }

    stream.writeBoolean(agendaItem.isActivated());
    // context.out.println( "AgendaItem bool:" + agendaItem.isActivated() );

    if (agendaItem.getFactHandle() != null) {
      stream.writeBoolean(true);
      stream.writeInt(agendaItem.getFactHandle().getId());
    } else {
      stream.writeBoolean(false);
    }

    org.drools.core.util.LinkedList list = agendaItem.getLogicalDependencies();
    if (list != null && !list.isEmpty()) {
      for (LogicalDependency node = (LogicalDependency) list.getFirst();
          node != null;
          node = (LogicalDependency) node.getNext()) {
        stream.writeShort(PersisterEnums.LOGICAL_DEPENDENCY);
        stream.writeInt(((InternalFactHandle) node.getJustified()).getId());
        // context.out.println( "Logical Depenency : int " + ((InternalFactHandle)
        // node.getFactHandle()).getId() );
      }
    }
    stream.writeShort(PersisterEnums.END);
  }
 public void addSoftConstraintMatch(RuleContext kcontext, final int weight) {
   softScore += weight;
   AgendaItem agendaItem = (AgendaItem) kcontext.getMatch();
   agendaItem.setActivationUnMatchListener(
       new ActivationUnMatchListener() {
         public void unMatch(Session workingMemory, Match activation) {
           softScore -= weight;
         }
       });
 }
  public static ProtobufMessages.Activation writeActivation(
      MarshallerWriteContext context, AgendaItem agendaItem) {
    ProtobufMessages.Activation.Builder _activation = ProtobufMessages.Activation.newBuilder();

    Rule rule = agendaItem.getRule();
    _activation.setPackageName(rule.getPackage());
    _activation.setRuleName(rule.getName());

    ProtobufMessages.Tuple.Builder _tb = ProtobufMessages.Tuple.newBuilder();
    for (LeftTuple entry = agendaItem.getTuple(); entry != null; entry = entry.getParent()) {
      InternalFactHandle handle = entry.getLastHandle();
      _tb.addHandleId(handle.getId());
    }
    _activation.setTuple(_tb.build());

    _activation.setSalience(agendaItem.getSalience());
    _activation.setIsActivated(agendaItem.isActivated());

    if (agendaItem.getActivationGroupNode() != null) {
      _activation.setActivationGroup(
          agendaItem.getActivationGroupNode().getActivationGroup().getName());
    }

    if (agendaItem.getFactHandle() != null) {
      _activation.setHandleId(agendaItem.getFactHandle().getId());
    }

    org.drools.core.util.LinkedList list = agendaItem.getLogicalDependencies();
    if (list != null && !list.isEmpty()) {
      for (LogicalDependency node = (LogicalDependency) list.getFirst();
          node != null;
          node = (LogicalDependency) node.getNext()) {
        _activation.addLogicalDependency(((InternalFactHandle) node.getJustified()).getId());
      }
    }
    return _activation.build();
  }
  public static Activation readActivation(MarshallerReaderContext context) throws IOException {
    ObjectInputStream stream = context.stream;
    InternalRuleBase ruleBase = context.ruleBase;
    InternalWorkingMemory wm = context.wm;

    long activationNumber = stream.readLong();

    int pos = stream.readInt();
    LeftTuple leftTuple = context.terminalTupleMap.get(pos);

    int salience = stream.readInt();

    String pkgName = stream.readUTF();
    String ruleName = stream.readUTF();
    Package pkg = ruleBase.getPackage(pkgName);
    Rule rule = pkg.getRule(ruleName);

    RuleTerminalNode ruleTerminalNode = (RuleTerminalNode) leftTuple.getLeftTupleSink();

    PropagationContext pc = context.propagationContexts.get(stream.readLong());

    AgendaItem activation;

    boolean scheduled = false;
    if (rule.getTimer() != null) {
      activation =
          new ScheduledAgendaItem(
              activationNumber, leftTuple, (InternalAgenda) wm.getAgenda(), pc, ruleTerminalNode);
      scheduled = true;
    } else {
      activation = new AgendaItem(activationNumber, leftTuple, salience, pc, ruleTerminalNode);
    }
    leftTuple.setObject(activation);

    if (stream.readBoolean()) {
      String activationGroupName = stream.readUTF();
      ((DefaultAgenda) wm.getAgenda())
          .getActivationGroup(activationGroupName)
          .addActivation(activation);
    }

    boolean activated = stream.readBoolean();
    activation.setActivated(activated);

    if (stream.readBoolean()) {
      InternalFactHandle handle = context.handles.get(stream.readInt());
      activation.setFactHandle(handle);
      handle.setObject(activation);
    }

    InternalAgendaGroup agendaGroup;
    if (rule.getAgendaGroup() == null
        || rule.getAgendaGroup().equals("")
        || rule.getAgendaGroup().equals(AgendaGroup.MAIN)) {
      // Is the Rule AgendaGroup undefined? If it is use MAIN,
      // which is added to the Agenda by default
      agendaGroup =
          (InternalAgendaGroup) ((DefaultAgenda) wm.getAgenda()).getAgendaGroup(AgendaGroup.MAIN);
    } else {
      // AgendaGroup is defined, so try and get the AgendaGroup
      // from the Agenda
      agendaGroup =
          (InternalAgendaGroup)
              ((DefaultAgenda) wm.getAgenda()).getAgendaGroup(rule.getAgendaGroup());
    }

    activation.setAgendaGroup(agendaGroup);

    if (!scheduled && activated) {
      if (rule.getRuleFlowGroup() == null) {
        agendaGroup.add(activation);
      } else {
        InternalRuleFlowGroup rfg =
            (InternalRuleFlowGroup)
                ((DefaultAgenda) wm.getAgenda()).getRuleFlowGroup(rule.getRuleFlowGroup());
        rfg.addActivation(activation);
      }
    }

    TruthMaintenanceSystem tms = context.wm.getTruthMaintenanceSystem();
    while (stream.readShort() == PersisterEnums.LOGICAL_DEPENDENCY) {
      int factHandleId = stream.readInt();
      InternalFactHandle handle = (InternalFactHandle) context.handles.get(factHandleId);
      ObjectTypeConf typeConf =
          context
              .wm
              .getObjectTypeConfigurationRegistry()
              .getObjectTypeConf(
                  ((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
      tms.addLogicalDependency(handle, activation, pc, rule, typeConf);
    }

    return activation;
  }