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

📄 abstractworkingmemory.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     */
    public Object getObject(final FactHandle handle) {
        // you must always take the value from the assertMap, incase the handle
        // is not from this WorkingMemory
        InternalFactHandle factHandle = (InternalFactHandle) this.assertMap.get( handle );
        if ( factHandle != null ) {
            return factHandle.getObject();
        }

        return null;

    }

    /**
     * @see WorkingMemory
     */
    public FactHandle getFactHandle(final Object object) {
        final FactHandle factHandle = (FactHandle) this.assertMap.get( object );

        return factHandle;
    }

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

    /**
     * A helper method used to avoid lookups when iterating over facthandles and
     * objects at once. DO NOT MAKE THIS METHOD PUBLIC UNLESS YOU KNOW WHAT YOU
     * ARE DOING
     * 
     * @return
     */
    public Map getFactHandleMap() {
        return Collections.unmodifiableMap( this.assertMap );
    }

    /**
     * @see WorkingMemory
     */
    public List getObjects() {
        final List list = new ArrayList( this.assertMap.size() );

        for ( final Iterator it = this.assertMap.keySet().iterator(); it.hasNext(); ) {
            list.add( ((InternalFactHandle) it.next()).getObject() );
        }
        return list;
    }

    public List getObjects(final Class objectClass) {
        final List list = new ArrayList();

        for ( final Iterator it = this.assertMap.keySet().iterator(); it.hasNext(); ) {
            final Object object = ((InternalFactHandle) it.next()).getObject();

            if ( objectClass.isInstance( object ) ) {
                list.add( object );
            }
        }

        return list;
    }

    public abstract QueryResults getQueryResults(String query);

    public AgendaGroup getFocus() {
        return this.agenda.getFocus();
    }

    public void setFocus(final String focus) {
        this.agenda.setFocus( focus );
    }

    public void setFocus(final AgendaGroup focus) {
        this.agenda.setFocus( focus );
    }

    public TruthMaintenanceSystem getTruthMaintenanceSystem() {
        return this.tms;
    }

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

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

    public FactHandle assertObject(final Object object,
                                   final boolean dynamic) throws FactException {
        return assertObject( object,
                             dynamic,
                             false,
                             null,
                             null );
    }

    public FactHandle assertLogicalObject(final Object object,
                                          final boolean dynamic) throws FactException {
        return assertObject( object,
                             dynamic,
                             true,
                             null,
                             null );
    }

    public FactHandle assertObject(final Object object,
                                   final boolean dynamic,
                                   boolean logical,
                                   final Rule rule,
                                   final Activation activation) throws FactException {
        if ( object == null ) {
            // you cannot assert a null object
            return null;
        }
        InternalFactHandle handle = null;
        this.lock.lock();
        try {
            // check if the object already exists in the WM
            handle = (InternalFactHandle) this.assertMap.get( object );

            EqualityKey key = null;

            if ( handle == null ) {
                // lets see if the object is already logical asserted
                key = this.tms.get( object );
            } else {
                // Object is already asserted, so check and possibly correct its
                // status and then return the handle
                key = handle.getEqualityKey();

                if ( key.getStatus() == EqualityKey.STATED ) {
                    // return null as you cannot justify a stated object.
                    return handle;
                }

                if ( !logical ) {
                    // this object was previously justified, so we have to
                    // override it to stated
                    key.setStatus( EqualityKey.STATED );
                    this.tms.removeLogicalDependencies( handle );
                } else {
                    // this was object is already justified, so just add new
                    // logical dependency
                    this.tms.addLogicalDependency( handle,
                                                   activation,
                                                   activation.getPropagationContext(),
                                                   rule );
                }

                return handle;
            }

            // At this point we know the handle is null
            if ( key == null ) {
                // key is also null, so treat as a totally new stated/logical
                // assert
                handle = this.handleFactory.newFactHandle( object );
                this.assertMap.put( handle,
                                    handle );
                key = new EqualityKey( handle );
                handle.setEqualityKey( key );
                this.tms.put( key );
                if ( !logical ) {
                    key.setStatus( EqualityKey.STATED );
                } else {
                    key.setStatus( EqualityKey.JUSTIFIED );
                    this.tms.addLogicalDependency( handle,
                                                   activation,
                                                   activation.getPropagationContext(),
                                                   rule );
                }
            } else if ( !logical ) {
                if ( key.getStatus() == EqualityKey.JUSTIFIED ) {
                    // Its previous justified, so switch to stated and remove
                    // logical dependencies
                    final InternalFactHandle justifiedHandle = key.getFactHandle();
                    this.tms.removeLogicalDependencies( justifiedHandle );

                    if ( this.discardOnLogicalOverride ) {
                        // override, setting to new instance, and return
                        // existing handle
                        key.setStatus( EqualityKey.STATED );
                        handle = key.getFactHandle();
                        handle.setObject( object );
                        return handle;
                    } else {
                        // override, then instantiate new handle for assertion
                        key.setStatus( EqualityKey.STATED );
                        handle = this.handleFactory.newFactHandle( object );
                        handle.setEqualityKey( key );
                        key.addFactHandle( handle );
                        this.assertMap.put( handle,
                                            handle );
                    }

                } else {
                    handle = this.handleFactory.newFactHandle( object );
                    this.assertMap.put( handle,
                                        handle );
                    key.addFactHandle( handle );
                    handle.setEqualityKey( key );
                }

            } else {
                if ( key.getStatus() == EqualityKey.JUSTIFIED ) {
                    // only add as logical dependency if this wasn't previously
                    // stated
                    this.tms.addLogicalDependency( key.getFactHandle(),
                                                   activation,
                                                   activation.getPropagationContext(),
                                                   rule );
                    return key.getFactHandle();
                } else {
                    // You cannot justify a previously stated equality equal
                    // object, so return null
                    return null;
                }
            }

            if ( dynamic ) {
                addPropertyChangeListener( object );
            }

            final PropagationContext propagationContext = new PropagationContextImpl( this.propagationIdCounter++,
                                                                                      PropagationContext.ASSERTION,
                                                                                      rule,
                                                                                      activation );

            // this.ruleBase.assertObject( handle,
            // object,
            // propagationContext,
            // this );

            doAssertObject( handle,
                            object,
                            propagationContext );

            this.workingMemoryEventSupport.fireObjectAsserted( propagationContext,
                                                               handle,
                                                               object );

            if ( !this.factQueue.isEmpty() ) {
                propagateQueuedActions();
            }
        } finally {
            this.lock.unlock();
        }
        return handle;
    }

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

            method.invoke( object,
                           this.addRemovePropertyChangeListenerArgs );
        } catch ( final 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 ( final 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 ( final 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 ( final 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 ( final 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() );
        }

⌨️ 快捷键说明

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