Search in sources :

Example 1 with ClassDefinition

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

the class KnowledgeBaseImpl method processTypeDeclaration.

protected void processTypeDeclaration(TypeDeclaration newDecl, InternalKnowledgePackage newPkg) throws ClassNotFoundException {
    JavaDialectRuntimeData runtime = ((JavaDialectRuntimeData) newPkg.getDialectRuntimeRegistry().getDialectData("java"));
    TypeDeclaration typeDeclaration = this.classTypeDeclaration.get(newDecl.getTypeClassName());
    if (typeDeclaration == null) {
        String className = newDecl.getTypeClassName();
        byte[] def = runtime != null ? runtime.getClassDefinition(convertClassToResourcePath(className)) : null;
        Class<?> definedKlass = registerAndLoadTypeDefinition(className, def);
        if (definedKlass == null && newDecl.isNovel()) {
            throw new RuntimeException("Registering null bytes for class " + className);
        }
        if (newDecl.getTypeClassDef() == null) {
            newDecl.setTypeClassDef(new ClassDefinition());
        }
        newDecl.setTypeClass(definedKlass);
        this.classTypeDeclaration.put(className, newDecl);
        typeDeclaration = newDecl;
    } else {
        Class<?> definedKlass = typeDeclaration.getTypeClass();
        newDecl.getTypeClassDef().setDefinedClass(definedKlass);
        newDecl.setTypeClass(definedKlass);
        mergeTypeDeclarations(typeDeclaration, newDecl);
    }
    // update existing OTNs
    updateDependentTypes(typeDeclaration);
}
Also used : JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 2 with ClassDefinition

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

the class AbstractTraitFactory method buildProxyClass.

protected Class<T> buildProxyClass(String key, K core, Class<?> trait) {
    Class coreKlass = core.getClass();
    // get the trait classDef
    ClassDefinition tdef = getTraitRegistry().getTrait(trait.getName());
    ClassDefinition cdef = getTraitRegistry().getTraitable(coreKlass.getName());
    if (tdef == null) {
        if (trait.getAnnotation(Trait.class) != null) {
            try {
                if (Thing.class.isAssignableFrom(trait)) {
                    tdef = buildClassDefinition(trait, null);
                } else {
                    throw new RuntimeException("Unable to create definition for class " + trait + " : trait interfaces should extend " + Thing.class.getName() + " or be DECLARED as traits explicitly");
                }
            } catch (IOException e) {
                throw new RuntimeException("Unable to create definition for class " + trait + " : " + e.getMessage());
            }
            getTraitRegistry().addTrait(tdef);
        } else {
            throw new RuntimeException("Unable to find Trait definition for class " + trait.getName() + ". It should have been DECLARED as a trait");
        }
    }
    if (cdef == null) {
        if (core.getClass().getAnnotation(Traitable.class) != null) {
            try {
                cdef = buildClassDefinition(core.getClass(), core.getClass());
            } catch (IOException e) {
                throw new RuntimeException("Unable to create definition for class " + coreKlass.getName() + " : " + e.getMessage());
            }
            getTraitRegistry().addTraitable(cdef);
        } else {
            throw new RuntimeException("Unable to find Core class definition for class " + coreKlass.getName() + ". It should have been DECLARED as a trait");
        }
    }
    String proxyName = getProxyName(tdef, cdef);
    String wrapperName = getPropertyWrapperName(tdef, cdef);
    KieComponentFactory rcf = getComponentFactory();
    TraitPropertyWrapperClassBuilder propWrapperBuilder = (TraitPropertyWrapperClassBuilder) rcf.getClassBuilderFactory().getPropertyWrapperBuilder();
    propWrapperBuilder.init(tdef, getTraitRegistry());
    try {
        byte[] propWrapper = propWrapperBuilder.buildClass(cdef, getRootClassLoader());
        registerAndLoadTypeDefinition(wrapperName, propWrapper);
    } catch (Exception e) {
        e.printStackTrace();
    }
    TraitProxyClassBuilder proxyBuilder = (TraitProxyClassBuilder) rcf.getClassBuilderFactory().getTraitProxyBuilder();
    proxyBuilder.init(tdef, rcf.getBaseTraitProxyClass(), getTraitRegistry());
    try {
        byte[] proxy = proxyBuilder.buildClass(cdef, getRootClassLoader());
        registerAndLoadTypeDefinition(proxyName, proxy);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        BitSet mask = getTraitRegistry().getFieldMask(trait.getName(), cdef.getDefinedClass().getName());
        Class<T> wrapperClass = (Class<T>) getRootClassLoader().loadClass(wrapperName);
        Class<T> proxyClass = (Class<T>) getRootClassLoader().loadClass(proxyName);
        return proxyClass;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : BitSet(java.util.BitSet) IOException(java.io.IOException) ClassDefinition(org.drools.core.factmodel.ClassDefinition) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) KieComponentFactory(org.drools.core.reteoo.KieComponentFactory)

Example 3 with ClassDefinition

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

the class TraitRegistry method init.

private void init() {
    TypeDeclaration thingType = new TypeDeclaration(Thing.class.getName());
    thingType.setKind(TypeDeclaration.Kind.TRAIT);
    thingType.setTypeClass(Thing.class);
    ClassDefinition def = new ClassDefinition();
    def.setClassName(thingType.getTypeClass().getName());
    def.setDefinedClass(Thing.class);
    addTrait(def);
    ClassDefinition individualDef = new ClassDefinition();
    individualDef.setClassName(Entity.class.getName());
    individualDef.setDefinedClass(Entity.class);
    individualDef.setInterfaces(new String[] { Serializable.class.getName(), TraitableBean.class.getName() });
    individualDef.setTraitable(true);
    addTraitable(individualDef);
    ClassDefinition mapcoreDef = new ClassDefinition();
    mapcoreDef.setClassName(MapCore.class.getName());
    mapcoreDef.setDefinedClass(MapCore.class);
    mapcoreDef.setInterfaces(new String[] { Serializable.class.getName(), TraitableBean.class.getName(), CoreWrapper.class.getName() });
    mapcoreDef.setTraitable(true);
    addTraitable(mapcoreDef);
    ClassDefinition logicalMapcoreDef = new ClassDefinition();
    logicalMapcoreDef.setClassName(LogicalMapCore.class.getName());
    logicalMapcoreDef.setDefinedClass(LogicalMapCore.class);
    logicalMapcoreDef.setInterfaces(new String[] { Serializable.class.getName(), TraitableBean.class.getName(), CoreWrapper.class.getName() });
    logicalMapcoreDef.setTraitable(true, true);
    addTraitable(logicalMapcoreDef);
}
Also used : Serializable(java.io.Serializable) MapCore(org.drools.core.factmodel.MapCore) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 4 with ClassDefinition

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

the class ClassDefinitionFactory method generateDeclaredBean.

/**
 * Generates a bean, and adds it to the composite class loader that
 * everything is using.
 */
public ClassDefinition generateDeclaredBean(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry, List<TypeDefinition> unresolvedTypeDefinitions, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    ClassDefinition def = createClassDefinition(typeDescr, type);
    boolean success = true;
    success &= wireAnnotationDefs(typeDescr, type, def, pkgRegistry.getTypeResolver());
    success &= wireEnumLiteralDefs(typeDescr, type, def);
    success &= wireFields(typeDescr, type, def, pkgRegistry, unresolvedTypeDefinitions);
    if (!success) {
        unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
    }
    // attach the class definition, it will be completed later
    type.setTypeClassDef(def);
    return def;
}
Also used : ClassDefinition(org.drools.core.factmodel.ClassDefinition) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition)

