Search in sources :

Example 16 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class AbstractTraitFactory method buildClassDefinition.

public ClassDefinition buildClassDefinition(Class<?> klazz, Class<?> wrapperClass) throws IOException {
    ClassFieldInspector inspector = new ClassFieldInspector(klazz);
    ClassFieldAccessorStore store = getClassFieldAccessorStore();
    ClassDefinition def;
    if (!klazz.isInterface()) {
        String className = wrapperClass.getName();
        String superClass = wrapperClass != klazz ? klazz.getName() : klazz.getSuperclass().getName();
        String[] interfaces = new String[klazz.getInterfaces().length + 1];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        interfaces[interfaces.length - 1] = CoreWrapper.class.getName();
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(wrapperClass);
        Traitable tbl = wrapperClass.getAnnotation(Traitable.class);
        def.setTraitable(true, tbl != null && tbl.logical());
        Map<String, Field> fields = inspector.getFieldTypesField();
        for (Field f : fields.values()) {
            if (f != null) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(f.getName());
                fld.setTypeName(f.getType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                if (inspector.getGetterMethods().containsKey(f.getName())) {
                    fld.setGetterName(inspector.getGetterMethods().get(f.getName()).getName());
                }
                if (inspector.getSetterMethods().containsKey(f.getName())) {
                    fld.setSetterName(inspector.getSetterMethods().get(f.getName()).getName());
                }
                def.addField(fld);
            }
        }
    } else {
        String className = klazz.getName();
        String superClass = Object.class.getName();
        String[] interfaces = new String[klazz.getInterfaces().length];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(klazz);
        Map<String, Method> properties = inspector.getGetterMethods();
        for (String key : properties.keySet()) {
            Method m = properties.get(key);
            if (m != null && m.getDeclaringClass() != TraitType.class && m.getDeclaringClass() != Thing.class && inspector.getSetterMethods().containsKey(key)) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(getterToFieldName(m.getName()));
                fld.setTypeName(m.getReturnType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                fld.setGetterName(m.getName());
                fld.setSetterName(inspector.getSetterMethods().get(key).getName());
                def.addField(fld);
            }
        }
    }
    return def;
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Method(java.lang.reflect.Method) ClassDefinition(org.drools.core.factmodel.ClassDefinition) Field(java.lang.reflect.Field) ClassFieldAccessor(org.drools.core.base.ClassFieldAccessor) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 17 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class TraitRegistry method bind.

private BitSet bind(String trait, String traitable) throws UnsupportedOperationException {
    ClassDefinition traitDef = getTrait(trait);
    if (traitDef == null) {
        throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a trait ");
    }
    ClassDefinition traitableDef = getTraitable(traitable);
    if (traitableDef == null) {
        throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a traitable ");
    }
    int j = 0;
    BitSet bitmask = new BitSet(traitDef.getFields().size());
    for (FactField field : traitDef.getFields()) {
        String alias = ((FieldDefinition) field).resolveAlias();
        FieldDefinition concreteField = traitableDef.getFieldByAlias(alias);
        if (concreteField != null) {
            if (!traitableDef.isFullTraiting() && !concreteField.getType().isAssignableFrom(field.getType())) {
                throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " :" + " trait field " + field.getName() + ":" + ((FieldDefinition) field).getTypeName() + " is incompatible with" + " concrete hard field " + concreteField.getName() + ":" + concreteField.getTypeName() + ". Consider enabling logical traiting" + " mode using @Traitable( logical = true )");
            }
            bitmask.set(j);
        }
        j++;
    }
    return bitmask;
}
Also used : FactField(org.kie.api.definition.type.FactField) FieldDefinition(org.drools.core.factmodel.FieldDefinition) BitSet(java.util.BitSet) ClassDefinition(org.drools.core.factmodel.ClassDefinition)

Example 18 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class TraitRegistry method mergeHierarchy.

private static HierarchyEncoder<String> mergeHierarchy(TraitRegistry first, TraitRegistry second) {
    for (String traitName : second.getHierarchy().getSortedMembers()) {
        ClassDefinition trait = second.traits.get(traitName);
        List<String> parentTraits = new ArrayList<String>();
        for (String candidateIntf : trait.getInterfaces()) {
            if (first.getHierarchy().getCode(candidateIntf) != null) {
                parentTraits.add(candidateIntf);
            }
        }
        first.getHierarchy().encode(traitName, parentTraits);
    }
    return first.getHierarchy();
}
Also used : ArrayList(java.util.ArrayList) ClassDefinition(org.drools.core.factmodel.ClassDefinition)

Example 19 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class TraitHelper method makeTraitable.

