groovy 使用构建脚本(gradle)运行UnetStack模拟

dsekswqp  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(40)

我希望使用构建工具(gradle)运行Unet模拟,我试图复制这篇UnetStack博客文章,其中给出了使用IntelliJ IDE运行模拟的描述:https://blog.unetstack.net/using-idea-with-unetstack
当我使用IntelliJ运行示例时,示例代码在日志文件中打印“Hello world”,并让shell代理运行,正如预期的那样。当我编译Unet并使用gradle插入模拟脚本时,Unet shell代理在模拟启动后启动关机。

最小工作示例。--

通过运行“gradle init”并输入以下内容来启动gradle groovy应用程序项目:application -> groovy -> no -> groovy -> helloworld-> helloworld -> 21 -> no。
根项目结构现在是

app/
gradle/
.gradle/
gradlew 
gradlew.bat
settings.gradle
.gitattributes
.gitignore

字符串
下载Unet(https://unetstack.net/)并将“unet-3.4.0”从“unet-community-3.4.0/”复制到项目根目录,并创建文件夹“app/logs/"。
删除“app/src/main/groovy/helloworld/App.groovy”和“app/src/test/groovy/helloworld/AppTest.groovy”。
将“AwesomeAgent.groovy”和“simple_sim.groovy”添加到“app/src/main/groovy/helloworld/",包含以下代码(复制自博客文章):
AwesomeAgent.groovy

import org.arl.fjage.Message
import org.arl.unet.UnetAgent

class AwesomeAgent extends UnetAgent {
    void setup() {
        log.info("Hello world!")
    }

    void startup() {
        // this method is called just after the stack is running
        // look up other agents and services here, as needed
        // subscribe to topics of interest here, to get notifications
    }

    Message processRequest(Message msg) {
        // process requests supported by the agent, and return responses
        // if request is not processed, return null
        return null
    }

    void processMessage(Message msg) {
        // process other messages, such as notifications here
    }

}


simple_sim.groovy

import org.arl.fjage.RealTimePlatform
import org.arl.unet.link.ReliableLink
import org.arl.unet.sim.channels.ProtocolChannelModel

platform = RealTimePlatform
channel.model = ProtocolChannelModel

simulate {
    node '1', address: 1, location: [0, 0, 0], shell: true, stack: { container ->
        container.add 'da', new AwesomeAgent()
        container.add 'link', new ReliableLink()
    }
}


将**“app/build.gradle”**的内容替换为

plugins {
    // Apply the groovy Plugin to add support for Groovy.
    id 'groovy'
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'
}
java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}
repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}
dependencies {
    // Resolve unet version (folder name) and add library files to project ---------
    String unet_dir=''
    String root_dir=((String)projectDir)+'/..' // Project root
    new File(root_dir).eachFile { file -> 
        if ( file.name.contains('unet') ){
            unet_dir=file.name
        }
    }
    if (unet_dir==''){
        println 'unet is missing in project root directory'
    }
    String unet_lib=unet_dir+'/lib' 
    // Add all jar files from unet except groovy 
    println 'looking for unet jars in: ' + root_dir+'/'+unet_lib
    new File(root_dir+'/'+unet_lib).eachFile { file ->
        if ( file.name.contains('.jar') && !file.name.contains('groovy') ){
        implementation files('../'+unet_lib+'/'+file.name) // Relative to this build script
        }
    }
    // Use the latest Groovy version for building this library
    implementation libs.groovy.all
    // This dependency is used by the application.
    implementation libs.guava
    // Use the awesome Spock testing and specification framework even with Java
    testImplementation libs.spock.core
    testImplementation libs.junit
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}
application { // "gradle run"
    // Define the main class for the application (using run).
    mainClass = 'org.arl.fjage.shell.GroovyBoot' // Boots sim script as input
}
run {
    standardInput = System.in
    // Insert these arguments into the simulator
    args=["cls://org.arl.unet.sim.initrc", "src/main/groovy/helloworld/simple_sim.groovy"]
}


与博客文章的不同之处在于,构建脚本找到unet/lib jar文件并将其作为实现依赖项添加,并且文件输入在运行配置中定义。
当我运行上面的例子(从项目根目录运行“gradle run”)时,unet成功地加载了AwesomeAgent并在终端中打印了一次shell提示符“>”;然而,shell代理随后意外地启动了关机。以下是日志文件的内容:

