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

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

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

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

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

          看看一個例子,列出商品中所有關(guān)于食品類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有關(guān)
                  EObjectCondition foodCondition = new EObjectReferencerCondition(food);
                  
          for(EObject object : productSet)//對于Set存儲的所有商品
                  {
                      
          if(foodCondition.isSatisfied(object))//如果滿足條件(是food類商品)
                      {
                          System.out.println(object);
                      }

                  }

           

          再看看下面的結(jié)構(gòu)圖(用于Feature Values 的條件):

          EObjectStructuralFeatureValueCondition 使得斷言在查詢對象的features的值變得非常方便。它既支持EAttributes又支持EReferences。取feature值是通過IEStructuralFeatureValueGetter的對象獲取structural features.的,大多數(shù)時候,你可以使用默認(rèn)的FeatureValueGetter,默認(rèn)的FeatureValueGetter是這樣一個對象:EStructuralFeatureValueGetter.getInstance()

          EObjectCondition是支持復(fù)合操作和策略使用的,作為子類的EObjectStructuralFeatureValueCondition 當(dāng)然也不會例外。

          EObjectReferenceValueCondition和EObjectAttributeValueConditions十分相似(使用角度),看它們的名稱就很容易猜到它們之間的各自的服務(wù)對象了,前者是針對Reference的,后者是針對Attribute的。看完下面的例子,就完全可以體會到它們基本的用法了。

           

                  ShopFactory shopFactory = ShopFactory.eINSTANCE;
                  
                  Category food 
          = shopFactory.createCategory();
                  food.setName(
          "食物");
                  Category commodity 
          = shopFactory.createCategory();
                  commodity.setName(
          "日用品");
                  Category equipment 
          = shopFactory.createCategory();
                  equipment.setName(
          "體育器材");
                  
                  
          //“人人樂”超市只進(jìn)貨兩種物品,“體育器材”在這里沒有銷售
                  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 三告習(xí)習(xí) 閱讀(1193) 評論(2)  編輯  收藏 所屬分類: emf/gef/gmf

          評論:
          # re: [emf-query與emf-ocl] EMF-Query與EMF-OCL學(xué)習(xí)筆記系列五(Feature Conditions) 2007-06-11 21:05 | 阿南
          好好把EMF總結(jié)一下啊~回來我到你Blog上學(xué)習(xí)學(xué)習(xí)  回復(fù)  更多評論
            
          # re: [emf-query與emf-ocl] EMF-Query與EMF-OCL學(xué)習(xí)筆記系列五(Feature Conditions) [未登錄] 2007-09-07 15:46 | z
          找到有research emf-ocl的真不容易,我遇到一個問題想請教一下,我在ecore中建了兩個node A and B,聚合關(guān)系,就是B是A的attribute,一個relationship,有三個type,single,mulitple,and input。我想讓一個B上只能連一個type類型的relationship.不知道用ocl怎么做限制,是否能做到?急切需要答案。  回復(fù)  更多評論
            
          主站蜘蛛池模板: 全椒县| 东丽区| 宁晋县| 民县| 五家渠市| 昭通市| 城市| 兴海县| 阳高县| 鄂伦春自治旗| 从江县| 华池县| 孟连| 毕节市| 扬中市| 安溪县| 颍上县| 临夏市| 榆中县| 宁海县| 乌鲁木齐市| 昌都县| 鹤壁市| 尼玛县| 昌邑市| 长海县| 郎溪县| 阿合奇县| 二连浩特市| 三穗县| 清水县| 邵武市| 应城市| 拜城县| 台中县| 台南市| 榆中县| 景宁| 额敏县| 措美县| 巨野县|