All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.core.marshalling.impl.MarshallerReaderContext Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.core.marshalling.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.util.HashMap;
import java.util.Map;

import org.drools.core.common.BaseNode;
import org.drools.core.common.InternalFactHandle;
import org.drools.core.common.InternalWorkingMemory;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.marshalling.impl.ProtobufInputMarshaller.PBActivationsFilter;
import org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey;
import org.drools.core.phreak.PhreakTimerNode;
import org.drools.core.phreak.PhreakTimerNode.Scheduler;
import org.drools.core.reteoo.LeftTuple;
import org.drools.core.reteoo.RightTuple;
import org.drools.core.rule.EntryPointId;
import org.drools.core.spi.PropagationContext;
import org.kie.api.marshalling.ObjectMarshallingStrategy;
import org.kie.api.marshalling.ObjectMarshallingStrategyStore;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.EnvironmentName;
import org.kie.api.runtime.KieRuntime;
import org.kie.internal.marshalling.MarshallerFactory;

public class MarshallerReaderContext extends ObjectInputStream {
    public final MarshallerReaderContext                                           stream;
    public final InternalKnowledgeBase                                             kBase;
    public InternalWorkingMemory                                                   wm;
    public KieRuntime                                                              kruntime;
    public final Map                                            sinks;

    public Map                                        handles;

    public final Map                                    rightTuples;
    public final Map                                           terminalTupleMap;
    public final PBActivationsFilter                                               filter;

    public final ObjectMarshallingStrategyStore                                    resolverStrategyFactory;
    public final Map                           usedStrategies;
    public final Map strategyContexts;

    public final Map                                           entryPoints;

    public final Map                               readersByInt;

    public final Map                                     propagationContexts;

    public final boolean                                                           marshalProcessInstances;
    public final boolean                                                           marshalWorkItems;
    public final Environment                                                       env;

    // this is a map to store node memory data indexed by node ID
    public final Map                                              nodeMemories;

    public Object                                                                  parameterObject;
    public ClassLoader                                                             classLoader;
    public Map>                                  timerNodeSchedulers;

    public MarshallerReaderContext(InputStream stream,
                                   InternalKnowledgeBase kBase,
                                   Map sinks,
                                   ObjectMarshallingStrategyStore resolverStrategyFactory,
                                   Map timerReaders,
                                   Environment env) throws IOException {
        this( stream,
              kBase,
              sinks,
              resolverStrategyFactory,
              timerReaders,
              true,
              true,
              env );
    }

    public MarshallerReaderContext(InputStream stream,
                                   InternalKnowledgeBase kBase,
                                   Map sinks,
                                   ObjectMarshallingStrategyStore resolverStrategyFactory,
                                   Map timerReaders,
                                   boolean marshalProcessInstances,
                                   boolean marshalWorkItems,
                                   Environment env) throws IOException {
        super( stream );
        this.stream = this;
        this.kBase = kBase;
        this.sinks = sinks;

        this.readersByInt = timerReaders;

        this.handles = new HashMap();
        this.rightTuples = new HashMap();
        this.terminalTupleMap = new HashMap();
        this.filter = new PBActivationsFilter();
        this.entryPoints = new HashMap();
        this.propagationContexts = new HashMap();
        if ( resolverStrategyFactory == null ) {
            ObjectMarshallingStrategy[] strats = (ObjectMarshallingStrategy[]) env.get( EnvironmentName.OBJECT_MARSHALLING_STRATEGIES );
            if ( strats == null ) {
                strats = new ObjectMarshallingStrategy[]{MarshallerFactory.newSerializeMarshallingStrategy()};
            }
            this.resolverStrategyFactory = new ObjectMarshallingStrategyStoreImpl( strats );
        }
        else {
            this.resolverStrategyFactory = resolverStrategyFactory;
        }
        this.usedStrategies = new HashMap();
        this.strategyContexts = new HashMap();

        this.marshalProcessInstances = marshalProcessInstances;
        this.marshalWorkItems = marshalWorkItems;
        this.env = env;

        this.nodeMemories = new HashMap();
        this.timerNodeSchedulers = new HashMap>();

        this.parameterObject = null;
    }

    @Override
    protected Class< ? > resolveClass(ObjectStreamClass desc) throws IOException,
                                                             ClassNotFoundException {
        String name = desc.getName();
        try {
            if ( this.classLoader == null ) {
                if ( this.kBase != null ) {
                    this.classLoader = this.kBase.getRootClassLoader();
                }
            }
            return Class.forName( name, false, this.classLoader );
        } catch ( ClassNotFoundException ex ) {
            return super.resolveClass( desc );
        }
    }
    
    public void addTimerNodeScheduler( int nodeId, TupleKey key, Scheduler scheduler ) {
        Map timers = timerNodeSchedulers.get( nodeId );
        if( timers == null ) {
            timers = new HashMap();
            timerNodeSchedulers.put( nodeId, timers );
        }
        timers.put( key, scheduler );
    }
    
    public Scheduler removeTimerNodeScheduler( int nodeId, TupleKey key ) {
        Map timers = timerNodeSchedulers.get( nodeId );
        if( timers != null ) {
            Scheduler scheduler = timers.remove( key );
            if( timers.isEmpty() ) {
                timerNodeSchedulers.remove( nodeId );
            }
            return scheduler;
        } 
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy