Package org.drools.core.factmodel

Examples of org.drools.core.factmodel.ClassDefinition


    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 );

                    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 ( Method m : properties.values() ) {
                if ( m != null && m.getDeclaringClass() != TraitType.class && m.getDeclaringClass() != Thing.class ) {
                    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 );

                    def.addField( fld );
                }
            }
        }

        return def;
View Full Code Here


            if ( definedKlass == null && typeDeclaration.isNovel() ) {
                throw new RuntimeException( "Registering null bytes for class " + className );
            }

            if (newDecl.getTypeClassDef() == null) {
                newDecl.setTypeClassDef( new ClassDefinition() );
            }
            newDecl.getTypeClassDef().setDefinedClass( definedKlass );
            newDecl.setTypeClass( definedKlass );

            this.classTypeDeclaration.put( className, newDecl );
View Full Code Here

        if ( hierarchy == null || hierarchy.size() <= 1 ) {
            hierarchy = other.hierarchy;
        } else {
            if ( other.traits != null ) {
                for ( String traitName : other.getHierarchy().getSortedMembers() ) {
                    ClassDefinition trait = other.traits.get( traitName );
                    List<String> parentTraits = new ArrayList<String>( );
                    for ( String candidateIntf : trait.getInterfaces() ) {
                        if ( getHierarchy().getCode( candidateIntf ) != null ) {
                            parentTraits.add( candidateIntf );
                        }
                    }
                    getHierarchy().encode( trait.getName(), parentTraits );
                }
            }
        }
    }
View Full Code Here

    protected ClassDefinition getTrait( String key ) {
        if ( key.endsWithTraitFactory.SUFFIX ) ) {
            key = key.replaceTraitFactory.SUFFIX , "" );
        }
        ClassDefinition traitDef = traits != null ? traits.get( key ) : null;
        if ( traitDef == null ) {

        }
        return traitDef;
    }
View Full Code Here

        return mask;
    }

    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 )" );
                }
View Full Code Here

  public KieRuntime getKieRuntime() {
        return getKnowledgeRuntime();
    }

    protected <K> TraitableBean<K,CoreWrapper<K>> asTraitable( K core, TraitFactory builder ) {
        ClassDefinition coreDef = lookupClassDefinition( core );

        if ( core instanceof Map ) {
            if ( ! coreDef.isTraitable() ) {
                throw new UnsupportedOperationException( "Error: cannot apply a trait to non-traitable class " + core.getClass() + ". Was it declared as @Traitable? ");
            }
            return coreDef.isFullTraiting() ? new LogicalMapCore( (Map) core ) : new MapCore( (Map) core );
        }

        CoreWrapper<K> wrapper = builder.getCoreWrapper( core.getClass(), coreDef );
        if ( wrapper == null ) {
            throw new UnsupportedOperationException( "Error: cannot apply a trait to non-traitable class " + core.getClass() + ". Was it declared as @Traitable? ");
View Full Code Here

                                if ( definedKlass == null && typeDeclaration.isNovel() ) {
                                    throw new RuntimeException( "Registering null bytes for class " + className );
                                }

                                if (newDecl.getTypeClassDef() == null) {
                                    newDecl.setTypeClassDef( new ClassDefinition() );
                                }
                                newDecl.getTypeClassDef().setDefinedClass( definedKlass );
                                newDecl.setTypeClass( definedKlass );

                                this.classTypeDeclaration.put( className, newDecl );
View Full Code Here

        Class coreKlass = core.getClass();


        // get the trait classDef
        ClassDefinition tdef = kBase.getTraitRegistry().getTrait( trait.getName() );
        ClassDefinition cdef = kBase.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() );
                }
                kBase.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() );
                }
                kBase.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 = kBase.getConfiguration().getComponentFactory();


        TraitPropertyWrapperClassBuilder propWrapperBuilder = (TraitPropertyWrapperClassBuilder) rcf.getClassBuilderFactory().getPropertyWrapperBuilder();

        propWrapperBuilder.init( tdef, kBase.getTraitRegistry() );
        try {
            byte[] propWrapper = propWrapperBuilder.buildClass( cdef, kBase.getRootClassLoader() );
            kBase.registerAndLoadTypeDefinition( wrapperName, propWrapper );
        } catch (Exception e) {
            e.printStackTrace();
        }


        TraitProxyClassBuilder proxyBuilder = (TraitProxyClassBuilder) rcf.getClassBuilderFactory().getTraitProxyBuilder();

        proxyBuilder.init( tdef, rcf.getBaseTraitProxyClass(), kBase.getTraitRegistry() );
        try {
            byte[] proxy = proxyBuilder.buildClass( cdef, kBase.getRootClassLoader() );
            kBase.registerAndLoadTypeDefinition( proxyName, proxy );
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            BitSet mask = kBase.getTraitRegistry().getFieldMask( trait.getName(), cdef.getDefinedClass().getName() );
            Class<T> wrapperClass = (Class<T>) kBase.getRootClassLoader().loadClass( wrapperName );
            Class<T> proxyClass = (Class<T>) kBase.getRootClassLoader().loadClass( proxyName );
            return proxyClass;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
View Full Code Here

            traitPackage.setClassFieldAccessorCache( kBase.getClassFieldAccessorCache() );
            kBase.getPackagesMap().put( pack, traitPackage );
        }
        ClassFieldAccessorStore store = traitPackage.getClassFieldAccessorStore();

        ClassDefinition def;
        if ( ! klazz.isInterface() ) {
            String className = wrapperClass.getName();
            String superClass = wrapperClass != klazz ? klazz.getName() : klazz.getSuperclass().getName();
            String[] interfaces = new String[] {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() );
        } 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, 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 );

                def.addField( fld );
            }
        }


        return def;
View Full Code Here

    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 );
    }
View Full Code Here

TOP

Related Classes of org.drools.core.factmodel.ClassDefinition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.