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

org.joinedworkz.common.info.ScenarioContext Maven / Gradle / Ivy

There is a newer version: 1.3.51
Show newest version
package org.joinedworkz.common.info;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

import org.joinedworkz.common.context.CommonGeneratorContext;
import org.joinedworkz.core.facility.Outlet;
import org.joinedworkz.core.model.CmnApplication;
import org.joinedworkz.core.model.CmnComponent;
import org.joinedworkz.core.model.CmnImplementedFeature;
import org.joinedworkz.core.model.CmnProvidedFeature;
import org.joinedworkz.core.model.CmnResource;
import org.joinedworkz.core.model.CmnResourceOperation;
import org.joinedworkz.core.model.invokation.CmnInvokeable;

public class ScenarioContext extends CommonGeneratorContext {

	private final CmnApplication application;


	private AtomicInteger idSequence = new AtomicInteger();
	private AtomicInteger orderNumberSequence = new AtomicInteger();
	private Map objectIds = new HashMap<>();
	private Map> featureObjectIds = new HashMap<>();

	private Map componentMap;
	private Map> invokeableImplementationMap;

	public ScenarioContext(Outlet defaultOutlet, CmnApplication application, Properties properties) {
		super(defaultOutlet, properties);
		this.application = application;
	}

	public int nextId() {
		return idSequence.incrementAndGet();
	}

	public Integer getId(Object obj) {
		return getId(obj, false);
	}
	
	public boolean idExists(Object obj) {
		return objectIds.get(obj) != null;
	}
	
	public Integer getId(Object obj, boolean mustExistAlready) {
		Integer id = objectIds.get(obj);
		if (id == null) {
			if (mustExistAlready) {
				if (obj instanceof CmnResourceOperation resourceOperation) {
					CmnResource resource = (CmnResource)resourceOperation.getContainer();
					throw new IllegalStateException("Id does not exist for " + obj.getClass().getCanonicalName() + ": " + resourceOperation.getOperationType()+" / " + resource.getName());
				} else {
					throw new IllegalStateException("Id does not exist for " + obj.getClass().getCanonicalName());
				}
				
			}
			id = nextId();
			objectIds.put(obj, id);
		}
		return id;
	}
	
	public Integer getId(CmnProvidedFeature feature, Object obj) {
		return getId(feature, obj, false);
	}
	
	public boolean idExists(CmnProvidedFeature feature, Object obj) {
		Map objectIdsForFeature = featureObjectIds.get(feature);
		if (objectIdsForFeature != null) {
			return objectIdsForFeature.get(obj) != null;
		}
		return false;
	}
	
	public Integer getId(CmnProvidedFeature feature, Object obj, boolean mustExistAlready) {
		Map objectIdsForFeature = featureObjectIds.get(feature);
		if (objectIdsForFeature == null) {
			objectIdsForFeature = new HashMap<>();
			featureObjectIds.put(feature, objectIdsForFeature);
		}
		Integer id = objectIdsForFeature.get(obj);
		if (id == null) {
			if (mustExistAlready) {
				throw new IllegalStateException("Id for does not exist");
			}
			id = nextId();
			objectIdsForFeature.put(obj, id);
		}
		return id;
	}

	public Map getComponentMap() {
		return componentMap;
	}
	
	public ComponentInfo getComponentInfo(CmnComponent component) {
		return componentMap.get(component);
	}
	
	public Integer idOfUsedComponent(CmnComponent component) {
		ComponentInfo componentInfo = componentMap.get(component);
		if (componentInfo != null) {
			componentInfo.checkAndAssignUsedOrderNumber(orderNumberSequence.incrementAndGet());
			return componentInfo.getId();
		}
//		Integer newId = getId(component);
//		componentMap.put(component, new ComponentInfo(-1));
//		return newId;
		for (var cc : componentMap.keySet()) {
			System.out.println("Component in map: " + cc.getName());
			
		}
		throw new RuntimeException("Id of used component not defined, component name: " + component.getName());
	}

	public void setComponentMap(Map componentMap) {
		this.componentMap = componentMap;
	}

	public CmnApplication getApplication() {
		return application;
	}
	
	public CmnImplementedFeature getImplementedFeature(CmnComponent component, CmnInvokeable invokeable) {
		if (component != null && invokeable != null) {
			if (invokeableImplementationMap == null) {
				invokeableImplementationMap = new LinkedHashMap<>();
			}
			Map mapForComponent = invokeableImplementationMap.get(component);
			if (mapForComponent == null) {
				mapForComponent = new LinkedHashMap<>();
				for (CmnImplementedFeature implementedFeature : component.getImplementedFeatures()) {
					if (implementedFeature.getResourceOperation() != null) {
						mapForComponent.put(implementedFeature.getResourceOperation(), implementedFeature);
					}
				}
			}
			return mapForComponent.get(invokeable);
		}
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy