/** * Gets a randomly-picked malfunction for a given unit scope. * * @param scope a collection of scope strings defining the unit. * @return a randomly-picked malfunction or null if there are none available. */ public Malfunction getMalfunction(Collection<String> scope) { Malfunction result = null; double totalProbability = 0D; if (malfunctions.size() > 0) { Iterator<Malfunction> i = malfunctions.iterator(); while (i.hasNext()) { Malfunction temp = i.next(); if (temp.unitScopeMatch(scope)) totalProbability += temp.getProbability(); } } double r = RandomUtil.getRandomDouble(totalProbability); Iterator<Malfunction> i = malfunctions.iterator(); while (i.hasNext()) { Malfunction temp = i.next(); double probability = temp.getProbability(); if (temp.unitScopeMatch(scope) && (result == null)) { if (r < probability) { try { result = temp.getClone(); result.determineRepairParts(); } catch (Exception e) { e.printStackTrace(System.err); } } else r -= probability; } } return result; }
/** * Gets the repair part probabilities per malfunction for a set of entity scope strings. * * @param scope a collection of entity scope strings. * @return map of repair parts and probable number of parts needed per malfunction. * @throws Exception if error finding repair part probabilities. */ Map<Part, Double> getRepairPartProbabilities(Collection<String> scope) { Map<Part, Double> result = new HashMap<Part, Double>(); Iterator<Malfunction> i = malfunctions.iterator(); while (i.hasNext()) { Malfunction malfunction = i.next(); if (malfunction.unitScopeMatch(scope)) { double malfunctionProbability = malfunction.getProbability() / 100D; MalfunctionConfig config = SimulationConfig.instance().getMalfunctionConfiguration(); String[] partNames = config.getRepairPartNamesForMalfunction(malfunction.getName()); for (String partName : partNames) { double partProbability = config.getRepairPartProbability(malfunction.getName(), partName) / 100D; int partNumber = config.getRepairPartNumber(malfunction.getName(), partName); double averageNumber = RandomUtil.getRandomRegressionIntegerAverageValue(partNumber); double totalNumber = averageNumber * partProbability * malfunctionProbability; Part part = (Part) ItemResource.findItemResource(partName); if (result.containsKey(part)) totalNumber += result.get(part); result.put(part, totalNumber); } } } return result; }