Example 5 with ClassDefinition

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

the class ClassDefinitionFactory method createClassDefinition.

protected ClassDefinition createClassDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    // extracts type, supertype and interfaces
    String fullName = typeDescr.getType().getFullName();
    if (type.getKind().equals(TypeDeclaration.Kind.CLASS)) {
        TypeDeclarationDescr tdescr = (TypeDeclarationDescr) typeDescr;
        if (tdescr.getSuperTypes().size() > 1) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Declared class " + fullName + "  - has more than one supertype;"));
            return null;
        } else if (tdescr.getSuperTypes().isEmpty()) {
            tdescr.addSuperType("java.lang.Object");
        }
    }
    Traitable traitableAnn = typeDescr.getTypedAnnotation(Traitable.class);
    boolean traitable = traitableAnn != null;
    String[] fullSuperTypes = new String[typeDescr.getSuperTypes().size() + 1];
    int j = 0;
    for (QualifiedName qname : typeDescr.getSuperTypes()) {
        fullSuperTypes[j++] = qname.getFullName();
    }
    fullSuperTypes[j] = Thing.class.getName();
    List<String> interfaceList = new ArrayList<String>();
    interfaceList.add(traitable ? Externalizable.class.getName() : Serializable.class.getName());
    if (traitable) {
        interfaceList.add(TraitableBean.class.getName());
    }
    String[] interfaces = interfaceList.toArray(new String[interfaceList.size()]);
    // prepares a class definition
    ClassDefinition def;
    switch(type.getKind()) {
        case TRAIT:
            def = new ClassDefinition(fullName, Object.class.getName(), fullSuperTypes);
            break;
        case ENUM:
            def = new EnumClassDefinition(fullName, fullSuperTypes[0], null);
            break;
        case CLASS:
        default:
            def = new ClassDefinition(fullName, fullSuperTypes[0], interfaces);
            def.setTraitable(traitable, traitableAnn != null && traitableAnn.logical());
    }
    return def;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) Traitable(org.drools.core.factmodel.traits.Traitable) Thing(org.drools.core.factmodel.traits.Thing)

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