gpt4 book ai didi

java - 文本文件解析 - 使用 mwe2 (Xtext) 实例化 Java Bean

转载 作者:行者123 更新时间:2023-12-02 12:47:21 27 4
gpt4 key购买 nike

我有要解析的文本文件(即转换为 Java Bean 实例),

形成所需的 XText 语法。

我的 XText 语法:

grammar org.eclipse.xtext.example.Domainmodel with org.eclipse.xtext.common.Terminals

generate domainmodel "http://www.eclipse.org/xtext/example/Domainmodel"

Model:
greetings+=Greeting*;

Greeting:
'Hello' name =ID '!' ;

能够生成 Greeting 和 Model Java Bean 类,通过运行“生成 XText Artifacts” 生成的 Bean 类如下所示

Greeting.java

package org.eclipse.xtext.example.domainmodel;

import org.eclipse.emf.ecore.EObject;

public interface Greeting extends EObject
{
String getName();
void setName(String value);

}

模型.java

package org.eclipse.xtext.example.domainmodel;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;

public interface Model extends EObject
{
EList<Greeting> getGreetings();
}

GenerateDomainmodel.mwe2 已生成。

能够使用.genmodel生成资源,如下面的Main(Partial)类所示

package org.eclipse.xtext.example;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.tools.Diagnostic;

import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage;
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
import org.eclipse.emf.codegen.ecore.genmodel.impl.GenModelImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;
import org.eclipse.emf.mwe2.language.mwe2.Mwe2Package;
import org.eclipse.emf.mwe2.runtime.workflow.IWorkflow;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.example.domainmodel.DomainmodelFactory;
import org.eclipse.xtext.example.domainmodel.Greeting;
import org.eclipse.xtext.example.domainmodel.Model;
import org.eclipse.xtext.example.domainmodel.impl.DomainmodelFactoryImpl;
import org.eclipse.xtext.example.domainmodel.impl.DomainmodelPackageImpl;
import org.eclipse.xtext.example.domainmodel.impl.GreetingImpl;

public class Main {

public static void main(String args[]){

new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = new ResourceSetImpl();
rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("genmodel", new XMIResourceFactoryImpl());

EPackage.Registry.INSTANCE.put(GenModelPackage.eNS_URI, GenModelPackage.eINSTANCE);

File f = new File("/data/TASKS/4. 2016-2017_PS1/XTEXT/ECLIPSE WORKSPACE 1/org.eclipse.xtext.example.domainmodel/model/generated/Domainmodel.genmodel");
System.out.println(f.canRead());
Resource resource = rs.getResource(URI.createURI(f.toURI().toString()), true);
}
}

完成所有这些后,如何使用以下代码生成 Greeting 类实例文本输入文件

Hello Ramasamy !
Hello Govinda Govinda !
Hello Kuppusamy !
Hello Karuppusamy !

我需要模型实例,其中包含 4 个问候语实例,并使用以下值作为名称。

  1. 问候实例1 -> 名称 ("Ramasamy ")
  2. 问候语实例1 -> name ("Govinda Govinda ")
  3. 问候语实例1 -> 名称 ("Kuppusamy ")
  4. 问候语实例1 -> 名称 ("Karuppusamy ")

我的主程序必须使用上面的文本输入文件生成 Greeting 实例,有人可以帮我解决这个问题

或者还有其他方法可以实现吗?

最佳答案

这是这样一个主要的示例

import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.Model;

import com.google.inject.Injector;

public class Main {

public static void main(String[] args) throws IOException {
Injector i = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = i.getInstance(ResourceSet.class);
Resource r = rs.getResource(URI.createFileURI("./test.mydsl"), true);
r.load(null);
Model m = (Model) r.getContents().get(0);
for (Greeting g : m.getGreetings()) {
System.out.println(g.getName());
}
}

}

更新为您的代码,看起来像

import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.example.DomainmodelStandaloneSetup;
import org.eclipse.xtext.example.domainmodel.Greeting;
import org.eclipse.xtext.example.domainmodel.Model;

import com.google.inject.Injector;

public class Main {

public static void main(String[] args) throws IOException {
Injector i = new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = i.getInstance(ResourceSet.class);
Resource r = rs.getResource(URI.createFileURI("./src/mydsl.dmodel"), true);
r.load(null);
Model m = (Model) r.getContents().get(0);
for (Greeting g : m.getGreetings()) {
System.out.println(g.getNamee());
}
}

}

关于java - 文本文件解析 - 使用 mwe2 (Xtext) 实例化 Java Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44716914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com