我已经成功地创建了一个GMF编辑器,该编辑器根据我的EMF模型绘制模型。我想做的是通过模型的Eclasses迭代。可以通过我的插件代码在运行时实现这一点,而无需读取GMF Editor的XML文件创建?是否有EMF的API?

有帮助吗?

解决方案

当您从GenModel文件生成测试代码时,然后在xyz.test插件内部,我正在搜索这样的代码。它穿过模型的XMI文件

// Create a resource set to hold the resources.
    //
    ResourceSet resourceSet = new ResourceSetImpl();

    // Register the appropriate resource factory to handle all file extensions.
    //
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
        (Resource.Factory.Registry.DEFAULT_EXTENSION, 
         new XMIResourceFactoryImpl());

    // Register the package to ensure it is available during loading.
    //
    resourceSet.getPackageRegistry().put
        (XYZmetamodelPackage.eNS_URI, 
         XYZmetamodelPackage.eINSTANCE);

    // If there are no arguments, emit an appropriate usage message.
    //
    if (args.length == 0) {
        System.out.println("Enter a list of file paths or URIs that have content like this:");
        try {
            Resource resource = resourceSet.createResource(URI.createURI("http:///My.metamodel"));
            ModelObject root = atagmetamodelFactory.eINSTANCE.createModelObject();
            resource.getContents().add(root);
            resource.save(System.out, null);
        }
        catch (IOException exception) {
            exception.printStackTrace();
        }
    }
    else {
        // Iterate over all the arguments.
        //
        for (int i = 0; i < args.length; ++i) {
            // Construct the URI for the instance file.
            // The argument is treated as a file path only if it denotes an existing file.
            // Otherwise, it's directly treated as a URL.
            //
            File file = new File(args[i]);
            URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()): URI.createURI(args[i]);

            try {
                // Demand load resource for this file.
                //
                Resource resource = resourceSet.getResource(uri, true);
                System.out.println("Loaded " + uri);

                // Validate the contents of the loaded resource.
                //
                for (EObject eObject : resource.getContents()) {
                    Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
                    if (diagnostic.getSeverity() != Diagnostic.OK) {
                        printDiagnostic(diagnostic, "");
                    }
                }
            }
            catch (RuntimeException exception) {
                System.out.println("Problem loading " + uri);
                exception.printStackTrace();
            }
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top