java操作R、aviator、easy-rules

一、JAVA操作R

        <dependency>
            <groupId>org.rosuda.REngine</groupId>
            <artifactId>Rserve</artifactId>
            <version>1.8.1</version>
        </dependency>
package com.citydo.faceadd.rserve;


import lombok.extern.slf4j.Slf4j;
import org.rosuda.REngine.Rserve.RConnection;

/**
 * @author nick
 */
@Slf4j
public class JavaR {

    public static void main(String[] args) {
        RConnection recon = null;
        try {
            // 建立与 Rserve 的连接
            recon = new RConnection("127.0.0.1");

            // eval() 函数用于令 R 执行 R 语句
            // 此处制造了一个 m1 矩阵
            recon.eval("m1 = matrix(c(1, 2, 3, 4), 2, 2, byrow=TRUE)");

            // solve() 函数在 R 中求 m1 矩阵的逆矩阵
            // 并将结果返回,asDoubleMatrix 函数可以将数据转换成
            // Java 中的 double 二维数组以表示矩阵
            double[][] m1 = recon.eval("solve(m1)").asDoubleMatrix();
            // 输出矩阵的内容
            for (int i = 0; i < m1.length; i++) {
                for (int j = 0; j < m1[0].length; j++) {
                    System.out.print(m1[i][j] + "\t");
                }
            }
        } catch (Exception e) {
           log.info("与Rserve的连接失败:{}",e);
        } finally {
            if (recon != null) {
                recon.close();
            }
        }
    }
}

二、aviator

        <dependency>
            <groupId>com.googlecode.aviator</groupId>
            <artifactId>aviator</artifactId>
            <version>5.1.0</version>
        </dependency>
package com.citydo.faceadd.rule;

import com.googlecode.aviator.AviatorEvaluator;
import java.util.HashMap;
import java.util.Map;

/**
 * 高性能、轻量级的java语言实现的表达式求值引擎
 * 仅支持逻辑表达式、算术表达式、三元表达式和正则匹配
 * @author nick
 */
public class Aviator {

    public static void main(String[] args) {
        Long result = (Long) AviatorEvaluator.execute("1*2*3");
        //算术表达式
        System.out.println(result);
        //逻辑表达式
        Boolean result2 = (Boolean)AviatorEvaluator.execute("3>1 && 2!=4 || true");
        System.out.println(result2);
        //变量和字符串相加
        Map env = new HashMap();
        env.put("yourname","aviator");
        String result3 = (String)AviatorEvaluator.execute(" 'hello ' + yourname ", env);
        System.out.println(result3);
        //三元表达式
        String result5=(String)AviatorEvaluator.execute("3>0? 'yes':'no'");
        System.out.println(result5);
        //函数
        AviatorEvaluator.execute("string.length('hello')");    //求字符串长度
        AviatorEvaluator.execute("string.contains('hello','h')");  //判断字符串是否包含字符串AviatorEvaluator.execute("string.startsWith('hello','h')");  //是否以子串开头AviatorEvaluator.execute("string.endsWith('hello','llo')");是否以子串结尾
        AviatorEvaluator.execute("math.pow(-3,2)");   //求n次方
        AviatorEvaluator.execute("math.sqrt(14.0)");   //开 平方根
        AviatorEvaluator.execute("math.sin(20)");    //正弦函数
    }
}

三、easy-rules

        <dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-rules-core</artifactId>
            <version>4.0.0</version>
        </dependency>
package com.citydo.faceadd.rule;

import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.api.RulesEngineParameters;
import org.jeasy.rules.core.DefaultRulesEngine;

/**
 * easy rules java规则引擎
 * @author nick
 */
public class EasyRules {


    /**
     * 规则一
     */
    @Rule
    public static class FizzRule1 {

        @Condition
        public boolean isFizz1(@Fact("number") Integer number) {
            return number == 1;
        }

        @Action
        public void printFizz1() {
            System.out.print("--------1---------------");
        }
    }

    /**
     * 规则二
     */
    @Rule
    public static class FizzRule2 {

        @Condition
        public boolean isFizz2(@Fact("number") Integer number) {
            return number == 2;
        }

        @Action
        public void printFizz2() {
            System.out.print("------------2------------");
        }
    }


    public static void main(String[] args) {
        //初始化规则
        RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true);
        RulesEngine fizzBuzzEngine = new DefaultRulesEngine(parameters);

        //创建规则
        Rules rules = new Rules();
        rules.register(new FizzRule1());
        rules.register(new FizzRule2());

        // 执行规则
        Facts facts = new Facts();
        facts.put("number", 2);
        fizzBuzzEngine.fire(rules, facts);
    }
}

你可能感兴趣的:(JAVA开发,练习案例,基础积累)