Search in sources :

Example 1 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class TraitTripleProxyClassBuilderImpl method buildClass.

public byte[] buildClass(ClassDefinition core, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
    FieldVisitor fv;
    MethodVisitor mv;
    // get the method bitmask
    BitSet mask = traitRegistry.getFieldMask(getTrait().getName(), core.getDefinedClass().getName());
    String name = TraitFactory.getPropertyWrapperName(getTrait(), core);
    String masterName = TraitFactory.getProxyName(getTrait(), core);
    Class<?> traitClass = getTrait().getDefinedClass();
    String internalWrapper = BuildUtils.getInternalType(name);
    String internalProxy = BuildUtils.getInternalType(masterName);
    String internalCore = Type.getInternalName(core.getDefinedClass());
    String descrCore = Type.getDescriptor(core.getDefinedClass());
    String internalTrait = Type.getInternalName(getTrait().getDefinedClass());
    MixinInfo mixinInfo = findMixinInfo(traitClass);
    ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalProxy, null, Type.getInternalName(proxyBaseClass), new String[] { internalTrait, Type.getInternalName(Externalizable.class) });
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, TraitType.traitNameField, Type.getDescriptor(String.class), null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PUBLIC, "object", descrCore, null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "store", Type.getDescriptor(TripleStore.class), null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "storeId", Type.getDescriptor(String.class), null, null);
        fv.visitEnd();
    }
    if (mixinInfo != null) {
        for (Class<?> mixinClass : mixinInfo.mixinClasses) {
            {
                fv = cw.visitField(ACC_PRIVATE, getMixinName(mixinClass), Type.getDescriptor(mixinClass), null, null);
                fv.visitEnd();
            }
        }
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(TripleStore.class) + Type.getDescriptor(TripleFactory.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(boolean.class) + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "getId", "()" + Type.getDescriptor(String.class), false);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
        buildConstructorCore(mv, internalProxy, internalWrapper, internalCore, descrCore, mixinInfo);
        initFields(mv, internalProxy);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "setObject", "(" + Type.getDescriptor(Object.class) + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, internalCore);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", null, new String[] { Type.getInternalName(IOException.class) });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", false);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalProxy, "getObject", "()" + Type.getDescriptor(TraitableBean.class), false);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, internalProxy, "store", Type.getDescriptor(TripleStore.class));
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
        mv.visitInsn(RETURN);
        // mv.visitMaxs( 2, 2 );
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", null, new String[] { Type.getInternalName(IOException.class), Type.getInternalName(ClassNotFoundException.class) });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
        mv.visitTypeInsn(CHECKCAST, internalCore);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(String.class));
        mv.visitFieldInsn(PUTFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TripleStore.class));
        mv.visitFieldInsn(PUTFIELD, internalProxy, "store", Type.getDescriptor(TripleStore.class));
        mv.visitInsn(RETURN);
        // mv.visitMaxs( 3, 2 );
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    helpBuildClass(core, cw, internalProxy, descrCore, mask);
    buildProxyAccessors(mask, cw, masterName, core, mixinInfo);
    boolean hasKeys = false;
    for (FactField ff : getTrait().getFields()) {
        if (ff.isKey()) {
            hasKeys = true;
            break;
        }
    }
    if (!hasKeys) {
        buildEqualityMethods(cw, masterName, core.getClassName());
    } else {
        buildKeyedEqualityMethods(cw, getTrait(), masterName);
    }
    buildMixinMethods(masterName, mixinInfo, cw);
    buildCommonMethods(cw, masterName);
    buildExtendedMethods(cw, getTrait(), core);
    buildShadowMethods(cw, trait, core);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : FactField(org.kie.api.definition.type.FactField) ObjectOutput(java.io.ObjectOutput) BitSet(java.util.BitSet) IOException(java.io.IOException) FieldVisitor(org.mvel2.asm.FieldVisitor) ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) MethodVisitor(org.mvel2.asm.MethodVisitor) MixinInfo(org.drools.core.factmodel.traits.TraitBuilderUtil.MixinInfo) TraitBuilderUtil.findMixinInfo(org.drools.core.factmodel.traits.TraitBuilderUtil.findMixinInfo) TripleStore(org.drools.core.util.TripleStore) Externalizable(java.io.Externalizable) ObjectInput(java.io.ObjectInput)

Example 2 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class DialectUtil method parseInsertDescr.

private static void parseInsertDescr(RuleBuildContext context, JavaBlockDescr block) {
    String expr = block.getTargetExpression();
    if (expr.startsWith("new ")) {
        int argsStart = expr.indexOf('(');
        if (argsStart > 0) {
            String className = expr.substring(4, argsStart).trim();
            Class<?> typeClass = findClassByName(context, className);
            TypeDeclaration typeDeclaration = typeClass == null ? null : context.getKnowledgeBuilder().getTypeDeclaration(typeClass);
            if (typeDeclaration != null) {
                ConsequenceMetaData.Statement statement = new ConsequenceMetaData.Statement(ConsequenceMetaData.Statement.Type.INSERT, typeClass);
                context.getRule().getConsequenceMetaData().addStatement(statement);
                String constructorParams = expr.substring(argsStart + 1, expr.indexOf(')')).trim();
                List<String> args = splitArgumentsList(constructorParams);
                ClassDefinition classDefinition = typeDeclaration.getTypeClassDef();
                List<FactField> fields = classDefinition.getFields();
                if (args.size() == fields.size()) {
                    for (int i = 0; i < args.size(); i++) {
                        statement.addField(fields.get(i).getName(), args.get(i));
                    }
                }
            }
        }
    }
}
Also used : FactField(org.kie.api.definition.type.FactField) ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 3 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class KnowledgeBuilderTest method testDeclaredSuperTypeFields.

