如何在 Java 中以编程方式合并 EMF 模型?

发布于 2024-10-15 03:35:18 字数 401 浏览 8 评论 0原文

有没有办法在 Java 中以编程方式将多个 Ecore 模型(2 个或更多)组合到一个 Ecore 模型中?所有模型都符合相同的元模型。

输入:

Model1 conforming to metamodelX
Model2 conforming to metamodelX
model3 conforming to metamodelX
model4 conforming to metamodelX
model5 conforming to metamodelX

输出:

modelOut conforming to metamodelX and merge of Model1, Model2, model3, model4, model5 ...

Is there a way to combine multiple Ecore models (2 or more) in a single Ecore model programmatically in Java? With all models conform to the same metamodel.

In:

Model1 conforming to metamodelX
Model2 conforming to metamodelX
model3 conforming to metamodelX
model4 conforming to metamodelX
model5 conforming to metamodelX

Out:

modelOut conforming to metamodelX and merge of Model1, Model2, model3, model4, model5 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2024-10-22 03:35:18

有一个用于处理 EMF 比较和合并的 Eclipse 项目,称为 EMF Compare。

这是他们提供的示例:

// Loading models
EObject model1 = ModelUtils.load(model1, resourceSet);
EObject model2 = ModelUtils.load(model2, resourceSet);

// Matching model elements
MatchModel match = MatchService.doMatch(model1, model2, Collections.<String, Object> emptyMap());
// Computing differences
DiffModel diff = DiffService.doDiff(match, false);
// Merges all differences from model1 to model2
List<DiffElement> differences = new ArrayList<DiffElement>(diff.getOwnedElements());
MergeService.merge(differences, true);

这确实提供了非常好的方法来处理模型合并和其他比较内容。您也可以手动完成更改。

这是他们提供的完整示例: 此处

There is Eclipse project for handling EMF comparing and Merging, called EMF Compare.

Here is example provided by them:

// Loading models
EObject model1 = ModelUtils.load(model1, resourceSet);
EObject model2 = ModelUtils.load(model2, resourceSet);

// Matching model elements
MatchModel match = MatchService.doMatch(model1, model2, Collections.<String, Object> emptyMap());
// Computing differences
DiffModel diff = DiffService.doDiff(match, false);
// Merges all differences from model1 to model2
List<DiffElement> differences = new ArrayList<DiffElement>(diff.getOwnedElements());
MergeService.merge(differences, true);

This really provides very good ways to handle model merging and other compare stuffs. You can also manually go through the changes.

Here is full example provided by them: Here

病女 2024-10-22 03:35:18

您需要定义“合并”对您意味着什么。您可以轻松地将所有 EMF 模型附加到同一资源并序列化它们。

您可能希望在 model1 和 model2 之间建立等效关系。找到 model1 和 model2 之间相等的一些对象。
在此之后,您可以找到差异。

例如:

Model1 is a FARM with serial number 33829. Children: 2 CHICKENS, 3 EGGS and 1 PIG
Model2 is a FARM with serial number 33829. Children: 4 CHICKENS, 3 EGGS and 1 PIG

匹配步骤建立以下等价关系:

Model1->FARM = Model2->FARM   because serial number is equal
all other entities have not been matched

此步骤之后是差异步骤:

REMOVED: 2 CHICKENS, 3 EGGS, 1 PIG
ADDED: 4 CHICKENS, 3 EGGS, 1 PIG

使用这些差异,您可以将它们应用到你的模型。仅应用“ADDED”差异将为您提供以下模型:

Model1+2 is a FARM with serial number 33829. Children: 2 CHICKENS, 3 EGGS, 1 PIG, 4 CHICKENS, 3 EGGS, 1 PIG

由您来确定“合并”的业务规则。您首先必须确定两个实体何时相同(匹配)。这可以基于唯一的键、它们在树中的位置,或者基于许多其他事物,具体取决于您的元模型。

结果,您将得到一个“差异”列表。由您来定义要应用哪些差异。

如果您将“合并”视为 SVN 合并(即 Model1 = Model0 + 更改,Model2 = Model0 + 其他更改),则 MergeService 已包含执行此操作的所有业务规则。

You will need to define what 'merge' means to you. You can easily attach all EMF models to the same resource and serialize them.

You will probably want to establish equivalencies between model1 and model2. Find some objects which are equal between model1 and model2.
After this, you can find the differences.

As an example:

Model1 is a FARM with serial number 33829. Children: 2 CHICKENS, 3 EGGS and 1 PIG
Model2 is a FARM with serial number 33829. Children: 4 CHICKENS, 3 EGGS and 1 PIG

The matching step establishes the following equivalencies:

Model1->FARM = Model2->FARM   because serial number is equal
all other entities have not been matched

After this step comes the differences step:

REMOVED: 2 CHICKENS, 3 EGGS, 1 PIG
ADDED: 4 CHICKENS, 3 EGGS, 1 PIG

Using those differences, you can apply them to your model. Applying only the 'ADDED' difference gives you the following model:

Model1+2 is a FARM with serial number 33829. Children: 2 CHICKENS, 3 EGGS, 1 PIG, 4 CHICKENS, 3 EGGS, 1 PIG

It's up to you to determine the business rules of 'merging'. You will first have to determine when two entities are the same (matching). This can be based on a unique key, on their place in the tree, or based on a lot of other things, depending on your metamodel.

As a result, you will have a list of 'differences'. It's up to you to define which differences to apply.

If you see 'merge' as an SVN Merge (i.e. Model1 = Model0 + changes, Model2 = Model0 + other changes), then the MergeService already contains all business rules to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文