/* (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(); }
protected ClassWriter buildClassHeader(ClassLoader classLoader, EnumClassDefinition classDef) { return createClassWriter( classLoader, ACC_PUBLIC + ACC_FINAL + ACC_SUPER + ACC_ENUM, BuildUtils.getInternalType(classDef.getClassName()), "Ljava/lang/Enum<" + BuildUtils.getTypeDescriptor(classDef.getClassName()) + ">;", BuildUtils.getInternalType(classDef.getSuperClass()), BuildUtils.getInternalTypes(classDef.getInterfaces())); }
protected List<String> defaultAppendOptions( CompileCppOptions options, List<String> command, VirtualFile file) { command = BuildUtils.appendAllOptions(command, options.getProjectCompilerOptions()); command = BuildUtils.appendAllOptions( command, options.getCompilerOptions().replace(MARKER, getEscapedPathToFile(file.getPath()))); return command; }
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(); }
@Nullable List<String> buildCommand(VirtualFile file, CompileCppOptions options) { List<String> commandLine = new ArrayList<String>(4); if (!options.doRun()) { commandLine = BuildUtils.appendOptions(commandLine, "-S"); } else { final String fileName = options.getOutputFileName(); if (fileName != null) { commandLine = BuildUtils.appendOptions(commandLine, "-o"); commandLine = BuildUtils.appendOptions(commandLine, fileName); } } commandLine = defaultAppendOptions(options, commandLine, file); myItems = BuildUtils.buildEnvironmentMap(options.getProject(), file); return BuildUtils.buildClangToolCall( CppSupportSettings.getInstance().getClangPath(), commandLine); }
@Nullable List<String> buildCommand(VirtualFile file, CompileCppOptions options) { try { List<String> commandLine = Arrays.asList("cl.exe"); if (!options.doRun()) commandLine = BuildUtils.appendOptions(commandLine, "/c"); else { final String fileName = options.getOutputFileName(); if (fileName != null) { commandLine = BuildUtils.appendOptions(commandLine, "/Fe" + fileName); } } commandLine = defaultAppendOptions(options, commandLine, file); return BuildUtils.buildVCToolInvokation(commandLine); } catch (IOException e) { throw new RuntimeException(e); } }
@Nullable List<String> buildCommand(VirtualFile file, CompileCppOptions options) { List<String> command = new ArrayList<String>(4); if (!options.doRun()) command = BuildUtils.appendOptions(command, "-c"); else { final String fileName = options.getOutputFileName(); if (fileName != null) { command = BuildUtils.appendOptions(command, "-o"); command = BuildUtils.appendOptions(command, fileName); } } command = defaultAppendOptions(options, command, file); myItems = BuildUtils.buildEnvironmentMap(options.getProject(), file); String gccPath = CppSupportSettings.getInstance() .getGccPath() .replaceAll("gcc", options.isCppFile() ? "g++" : "gcc"); return BuildUtils.buildGccToolCall(gccPath, command); }
protected void buildLiterals(ClassWriter cw, EnumClassDefinition classDef) { FieldVisitor fv; for (EnumLiteralDefinition lit : classDef.getEnumLiterals()) { fv = cw.visitField( ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM, lit.getName(), BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null); fv.visitEnd(); } { fv = cw.visitField( ACC_PRIVATE + ACC_FINAL + ACC_STATIC + ACC_SYNTHETIC, "$VALUES", "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null); fv.visitEnd(); } }
protected void buildFields(ClassWriter cw, EnumClassDefinition classDef) { FieldVisitor fv; for (FieldDefinition fld : classDef.getFieldsDefinitions()) { fv = cw.visitField( ACC_PRIVATE + ACC_FINAL, fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName()), null, null); fv.visitEnd(); } { fv = cw.visitField( ACC_PRIVATE + ACC_STATIC, "$context", "Lorg/mvel2/ParserContext;", null, null); fv.visitEnd(); } }
public void build(BuildContext context, BuildUtils utils, RuleConditionElement rce) { NamedConsequence namedConsequence = (NamedConsequence) rce; Timer timer = context.getRule().getTimer(); if (timer != null) { ReteooComponentBuilder builder = utils.getBuilderFor(Timer.class); builder.build(context, utils, context.getRule().getTimer()); } RuleTerminalNode terminalNode = buildTerminalNodeForNamedConsequence(context, namedConsequence); terminalNode.attach(context); terminalNode.networkUpdated(new UpdateContext()); // adds the terminal node to the list of nodes created/added by this sub-rule context.getNodes().add(terminalNode); if (timer != null) { context.setTupleSource(context.getTupleSource().getLeftTupleSource()); } context.setTerminated(namedConsequence.isTerminal()); }
protected void buildGettersAndSetters(ClassWriter cw, EnumClassDefinition classDef) { MethodVisitor mv; { mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC, "values", "()[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null); mv.visitCode(); mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType(classDef.getClassName()), "$VALUES", "[" + BuildUtils.getTypeDescriptor(classDef.getClassName())); mv.visitMethodInsn( INVOKEVIRTUAL, "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), "clone", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "[" + BuildUtils.getTypeDescriptor(classDef.getClassName())); mv.visitInsn(ARETURN); mv.visitMaxs(1, 0); mv.visitEnd(); } for (FieldDefinition fld : classDef.getFieldsDefinitions()) { mv = cw.visitMethod( ACC_PUBLIC, BuildUtils.getterName(fld.getName(), fld.getTypeName()), "()" + BuildUtils.getTypeDescriptor(fld.getTypeName()), null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType(classDef.getName()), fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName())); mv.visitInsn(BuildUtils.returnType(fld.getTypeName())); mv.visitMaxs(BuildUtils.sizeOf(fld.getTypeName()), 1); mv.visitEnd(); mv = cw.visitMethod( ACC_PUBLIC, BuildUtils.setterName(fld.getName(), fld.getTypeName()), "(" + BuildUtils.getTypeDescriptor(fld.getTypeName()) + ")V", null, null); mv.visitCode(); mv.visitInsn(RETURN); mv.visitMaxs(0, 1 + BuildUtils.sizeOf(fld.getTypeName())); mv.visitEnd(); } { mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC, "get$context", "()Lorg/mvel2/ParserContext;", null, null); mv.visitCode(); mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType(classDef.getName()), "$context", "Lorg/mvel2/ParserContext;"); mv.visitInsn(ARETURN); mv.visitMaxs(1, 0); mv.visitEnd(); } { mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC, "set$context", "(Lorg/mvel2/ParserContext;)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn( PUTSTATIC, BuildUtils.getInternalType(classDef.getName()), "$context", "Lorg/mvel2/ParserContext;"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } }
protected void buildConstructors(ClassWriter cw, EnumClassDefinition classDef) throws IOException, ClassNotFoundException { MethodVisitor mv; String argTypes = ""; int size = 0; for (FieldDefinition fld : classDef.getFieldsDefinitions()) { argTypes += BuildUtils.getTypeDescriptor(fld.getTypeName()); size += BuildUtils.sizeOf(fld.getTypeName()); } { int ofs = 3; mv = cw.visitMethod( ACC_PRIVATE, "<init>", "(Ljava/lang/String;I" + argTypes + ")V", "(" + argTypes + ")V", null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ILOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V"); for (FieldDefinition fld : classDef.getFieldsDefinitions()) { mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(BuildUtils.varType(fld.getTypeName()), ofs); mv.visitFieldInsn( PUTFIELD, BuildUtils.getInternalType(classDef.getName()), fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName())); ofs += BuildUtils.sizeOf(fld.getTypeName()); } mv.visitInsn(RETURN); mv.visitMaxs(3, ofs); mv.visitEnd(); } { mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); int N = classDef.getEnumLiterals().size(); mv.visitTypeInsn(NEW, BuildUtils.getInternalType(classDef.getClassName())); for (int j = 0; j < N; j++) { EnumLiteralDefinition lit = classDef.getEnumLiterals().get(j); mv.visitInsn(DUP); mv.visitLdcInsn(lit.getName()); BuildUtils.pushInt(mv, j); List<String> args = lit.getConstructorArgs(); for (int k = 0; k < args.size(); k++) { String argType = classDef.getField(k).getTypeName(); mv.visitLdcInsn(args.get(k)); mv.visitMethodInsn( INVOKESTATIC, "org/mvel2/MVEL", "eval", "(Ljava/lang/String;)Ljava/lang/Object;"); if (BuildUtils.isPrimitive(argType)) { mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(BuildUtils.box(argType))); mv.visitMethodInsn( INVOKEVIRTUAL, BuildUtils.getInternalType(BuildUtils.box(argType)), BuildUtils.numericMorph(BuildUtils.box(argType)), "()" + BuildUtils.getTypeDescriptor(argType)); } else { mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(argType)); } } mv.visitMethodInsn( INVOKESPECIAL, BuildUtils.getInternalType(classDef.getClassName()), "<init>", "(Ljava/lang/String;I" + argTypes + ")V"); mv.visitFieldInsn( PUTSTATIC, BuildUtils.getInternalType(classDef.getClassName()), lit.getName(), BuildUtils.getTypeDescriptor(classDef.getClassName())); mv.visitTypeInsn(NEW, BuildUtils.getInternalType(classDef.getClassName())); } BuildUtils.pushInt(mv, N); mv.visitTypeInsn(ANEWARRAY, BuildUtils.getInternalType(classDef.getClassName())); for (int j = 0; j < N; j++) { EnumLiteralDefinition lit = classDef.getEnumLiterals().get(j); mv.visitInsn(DUP); BuildUtils.pushInt(mv, j); mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType(classDef.getClassName()), lit.getName(), BuildUtils.getTypeDescriptor(classDef.getClassName())); mv.visitInsn(AASTORE); } mv.visitFieldInsn( PUTSTATIC, BuildUtils.getInternalType(classDef.getClassName()), "$VALUES", "[" + BuildUtils.getTypeDescriptor(classDef.getClassName())); mv.visitInsn(RETURN); mv.visitMaxs(4 + size, 0); mv.visitEnd(); } { mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC, "valueOf", "(Ljava/lang/String;)" + BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null); mv.visitCode(); mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(classDef.getClassName()))); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn( INVOKESTATIC, "java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;"); mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(classDef.getClassName())); mv.visitInsn(ARETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } }
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); }