@Test
public void testDeclaredSuperTypeFields() throws Exception {
    String drl = "package foo \n" + "declare Bean1 \n" + "age: int \n" + "name : String \n" + "end \n" + "declare Bean2 extends Bean1\n" + "cheese : String \n" + "end";
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    builder.addPackageFromDrl(new StringReader(drl));
    List<FactField> fieldsBean1 = builder.getPackage("foo").getFactType("foo.Bean1").getFields();
    assertEquals(2, fieldsBean1.size());
    assertEquals("age", fieldsBean1.get(0).getName());
    assertEquals(int.class, fieldsBean1.get(0).getType());
    assertEquals("name", fieldsBean1.get(1).getName());
    assertEquals(String.class, fieldsBean1.get(1).getType());
    List<FactField> fieldsBean2 = builder.getPackage("foo").getFactType("foo.Bean2").getFields();
    assertEquals(3, fieldsBean2.size());
    assertEquals("age", fieldsBean2.get(0).getName());
    assertEquals(int.class, fieldsBean2.get(0).getType());
    assertEquals("name", fieldsBean2.get(1).getName());
    assertEquals(String.class, fieldsBean2.get(1).getType());
    assertEquals("cheese", fieldsBean2.get(2).getName());
    assertEquals(String.class, fieldsBean2.get(2).getType());
}
Also used : FactField(org.kie.api.definition.type.FactField) StringReader(java.io.StringReader) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 4 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class KieCompilationCacheTest method testCompilationCache.

@Test
public void testCompilationCache() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare type X\n" + "    foo : String\n" + "end\n" + "rule R1 when\n" + "   $m : X( foo == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    ks.newKieBuilder(kfs).buildAll();
    ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieBase kbase = kc.getKieBase();
    FactType type = kbase.getFactType("org.drools.compiler", "X");
    FactField foo = type.getField("foo");
    Object x = type.newInstance();
    foo.set(x, "Hello World");
    KieSession ksession = kc.newKieSession();
    ksession.insert(x);
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : FactField(org.kie.api.definition.type.FactField) KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) FactType(org.kie.api.definition.type.FactType) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 5 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class KnowledgePackageMetaDataTest method testMetaData.

@Test
public void testMetaData() {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        fail(kBuilder.getErrors().toString());
    }
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(kBuilder.getKnowledgePackages());
    KiePackage pack = kBase.getPackage("org.drools.compiler.test.definitions");
    assertNotNull(pack);
    assertEquals(2, pack.getFunctionNames().size());
    assertTrue(pack.getFunctionNames().contains("fun1"));
    assertTrue(pack.getFunctionNames().contains("fun2"));
    assertEquals(2, pack.getGlobalVariables().size());
    GlobalImpl g1 = new GlobalImpl("N", "java.lang.Integer");
    GlobalImpl g2 = new GlobalImpl("list", "java.util.List");
    assertTrue(pack.getGlobalVariables().contains(g1));
    assertTrue(pack.getGlobalVariables().contains(g2));
    assertEquals(2, pack.getFactTypes().size());
    FactType type;
    for (int j = 0; j < 2; j++) {
        type = pack.getFactTypes().iterator().next();
        if (type.getName().equals("org.drools.compiler.test.definitions.Person")) {
            assertEquals(2, type.getFields().size());
        } else if (type.getName().equals("org.drools.compiler.test.definitions.Foo")) {
            assertEquals("org.drools.compiler.test.definitions.Person", type.getSuperClass());
            FactField fld = type.getField("bar");
            assertEquals(2, fld.getIndex());
            assertEquals(String.class, fld.getType());
        } else {
            fail("Unexpected fact type " + type);
        }
    }
    assertEquals(2, pack.getQueries().size());
    for (Query q : pack.getQueries()) {
        assertTrue(q.getName().equals("qry1") || q.getName().equals("qry2"));
    }
    assertEquals(4, pack.getRules().size());
    assertTrue(pack.getRules().containsAll(pack.getQueries()));
}
Also used : FactField(org.kie.api.definition.type.FactField) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Query(org.kie.api.definition.rule.Query) KiePackage(org.kie.api.definition.KiePackage) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) GlobalImpl(org.drools.core.definitions.rule.impl.GlobalImpl) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Aggregations

FactField (org.kie.api.definition.type.FactField)14 Test (org.junit.Test)7 FactType (org.kie.api.definition.type.FactType)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)4 FieldDefinition (org.drools.core.factmodel.FieldDefinition)3 BitSet (java.util.BitSet)2 ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 KiePackage (org.kie.api.definition.KiePackage)2 Type (org.mvel2.asm.Type)2 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1