Implementation of formula library based on groovy

formula

Formula library based on groovy implementation

Project address

Github

grammar

Formula name (parameter)

For example:

Echo (great hero Wang Bobo)

Formula nesting is supported:

Formula name 1 (formula name 2 (parameter), parameter)

For example:

ECHO(UUID())

Quick start

  1. Create formula object formula
  2. function formula.run("script")

Here is an example:

package tk.fishfish.formula;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Formula test
 *
 * @author byebye disco 
 * @since 1.0
 */
public class FormulaTest {

    private Formula formula;

    @Before
    public void setup() {
        formula = new Formula();
    }

    @Test
    public void lower() {
        Object result = formula.run("LOWER(ABC)");
        Assert.assertEquals("abc", result);
    }

}

Default formula

The following text formula has been realized:

  • UUID() returns uuid
  • LOWER(xxx) to lowercase
  • Uppercase (xxx)

There is no rich formula such as text, time, mathematics and logic, but an extension mechanism is provided for you to customize your own formula library.

Develop your own formula

  • Inherit Plugin interface

    package tk.fishfish.formula.plugin;
    
    import tk.fishfish.formula.annotation.FormulaMapping;
    
    /**
     * Custom formula
     *
     * @author byebye disco 
     * @since 1.0
     */
    public class CustomPlugin implements Plugin {
    
        /**
         * Implement your own formula
         *
         * @param name parameter
         * @return result
         */
        @FormulaMapping("ECHO")
        public String echo(String name) {
            return "echo: " + name;
        }
    
    }
    
    
  • Custom method, add @ FormulaMapping annotation mapping formula name

  • Install plug-in class, FormulaScript.installPlugin(CustomPlugin.class)

    package tk.fishfish.formula;
    
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import tk.fishfish.formula.plugin.CustomPlugin;
    import tk.fishfish.formula.script.FormulaScript;
    
    import java.math.BigDecimal;
    
    /**
     * Formula test
     *
     * @author byebye disco 
     * @since 1.0
     */
    public class FormulaTest {
    
        private Formula formula;
    
        @BeforeClass
        public static void init() {
            // Install your own formula plug-in
            FormulaScript.installPlugin(CustomPlugin.class);
        }
    
        @Before
        public void setup() {
            formula = new Formula();
        }
    
        @Test
        public void plugin() {
            Object result = formula.run("ECHO(xxx)");
            System.out.println(result);
        }
    
    }
    
    

be careful:

  • First install your own Formula, and then create the Formula object
  • Formula name cannot be repeated globally

SPI extension

Except for manual FormulaScript.installPlugin ( CustomPlugin.class )In addition to installing custom formulas, you can also register through SPI.

In the src/main/resources/META-INF/services directory, create the name as tk.fishfish.formula.plugin.Plugin The file contains the full class name of the implementation class

# custom plugin
tk.fishfish.formula.plugin.CustomPlugin

At this time, it will automatically discover the implementation class through SPI mechanism, install it automatically, and realize decoupling.

about

variable

In most cases, business customization formulas contain variables, such as:

Formula name (variable)

In general, variables are replaced at run time.

package tk.fishfish.formula;

import groovy.lang.Binding;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tk.fishfish.formula.plugin.CustomPlugin;
import tk.fishfish.formula.script.FormulaScript;

/**
 * Formula test
 *
 * @author byebye disco 
 * @since 1.0
 */
public class FormulaTest {

    private Formula formula;

    @Before
    public void setup() {
        formula = new Formula();
    }

    @Test
    public void variable() {
        Binding binding = new Binding();
        binding.setVariable("xxx", "ABC");
        // Incoming Binding context
        Object result = formula.run("LOWER(xxx)", binding);
        Assert.assertEquals("abc", result);
    }

}

rely on

What's more, there are dependencies, such as:

  • The formula for value A is: Formula 1(xxx)
  • The formula of value B is: Formula 2(A), note that B depends on the result of A

At this point, there is A dependency relationship, that is, B depends on A

No code is provided here to solve this scenario. You can give a hint here. Dependency can be converted into DAG (directed acyclic graph) calculation.

This part of the code is not open source.

Database formula

This is also a business necessity. It is still not open source.

You can use groovy SQL or JdbcTemplate.

Tags: Java Junit github Database

Posted on Tue, 12 May 2020 11:17:33 -0400 by skkeeper