gradlegroovybuild.gradlegpars

unable to resolve class DataflowVariable on GPars/Groovy


I am a beginner in Groovy/GPars. I am trying to run the example code from the following documentation (http://gpars.org/1.0.0/guide/guide/dataflow.html):

import static groovyx.gpars.dataflow.Dataflow.task
final def x = new DataflowVariable()
final def y = new DataflowVariable()
final def z = new DataflowVariable()

task {
    z << x.val + y.val
}

task {
    x << 10
}

task {
    y << 5
}

println "Result: ${z.val}"

My build.gradle file has the following dependencies/configuration:

    plugins {
    id 'groovy'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.apache.groovy:groovy:4.0.2'
    implementation group: 'org.codehaus.gpars', name: 'gpars', version: '1.0.0' //1.2.1
    implementation "org.codehaus.jsr166-mirror:jsr166y:1.7.0"
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

But when I run my file, I obtain the error "unable to resolve class DataflowVariable". I have tried to change the gpars version to 1.0.0 to correspond with the following documentation: http://gpars.org/1.0.0/guide/guide/dataflow.html

I have no issues with other imports such as "groovyx.gpars.dataflow.stream.DataflowStream", so I don't understand why I have this error for DataflowVariable.


Solution

  • @cfrick's comment was right you were missing the import groovyx.gpars.dataflow.DataflowVariable

    import static groovyx.gpars.dataflow.Dataflow.task
    import groovyx.gpars.dataflow.DataflowVariable
    
    final def x = new DataflowVariable()
    final def y = new DataflowVariable()
    final def z = new DataflowVariable()
    
    task {
        z << x.val + y.val
    }
    
    task {
        x << 10
    }
    
    task {
        y << 5
    }
    
    println "Result: ${z.val}"
    

    Try it in the Groovy Web Console