こんにちは。渡邊です。
商用ルールエンジン、Corticonの研究をしています。
“Corticon Foundation User Guide.pdf” を一読しました。
概要についてはCorticon 5.3 : Corticon Foundation User Guide.pdf メモにまとめました。本記事では、Sharing Model API Stateついて掘り下げます。
この記事は、Corticon 5.3 : Demand-Loading and Lazy-Loadingの続きです。
Vocabulary編集者の変更がRulesheet編集者側に反映されないのは、都合が悪いかもしれません。Sharing Model API Stateがこのような不都合を解決します。IModelAPIStateインスタンスを生成し、各APIに設定します。
Corticon 5.3 : Demand-Loading and Lazy-Loadingで示したコードの一部を変更しました。変更箇所を赤字で示します。完全なコードはこちらからダウンロード可能です。
メインロジック
流れは変わっていません。ModelAPIStateImplを生成し、各編集者に渡しています。
public void start(String vocabularyFileName, String rulesheetFileName) {
IModelAPIState sharing = new ModelAPIStateImpl();
// Vocabulary編集開始
VocabularyEditor vocabularyEditor = new VocabularyEditor(vocabularyFileName, sharing);
vocabularyEditor.addEntity("商品");
vocabularyEditor.save();
vocabularyEditor.addEntity("注文");
// Rulesheet編集開始
RulesheetEditor rulesheetEditor = new RulesheetEditor(rulesheetFileName, sharing);
rulesheetEditor.addScopeEntity("商品");
rulesheetEditor.addScopeEntity("注文");
vocabularyEditor.save();
rulesheetEditor.addScopeEntity("注文");
rulesheetEditor.load(rulesheetFileName);
rulesheetEditor.addScopeEntity("注文");
rulesheetEditor.save();
// 後処理
vocabularyEditor.dispose();
rulesheetEditor.dispose();
}
Vocabulary編集者
IVocabularyTreeModelAPIにIModelAPIStateを設定しています。 リソースのロード前に設定していることに留意してください。
class VocabularyEditor {
private IVocabularyTreeModelAPI vocabularyAPI;
private Resource resource;
public VocabularyEditor(String fileName, IModelAPIState sharing) {
systemout("Vocabulary> 編集を開始(API生成)します。");
vocabularyAPI = VocabularyTreeModelAPIFactory.getInstance();
vocabularyAPI.setModelAPIState(sharing);
load(fileName);
}
public void load(String fileName) {
systemout("Vocabulary> リソースをロードします。");
resource = vocabularyAPI.loadResource(fileName);
}
public void addEntity(String name) {
IEntity newEntity1 = vocabularyAPI.addEntity();
vocabularyAPI.setEntityName(newEntity1, name);
systemout("Vocabulary> エンティティ'" + name + "'を語彙に追加しました。");
}
public void save() {
vocabularyAPI.saveResource(resource);
systemout("Vocabulary> 保存しました。");
}
public void dispose() {
vocabularyAPI.dispose();
}
}
Rulesheet編集者
IRulesheetTableModelAPIにIModelAPIStateを設定しています。 リソースのロード前に設定していることに留意してください。
class RulesheetEditor {
private IRulesheetTableModelAPI rulesheetAPI;
private Resource resource;
public RulesheetEditor(String fileName, IModelAPIState sharing) {
systemout("Rulesheet> 編集を開始(API生成)します。");
rulesheetAPI = RulesheetTableModelAPIFactory.getInstance();
rulesheetAPI.setModelAPIState(sharing);
load(fileName);
}
public void load(String fileName) {
systemout("Rulesheet> リソースをロードします。");
resource = rulesheetAPI.loadResource(fileName);
}
public void addScopeEntity(String name) {
IVocabularyModelAPI vocabularyAPI = rulesheetAPI.getVocabularyModelAPI();
IEntity newEntity = vocabularyAPI.findEntity(name);
if (newEntity != null) {
rulesheetAPI.addScopeEntity(newEntity);
systemout("Rulesheet> エンティティ'" + name + "'をスコープに追加しました。");
} else {
systemout("Rulesheet> エンティティ'" + name + "'は存在しません。スコープに登録できません。");
}
}
public void save() {
rulesheetAPI.saveResource(resource);
systemout("Rulesheet> 保存しました。");
}
public void dispose() {
rulesheetAPI.dispose();
}
}
実行結果
リソースを共有しているのではなく、Canonical APIの状態を共有しているので、Vocabulary編集者が語彙「注文」の登録を保存する前に、Rulesheet編集者は語彙「注文」を取得することができています。
01: Vocabulary> 編集を開始(API生成)します。 02: Vocabulary> リソースをロードします。 03: Vocabulary> エンティティ'商品'を語彙に追加しました。 04: Vocabulary> 保存しました。 05: Vocabulary> エンティティ'注文'を語彙に追加しました。 06: Rulesheet> 編集を開始(API生成)します。 07: Rulesheet> リソースをロードします。 08: Rulesheet> エンティティ'商品'をスコープに追加しました。 09: Rulesheet> エンティティ'注文'をスコープに追加しました。 10: Vocabulary> 保存しました。 11: Rulesheet> エンティティ'注文'をスコープに追加しました。 12: Rulesheet> リソースをロードします。 13: Rulesheet> エンティティ'注文'をスコープに追加しました。 14: Rulesheet> 保存しました。
保存されたRulesheetをStudioから開きました。スコープに「注文」が3個登録されています。まだ試していませんが、それぞれに重複しないようなエイリアスを付ければ、エラー表示が消えるのではないでしょうか。
![]()