private (bool success, string message) openModel(string _path) { var path = _path + "\\model.json"; if (!File.Exists(path)) { return(false, "モデル定義ファイル(model.json)が存在しません。"); } string json; using (var sr = new StreamReader(path, Encoding.UTF8)) { json = sr.ReadToEnd(); } bool deserializeErrored = false; model = JsonConvert.DeserializeObject <MCBEModel>(json, new JsonSerializerSettings() { Error = (sender, errorArgs) => { deserializeErrored = true; errorArgs.ErrorContext.Handled = true; } }); if (deserializeErrored) { return(false, "モデル定義ファイルのロード中にエラーが発生しました。正しいJSON形式でない可能性があります。"); } identifier = new MCBECore.Identifier(model, shortcut: shortcutMode); return(true, null); }
private List <string> validateDescriptions(MCBEModel model) { var errors = new List <string>(); if (model.descriptions == null || model.descriptions.Count == 0) { errors.Add("特徴リストが空です (model.descriptions)"); return(errors); } foreach (var desc in model.descriptions) { if (string.IsNullOrEmpty(desc.id)) { errors.Add("特徴idが空です (model.descriptions)"); continue; } //Guid id; //if (!Guid.TryParse(desc.id, out id)) //{ // errors.Add($"特徴idの構文が間違っています"); //} if (string.IsNullOrEmpty(desc.text)) { errors.Add($"特徴のテキストが空です (model.descriptions.{desc.id})"); } } return(errors); }
public bool Validate(MCBEModel model, ValidateType type = AllTypes) { var errors = new List <string>(); if (type.HasFlag(ValidateType.ModelInfo)) { errors.AddRange(validateModelInfo(model)); } if (type.HasFlag(ValidateType.Descriptions)) { errors.AddRange(validateDescriptions(model)); } if (type.HasFlag(ValidateType.Rules)) { errors.AddRange(validateRules(model)); } if (errors.Count > 0) { Errors = errors; return(false); } return(true); }
private MCBEModel createModel() { var model = new MCBEModel(); model.id = Guid.NewGuid().ToString(); model.name = modelName; model.descriptions = new List <MCBEDescription>(); foreach (MCBEDescription item in descriptionsListBoxTabDescsItem.Items) { // コピーを作成 model.descriptions.Add(new MCBEDescription(item)); } model.rules = new List <MCBERule>(); var priority = 1; foreach (MCBERule item in rulesListBoxTabRulesItem.Items) { model.rules.Add(new MCBERule(item)); model.rules.Last().priority = priority; priority++; } return(model); }
private List <string> validateRules(MCBEModel model) { var errors = new List <string>(); if (model.rules == null || model.rules.Count == 0) { errors.Add("ルールリストが空です (model.rules)"); return(errors); } foreach (var rule in model.rules) { if (string.IsNullOrEmpty(rule.id)) { errors.Add("ルールidが空です (model.rules)"); continue; } if (rule.priority <= 0) { errors.Add($"ルールの優先度が0以下です (model.rules.{rule.id}, {rule.priority})"); } if (rule.antecedents.Count == 0) { errors.Add($"ルールの仮定部が空です (model.rules.{rule.id})"); continue; } if (rule.consequents.Count == 0) { errors.Add($"ルールの結論部が空です (model.rules.{rule.id})"); continue; } var intersects = rule.consequents.Intersect(rule.antecedents).Count(); if (intersects > 0) { errors.Add($"ルールの仮定部と結論部に同じ項目が含まれています (model.rules.{rule.id}, count: {intersects})"); continue; } foreach (var desc in rule.antecedents) { if (!model.descriptions.Any(d => d.id == desc.Key)) { errors.Add($"存在しない特徴を参照しています (仮定部, model.rules.{rule.id}.antecedents.{desc.Key})"); } } foreach (var desc in rule.consequents) { if (!model.descriptions.Any(d => d.id == desc.Key)) { errors.Add($"存在しない特徴を参照しています (結論部, model.rules.{rule.id}.consequents.{desc.Key})"); } } } return(errors); }
public Identifier(MCBEModel _model, bool loadOnly = false, bool shortcut = false) { model = _model; shortcutDeterminating = shortcut; if (!loadOnly) { init(); } }
private static MCBEModel createModel() { var model = new MCBEModel(); model.name = "Animal Expert"; model.id = Guid.NewGuid().ToString(); model.descriptions = new List <MCBEDescription>(); { var descs = new Dictionary <string, (string text, bool isInternal)>() { { "D1", ("体毛を持つ", false) }, { "D2", ("ほ乳動物である", true) },
private bool validate(MCBEModel model = null, ModelValidator.ValidateType type = ModelValidator.AllTypes, bool showError = true) { if (model == null) { model = createModel(); } var result = validator.Validate(model, type); if (!result && showError) { var errorWindow = new ValidationErrorWindow(); errorWindow.SetErrors(validator.Errors); errorWindow.Show(); } return(result); }
public void モデルは正常にシリアライズできる() { var model = new MCBEModel(); model.name = "テストモデル"; model.id = "1"; model.descriptions = new List <MCBEDescription>() { TestD1, TestD2 }; model.rules = new List <MCBERule>() { TestR1 }; var json = JsonConvert.SerializeObject(model, Formatting.Indented); Assert.AreEqual(testJson, json); }
private List <string> validateModelInfo(MCBEModel model) { var errors = new List <string>(); if (string.IsNullOrEmpty(model.name)) { errors.Add("モデルの名称が設定されていません (model.name)"); } if (model.name.IndexOfAny(invalidFileNameChars) >= 0) { errors.Add($"モデルの名称にファイル名に使えない文字が含まれています (model.name, {model.name})"); } if (string.IsNullOrEmpty(model.id)) { errors.Add("モデルのidが設定されていません (model.id)"); } return(errors); }