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

com.bstek.urule.runtime.rete.AndActivity Maven / Gradle / Ivy

There is a newer version: 2.1.7
Show newest version
/*******************************************************************************
 * Copyright 2017 Bstek
 * 
 * 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 com.bstek.urule.runtime.rete;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.bstek.urule.model.rule.lhs.BaseCriteria;

/**
 * @author Jacky.gao
 * @since 2015年1月8日
 */
public class AndActivity extends JoinActivity {
	private List fromPaths=new ArrayList();
	private boolean pass;
	private FactTracker currentTracker;
	private Map>> toActivityMap=new HashMap>>();

	public Collection enter(EvaluationContext context, Object obj,FactTracker tracker,Map variableMap) {
		Activity prevActivity=context.getPrevActivity();
		if(prevActivity!=null){
			if(toActivityMap.containsKey(prevActivity)){
				List> variableList=toActivityMap.get(prevActivity);
				if(pass){
					variableList.clear();
				}
				variableList.add(variableMap);
			}else{
				List> variableList=new ArrayList>();
				variableList.add(variableMap);
				toActivityMap.put(prevActivity, variableList);
			}
		}
		if(currentTracker!=null){
			Map> map=tracker.getObjectCriteriaMap();
			Map> currentMap=currentTracker.getObjectCriteriaMap();
			for(Object key:currentMap.keySet()){
				if(map.containsKey(key)){
					continue;
				}
				map.put(key, currentMap.get(key));
			}
		}
		currentTracker=tracker;
		if(isAllPassed()){
			context.setPrevActivity(this);
			pass=true;
			List allTrackers=new ArrayList();
			List> list=buildPathVariableMaps();
			for(Map varMap:list){
				List trackers=visitPahs(context,obj,tracker,varMap);
				if(trackers!=null && trackers.size()>0){
					allTrackers.addAll(trackers);
				}
			}
			return allTrackers;
		}
		return null;
	}
	
	private boolean isAllPassed(){
		boolean passed=true;
		for(Path path:fromPaths){
			if(!path.isPassed()){
				passed=false;
				break;
			}
		}
		return passed;
	}
	
	public void addFromPath(Path fromPath){
		fromPaths.add(fromPath);
	}
	
	private List> buildPathVariableMaps(){
		List> result=new ArrayList>();
		Iterator>> iter=toActivityMap.values().iterator();
		while(iter.hasNext()){
			List> list1=iter.next();
			result=buildVariableMaps(list1,result);
		}
		return result;
	}
	
	private static List> buildVariableMaps(List> list1,List> list2){
		List> resultList=new ArrayList>();
		for(Map map1:list1){
			if(list2.size()==0){
				resultList.add(map1);
			}else{
				for(Map map2:list2){
					Map newMap=new HashMap();
					newMap.putAll(map1);
					newMap.putAll(map2);
					resultList.add(newMap);
				}
			}
		}
		return resultList;
	}
	
	@Override
	public boolean orNodeIsPassed() {
		return false;
	}
	
	@Override
	public void reset() {
		toActivityMap.clear();
		pass=false;
		currentTracker=null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy