隨筆-35  評論-97  文章-0  trackbacks-0

          Attribute和Reference是模型中的其中兩大元素,通常我們查詢都會根據(jù)Attribute和Reference來查詢。EMF-Query中提供了針對這兩種元素的封裝了條件類哦,怎么使用?easy~~

          接觸了eclipse那么久,對eclipse所說的feature的理解感覺還是有點模糊,僅僅就是我們理解成的“功能部件”嗎?很懷疑,還得仔細查查資料。要是有哪位兄臺能在這里給我指點一下就好了,呵呵。Feature Conditions是有關features of model elements的(不知如何翻譯這個詞才恰當,保留它吧)。給我自己的感覺,在EMF中,“features of model elements”的feature包含的模型元素的Attribute和Reference,或許Containment也算。看看結構圖來分析:

          EObjectStructuralFeatureCondition關聯(lián)了一個EStructuralFeature,主要用來判斷模型元素feature 是否有效(模型元素的 EClass 是否具有feature)。

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

          看看一個例子,列出商品中所有關于食品類food的產(chǎn)品product:

                  ShopFactory shopFactory = ShopFactory.eINSTANCE;
                  
          //商品種類
                  Category food = shopFactory.createCategory();
                  food.setName(
          "食物");
                  Category commodity 
          = shopFactory.createCategory();
                  commodity.setName(
          "日用品");
                  
          //食品類產(chǎn)品
                  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);
                  
          //日用品類產(chǎn)品
                  Product towel = shopFactory.createProduct();
                  towel.setName(
          "毛巾");
                  commodity.getProducts().add(towel);
                  towel.setCategory(commodity);
                  
                  
          //將產(chǎn)品用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.的,大多數(shù)時候,你可以使用默認的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));

           

           下一節(jié),將介紹查詢的執(zhí)行,也就是通過SELECT Statement來查詢對象。

           

          posted on 2007-06-11 19:00 三告習習 閱讀(1189) 評論(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怎么做限制,是否能做到?急切需要答案。  回復  更多評論
            
          主站蜘蛛池模板: 深水埗区| 诏安县| 德惠市| 前郭尔| 鄢陵县| 安岳县| 宜川县| 祥云县| 天门市| 沁水县| 喀喇| 长葛市| 尼勒克县| 涪陵区| 漳州市| 开平市| 大同县| 湘阴县| 吉木萨尔县| 儋州市| 军事| 绥化市| 新乡市| 绵阳市| 包头市| 汝城县| 平凉市| 彩票| 维西| 石屏县| 澄城县| 文山县| 黄陵县| 松滋市| 广饶县| 盱眙县| 澜沧| 永福县| 巴林左旗| 普兰县| 浮梁县|