引用 xText 中 Java 项目的源代码
Referring to source code of Java project in xText
我正在 xText 中创建 DSL,用于对应用程序的功能行为进行建模。我的目标是将资源需求(例如 CPU 周期数、硬盘驱动器上的写入操作)与我想要使用 DSL 建模的功能行为相结合。 DSL 是使用 Eclipse IDE 在 xText 中编写的。可以在下面找到包含注释的 DSL 语法。
现在它是一个非常简单的 DSL 来模拟功能行为;组合 if/else 和 for 语句并向它们添加 libraryFunctions。我自己想出了后一个术语;它用于指代作为我的功能行为的基本步骤的操作(例如登录、加密、显示;您可以将它们视为编程语言中的方法)。现在我想通过选项扩展我的 DSL,以便能够引用 Java 项目的源代码。我创建了一个小的 Java 程序,它类似于带有登录屏幕和创建帐户屏幕的基本程序(参见下面的 class 图)。为了使用 DSL 对该程序的功能行为进行建模,我希望能够引用 Java 程序源代码的某些细节,以便我可以直接从源代码中提取这些细节并将其用于数字用户线。例如;假设我想引用 Java 程序中使用的某些方法。现在我的 DSL 中有简单的枚举 'libraryFunctionsEnum',但如果我能以某种方式直接引用 Java 程序源代码中使用的方法就好了(这样当我编译 DSL 时并使用它,xText 编辑器会自动提供我可以参考的可用方法列表。
我尝试使用 ecore 模型转换我的 Java 项目的 class 图表并将它们集成到 xText 中,但我感觉我有点不知所措。我还研究了 xBase 和 xTend(两种旨在使 xText 与 Java 更具互操作性的语言),但到目前为止,我发现它们更侧重于从 xText 模型自动生成 Java 源代码。我想反过来做(参考外部项目的 Java 源代码,以便我可以在我的 DSL 中使用这些参考)。我不知道我上面提到的方法(ecore、xBase、xTend)是否是实现我想要的目标的正确方法。如果您有更好的想法或解释,那么我很高兴听到!
顺便说一下,我对 xText 和 DSL modelling/DSL 开发还是个新手。我可能忘记了一些重要的 details/explanations。如果您遗漏了什么,请告诉我。
grammar org.xtext.example.mydsl.FinalDsl with org.eclipse.xtext.common.Terminals
generate finalDsl "http://www.xtext.org/example/mydsl/FinalDsl"
Model:
'functionName' name = STRING
functions += FunctionElements*
;
// Function elements of which the model exists. The model can contain
// library functions, for loops, and if/else statements.
FunctionElements:
(
functions += libraryFunctionsEnum |
forLoops += ForLoops |
ifElseStatements += IfElseStatements
)
;
// IfElse Statements requiring if statements and optionally followed by
// one else statement.
IfElseStatements:
ifStatements += IfStatements
(elseStatement = ElseStatement)?
;
// If statements requiring conditions and optionally followed by
// library functions or for loops.
IfStatements:
'if'
conditions = Conditions
(ifFunctions += libraryFunctionsEnum | forLoops += ForLoops)
;
// Else statement requiring one or multiple library functions.
ElseStatement:
'else' elseFunctions += libraryFunctionsEnum
;
// For loops requiring one condition and followed by zero or more
// library functions
ForLoops:
'for'
conditions = Conditions
libraryFunctions += libraryFunctionsEnum*
;
//*Eventually filled with details from class diagram, but for now we manually fill it for the sake of testing.
enum libraryFunctionsEnum:
createAccount='createInstance'|
login='login'|
hasCode= 'encrypt'|
display='display'
;
Conditions:
STRING
operator=logicalOperators
STRING
;
enum logicalOperators:
greaterThan='>'|
smallerThan='<'|
greaterOrEqualThan='=>'|
smallerOrEqualThan='<='|
equalTo='=='
;
Java Class 图:
我觉得你想要达到的效果并不容易。不过,一些想法:
- 您的 "libraryFunctions" 可以 link 到 Java 个元素通过 Java 反射 API 获得。我想您会以某种方式导入一些“.java”或“.class”文件,并通过反射引用其方法或 Class 对象。
- 相反,也许有可能,但我真的不确定,引用 Eclipse JDT 工具提供的元素,这样您就可以引用 Java 源文件中的元素并轻松地 link 到它(例如,用 ctrl + 左键单击打开它的编辑器)。或者您可能需要找出如何从通过反射 API 检索到的元素中,您将能够 link 到源 Java 代码(如果可能的话)。
我正在 xText 中创建 DSL,用于对应用程序的功能行为进行建模。我的目标是将资源需求(例如 CPU 周期数、硬盘驱动器上的写入操作)与我想要使用 DSL 建模的功能行为相结合。 DSL 是使用 Eclipse IDE 在 xText 中编写的。可以在下面找到包含注释的 DSL 语法。
现在它是一个非常简单的 DSL 来模拟功能行为;组合 if/else 和 for 语句并向它们添加 libraryFunctions。我自己想出了后一个术语;它用于指代作为我的功能行为的基本步骤的操作(例如登录、加密、显示;您可以将它们视为编程语言中的方法)。现在我想通过选项扩展我的 DSL,以便能够引用 Java 项目的源代码。我创建了一个小的 Java 程序,它类似于带有登录屏幕和创建帐户屏幕的基本程序(参见下面的 class 图)。为了使用 DSL 对该程序的功能行为进行建模,我希望能够引用 Java 程序源代码的某些细节,以便我可以直接从源代码中提取这些细节并将其用于数字用户线。例如;假设我想引用 Java 程序中使用的某些方法。现在我的 DSL 中有简单的枚举 'libraryFunctionsEnum',但如果我能以某种方式直接引用 Java 程序源代码中使用的方法就好了(这样当我编译 DSL 时并使用它,xText 编辑器会自动提供我可以参考的可用方法列表。
我尝试使用 ecore 模型转换我的 Java 项目的 class 图表并将它们集成到 xText 中,但我感觉我有点不知所措。我还研究了 xBase 和 xTend(两种旨在使 xText 与 Java 更具互操作性的语言),但到目前为止,我发现它们更侧重于从 xText 模型自动生成 Java 源代码。我想反过来做(参考外部项目的 Java 源代码,以便我可以在我的 DSL 中使用这些参考)。我不知道我上面提到的方法(ecore、xBase、xTend)是否是实现我想要的目标的正确方法。如果您有更好的想法或解释,那么我很高兴听到!
顺便说一下,我对 xText 和 DSL modelling/DSL 开发还是个新手。我可能忘记了一些重要的 details/explanations。如果您遗漏了什么,请告诉我。
grammar org.xtext.example.mydsl.FinalDsl with org.eclipse.xtext.common.Terminals
generate finalDsl "http://www.xtext.org/example/mydsl/FinalDsl"
Model:
'functionName' name = STRING
functions += FunctionElements*
;
// Function elements of which the model exists. The model can contain
// library functions, for loops, and if/else statements.
FunctionElements:
(
functions += libraryFunctionsEnum |
forLoops += ForLoops |
ifElseStatements += IfElseStatements
)
;
// IfElse Statements requiring if statements and optionally followed by
// one else statement.
IfElseStatements:
ifStatements += IfStatements
(elseStatement = ElseStatement)?
;
// If statements requiring conditions and optionally followed by
// library functions or for loops.
IfStatements:
'if'
conditions = Conditions
(ifFunctions += libraryFunctionsEnum | forLoops += ForLoops)
;
// Else statement requiring one or multiple library functions.
ElseStatement:
'else' elseFunctions += libraryFunctionsEnum
;
// For loops requiring one condition and followed by zero or more
// library functions
ForLoops:
'for'
conditions = Conditions
libraryFunctions += libraryFunctionsEnum*
;
//*Eventually filled with details from class diagram, but for now we manually fill it for the sake of testing.
enum libraryFunctionsEnum:
createAccount='createInstance'|
login='login'|
hasCode= 'encrypt'|
display='display'
;
Conditions:
STRING
operator=logicalOperators
STRING
;
enum logicalOperators:
greaterThan='>'|
smallerThan='<'|
greaterOrEqualThan='=>'|
smallerOrEqualThan='<='|
equalTo='=='
;
Java Class 图:
我觉得你想要达到的效果并不容易。不过,一些想法:
- 您的 "libraryFunctions" 可以 link 到 Java 个元素通过 Java 反射 API 获得。我想您会以某种方式导入一些“.java”或“.class”文件,并通过反射引用其方法或 Class 对象。
- 相反,也许有可能,但我真的不确定,引用 Eclipse JDT 工具提供的元素,这样您就可以引用 Java 源文件中的元素并轻松地 link 到它(例如,用 ctrl + 左键单击打开它的编辑器)。或者您可能需要找出如何从通过反射 API 检索到的元素中,您将能够 link 到源 Java 代码(如果可能的话)。