コード例 #1
0
ファイル: PatternBuilder.java プロジェクト: spyros3/drools
  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();
    }
  }
コード例 #2
0
ファイル: FromBuilder.java プロジェクト: chearius/drools
  /* (non-Javadoc)
   * @see org.kie.reteoo.builder.ReteooComponentBuilder#build(org.kie.reteoo.builder.BuildContext, org.kie.reteoo.builder.BuildUtils, org.kie.rule.RuleConditionElement)
   */
  public void build(
      final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final From from = (From) rce;
    context.pushRuleComponent(from);

    @SuppressWarnings("unchecked")
    BetaConstraints betaConstraints =
        utils.createBetaNodeConstraint(
            context, (List<BetaNodeFieldConstraint>) context.getBetaconstraints(), true);

    context.setTupleSource(
        (LeftTupleSource)
            utils.attachNode(
                context,
                context
                    .getComponentFactory()
                    .getNodeFactoryService()
                    .buildFromNode(
                        context.getNextId(),
                        from.getDataProvider(),
                        context.getTupleSource(),
                        context
                            .getAlphaConstraints()
                            .toArray(
                                new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]),
                        betaConstraints,
                        context.isTupleMemoryEnabled(),
                        context,
                        from)));
    context.setAlphaConstraints(null);
    context.setBetaconstraints(null);
    context.popRuleComponent();
  }
コード例 #3
0
ファイル: PatternBuilder.java プロジェクト: spyros3/drools
  private void attachPattern(
      final BuildContext context, final BuildUtils utils, final Pattern pattern)
      throws InvalidPatternException {

    // Set pattern offset to the appropriate value
    pattern.setOffset(context.getCurrentPatternOffset());

    final List<Constraint> alphaConstraints = new LinkedList<Constraint>();
    final List<Constraint> betaConstraints = new LinkedList<Constraint>();
    final List<Behavior> behaviors = new LinkedList<Behavior>();

    this.createConstraints(context, utils, pattern, alphaConstraints, betaConstraints);

    // Create BetaConstraints object
    context.setBetaconstraints(betaConstraints);

    // set behaviors list
    behaviors.addAll(pattern.getBehaviors());
    context.setBehaviors(behaviors);

    if (pattern.getSource() != null) {
      context.setAlphaConstraints(alphaConstraints);
      final int currentOffset = context.getCurrentPatternOffset();

      PatternSource source = pattern.getSource();

      ReteooComponentBuilder builder = utils.getBuilderFor(source);

      builder.build(context, utils, source);
      // restoring offset
      context.setCurrentPatternOffset(currentOffset);
    }

    if (pattern.getSource() == null || context.getCurrentEntryPoint() != EntryPoint.DEFAULT) {
      attachAlphaNodes(context, utils, pattern, alphaConstraints);

      if (context.getCurrentEntryPoint() != EntryPoint.DEFAULT) {
        context.setObjectSource(
            (ObjectSource)
                utils.attachNode(
                    context,
                    new PropagationQueuingNode(
                        context.getNextId(), context.getObjectSource(), context)));
        // the entry-point specific network nodes are attached, so, set context to default
        // entry-point
        context.setCurrentEntryPoint(EntryPoint.DEFAULT);
      }
    }

    // last thing to do is increment the offset, since if the pattern has a source,
    // offset must be overriden
    context.incrementCurrentPatternOffset();
  }
コード例 #4
0
  static RuleTerminalNode buildTerminalNodeForNamedConsequence(
      BuildContext context, NamedConsequence namedConsequence) {
    RuleImpl rule = context.getRule();
    GroupElement subrule = (GroupElement) context.peek();

    ActivationListenerFactory factory =
        context
            .getKnowledgeBase()
            .getConfiguration()
            .getActivationListenerFactory(rule.getActivationListener());

    context.setConsequenceName(namedConsequence.getConsequenceName());
    TerminalNode terminal =
        factory.createActivationListener(
            context.getNextId(),
            context.getTupleSource(),
            rule,
            subrule,
            0, // subruleIndex,
            context);
    context.setConsequenceName(null);

    return (RuleTerminalNode) terminal;
  }
コード例 #5
0
ファイル: PatternBuilder.java プロジェクト: spyros3/drools
  public void attachAlphaNodes(
      final BuildContext context,
      final BuildUtils utils,
      final Pattern pattern,
      List<Constraint> alphaConstraints)
      throws InvalidPatternException {

    // Drools Query ObjectTypeNode never has memory, but other ObjectTypeNode/AlphaNoesNodes may (if
    // not in sequential),
    // so need to preserve, so we can restore after this node is added. LeftMemory  and Terminal
    // remain the same once set.

    boolean objectMemory = context.isObjectTypeNodeMemoryEnabled();
    boolean alphaMemory = context.isAlphaMemoryAllowed();

    ObjectType objectType = pattern.getObjectType();

    if (pattern.getObjectType() instanceof ClassObjectType) {
      // Is this the query node, if so we don't want any memory
      if (DroolsQuery.class == ((ClassObjectType) pattern.getObjectType()).getClassType()) {
        context.setTupleMemoryEnabled(false);
        context.setObjectTypeNodeMemoryEnabled(false);
        context.setTerminalNodeMemoryEnabled(false);
        context.setAlphaNodeMemoryAllowed(false);
      }
    }

    context.setObjectSource(
        (ObjectSource)
            utils.attachNode(
                context,
                new EntryPointNode(context.getNextId(), context.getRuleBase().getRete(), context)));

    ObjectTypeNode otn =
        new ObjectTypeNode(
            context.getNextId(), (EntryPointNode) context.getObjectSource(), objectType, context);
    if (objectType.isEvent()
        && EventProcessingOption.STREAM.equals(
            context.getRuleBase().getConfiguration().getEventProcessingMode())) {
      long expirationOffset = getExpiratioOffsetForType(context, objectType);
      if (expirationOffset != -1) {
        // expiration policy is set, so use it
        otn.setExpirationOffset(expirationOffset);
      } else {
        // otherwise calculate it based on behaviours and temporal constraints
        for (Behavior behavior : pattern.getBehaviors()) {
          if (behavior.getExpirationOffset() != -1) {
            expirationOffset = Math.max(behavior.getExpirationOffset(), expirationOffset);
          }
        }
        long distance = context.getTemporalDistance().getExpirationOffset(pattern);
        if (distance == -1) {
          // it means the rules have no temporal constraints, or
          // the constraints require events to be hold forever. In this
          // case, we allow type declarations to override the implicit
          // expiration offset by defining an expiration policy with the
          // @expires tag
          otn.setExpirationOffset(expirationOffset);
        } else {
          otn.setExpirationOffset(Math.max(distance, expirationOffset));
        }
      }
    }

    context.setObjectSource((ObjectSource) utils.attachNode(context, otn));

    for (final Iterator<Constraint> it = alphaConstraints.iterator(); it.hasNext(); ) {
      final AlphaNodeFieldConstraint constraint = (AlphaNodeFieldConstraint) it.next();

      context.pushRuleComponent(constraint);
      context.setObjectSource(
          (ObjectSource)
              utils.attachNode(
                  context,
                  new AlphaNode(
                      context.getNextId(),
                      (AlphaNodeFieldConstraint) constraint,
                      context.getObjectSource(),
                      context)));
      context.popRuleComponent();
    }

    // now restore back to original values
    context.setObjectTypeNodeMemoryEnabled(objectMemory);
    context.setAlphaNodeMemoryAllowed(alphaMemory);
  }