如何在文件更改时自动更新 EMF ResourceSet?

发布于 2024-10-17 05:11:11 字数 690 浏览 6 评论 0原文

我需要在程序中始终保留某些文件的最新内容。 我创建了一个 EMF 资源集。

由于 resourceSet.getResource(resourceURI, true) 需要花费大量时间才能完成,因此我将资源集存储在静态字段中,以便可以缓存文件。

即,一旦针对某个 URI 调用 resourceSet.getResource(resourceURI, true),该文件就会缓存在 resourceSet 中。

问题是 resourceSet 不会自动更新其缓存:

即:

resourceSet.getResource(resourceURI, true);
// delete resourceURI from file system

// Here I expect null, but old version of the file is returned
resourceSet.getResource(resourceURI, true);

如何强制 resourceSet 在需要时更新缓存?

我正在使用 org.eclipse.emf.ecore.resource.impl.ResourceSetImpl,但我可能需要考虑修改标记的另一个版本的 ResourceSet?

I need to have always up to date content of some file in my program.
I've created an EMF resource set.

Because resourceSet.getResource(resourceURI, true) takes a lot of time to complete I store the resource set in a static field, so files can be cached.

I.e. once resourceSet.getResource(resourceURI, true) is called for some URI the file is cached in resourceSet.

The problem is that resourceSet doesn't update it's cache automatically:

I.e.:

resourceSet.getResource(resourceURI, true);
// delete resourceURI from file system

// Here I expect null, but old version of the file is returned
resourceSet.getResource(resourceURI, true);

How to force resourceSet to update cache if needed?

I'm using org.eclipse.emf.ecore.resource.impl.ResourceSetImpl, but probably I need another version of ResourceSet that takes modification stamps into account?

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

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

发布评论

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

评论(1

毁梦 2024-10-24 05:11:11

有两件事:首先,如果您想重新加载资源,则必须调用

aResource.unload([..]);
aResource.load([..])

由于 EMF 不需要 Eclipse 以任何方式,因此 ResourceResourceSet 类不相同作为 Eclipse 工作区 IResource 子类,这意味着更改文件系统上的文件不会导致 EMF 资源重新加载。

不过,这很容易做到,看看为您生成的 XxxxEditor EMF,该类实例化了一个 IResourceChangeListener,当文件被编辑时,它将从 Eclipse 工作区接收增量。修改的。生成的侦听器通过重新加载资源来处理这些增量。

protected IResourceChangeListener resourceChangeListener = new IResourceChangeListener() {
    public void resourceChanged(IResourceChangeEvent event) {
        IResourceDelta delta = event.getDelta();
        //find out which EMF Resource matches with the IResource and reload it
    }
}

Two things: first if you want to reload the resource, you'll have to call

aResource.unload([..]);
aResource.load([..])

As EMF is not requiring Eclipse in any way the Resource and ResourceSet classes are not the same as the Eclipse workspace IResource subclasses, which mean changing a file on the file system will not cause the EMF Resources to be reloaded.

It's pretty easy to do though, have a look on the XxxxEditor EMF generated for you, the class instantiate a IResourceChangeListener which will receive deltas from the Eclipse workspace when a file is being modified. The generated listener process these deltas by reloading the resources.

protected IResourceChangeListener resourceChangeListener = new IResourceChangeListener() {
    public void resourceChanged(IResourceChangeEvent event) {
        IResourceDelta delta = event.getDelta();
        //find out which EMF Resource matches with the IResource and reload it
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文