gt: 			UUID passiveId = rel.getPassiveEntity().getDeviceUuid();
context: package osh.esc.grid;
  
  import java.io.FileNotFoundException;
  import java.io.Serializable;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.UUID;
  
  import javax.xml.bind.JAXBException;
  
  import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
  import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
  import it.unimi.dsi.fastutil.objects.ObjectArrayList;
  import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
  import osh.configuration.grid.DevicePerMeter;
  import osh.configuration.grid.GridLayout;
  import osh.configuration.grid.LayoutConnection;
  import osh.datatypes.commodity.AncillaryCommodity;
  import osh.datatypes.commodity.AncillaryMeterState;
  import osh.datatypes.commodity.Commodity;
  import osh.esc.LimitedCommodityStateMap;
  import osh.esc.UUIDCommodityMap;
  import osh.esc.grid.carrier.Electrical;
  import osh.utils.xml.XMLSerialization;
  
  /**
   * 
   * @author Ingo Mauser, Sebastian Kramer
   *
   */
  public class ElectricalEnergyGrid implements EnergyGrid, Serializable {
  
  	/** Serial ID */
  	private static final long serialVersionUID = 420822007199349391L;
  
  	private final Set<EnergySourceSink> sourceSinkList = new ObjectOpenHashSet<EnergySourceSink>();
  
  	private final Set<UUID> meterUUIDs = new ObjectOpenHashSet<UUID>();
  
  	private final List<EnergyRelation<Electrical>> relationList = new ObjectArrayList<>();
  	
  	private InitializedEnergyRelation[] initializedImprovedActiveToPassiveArray;
  	
  	private List<EnergyRelation<Electrical>> initializedPassiveToActiveRelationList = new ObjectArrayList<>();
  	
  	private boolean isSingular = false;
  	private int singularMeter = -1;
  	private int singularPvDevice = -1;
  	private int singularChpDevice = -1;
  	private int singularBatDevice = -1;
  
  	
  	private boolean hasBeenInitialized = false;
  	private boolean hasBat = true, hasPV = true, hasCHP = true;
  
  	private final Set<UUID> activeUUIDs = new ObjectOpenHashSet<UUID>();
  	private final Set<UUID> passiveUUIDs = new ObjectOpenHashSet<UUID>();
  
  	//not needed at the moment, but could be useful in the future
  	//	private final Map<UUID, Set<UUID>> devicesPerMeter = new HashMap<UUID, Set<UUID>>();
  
  	private final Map<UUID, Map<String, Set<UUID>>> devicesByTypePerMeter = new Object2ObjectOpenHashMap<UUID, Map<String, Set<UUID>>>();
  
  	public ElectricalEnergyGrid(String layoutFilePath) throws JAXBException, FileNotFoundException {
  
  		Object unmarshalled = XMLSerialization.file2Unmarshal(layoutFilePath, GridLayout.class);
  
  		if (unmarshalled instanceof GridLayout) {
  			GridLayout layout = (GridLayout) unmarshalled;			
  
  			for (LayoutConnection conn : layout.getConnections()) {
  				EnergySourceSink act = new EnergySourceSink(UUID.fromString(conn.getActiveEntityUUID()));
  				EnergySourceSink pass = new EnergySourceSink(UUID.fromString(conn.getPassiveEntityUUID()));
  
  				activeUUIDs.add(UUID.fromString(conn.getActiveEntityUUID()));
  				passiveUUIDs.add(UUID.fromString(conn.getPassiveEntityUUID()));
  
  				sourceSinkList.add(act);
  				sourceSinkList.add(pass);
  
  				relationList.add(new EnergyRelation<Electrical>(act, pass, 
  						new Electrical(Commodity.fromString(conn.getActiveToPassiveCommodity())),
  						new Electrical(Commodity.fromString(conn.getPassiveToActiveCommodity()))));				
  			}
  
  			for(String uuid : layout.getMeterUUIDs()) {
  				meterUUIDs.add(UUID.fromString(uuid));
  			}
  
  			for (DevicePerMeter dev : layout.getDeviceMeterMap()) {
  
  				UUID meter = UUID.fromString(dev.getMeterUUID());
  				UUID device = UUID.fromString(dev.getDeviceUUID());
  
  				//				Set<UUID> deviceSet = devicesPerMeter.get(meter);
  				//				if (deviceSet == null) {
  				//					deviceSet = new HashSet<UUID>();
  				//					devicesPerMeter.put(meter, deviceSet);
  				//				}
  				//				deviceSet.add(device);
  
  				Map<String, Set<UUID>> deviceTypeMap = devicesByTypePerMeter.get(meter);
  				if (deviceTypeMap == null) {
  					deviceTypeMap = new Object2ObjectOpenHashMap<String, Set<UUID>>();
  					devicesByTypePerMeter.put(meter, deviceTypeMap);
  				}
  				Set<UUID> devicesByType = deviceTypeMap.get(dev.getDeviceType());
  				if (devicesByType == null) {
  					devicesByType = new ObjectOpenHashSet<UUID>();
  					deviceTypeMap.put(dev.getDeviceType(), devicesByType);
  				}
  				devicesByType.add(device);				
  			}
  
  		} else 
  			throw new IllegalArgumentException("layoutFile not instance of GridLayout-class (should not be possible)");
  
  		//sanity check
  		if (!Collections.disjoint(activeUUIDs, passiveUUIDs))
  			throw new IllegalArgumentException("Same UUID is active and passive");	
  	}
  	
  	/**
  	 * only for serialisation, do not use normally
  	 */
  	@Deprecated
  	protected ElectricalEnergyGrid() {
  	}
  
  	@Override
  	public void initializeGrid(Set<UUID> allActiveNodes, Set<UUID> activeNeedsInputNodes, 
  			Set<UUID> passiveNodes, Object2IntOpenHashMap<UUID> uuidToIntMap,
  			Object2ObjectOpenHashMap<UUID, Commodity[]> uuidOutputMap) {
  		
  		
  		initializedPassiveToActiveRelationList = new ObjectArrayList<EnergyRelation<Electrical>>();
  		List<InitializedEnergyRelation> initializedImprovedActiveToPassiveList  = new ObjectArrayList<InitializedEnergyRelation>();
  		Map<UUID, InitializedEnergyRelation> tempHelpMap = new Object2ObjectOpenHashMap<>();
  		
  		for (EnergyRelation<Electrical> rel : relationList) {
  
  			UUID activeId = rel.getActiveEntity().getDeviceUuid();
  			UUID passiveId = rel.getPassiveEntity().getDeviceUuid();
  
  			boolean activeTypeNI = activeNeedsInputNodes.contains(activeId);
  			boolean activeType = allActiveNodes.contains(activeId);
  			boolean passiveType = passiveNodes.contains(passiveId);
  			boolean isMeter = meterUUIDs.contains(passiveId);
  
  			//if both exist and the device really puts out the commodity add to the respective lists
  			if (activeType && (passiveType || isMeter) 
  					&& Arrays.stream(uuidOutputMap.get(activeId)).anyMatch(c -> c.equals(rel.getActiveToPassive().getCommodity()))) {
  				InitializedEnergyRelation relNew = tempHelpMap.get(activeId);
  
  				if (relNew == null) {
  					relNew = new InitializedEnergyRelation(uuidToIntMap.getInt(activeId), new ObjectArrayList<InitializedEnergyRelationTarget>());
  					tempHelpMap.put(activeId, relNew);
  				}
  
  				relNew.addEnergyTarget(new InitializedEnergyRelationTarget(uuidToIntMap.getInt(passiveId), rel.getActiveToPassive().getCommodity()));
  			}
  			if (activeTypeNI && passiveType)
  				initializedPassiveToActiveRelationList.add(rel);
  		}
  		
  		initializedImprovedActiveToPassiveList = new ObjectArrayList<InitializedEnergyRelation>(tempHelpMap.values());
  		initializedImprovedActiveToPassiveList.forEach(e -> e.transformToArrayTargets());
  		
  		initializedImprovedActiveToPassiveArray = new InitializedEnergyRelation[initializedImprovedActiveToPassiveList.size()];
  		initializedImprovedActiveToPassiveArray = initializedImprovedActiveToPassiveList.toArray(initializedImprovedActiveToPassiveArray);
  		
  		if (meterUUIDs.size() == 1) {
  			UUID singularMeterUUID = meterUUIDs.iterator().next();
  			singularMeter = uuidToIntMap.getInt(singularMeterUUID);
  			
  			if (devicesByTypePerMeter.get(singularMeterUUID).get("pv").stream().filter(e -> (allActiveNodes.contains(e) || passiveNodes.contains(e))).count() <= 1
  					&& devicesByTypePerMeter.get(singularMeterUUID).get("chp").stream().filter(e -> (allActiveNodes.contains(e) || passiveNodes.contains(e))).count() <= 1
  					&& devicesByTypePerMeter.get(singularMeterUUID).get("battery").stream().filter(e -> (allActiveNodes.contains(e) || passiveNodes.contains(e))).count() <= 1) {
  				
  				isSingular = true;
  				singularPvDevice = uuidToIntMap.getInt(devicesByTypePerMeter.get(singularMeterUUID).get("pv").stream().filter(e -> (allActiveNodes.contains(e) || passiveNodes.contains(e))).findFirst().orElse(null));
  				singularChpDevice = uuidToIntMap.getInt(devicesByTypePerMeter.get(singularMeterUUID).get("chp").stream().filter(e -> (allActiveNodes.contains(e) || passiveNodes.contains(e))).findFirst().orElse(null));
  				singularBatDevice = uuidToIntMap.getInt(devicesByTypePerMeter.get(singularMeterUUID).get("battery").stream().filter(e -> (allActiveNodes.contains(e) || passiveNodes.contains(e))).findFirst().orElse(null));
  				
  			}
  		}
  		
  		hasBat = false; 
  		hasPV = false;
  		hasCHP = false;
  		
  		for (UUID meter : meterUUIDs) {
  
  			Map<String, Set<UUID>> meterDevices = devicesByTypePerMeter.get(meter);
  
  			for (UUID pv : meterDevices.get("pv")) {
  				if (allActiveNodes.contains(pv) || passiveNodes.contains(pv)) {
  					hasPV = true;
  					break;
  				}
  			}
  
  			for (UUID chp : meterDevices.get("chp")) {
  				if (allActiveNodes.contains(chp) || passiveNodes.contains(chp)) {
  					hasCHP = true;
  					break;
  				}
  			}
  
  			for (UUID battery : meterDevices.get("battery")) {
  				if (allActiveNodes.contains(battery) || passiveNodes.contains(battery)) {
  					hasBat = true;
  					break;
  				}
  			}
  		}
  		
  		hasBeenInitialized = true;
  	}
  	
  	@Override
  	public void finalizeGrid() {
  		hasBeenInitialized = false;
  		initializedPassiveToActiveRelationList = null;
  		
  		singularMeter = -1;
  		singularPvDevice = -1;
  		singularChpDevice = -1;
  		singularBatDevice = -1;
  		
  		isSingular = false;
  		hasBat = true; 
  		hasPV = true;
  		hasCHP = true;
  	}
  
  	@Override	
  	public void doCalculation(
  			Map<UUID, LimitedCommodityStateMap> localCommodityStates,
  			Map<UUID, LimitedCommodityStateMap> totalInputStates,
  			AncillaryMeterState ancillaryMeterState) {
  
  		for (EnergyRelation<Electrical> rel : relationList) {
  
  			UUID activeId = rel.getActiveEntity().getDeviceUuid();
  