/// <summary> /// Gets the saga type to instantiate and invoke if an existing saga couldn't be found by /// the given finder using the given message. /// </summary> /// <param name="message"></param> /// <param name="finder"></param> /// <returns></returns> public static Type GetSagaTypeToStartIfMessageNotFoundByFinder(object message, IFinder finder) { Type sagaEntityType; FinderTypeToSagaEntityTypeLookup.TryGetValue(finder.GetType(), out sagaEntityType); if (sagaEntityType == null) return null; Type sagaType; SagaEntityTypeToSagaTypeLookup.TryGetValue(sagaEntityType, out sagaType); if (sagaType == null) return null; List<Type> messageTypes; SagaTypeToMessagTypesRequiringSagaStartLookup.TryGetValue(sagaType, out messageTypes); if (messageTypes == null) return null; if (messageTypes.Contains(message.GetType())) return sagaType; foreach (Type msgTypeHandleBySaga in messageTypes) if (msgTypeHandleBySaga.IsAssignableFrom(message.GetType())) return sagaType; return null; }
/// <summary> /// Gets a reference to the generic "FindBy" method of the given finder /// for the given message type using a hashtable lookup rather than reflection. /// </summary> /// <param name="finder"></param> /// <param name="message"></param> /// <returns></returns> public static MethodInfo GetFindByMethodForFinder(IFinder finder, object message) { MethodInfo result = null; IDictionary <Type, MethodInfo> methods; FinderTypeToMessageToMethodInfoLookup.TryGetValue(finder.GetType(), out methods); if (methods != null) { methods.TryGetValue(message.GetType(), out result); if (result == null) { foreach (Type messageType in methods.Keys) { if (messageType.IsInstanceOfType(message)) { result = methods[messageType]; } } } } return(result); }
/// <summary> /// Gets a reference to the generic "FindBy" method of the given finder /// for the given message type using a hashtable lookup rather than reflection. /// </summary> public MethodInfo GetFindByMethodForFinder(IFinder finder, object message) { MethodInfo result = null; Dictionary<Type, MethodInfo> methods; FinderTypeToMessageToMethodInfoLookup.TryGetValue(finder.GetType(), out methods); if (methods != null) { methods.TryGetValue(message.GetType(), out result); if (result == null) { foreach (var messageTypePair in methods) { if (messageTypePair.Key.IsInstanceOfType(message)) { result = messageTypePair.Value; } } } } return result; }
public Solver(Phrases phrases, IFinder finder, IOracle oracle, int bestSugessionsCount = 20, double metricEpsilon = 1) { this.phrases = phrases; Finder = finder; Oracle = oracle; this.bestSugessionsCount = bestSugessionsCount; this.metricEpsilon = metricEpsilon; Name = oracle.GetType().Name + "-" + finder.GetType().Name; }
/// <summary> /// Gets the saga type to instantiate and invoke if an existing saga couldn't be found by /// the given finder using the given message. /// </summary> /// <param name="message"></param> /// <param name="finder"></param> /// <returns></returns> public static Type GetSagaTypeToStartIfMessageNotFoundByFinder(object message, IFinder finder) { Type sagaEntityType; FinderTypeToSagaEntityTypeLookup.TryGetValue(finder.GetType(), out sagaEntityType); if (sagaEntityType == null) { return(null); } Type sagaType; SagaEntityTypeToSagaTypeLookup.TryGetValue(sagaEntityType, out sagaType); if (sagaType == null) { return(null); } List <Type> messageTypes; SagaTypeToMessagTypesRequiringSagaStartLookup.TryGetValue(sagaType, out messageTypes); if (messageTypes == null) { return(null); } if (messageTypes.Contains(message.GetType())) { return(sagaType); } foreach (Type msgTypeHandleBySaga in messageTypes) { if (msgTypeHandleBySaga.IsInstanceOfType(message)) { return(sagaType); } } return(null); }
/// <summary> /// Gets a reference to the generic "FindBy" method of the given finder /// for the given message type using a hashtable lookup rather than reflection. /// </summary> /// <param name="finder"></param> /// <param name="message"></param> /// <returns></returns> public static MethodInfo GetFindByMethodForFinder(IFinder finder, IMessage message) { MethodInfo result = null; IDictionary<Type, MethodInfo> methods; FinderTypeToMessageToMethodInfoLookup.TryGetValue(finder.GetType(), out methods); if (methods != null) { methods.TryGetValue(message.GetType(), out result); if (result == null) foreach (Type messageType in methods.Keys) if (messageType.IsAssignableFrom(message.GetType())) result = methods[messageType]; } return result; }
/// <summary> /// Gets a reference to the generic "FindBy" method of the given finder /// for the given message type using a hashtable lookup rather than reflection. /// </summary> /// <param name="finder"></param> /// <param name="message"></param> /// <returns></returns> public static MethodInfo GetFindByMethodForFinder(IFinder finder, object message) { MethodInfo result = null; IDictionary<Type, MethodInfo> methods; FinderTypeToMessageToMethodInfoLookup.TryGetValue(finder.GetType(), out methods); if (methods != null) { methods.TryGetValue(message.GetType(), out result); if (result == null) foreach (Type messageType in methods.Keys) if (messageType.IsAssignableFrom(message.GetType())) result = methods[messageType]; } return result; }
/// <summary> /// Gets the saga type to instantiate and invoke if an existing saga couldn't be found by /// the given finder using the given message. /// </summary> /// <param name="message"></param> /// <param name="finder"></param> /// <returns></returns> public static Type GetSagaTypeToStartIfMessageNotFoundByFinder(IMessage message, IFinder finder) { Type sagaEntityType; FinderTypeToSagaEntityTypeLookup.TryGetValue(finder.GetType(), out sagaEntityType); if (sagaEntityType == null) return null; Type sagaType; SagaEntityTypeToSagaTypeLookup.TryGetValue(sagaEntityType, out sagaType); if (sagaType == null) return null; List<Type> messageTypes; SagaTypeToMessagTypesRequiringSagaStartLookup.TryGetValue(sagaType, out messageTypes); if (messageTypes == null) return null; if (messageTypes.Contains(message.GetType())) return sagaType; foreach (Type msgTypeHandleBySaga in messageTypes) if (msgTypeHandleBySaga.IsAssignableFrom(message.GetType())) return sagaType; return null; }
static TextWrapper RunStep(TextWrapper baseScv, int i, int fileCount, IMap map, IHeuristic h, IFinder finder, string path, string mapName, string plus = "") { var csv = baseScv; csv.MapNumber = i.ToString(); csv.MapTypeGenerate = map.MapType.ToString(); csv.Alg = finder.Name; csv.Heuristic = h.GetType().Name; csv.MapDiagonal = map.Diagonal.ToString(); csv.MapSize = $"{map.Width}x{map.Height}"; Console.CursorLeft = 0; if (Console.CursorTop > 0) { Console.Write(new string(' ', 80)); Console.CursorLeft = 0; } Console.WriteLine($" ({i}) {csv.Alg} - { csv.Heuristic } - {csv.MapDiagonal} ({csv.MapSize}/{csv.MapTypeGenerate})"); DrawTextProgressBar(i, fileCount); if (finder.Find(map, h)) { csv.PathLength = map.GetPath().OrderBy(x => x.G).Last().G.ToString(); Console.ForegroundColor = ConsoleColor.Green; csv.Solution = "Yes"; } else { csv.Solution = "No"; csv.PathLength = "-1"; Console.ForegroundColor = ConsoleColor.Red; } map.Clear(); csv.Time = finder.GetProcessedTime().ToString(); csv.MaxNodes = map.GetMaxExpandedNodes().ToString(); Console.CursorTop -= 1; Console.CursorLeft = 0; Console.WriteLine($"{csv.Solution}-{csv.Time}ms"); Console.ForegroundColor = ConsoleColor.White; // save solutions var solutionPath = Path.Combine(path, "solutions", map.MapType.ToString(), finder.GetType().Name, h.GetType().Name); var fileName = mapName; if (finder is IGeneticAlgorithm ga) { solutionPath = Path.Combine(solutionPath, ga.Fitness.GetType().Name, ga.Selection.GetType().Name); fileName = $"{Path.GetFileNameWithoutExtension(fileName)}_{ga.Mutate.GetType().Name}_{ga.Crossover.GetType().Name}_{i}.txt"; } if (!Directory.Exists(solutionPath)) { Directory.CreateDirectory(solutionPath); } var text = FileTool.GetTextRepresentation(map, false, map.GetPath()); File.WriteAllText(Path.Combine(solutionPath, fileName), text); return(csv); }
/// <summary> /// Questo metodo mi permette di isolare la gestione della ricerca e di settare il finder nella property. /// Questo metodo viene delegato allo sviluppatore. E' necessario implementarlo per eseguire il DoSearchHeader. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="THeader"></typeparam> /// <returns></returns> public ICollection <THeader> FinderExecuteDoSearchHeader <T, THeader>() { IFinder <T, THeader> finderToExecute = null; try { finderToExecute = Finder as IFinder <T, THeader>; if (finderToExecute != null) { if (ImpersonateCurrentUser) { return(ExecuteSearchWithImpersonation <ICollection <THeader> >(finderToExecute.DoSearchHeader)); } else { return(finderToExecute.DoSearchHeader()); } } return(new List <THeader>()); } catch (Exception ex) { PrintSearchException(ex); FileLogger.Warn(LogName.FileLog, String.Format("Errore nell'uso del finder [{0}]", finderToExecute.GetType()), ex); return(null); } }