private <K> TraitableBean makeTraitable(K core, TraitFactory builder, boolean logical, Activation activation) {
    boolean needsWrapping = !(core instanceof TraitableBean);
    ClassDefinition coreDef = lookupClassDefinition(core);
    TraitableBean<K, ? extends TraitableBean> inner = needsWrapping ? builder.asTraitable(core, coreDef) : (TraitableBean<K, ? extends TraitableBean>) core;
    if (needsWrapping) {
        InternalFactHandle h = (InternalFactHandle) lookupFactHandle(core);
        WorkingMemoryEntryPoint ep = h != null ? h.getEntryPoint() : ((StatefulKnowledgeSessionImpl) workingMemory).getEntryPoint("DEFAULT");
        ObjectTypeConfigurationRegistry reg = ep.getObjectTypeConfigurationRegistry();
        ObjectTypeConf coreConf = reg.getObjectTypeConf(ep.getEntryPoint(), core);
        ObjectTypeConf innerConf = reg.getObjectTypeConf(ep.getEntryPoint(), inner);
        if (coreConf.isTMSEnabled()) {
            innerConf.enableTMS();
        }
        if (inner._getFieldTMS() != null && inner._getFieldTMS().needsInit()) {
            inner._getFieldTMS().init(workingMemory);
        }
    } else {
        TraitFieldTMS ftms = inner._getFieldTMS();
        if (ftms != null) {
            FactHandle handle = lookupFactHandle(inner);
            if (handle == null) {
                handle = this.workingMemory.insert(inner, false, activation.getRule(), activation.getTuple().getTupleSink());
            }
            if (ftms.needsInit()) {
                ftms.init(workingMemory);
            }
        }
    }
    return inner;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) ClassDefinition(org.drools.core.factmodel.ClassDefinition) InternalFactHandle(org.drools.core.common.InternalFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) ObjectTypeConfigurationRegistry(org.drools.core.common.ObjectTypeConfigurationRegistry)

Example 20 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class KieModuleMavenTest method testKieModulePojoDependencies.

@Test
public void testKieModulePojoDependencies() throws Exception {
    KieServices ks = KieServices.Factory.get();
    // Create and deploy a standard mavenized pojo jar
    String pojoNS = "org.kie.pojos";
    ReleaseId pojoID = KieServices.Factory.get().newReleaseId(pojoNS, "pojojar", "2.0.0");
    String className = "Message";
    ClassDefinition def = new ClassDefinition(pojoNS + "." + className);
    def.addField(new FieldDefinition("text", String.class.getName()));
    byte[] messageClazz = ClassBuilderFactory.getDefaultBeanClassBuilder().buildClass(def, null);
    MemoryFileSystem mfs = new MemoryFileSystem();
    mfs.write(pojoNS.replace('.', '/') + "/" + className + ".class", messageClazz);
    byte[] pomContent = generatePomXml(pojoID).getBytes();
    mfs.write("META-INF/maven/" + pojoID.getGroupId() + "/" + pojoID.getArtifactId() + "/pom.xml", pomContent);
    mfs.write("META-INF/maven/" + pojoID.getGroupId() + "/" + pojoID.getArtifactId() + "/pom.properties", generatePomProperties(pojoID).getBytes());
    byte[] pojojar = mfs.writeAsBytes();
    MavenRepository.getMavenRepository().installArtifact(pojoID, pojojar, pomContent);
    // Create and deploy a kjar that depends on the previous pojo jar
    String kjarNS = "org.kie.test1";
    ReleaseId kjarID = KieServices.Factory.get().newReleaseId(kjarNS, "rkjar", "1.0.0");
    String rule = getRule(kjarNS, pojoNS, "R1");
    String pom = generatePomXml(kjarID, pojoID);
    byte[] rkjar = createKJar(ks, kjarID, pom, rule);
    KieModule kmodule = deployJar(ks, rkjar);
    assertNotNull(kmodule);
    KieContainer kContainer = ks.newKieContainer(kjarID);
    KieSession kSession = kContainer.newKieSession();
    List<?> list = new ArrayList<Object>();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ClassDefinition(org.drools.core.factmodel.ClassDefinition) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

ClassDefinition (org.drools.core.factmodel.ClassDefinition)20 TypeDeclaration (org.drools.core.rule.TypeDeclaration)9 FieldDefinition (org.drools.core.factmodel.FieldDefinition)6 ArrayList (java.util.ArrayList)4 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)4 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)3 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)3 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)3 IOException (java.io.IOException)2 BitSet (java.util.BitSet)2 ClassFieldAccessor (org.drools.core.base.ClassFieldAccessor)2 ClassFieldAccessorStore (org.drools.core.base.ClassFieldAccessorStore)2 EnumClassDefinition (org.drools.core.factmodel.EnumClassDefinition)2 Traitable (org.drools.core.factmodel.traits.Traitable)2 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)2 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)2 FactField (org.kie.api.definition.type.FactField)2 ByteString (com.google.protobuf.ByteString)1 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1