2015-11-16 27 views
1

我使用xtext創建了一個新的DSL,如下所示。 (其實我將訪問RCP應用的DSL)。xtext中的跨文件引用

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals 

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl" 


Configuration: 
components+=(Component)*; 

Component: 
'Component' name=ID 
'{' 
(('display' display=STRING) & 
('dependency' dependency=[Component|ID])?) 
'}' 
; 

我有兩個文件: sample1.mydsl

Component comp1 { 
    display "comp1" 
    dependency comp2 
} 

sampl2.mydsl

Component comp2 { 
    display "comp2" 
} 

要檢查參考從另一個文件, 我試圖運行一個測試代碼作爲獨立的,但我無法準確得到eobject。

測試代碼

public static final void main(String arg[]) { 
    new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../"); 
    Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration(); 
    XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); 
    resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE); 
    File file=new File("/Users/nuckee/Work/temp/mydsl/sample1.mydsl"); 
    Resource resource = resourceSet.getResource(
      URI.createURI(file.toURI().toString()), true); 

    Configuration config = (Configuration) resource.getContents().get(0); 
    Component comp1 = config.getComponents().get(0); 
    if (comp1 != null) { 
     System.out.println("configuration displayed name : " + comp1.getDisplay()); 
     Component dep = comp1.getDependency() ; 
     if (dep != null) { 
      System.out.println("dep : " + dep); 
      System.out.println("dep displayed name : " + dep.getDisplay()); 
     } 
    } 
} 

結果

configuration displayed name : comp1 
    dep : [email protected] (eProxyURI: file:/Users/nuckee/Work/temp/mydsl/sample1.mydsl#|0) 
    dep displayed name : null 

如何能準確地我得到 「COMP2」 從另一個文件顯示?

我希望有人能幫我解決它。謝謝。

+0

你必須自己加載所有相關的文件到資源集 –

+0

@Christian Dietrich我沒有加載所有文件。 ;;它現在運作良好。謝謝。 – user2663410

回答

0

您必須將所有模型文件加載到資源集中。 Xtext不會自動發現文件