Simple ChemicalTerms evaluator

    
    /*
    * Evaluate Chemical Terms expression
    * 
    * For API example see  
    * https://www.chemaxon.com/jchem/doc/dev/java/api/chemaxon/jep/Evaluator.html
    *
    * Usage:
    * 1. Edit the name of the Structure field
    * 2. Set your ChemTerms expression
    * 3. Run button script
    *
    * @author David Pech <dpech@chemaxon.com>
    */
    import com.im.df.api.*
    import chemaxon.struc.Molecule
    import chemaxon.jep.*
    import chemaxon.jep.context.MolContext
    init = { widget ->
    }
    destroy = { widget ->
    }
    evaluate = { widget ->
        def ety = dataTree.rootVertex.entity // assumes you have reference to the data tree
        def edp = ety.schema.dataProvider.getEntityDataProvider(ety)
        def molFld = ety.fields.items.find { it.name == 'Structure' } // find the structure field
        def rs = ety.schema.dataProvider.getDefaultResultSet(dataTree, false, DFEnvironmentRO.DEV_NULL) // find the ResultSet
        def rootVS = rs.getVertexState(dataTree.rootVertex) // obtain the VertexState
    
        List ids = rootVS.getSelectedRowsIds() // get the selected IDs
        if (ids.size == 1) {
            Map rows = rootVS.getData(ids, DFEnvironmentRO.DEV_NULL) // get the data
            Map row = rows[ids[0]] // get the first and only row
            MarvinStructure mol = row[molFld.id] // Get the Structure. Its a com.im.df.api.chem.MarvinStructure instance
            Molecule cxnMol = mol.getNative() // obtain the chemaxon.struc.Molecule instance
    
            // create Evaluator and create
            // ChemJEP and compile ChemTerms expression
            Evaluator evaluator = new Evaluator()
            ChemJEP chemJEP = evaluator.compile("logP()")
    
            // create context and parse current molecule
            MolContext context = new MolContext()
            context.setMolecule(cxnMol)
            // evaluate and print out the result from context
            result = chemJEP.evaluate(context)
            println result
    
        } else {
            println "bad selection"
        }
    }
    on_change = { widget, button ->
    
    }