protected virtual async Task <WorkflowRules[]> GetFileSystemRulesAsync(Type type, CancellationToken cancellationToken = default) { var ruleId = GetRuleId(type); var ruleFile = GetRuleName(type); var fileInfo = FileProvider.GetFileInfo(ruleFile); if (fileInfo != null && fileInfo.Exists) { // 规则文件监控 // TODO: 删除模块的规则缓存还需要删除RulesEngine中rulesCache已编译的规则缓存 ChangeToken.OnChange( () => FileProvider.Watch(ruleFile), (int ruleId) => { RulesCache.Remove(ruleId); }, ruleId); // 打开文本流 using (var stream = fileInfo.CreateReadStream()) { var result = new byte[stream.Length]; await stream.ReadAsync(result, 0, (int)stream.Length); var ruleDsl = Encoding.UTF8.GetString(result); // 解析 return(JsonSerializer.Deserialize <WorkflowRules[]>(ruleDsl)); } } return(new WorkflowRules[0]); }
async Task <RulesCache> LoadRules(IRulesLoader rulesLoader) { RulesCache cache = new RulesCache(new Dictionary <string, RuleEngine>(), new Dictionary <string, string>()); StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFolder rulesFolder = await appInstalledFolder.GetFolderAsync("Rules"); IReadOnlyList <StorageFile> files = await rulesFolder.GetFilesAsync(); Dictionary <string, StorageFile> rules = files.Where(f => f.Name.EndsWith(".module")).ToDictionary(f => Path.GetFileNameWithoutExtension(f.Name).ToLowerInvariant()); Dictionary <string, StorageFile> resources = files.Where(f => f.Name.EndsWith(".resources")).ToDictionary(f => Path.GetFileNameWithoutExtension(f.Name).ToLowerInvariant()); int count = 0; foreach (string key in rules.Keys) { count++; await SetProgressValue(((double)count / rules.Count) * 100); rulesLoader.LoadRules(new RulesModuleModel { Name = key, ResourceSetFile = GetBytes(resources[key]), RuleSetFile = GetBytes(rules[key]) }, cache); } return(cache); }
private void _executeRulesEngine() { UpdateProgressList(); RuleEngine ruleEngine = RulesCache.GetRuleEngine(_moduleBeginName); if (ruleEngine == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.moduleNotFoundFormat, _moduleBeginName)); } ruleEngine.Execute(this.FlowActivity); }
async Task <RulesCache> LoadRules(IRulesLoader rulesLoader) { RulesCache cache = new RulesCache(new ConcurrentDictionary <string, RuleEngine>(), new ConcurrentDictionary <string, string>()); var assembly = IntrospectionExtensions.GetTypeInfo(typeof(ExtendedSplashViewModel)).Assembly; string[] uwpResources = GetResourceStrings(assembly); Dictionary <string, string> rules = uwpResources .Where(f => f.EndsWith(".module")) .ToDictionary(f => GetKey(f).ToLowerInvariant()); Dictionary <string, string> resources = uwpResources .Where(f => f.EndsWith(".resources")) .ToDictionary(f => GetKey(f).ToLowerInvariant()); int count = 0; DateTime dt = DateTime.Now; await Task.WhenAll ( rules.Keys.Select ( key => LoadRules ( new RulesModuleModel { Name = key, ResourceSetFile = GetBytes(resources[key], assembly), RuleSetFile = GetBytes(rules[key], assembly) } ) ) ); return(cache); Task LoadRules(RulesModuleModel module) { return(Task.Run(() => { rulesLoader.LoadRules(module, cache); count++; Progress = (double)count / rules.Count; })); } string GetKey(string fullResourceName) => Path.GetExtension(Path.GetFileNameWithoutExtension(fullResourceName)).Substring(1); }
private async Task <WorkflowRules[]> GetCachedRulesAsync(Type type, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); var ruleId = GetRuleId(type); return(await RulesCache.GetOrCreateAsync(ruleId, async (entry) => { entry.SetAbsoluteExpiration(TimeSpan.FromMinutes(30)); return await GetFileSystemRulesAsync(type, cancellationToken); })); }
private void _startInitialFlow(string module) { if (string.IsNullOrEmpty(module)) { throw new ArgumentException(Strings.ruleSetCannotBeNull); } ClearFlowVariables(); UpdateProgressList(module); RuleEngine ruleEngine = RulesCache.GetRuleEngine(module); if (ruleEngine == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.moduleNotFoundFormat, module)); } ruleEngine.Execute(this.FlowActivity); }