随笔-35  评论-97  文章-0  trackbacks-0

Attribute和Reference是模型中的其中两大元素,通常我们查询都会根据Attribute和Reference来查询。EMF-Query中提供了针对这两种元素的封装了条件类哦,怎么使用?easy~~

接触了eclipse那么久,对eclipse所说的feature的理解感觉还是有点模糊,仅仅就是我们理解成的“功能部件”吗?很怀疑,还得仔细查查资料。要是有哪位兄台能在这里给我指点一下就好了,呵呵。Feature Conditions是有关features of model elements的(不知如何翻译这个词才恰当,保留它吧)。给我自己的感觉,在EMF中,“features of model elements”的feature包含的模型元素的Attribute和Reference,或许Containment也算。看看结构图来分析:

EObjectStructuralFeatureCondition关联了一个EStructuralFeature,主要用来判断模型元素feature 是否有效(模型元素的 EClass 是否具有feature)。

EObjectReferencerCondition用于选取元素所有引用到的对象,并且不考虑contaiment(或container features )。

看看一个例子,列出商品中所有关于食品类food的产品product:

        ShopFactory shopFactory = ShopFactory.eINSTANCE;
        
//商品种类
        Category food = shopFactory.createCategory();
        food.setName(
"食物");
        Category commodity 
= shopFactory.createCategory();
        commodity.setName(
"日用品");
        
//食品类产品
        Product creamery = shopFactory.createProduct();
        creamery.setName(
"牛奶");
        Product apple 
= shopFactory.createProduct();
        apple.setName(
"苹果");
        Product pork 
= shopFactory.createProduct();
        pork.setName(
"猪肉");
        food.getProducts().add(creamery);
        food.getProducts().add(apple);
        food.getProducts().add(pork);
        creamery.setCategory(food);
        apple.setCategory(food);
        pork.setCategory(food);
        
//日用品类产品
        Product towel = shopFactory.createProduct();
        towel.setName(
"毛巾");
        commodity.getProducts().add(towel);
        towel.setCategory(commodity);
        
        
//将产品用Set存储起来
        Set<Product> productSet = new HashSet<Product>();
        productSet.add(creamery);
        productSet.add(apple);
        productSet.add(pork);
        productSet.add(towel);
        
        
//建立条件:所有同食品food有关
        EObjectCondition foodCondition = new EObjectReferencerCondition(food);
        
for(EObject object : productSet)//对于Set存储的所有商品
        {
            
if(foodCondition.isSatisfied(object))//如果满足条件(是food类商品)
            {
                System.out.println(object);
            }

        }

 

再看看下面的结构图(用于Feature Values 的条件):

EObjectStructuralFeatureValueCondition 使得断言在查询对象的features的值变得非常方便。它既支持EAttributes又支持EReferences。取feature值是通过IEStructuralFeatureValueGetter的对象获取structural features.的,大多数时候,你可以使用默认的FeatureValueGetter,默认的FeatureValueGetter是这样一个对象:EStructuralFeatureValueGetter.getInstance()

EObjectCondition是支持复合操作和策略使用的,作为子类的EObjectStructuralFeatureValueCondition 当然也不会例外。

EObjectReferenceValueCondition和EObjectAttributeValueConditions十分相似(使用角度),看它们的名称就很容易猜到它们之间的各自的服务对象了,前者是针对Reference的,后者是针对Attribute的。看完下面的例子,就完全可以体会到它们基本的用法了。

 

        ShopFactory shopFactory = ShopFactory.eINSTANCE;
        
        Category food 
= shopFactory.createCategory();
        food.setName(
"食物");
        Category commodity 
= shopFactory.createCategory();
        commodity.setName(
"日用品");
        Category equipment 
= shopFactory.createCategory();
        equipment.setName(
"体育器材");
        
        
//“人人乐”超市只进货两种物品,“体育器材”在这里没有销售
        Shop shop = shopFactory.createShop();
        shop.setName(
"人人乐");
        shop.getCategories().add(food);
        shop.getCategories().add(commodity);
         
        
//============================================
        Condition name = new SubStringValue("食物");
        EObjectCondition categoryName 
= new EObjectAttributeValueCondition(
                ShopPackage.Literals.NAMED_ELEMENT__NAME, name);
        System.out.println(
"先测一下food的名称是不是“食物”?"+categoryName.isSatisfied(food));
        
        
//商场中所有的商品种类名称都是“食物”?
        Condition allFood = new EObjectReferenceValueCondition(
                ShopPackage.Literals.SHOP__CATEGORIES, categoryName,
                ConditionPolicy.ALL, EStructuralFeatureValueGetter.getInstance());

        
//商场中所有的商品种类名称至少有一种是“食物”?
        Condition anyFood = new EObjectReferenceValueCondition(
                ShopPackage.Literals.SHOP__CATEGORIES, categoryName,
                ConditionPolicy.ANY, EStructuralFeatureValueGetter.getInstance());
         
        System.out.println(
"商场中所有的商品种类名称都是“食物”? " + allFood.isSatisfied(shop));
        System.out.println(
"商场中所有的商品种类名称至少有一种是“食物”? " + anyFood.isSatisfied(shop));

 

 下一节,将介绍查询的执行,也就是通过SELECT Statement来查询对象。

 

posted on 2007-06-11 19:00 三告习习 阅读(1169) 评论(2)  编辑  收藏 所属分类: emf/gef/gmf

评论:
# re: [emf-query与emf-ocl] EMF-Query与EMF-OCL学习笔记系列五(Feature Conditions) 2007-06-11 21:05 | 阿南
好好把EMF总结一下啊~回来我到你Blog上学习学习  回复  更多评论
  
# re: [emf-query与emf-ocl] EMF-Query与EMF-OCL学习笔记系列五(Feature Conditions) [未登录] 2007-09-07 15:46 | z
找到有research emf-ocl的真不容易,我遇到一个问题想请教一下,我在ecore中建了两个node A and B,聚合关系,就是B是A的attribute,一个relationship,有三个type,single,mulitple,and input。我想让一个B上只能连一个type类型的relationship.不知道用ocl怎么做限制,是否能做到?急切需要答案。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: