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

📄 workingmemoryimpl.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            System.err.println( "Warning: The SecurityManager controlling the class " + object.getClass( ) + " did not allow the lookup of a" + " removePropertyChangeListener method" + " so Drools will be unable to stop processing JavaBean"
                                + " PropertyChangeEvents on the retracted Object: " + e.getMessage( ) );
        }
    }

    /**
     * Associate an object with its handle.
     * 
     * @param handle
     *            The handle.
     * @param object
     *            The object.
     */
    Object putObject(FactHandle handle,
                     Object object)
    {
        Object oldValue = this.objects.put( ((FactHandleImpl) handle).getId( ),
                                            object );

        this.identityMap.put( object,
                              handle );

        return oldValue;
    }

    Object removeObject(FactHandle handle)
    {
        Object object = this.objects.remove( ((FactHandleImpl) handle).getId( ) );

        this.identityMap.remove( object );        

        return object;
    }
    
    public void retractObject(FactHandle handle) throws FactException
    {
        retractObject( handle,
                       true,
                       true,
                       null,
                       null );
    }
                              

    /**
     * @see WorkingMemory
     */
    public void retractObject(FactHandle handle,
                              boolean removeLogical,
                              boolean updateEqualsMap,
                              Rule rule,
                              Activation activation) throws FactException
    {
        removePropertyChangeListener( handle );

        ruleBase.retractObject( handle,
                                new PropagationContextImpl( PropagationContext.RETRACTION,
                                                        rule,
                                                        activation ),                                                         
                                this );

        Object oldObject = removeObject( handle );
        
        /* check to see if this was a logical asserted object */
        if ( removeLogical )
        {        
            FactHandleImpl handleImpl = (FactHandleImpl) handle;            
            Set activations = (Set) this.justified.remove( handleImpl.getId() );
            if ( activations != null )
            {
                Iterator it = activations.iterator();
                while ( it.hasNext() )
                {
                    this.justifiers.remove( it.next() );
                }
            }
            this.equalsMap.remove( oldObject );
        }
        
        if ( updateEqualsMap )
        {
            this.equalsMap.remove( oldObject );
        }
        
        

        factHandlePool.push( ((FactHandleImpl) handle).getId( ) );

        this.workingMemoryEventSupport.fireObjectRetracted( handle,
                                                            oldObject );

        ((FactHandleImpl) handle).invalidate( );
    }

    public void modifyObject(FactHandle handle,
                             Object object) throws FactException
    {
        modifyObject( handle,
                      object,
                      null,
                      null );
    }
    
    /**
     * @see WorkingMemory
     */
    public void modifyObject(FactHandle handle,
                             Object object,
                             Rule rule,
                             Activation activation) throws FactException
    {
        Object originalObject = removeObject( handle );

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

        putObject( handle,
                   object );
        
        /* check to see if this is a logically asserted object */
        FactHandleImpl handleImpl = (FactHandleImpl) handle;
        if ( this.justified.get(handleImpl.getId() ) != null )
        {
            this.equalsMap.remove( originalObject );          
            this.equalsMap.put( object,
                                handle );
        }


        this.ruleBase.retractObject( handle,
                                     new PropagationContextImpl( PropagationContext.MODIFICATION,
                                                             rule,
                                                             activation ),
                                     this );

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


        /*
         * this.ruleBase.modifyObject( handle, object, this );
         */
        this.workingMemoryEventSupport.fireObjectModified( handle,
                                                           originalObject,
                                                           object );
    }

    /**
     * Retrieve the <code>JoinMemory</code> for a particular
     * <code>JoinNode</code>.
     * 
     * @param node
     *            The <code>JoinNode</code> key.
     * 
     * @return The node's memory.
     */
    public Object getNodeMemory(NodeMemory node)
    {
       Object memory = this.nodeMemories.get( node.getId() );

        if ( memory == null )
        {
            memory = node.createMemory();

            this.nodeMemories.put( node.getId(),
                                   memory );
        }

        return memory;
    }

    public WorkingMemoryEventSupport getWorkingMemoryEventSupport()
    {
        return workingMemoryEventSupport;
    }
    
    public AgendaEventSupport getAgendaEventSupport()
    {
        return agendaEventSupport;
    } 
    
    public ReteooNodeEventSupport getReteooNodeEventSupport()
    {
        return reteooNodeEventSupport;
    }    

    /**
     * Sets the AsyncExceptionHandler to handle exceptions thrown by the Agenda
     * Scheduler used for duration rules.
     * 
     * @param handler
     */
    public void setAsyncExceptionHandler(AsyncExceptionHandler handler)
    {
        this.agenda.setAsyncExceptionHandler( handler );
    }

    /*
     * public void dumpMemory() { Iterator it = this.joinMemories.keySet(
     * ).iterator( ); while ( it.hasNext( ) ) { ((JoinMemory)
     * this.joinMemories.get( it.next( ) )).dump( ); } }
     */

    public void propertyChange(PropertyChangeEvent event)
    {
        Object object = event.getSource( );

        try
        {
            modifyObject( getFactHandle( object ),
                          object );
        }
        catch ( NoSuchFactHandleException e )
        {
            // Not a fact so unable to process the chnage event
        }
        catch ( FactException e )
        {
            throw new RuntimeException( e.getMessage( ) );
        }
    }

    /*
    public PrimitiveLongMap getJustified()
    {
        return this.justified;
    }

    public Map getJustifiers()
    {
        return this.justifiers;
    }
    */
    public void removeLogicalAssertions(TupleKey key,
                                        PropagationContext context,
                                        Rule rule) throws FactException
    {
        AgendaItem item = null;
        Iterator it = this.justifiers.keySet().iterator();
        while ( it.hasNext() )
        {
            item = (AgendaItem) it.next( );

            if ( item.getRule( ) == rule && item.getKey( ).containsAll( key ) )
            {     
                removeLogicalAssertions( item,
                                         context,
                                         rule );
            }
        }
        
    }
    
    public void removeLogicalAssertions(Activation activation,
                                        PropagationContext context,
                                        Rule rule) throws FactException
    {
        FactHandleImpl handle = null;
        Set activations = null;
        Set handles = (Set) this.justifiers.remove( activation );
        /* no justified facts for this activation */
        if ( handles == null )
        {
            return;
        }
        Iterator it = handles.iterator();
        while ( it.hasNext() )
        {
            handle = (FactHandleImpl) it.next();
            activations = (Set) this.justified.get( handle.getId() );
            activations.remove( activation );
            if ( activations.isEmpty() )
            {
                this.justified.remove( handle.getId() );
                retractObject( handle,
                               false,
                               true,
                               context.getRuleOrigin(),
                               context.getActivationOrigin() );
            }
            
        }        
    }

    public PrimitiveLongMap getJustified()
    {
        return this.justified;
    }

    public Map getJustifiers()
    {
        return this.justifiers;
    }

    public void dispose()
    {
        this.ruleBase.disposeWorkingMemory( this );        
    }

}

⌨️ 快捷键说明

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