Esempio n. 1
0
  public static void writeInitialFactHandleRightTuples(MarshallerWriteContext context)
      throws IOException {
    ObjectOutputStream stream = context.stream;
    InternalRuleBase ruleBase = context.ruleBase;

    ObjectTypeNode initialFactNode =
        ruleBase
            .getRete()
            .getEntryPointNode(EntryPoint.DEFAULT)
            .getObjectTypeNodes()
            .get(ClassObjectType.InitialFact_ObjectType);

    // do we write the fact to the objecttypenode memory
    if (initialFactNode != null) {
      ObjectHashSet initialFactMemory = (ObjectHashSet) context.wm.getNodeMemory(initialFactNode);
      if (initialFactMemory != null && !initialFactMemory.isEmpty()) {
        // context.out.println( "InitialFactMemory true int:" + initialFactNode.getId() );
        stream.writeBoolean(true);
        stream.writeInt(initialFactNode.getId());

        // context.out.println( "InitialFact RightTuples" );
        writeRightTuples(context.wm.getInitialFactHandle(), context);
      } else {
        // context.out.println( "InitialFactMemory false " );
        stream.writeBoolean(false);
      }
    } else {
      // context.out.println( "InitialFactMemory false " );
      stream.writeBoolean(false);
    }
  }
Esempio n. 2
0
  public static ObjectTypeNode attachObjectTypeNode(BuildContext context, ObjectType objectType) {
    final InternalRuleBase ruleBase = context.getRuleBase();
    ruleBase.readLock();
    try {
      InternalWorkingMemory[] wms = context.getWorkingMemories();

      EntryPointNode epn = ruleBase.getRete().getEntryPointNode(context.getCurrentEntryPoint());
      if (epn == null) {
        epn = new EntryPointNode(context.getNextId(), ruleBase.getRete(), context);
        if (wms.length > 0) {
          epn.attach(wms);
        } else {
          epn.attach();
        }
      }

      ObjectTypeNode otn = new ObjectTypeNode(context.getNextId(), epn, objectType, context);

      long expirationOffset = getExpiratioOffsetForType(context, objectType);
      otn.setExpirationOffset(expirationOffset);

      if (wms.length > 0) {
        otn.attach(wms);
      } else {
        otn.attach();
      }

      return otn;
    } finally {
      ruleBase.readUnlock();
    }
  }
  public static void readPropagationContext(MarshallerReaderContext context) throws IOException {
    ObjectInputStream stream = context.stream;
    InternalRuleBase ruleBase = context.ruleBase;

    int type = stream.readInt();

    Rule rule = null;
    if (stream.readBoolean()) {
      String pkgName = stream.readUTF();
      String ruleName = stream.readUTF();
      Package pkg = ruleBase.getPackage(pkgName);
      rule = pkg.getRule(ruleName);
    }

    LeftTuple leftTuple = null;
    if (stream.readBoolean()) {
      int tuplePos = stream.readInt();
      leftTuple = context.terminalTupleMap.get(tuplePos);
    }

    long propagationNumber = stream.readLong();

    int factHandleId = stream.readInt();
    InternalFactHandle factHandle = context.handles.get(factHandleId);

    int activeActivations = stream.readInt();
    int dormantActivations = stream.readInt();
    String entryPointId = stream.readUTF();

    EntryPoint entryPoint = context.entryPoints.get(entryPointId);
    if (entryPoint == null) {
      entryPoint = new EntryPoint(entryPointId);
      context.entryPoints.put(entryPointId, entryPoint);
    }

    PropagationContext pc =
        new PropagationContextImpl(
            propagationNumber,
            type,
            rule,
            leftTuple,
            factHandle,
            activeActivations,
            dormantActivations,
            entryPoint);
    context.propagationContexts.put(propagationNumber, pc);
  }
 public static List<String> getSettableProperties(InternalRuleBase ruleBase, Class<?> nodeClass) {
   if (nodeClass == null) {
     return null;
   }
   TypeDeclaration typeDeclaration = ruleBase.getTypeDeclaration(nodeClass);
   if (typeDeclaration == null) {
     return ClassUtils.getSettableProperties(nodeClass);
   }
   typeDeclaration.setTypeClass(nodeClass);
   return typeDeclaration.getSettableProperties();
 }
  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;
  }
  // Input methods
  public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
    InternalRuleBase ruleBase = context.ruleBase;
    InternalWorkingMemory wm = context.wm;

    JBPMMessages.ProcessInstance _instance =
        (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance) context.parameterObject;
    if (_instance == null) {
      // try to parse from the stream
      ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
      Header _header;
      try {
        _header = PersisterHelper.readFromStreamWithHeader(context, registry);
      } catch (ClassNotFoundException e) {
        // Java 5 does not accept [new IOException(String, Throwable)]
        IOException ioe = new IOException("Error deserializing process instance.");
        ioe.initCause(e);
        throw ioe;
      }
      _instance = JBPMMessages.ProcessInstance.parseFrom(_header.getPayload(), registry);
    }

    WorkflowProcessInstanceImpl processInstance = createProcessInstance();
    processInstance.setId(_instance.getId());
    String processId = _instance.getProcessId();
    processInstance.setProcessId(processId);
    String processXml = _instance.getProcessXml();
    Process process = null;
    if (processXml != null && processXml.trim().length() > 0) {
      processInstance.setProcessXml(processXml);
      process = processInstance.getProcess();
    } else {
      process = ruleBase.getProcess(processId);
      processInstance.setProcess(process);
    }
    processInstance.setState(_instance.getState());
    long nodeInstanceCounter = _instance.getNodeInstanceCounter();
    processInstance.setKnowledgeRuntime(wm.getKnowledgeRuntime());

    if (_instance.getSwimlaneContextCount() > 0) {
      Context swimlaneContext =
          ((org.jbpm.process.core.Process) process)
              .getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
      SwimlaneContextInstance swimlaneContextInstance =
          (SwimlaneContextInstance) processInstance.getContextInstance(swimlaneContext);
      for (JBPMMessages.ProcessInstance.SwimlaneContextInstance _swimlane :
          _instance.getSwimlaneContextList()) {
        swimlaneContextInstance.setActorId(_swimlane.getSwimlane(), _swimlane.getActorId());
      }
    }

    for (JBPMMessages.ProcessInstance.NodeInstance _node : _instance.getNodeInstanceList()) {
      context.parameterObject = _node;
      readNodeInstance(context, processInstance, processInstance);
    }

    for (JBPMMessages.ProcessInstance.ExclusiveGroupInstance _excl :
        _instance.getExclusiveGroupList()) {
      ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
      processInstance.addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
      for (Long nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
        NodeInstance nodeInstance = processInstance.getNodeInstance(nodeInstanceId);
        if (nodeInstance == null) {
          throw new IllegalArgumentException(
              "Could not find node instance when deserializing exclusive group instance: "
                  + nodeInstanceId);
        }
        exclusiveGroupInstance.addNodeInstance(nodeInstance);
      }
    }

    if (_instance.getVariableCount() > 0) {
      Context variableScope =
          ((org.jbpm.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
      VariableScopeInstance variableScopeInstance =
          (VariableScopeInstance) processInstance.getContextInstance(variableScope);
      for (JBPMMessages.Variable _variable : _instance.getVariableList()) {
        try {
          Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
          variableScopeInstance.internalSetVariable(_variable.getName(), _value);
        } catch (ClassNotFoundException e) {
          throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
        }
      }
    }
    processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
    if (wm != null) {
      processInstance.reconnect();
    }
    return processInstance;
  }