1704206651962|INFO|org.arl.fjage.shell.GroovyBoot@1:main|fjage Build: fjage-1.9.1/37c49259/5-09-2021_13:26:12
1704206652270|INFO|org.arl.fjage.shell.GroovyBoot@1:main|Running cls://org.arl.unet.sim.initrc
1704206652273|INFO|org.arl.fjage.remote.EnumTypeAdapter@1:<clinit>|Groovy detected, using GroovyClassLoader
1704206652280|INFO|org.arl.fjage.remote.MessageAdapterFactory@1:<clinit>|Groovy detected, using GroovyClassLoader
1704206652313|INFO|org.arl.fjage.shell.GroovyBoot@1:main|Running src/main/groovy/helloworld/simple_sim.groovy
1704206652419|INFO|org.arl.unet.nodeinfo.NodeInfo@1:setAddress|Node address changed to 1
1704206652423|INFO|Script1@1:call|Starting console shell
1704206652496|INFO|Script1@1:doInvoke|Created static node 1 (1) @ [0, 0, 0]
1704206652506|INFO|Script1@1:doInvoke| --- BEGIN SIMULATION #1 ---
1704206652508|INFO|org.arl.unet.sim.SimulationContainer@1:init|Initializing agents...
1704206652508|INFO|org.arl.fjage.shell.ShellAgent/1@33:init|Agent shell init
1704206652508|INFO|AwesomeAgent/1@34:init|Loading agent da [AwesomeAgent] on 1
1704206652509|INFO|AwesomeAgent/1@34:call|Hello world!
1704206652509|INFO|org.arl.unet.sim.SimulationAgent/1@38:doInvoke|Loading simulator : SimulationAgent
1704206652509|INFO|org.arl.unet.nodeinfo.NodeInfo/1@37:init|Loading agent node v3.4.0/93008cfa/30-10-2021_02:33:22 [org.arl.unet.nodeinfo.NodeInfo] on 1
1704206652509|INFO|org.arl.unet.link.ReliableLink/1@36:init|Loading agent link v3.4.0/93008cfa/30-10-2021_02:33:22 [org.arl.unet.link.ReliableLink] on 1
1704206652509|INFO|org.arl.unet.sim.HalfDuplexModem/1@35:init|Loading agent phy v3.4.0/93008cfa/30-10-2021_02:33:22 [org.arl.unet.sim.HalfDuplexModem] on 1
1704206652609|INFO|org.arl.unet.sim.SimulationContainer@1:init|Agents ready...
1704206652609|INFO|org.arl.unet.sim.SimulationContainer@1:start|Starting container...
1704206652610|INFO|org.arl.unet.link.ReliableLink/1@36:startup|No PHY specified, auto detecting...
1704206652610|INFO|org.arl.unet.nodeinfo.NodeInfo/1@37:obtainAddress|Node name is 1, address is 1, address size is 8 bits
1704206652610|INFO|org.arl.unet.link.ReliableLink/1@36:startup|Using agent 'phy' for PHY
1704206652610|INFO|org.arl.unet.link.ReliableLink/1@36:startup|No MAC specified, auto detecting...
1704206652610|INFO|org.arl.unet.link.ReliableLink/1@36:startup|No MAC detected, continuing without MAC
1704206652614|INFO|org.arl.fjage.shell.ShellAgent/1@33:shutdown|Agent shell shutdown
1704206652614|INFO|org.arl.unet.sim.SimulationContainer@43:shutdown|Initiating shutdown...
1704206652614|INFO|org.arl.unet.sim.SimulationContainer@43:shutdown|All agents have shutdown


由于我在“simple_sim.groovy”中使用了platform = RealTimePlatform,所以模拟不应该终止。如果您能帮助解决这个问题,我将不胜感激。

nukf8bse

nukf8bse1#

run任务不会将您的终端连接到已启动的进程。
因此,我猜启动的进程看到了stdin的结尾,因此终止了。
如果配置

tasks.run {
    standardInput = System.in
}

字符串
它应该最有可能像你想的那样工作。

相关问题