/** * Construct given a semantic <code>ObjectType</code> and the provided unique id. All <code> * ObjectTypdeNode</code> have node memory. * * @param id The unique id for the node. * @param objectType The semantic object-type differentiator. */ public ObjectTypeNode( final int id, final EntryPointNode source, final ObjectType objectType, final BuildContext context) { super( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation(), source, context.getRuleBase().getConfiguration().getAlphaNodeHashingThreshold()); this.objectType = objectType; setObjectMemoryEnabled(context.isObjectTypeNodeMemoryEnabled()); }
public void setupJoinNode() { buildContext = createContext(); joinNode = (JoinNode) BetaNodeBuilder.create(NodeTypeEnums.JoinNode, buildContext) .setLeftType(A.class) .setBinding("object", "$object") .setRightType(B.class) .setConstraint("object", "!=", "$object") .build(); sinkNode = new JoinNode(); sinkNode.setId(1); sinkNode.setConstraints(new EmptyBetaConstraints()); joinNode.addTupleSink(sinkNode); wm = (InternalWorkingMemory) buildContext.getRuleBase().newStatefulSession(true); bm = (BetaMemory) wm.getNodeMemory(joinNode); bm0 = (BetaMemory) wm.getNodeMemory(sinkNode); smem = new SegmentMemory(joinNode); bm.setSegmentMemory(smem); smem0 = new SegmentMemory(sinkNode); bm0.setSegmentMemory(smem0); smem.add(smem0); }
/** * Constructor specifying the unique id of the node in the Rete network, the position of the propagating <code>FactHandleImpl</code> in * <code>ReteTuple</code> and the source that propagates the receive <code>ReteTuple<code>s. * * @param id * Unique id * @param source * The <code>TupleSource</code> which propagates the received <code>ReteTuple</code> */ public RightInputAdapterNode( final int id, final LeftTupleSource source, final BuildContext context) { super( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation()); this.tupleSource = source; this.tupleMemoryEnabled = context.isTupleMemoryEnabled(); }
public EntryPointNode(final int id, final ObjectSource objectSource, final BuildContext context) { this( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation(), objectSource, context .getCurrentEntryPoint()); // irrelevant for this node, since it overrides sink // management }
/** * Construct given a semantic <code>ObjectType</code> and the provided unique id. All <code> * ObjectTypdeNode</code> have node memory. * * @param id The unique id for the node. * @param objectType The semantic object-type differentiator. */ public ObjectTypeNode( final int id, final EntryPointNode source, final ObjectType objectType, final BuildContext context) { super( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation(), source, context.getRuleBase().getConfiguration().getAlphaNodeHashingThreshold()); this.objectType = objectType; this.lrUnlinkingEnabled = context.getRuleBase().getConfiguration().isLRUnlinkingEnabled(); setObjectMemoryEnabled(context.isObjectTypeNodeMemoryEnabled()); if (ClassObjectType.DroolsQuery_ObjectType.isAssignableFrom(objectType)) { queryNode = true; } }
/** * Constructus a LeftInputAdapterNode with a unique id that receives <code>FactHandle</code> from * a parent <code>ObjectSource</code> and adds it to a given pattern in the resulting Tuples. * * @param id The unique id of this node in the current Rete network * @param source The parent node, where Facts are propagated from */ public LeftInputAdapterNode(final int id, final ObjectSource source, final BuildContext context) { super( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation()); this.objectSource = source; this.leftTupleMemoryEnabled = context.isTupleMemoryEnabled(); ObjectSource current = source; while (!(current instanceof ObjectTypeNode)) { current = current.getParentObjectSource(); } ObjectTypeNode otn = (ObjectTypeNode) current; rootQueryNode = ClassObjectType.DroolsQuery_ObjectType.isAssignableFrom(otn.getObjectType()); }
/** * Construct. * * @param id The unique id for this node. * @param leftInput The left input <code>TupleSource</code>. * @param rightInput The right input <code>ObjectSource</code>. * @param joinNodeBinder The constraints to be applied to the right objects */ public ExistsNode( final int id, final LeftTupleSource leftInput, final ObjectSource rightInput, final BetaConstraints joinNodeBinder, final BuildContext context) { super( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation(), leftInput, rightInput, joinNodeBinder, context); this.tupleMemoryEnabled = context.isTupleMemoryEnabled(); }
public QueryElementNode( final int id, final LeftTupleSource tupleSource, final QueryElement queryElement, final boolean tupleMemoryEnabled, final boolean openQuery, final BuildContext context) { super( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation()); this.tupleSource = tupleSource; this.queryElement = queryElement; this.tupleMemoryEnabled = tupleMemoryEnabled; this.openQuery = openQuery; initMasks(context, tupleSource); }
@Override protected void initDeclaredMask(BuildContext context, LeftTupleSource leftInput) { if (context == null || context.getLastBuiltPatterns() == null) { // only happens during unit tests rightDeclaredMask = Long.MAX_VALUE; super.initDeclaredMask(context, leftInput); return; } if (!(rightInput instanceof RightInputAdapterNode)) { Pattern pattern = context.getLastBuiltPatterns()[0]; // right input pattern ObjectType objectType = pattern.getObjectType(); if (!(objectType instanceof ClassObjectType)) { // InitialFact has no type declaration and cannot be property specific // Only ClassObjectType can use property specific rightDeclaredMask = Long.MAX_VALUE; } Class objectClass = ((ClassObjectType) objectType).getClassType(); if (isPropertyReactive(context, objectClass)) { rightListenedProperties = pattern.getListenedProperties(); List<String> settableProperties = getSettableProperties(context.getRuleBase(), objectClass); rightDeclaredMask = calculatePositiveMask(rightListenedProperties, settableProperties); rightDeclaredMask |= constraints.getListenedPropertyMask(settableProperties); rightNegativeMask = calculateNegativeMask(rightListenedProperties, settableProperties); } else { // if property reactive is not on, then accept all modification propagations rightDeclaredMask = Long.MAX_VALUE; } } else { rightDeclaredMask = Long.MAX_VALUE; // There would have been no right input pattern, so swap current to first, so leftInput can // still work context.setLastBuiltPattern(context.getLastBuiltPatterns()[0]); } super.initDeclaredMask(context, leftInput); }
public AccumulateNode( final int id, final LeftTupleSource leftInput, final ObjectSource rightInput, final AlphaNodeFieldConstraint[] resultConstraints, final BetaConstraints sourceBinder, final BetaConstraints resultBinder, final Accumulate accumulate, final boolean unwrapRightObject, final BuildContext context) { super( id, context.getPartitionId(), context.getRuleBase().getConfiguration().isMultithreadEvaluation(), leftInput, rightInput, sourceBinder, context); this.resultBinder = resultBinder; this.resultConstraints = resultConstraints; this.accumulate = accumulate; this.unwrapRightObject = unwrapRightObject; this.tupleMemoryEnabled = context.isTupleMemoryEnabled(); }
public void execute(Map<String, Object> context, List<String[]> args) { BuildContext buildContext = (BuildContext) context.get("BuildContext"); if (args.size() >= 1) { // The first argument list is the node parameters String[] a = args.get(0); String name = a[0]; String leftInput = a[1]; String rightInput = a[2]; String sourceType = a[3]; String expr = a[4]; LeftTupleSource leftTupleSource; if ("mock".equals(leftInput)) { leftTupleSource = Mockito.mock(LeftTupleSource.class); } else { leftTupleSource = (LeftTupleSource) context.get(leftInput); } ObjectSource rightObjectSource; if ("mock".equals(rightInput)) { rightObjectSource = Mockito.mock(ObjectSource.class); } else { rightObjectSource = (ObjectSource) context.get(rightInput); } Pattern sourcePattern; Pattern resultPattern; try { sourcePattern = reteTesterHelper.getPattern(0, sourceType); // we always use the accumulate function "sum", so return type is always Number resultPattern = reteTesterHelper.getPattern(buildContext.getNextId(), Number.class.getName()); } catch (Exception e) { throw new IllegalArgumentException( "Not possible to process arguments: " + Arrays.toString(a)); } BetaConstraints betaSourceConstraints = new EmptyBetaConstraints(); AlphaNodeFieldConstraint[] alphaResultConstraint = new AlphaNodeFieldConstraint[0]; // the following arguments are constraints for (int i = 1; i < args.size(); i++) { a = args.get(i); String type = a[0]; String fieldName = a[1]; String operator = a[2]; String val = a[3]; if ("source".equals(type)) { Declaration declr = (Declaration) context.get(val); try { BetaNodeFieldConstraint sourceBetaConstraint = this.reteTesterHelper.getBoundVariableConstraint( sourcePattern, fieldName, declr, operator); betaSourceConstraints = new SingleBetaConstraints( sourceBetaConstraint, buildContext.getRuleBase().getConfiguration()); } catch (IntrospectionException e) { throw new IllegalArgumentException(); } } else if ("result".equals(type)) { alphaResultConstraint = new AlphaNodeFieldConstraint[1]; try { alphaResultConstraint[0] = this.reteTesterHelper.getLiteralConstraint(resultPattern, fieldName, operator, val); } catch (IntrospectionException e) { throw new IllegalArgumentException( "Unable to configure alpha constraint: " + Arrays.toString(a), e); } } } NodeTestCase testCase = (NodeTestCase) context.get("TestCase"); List<String> classImports = new ArrayList<String>(); List<String> pkgImports = new ArrayList<String>(); for (String imp : testCase.getImports()) { if (imp.endsWith(".*")) { pkgImports.add(imp.substring(0, imp.lastIndexOf('.'))); } else { classImports.add(imp); } } // build an external function executor MVELCompilationUnit compilationUnit = new MVELCompilationUnit( name, expr, pkgImports.toArray(new String[0]), // pkg imports classImports.toArray(new String[0]), // imported classes new String[] {}, // imported methods new String[] {}, // imported fields new String[] {}, // global identifiers new Declaration[] {}, // previous declarations new Declaration[] {(Declaration) context.get(expr)}, // local declarations new String[] {}, // other identifiers new String[] {}, // input identifiers new String[] {}, // input types 4, false); AccumulateFunction accFunction = new SumAccumulateFunction(); Accumulator accumulator = new MVELAccumulatorFunctionExecutor(compilationUnit, accFunction); ((MVELCompileable) accumulator).compile(Thread.currentThread().getContextClassLoader()); Accumulate accumulate = new Accumulate( sourcePattern, new Declaration[] {}, // required declaration new Declaration[] {}, // inner declarations new Accumulator[] {accumulator}); AccumulateNode accNode = new AccumulateNode( buildContext.getNextId(), leftTupleSource, rightObjectSource, alphaResultConstraint, betaSourceConstraints, new EmptyBetaConstraints(), new Behavior[] {}, accumulate, false, buildContext); accNode.attach(); context.put(name, accNode); } else { StringBuilder msgBuilder = new StringBuilder(); msgBuilder.append("Can not parse AccumulateNode step arguments: \n"); for (String[] arg : args) { msgBuilder.append(" "); msgBuilder.append(Arrays.toString(arg)); msgBuilder.append("\n"); } throw new IllegalArgumentException(msgBuilder.toString()); } }
public static boolean isPropertyReactive(BuildContext context, Class<?> objectClass) { TypeDeclaration typeDeclaration = context.getRuleBase().getTypeDeclaration(objectClass); return typeDeclaration != null && typeDeclaration.isPropertyReactive(); }