⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workingmemoryimpl.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    {
        fireAllRules( null );
    }

    /**
     * @see WorkingMemory
     */
    public Object getObject(FactHandle handle) throws NoSuchFactObjectException
    {
        Object object = this.objects.get( ((FactHandleImpl) handle).getId( ) );

        if ( object == null )
        {
            throw new NoSuchFactObjectException( handle );
        }

        return object;
    }

    /**
     * @see WorkingMemory
     */
    public FactHandle getFactHandle(Object object) throws NoSuchFactHandleException
    {
        FactHandle factHandle = (FactHandle) this.identityMap.get( object );

        if ( factHandle == null )
        {
            throw new NoSuchFactHandleException( object );
        }

        return factHandle;
    }

    public List getFactHandles()
    {
        return new ArrayList( this.identityMap.values( ) );
    }

    /**
     * @see WorkingMemory
     */
    public List getObjects()
    {
        return new ArrayList( this.objects.values( ) );
    }

    public List getObjects(Class objectClass)
    {
        List matching = new LinkedList( );
        Iterator objIter = this.objects.values( ).iterator( );
        Object obj;
        while ( objIter.hasNext( ) )
        {
            obj = objIter.next( );

            if ( objectClass.isInstance( obj ) )
            {
                matching.add( obj );
            }
        }

        return matching;
    }

    /**
     * @see WorkingMemory
     */
    public boolean containsObject(FactHandle handle)
    {
        return this.objects.containsKey( ((FactHandleImpl) handle).getId( ) );
    }

    /**
     * @see WorkingMemory
     */
    public FactHandle assertObject(Object object) throws FactException
    {
        return assertObject( object, /* Not-Dynamic */
                             false,
                             false,
                             null,
                             null );
    }
    
    /**
     * @see WorkingMemory
     */
    public FactHandle assertLogicalObject(Object object) throws FactException
    {
        return assertObject( object, /* Not-Dynamic */
                             false,
                             true,
                             null,
                             null);
    }    
    
    public FactHandle assertObject(Object object,
                                   boolean dynamic) throws FactException
    {
        return assertObject( object,
                             dynamic,
                             false,
                             null,
                             null ); 
    }    
    
    public FactHandle assertLogicalObject(Object object,
                                          boolean dynamic) throws FactException
    {
        return assertObject( object,
                             dynamic,
                             true,
                             null,
                             null ); 
    }

    FactHandle assertObject(Object object,
                            boolean dynamic,
                            boolean logical,
                            Rule rule,
                            Activation activation) throws FactException
    {
        /* check if the object already exists in the WM */
        FactHandle handle = (FactHandle) this.identityMap.get( object );        

        /* only return if the handle exists and this is a logical assertion */
        if ( ( handle != null ) && ( logical ) )
        {
            return handle;
        }
        
        /* lets see if the object is already logical asserted */
        Object logicalState = this.equalsMap.get( object );
        
        /* if we have a handle and this STATED fact was previously STATED */
        if ( ( handle != null ) && ( !logical ) && logicalState == STATED )
        {
            return handle;
        }
        
        if ( !logical )
        {           
            /* If this stated assertion already has justifications
             * then we need to cancel them
             */           
            if (logicalState instanceof FactHandleImpl )
            {
                handle = (FactHandleImpl) logicalState;
                /* remove handle from the justified Map and then iterate each of each Activations.
                 * For each Activation remove the handle. If the Set is empty then remove the activation
                 * from justiers. 
                 */
                Set activationList = (Set) this.justified.remove( ( (FactHandleImpl) handle ).getId() );
                Iterator it = activationList.iterator();
                Activation eachActivation;
                while ( it.hasNext() )
                {
                    eachActivation = (Activation) it.next();
                    Set handles = (Set) this.justifiers.get( eachActivation );
                    handles.remove( handle );
                    // if an activation has no justified assertions then remove it
                    if ( handles.isEmpty() )
                    {
                        this.justifiers.remove( eachActivation );
                    }
                }                
            } 
            else 
            {
                handle = newFactHandle( );
            }

            putObject( handle,
                       object );
            
            equalsMap.put( object,
                           STATED );             

            if ( dynamic )
            {
                addPropertyChangeListener( object );
            }            
        }
        else
        {
            /* This object is already STATED, we cannot make it justifieable */
            if ( logicalState == STATED )
            {
                return null;
            }
            
            handle = (FactHandleImpl) logicalState;
            /* we create a lookup handle for the first asserted equals object
             * all future equals objects will use that handle
             */
            if ( handle == null )
            {
                handle = (FactHandleImpl) newFactHandle( );

                putObject( handle,
                           object );     
                
                this.equalsMap.put( object,
                               handle );                    
            }
            Set activationList = (Set) this.justified.get( ( (FactHandleImpl) handle ).getId() );
            if ( activationList == null )
            {
                activationList = new HashSet();
                this.justified.put( ( (FactHandleImpl) handle ).getId(),
                                    activationList );           
            } 
            activationList.add( activation );
            
            Set handles = (Set) this.justifiers.get( activation );
            if ( handles == null )
            {
                handles = new HashSet();
                this.justifiers.put( activation,
                                     handles );
            }
            handles.add( handle );           
        }                        

        ruleBase.assertObject( handle,
                               object,
                               new PropagationContextImpl( PropagationContext.ASSERTION,
                                                       rule,
                                                       activation ),
                               this );

        this.workingMemoryEventSupport.fireObjectAsserted( handle,
                                                           object );
        return handle;
    }

    private void addPropertyChangeListener(Object object)
    {
        try
        {
            Method method = object.getClass( ).getMethod( "addPropertyChangeListener",
                                                          ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES );

            method.invoke( object,
                           addRemovePropertyChangeListenerArgs );
        }
        catch ( NoSuchMethodException e )
        {
            System.err.println( "Warning: Method addPropertyChangeListener not found" + " on the class " + object.getClass( ) + " so Drools will be unable to process JavaBean" + " PropertyChangeEvents on the asserted Object" );
        }
        catch ( IllegalArgumentException e )
        {
            System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass( ) + " does not take" + " a simple PropertyChangeListener argument" + " so Drools will be unable to process JavaBean"
                                + " PropertyChangeEvents on the asserted Object" );
        }
        catch ( IllegalAccessException e )
        {
            System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass( ) + " is not public" + " so Drools will be unable to process JavaBean" + " PropertyChangeEvents on the asserted Object" );
        }
        catch ( InvocationTargetException e )
        {
            System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass( ) + " threw an InvocationTargetException" + " so Drools will be unable to process JavaBean"
                                + " PropertyChangeEvents on the asserted Object: " + e.getMessage( ) );
        }
        catch ( SecurityException e )
        {
            System.err.println( "Warning: The SecurityManager controlling the class " + object.getClass( ) + " did not allow the lookup of a" + " addPropertyChangeListener method" + " so Drools will be unable to process JavaBean"
                                + " PropertyChangeEvents on the asserted Object: " + e.getMessage( ) );
        }
    }

    private void removePropertyChangeListener(FactHandle handle) throws NoSuchFactObjectException
    {
        Object object = null;
        try
        {
            object = getObject( handle );

            Method mehod = handle.getClass( ).getMethod( "removePropertyChangeListener",
                                                         ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES );

            mehod.invoke( handle,
                          addRemovePropertyChangeListenerArgs );
        }
        catch ( NoSuchMethodException e )
        {
            // The removePropertyChangeListener method on the class
            // was not found so Drools will be unable to
            // stop processing JavaBean PropertyChangeEvents
            // on the retracted Object
        }
        catch ( IllegalArgumentException e )
        {
            System.err.println( "Warning: The removePropertyChangeListener method" + " on the class " + object.getClass( ) + " does not take" + " a simple PropertyChangeListener argument" + " so Drools will be unable to stop processing JavaBean"
                                + " PropertyChangeEvents on the retracted Object" );
        }
        catch ( IllegalAccessException e )
        {
            System.err.println( "Warning: The removePropertyChangeListener method" + " on the class " + object.getClass( ) + " is not public" + " so Drools will be unable to stop processing JavaBean" + " PropertyChangeEvents on the retracted Object" );
        }
        catch ( InvocationTargetException e )
        {
            System.err.println( "Warning: The removePropertyChangeL istener method" + " on the class " + object.getClass( ) + " threw an InvocationTargetException" + " so Drools will be unable to stop processing JavaBean"
                                + " PropertyChangeEvents on the retracted Object: " + e.getMessage( ) );
        }
        catch ( SecurityException e )
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -