我使用的是IBM 8.9.2,我们有一个场景,我需要根据列表Y中的值创建一个列表X,同时对这些值进行分组
例如,假设我有一个城市列表,并且每个City对象(在cityList列表中)都有一个属性-country。现在,我想反转关系,创建一个国家列表,该列表由包含城市的国家对象组成。
我的规则是
definitions
set 'cities' to all cities in cityList;
set 'a city' to a city in 'cities'
set 'countries' to all countries in countryList;
set 'a country' to a country in 'countries'
if
the country code of 'a city' is the country code of 'a country'
then
add 'a city' to the contained cities of 'a country' ; (** Assume B2X/XOM has method for adding the city to the country list)
else
create country for 'a city' and add it to countryList ; (** Assume appropriate B2X/XOM)
将国家添加到countryList不会更新其目标状态,因此在城市列表的第一个城市运行规则后,不会将其重新列入重新评估规则的议程
因此,结果是为每个城市创建了一个新的Country对象的国家列表,而不是计划的分组
我的目标是在内存中插入cityList和countryList,并打开Rete,这样模式匹配就可以在内存中动态进行
寻找如何实现这一目标的建议。
我将编写两个独立的规则。一个用于将每个城市的国家添加到国家列表中。另一个是将每个城市添加到适当的国家。两个BOM"添加"方法都应选中"更新对象状态"。注意:我已经在ODM不允许的规则中添加了注释。
第一条规则
definitions
set 'the city' to a city in cityList ;
if
the country code of 'the city' is not one of the country codes of countryList // Assume BOM method exists
then
add the country code of 'the city' to the country codes of countryList ; // Assume BOM method exists
第二条规则
definitions
set 'the city' to a city in cityList ;
set 'the country' to a country in countryList
where the country code of this country is the country code of 'the city';
if
'the city' is not one of the cities of 'the country' // Requires City.equals method
then
add 'the city' to the cities of 'the country' ; // Assume BOM method exists