package schedframe; import gridsim.GridSim; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; import schedframe.exceptions.ResourceException; import schedframe.resources.ResourceHistoryItem; import schedframe.resources.computing.ComputingResource; import schedframe.scheduling.Scheduler; public class SimulatedEnvironment { protected static Scheduler scheduler; protected static List computingResources; protected List toInit; protected Set compResLayers; protected static List compResHistory = new ArrayList(); public SimulatedEnvironment(Scheduler logicalStructure, List compResources){ scheduler = logicalStructure; computingResources = compResources; } public Scheduler getScheduler() { return scheduler; } public List getComputingResources() { return computingResources; } public static ComputingResource getComputingResourceByName(String resourceName) throws ResourceException { ComputingResource resourceWithName = null; for (int i = 0; i < computingResources.size() && resourceWithName == null; i++) { ComputingResource resource = computingResources.get(i); if (resource.getFullName().equals(resourceName)) resourceWithName = resource; else resourceWithName = resource.getDescendantByName(resourceName); } return resourceWithName; } private static Scheduler getSchedulerByName(String schedulerName) { Scheduler schedulerWithName = null; if (scheduler.getName().compareTo(schedulerName) == 0) schedulerWithName = scheduler; else if (scheduler.getChildren() != null) { LinkedList toExamine = new LinkedList(); toExamine.push(scheduler); while (!toExamine.isEmpty() && schedulerWithName != null) { Scheduler scheduler = toExamine.pop(); List schedulers = scheduler.getChildren(); int numberOfSched = schedulers.size(); for (int i = 0; i < numberOfSched; i++) { Scheduler schedulerChild = schedulers.get(i); if(scheduler.getName().equals(schedulerName)){ schedulerWithName = schedulerChild; break; } else toExamine.addLast(schedulerChild); } } } return schedulerWithName; } public static Scheduler getScheduler(String resName){ if(GridSim.getEntityId(resName) != -1){ return getSchedulerByName(resName); } ComputingResource resourceWithName = null; try { resourceWithName = getComputingResourceByName(resName); } catch (ResourceException e) { } /*for(int i = 0 ; i < computingResources.size() && resourceWithName == null; i++){ ComputingResource resource = computingResources.get(i); if(resource.getName().equals(resName)) resourceWithName = resource; else try { resourceWithName = resource.getDescendantByName(resName); } catch (ResourceException e) { return null; } }*/ if(resourceWithName == null) return null; List children = resourceWithName.getChildren(); Set childrenSchedulers = new HashSet(); if(children.isEmpty()) return null; for(ComputingResource child: children) { childrenSchedulers.add(child.getScheduler()); } Set tempChildrenSchedulers = new HashSet(childrenSchedulers); while(childrenSchedulers.size() > 1){ childrenSchedulers = new HashSet(); for(Scheduler s: tempChildrenSchedulers){ childrenSchedulers.add(s.getParent()); } tempChildrenSchedulers = new HashSet(childrenSchedulers); } Iterator it = childrenSchedulers.iterator(); Scheduler potentialScheduler = it.next(); if(potentialScheduler.getCompResources().containsAll(children)) return potentialScheduler; return null; } public static ComputingResource getCommonComputingResourceParent(List compResources){ if(compResources.size() == 0) return null; Set candidates = new HashSet(compResources); while(candidates.size() > 1){ Set parents = new HashSet(); Iterator it = candidates.iterator(); while(it.hasNext()){ ComputingResource compRes = it.next(); if(compRes.getParent() != null){ parents.add(compRes.getParent()); } it.remove(); } candidates.addAll(parents); } return candidates.toArray(new ComputingResource[0])[0]; } public void setInitList(List toI) { toInit = toI; } public List getToInit() { return toInit; } public Set getComputingResourceLayers() { return compResLayers; } public void setCompResLayers(Set compResLayers) { this.compResLayers = compResLayers; } public static void traceResource(long timestamp, String resourceName, String operation, String paramter){ ResourceHistoryItem rhc = new ResourceHistoryItem(timestamp, resourceName, operation, paramter); compResHistory.add(rhc); } public static List getCompResHistory() { return compResHistory; } }