欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

范式理论的程序设计(四)

程序员文章站 2022-05-08 11:23:57
...

关于数据库范式理论的程序设计 - ArmstrongUtils

这里我使用的工具类全部都是使用接口,public interface ArmstrongUtils extends CollectionUtils是关于Armstrong公理的工具,因为后面算法都不用就算F+,所有这些性质都很少会直接用到重点内容

Utils工具的继承关系

范式理论的程序设计(四)

ArmstrongUtils接口

import com.ruoxing.dbs.bean.FunctionalDependency;

/**
 * @author :ruoxing
 * @type :Interface
 * @date :2018年5月
 * @description :Armstrong公理
 */
public interface ArmstrongUtils extends CollectionUtils {
}

范式理论的程序设计(四)

自反律(reflexivity)

1. 方法代码

    /**
     * 描述:自反律: β包含于α  => α → β
     * @param sequenceA α
     * @param sequenceB β
     * @return FunctionalDependency  α → β
     */
    default FunctionalDependency reflexivity(CharSequence sequenceA, CharSequence sequenceB) {
        if (contains(sequenceA, sequenceB)) {
            return new FunctionalDependency(sequenceA, sequenceB);
        }
        return null;
    }

2. 测试代码(Junit)

import com.ruoxing.dbs.bean.FunctionalDependency;
import com.ruoxing.dbs.util.ArmstrongUtils;
import org.junit.Test;

import java.util.Arrays;

/**
 * 描述:测试Armstrong公理的实现
 */
public class Test3 implements ArmstrongUtils {

}
    @Test
    public void test01() {
        System.out.println("自反律:");
        System.out.println(reflexivity("AB", "B"));//AB→B
        System.out.println(reflexivity("A", "BC"));//null
    }

增补律(augmentation)

1. 方法代码

    /**
     * 描述:增补律
     * @param fd FunctionalDependency  α → β
     * @param sequence CharSequence    γ
     * @return FunctionalDependency    γα → γβ
     */
    default FunctionalDependency augmentation(FunctionalDependency fd, CharSequence sequence) {
        return new FunctionalDependency("" + fd.getLeft() + sequence, "" + fd.getRight() + sequence);
    }

2. 测试代码

    @Test
    public void test02() {
        System.out.println("增补律:");
        final FunctionalDependency fd = new FunctionalDependency("α", "β");//α → β
        System.out.println(augmentation(fd, "γ"));//γα → γβ
    }

传递律(transitivity)

1. 方法代码

    /**
     * 描述:传递律
     * @param fd1 FunctionalDependency  α → β
     * @param fd2 FunctionalDependency  β → γ
     * @return FunctionalDependency  α → γ
     */
    default FunctionalDependency transitivity(FunctionalDependency fd1, FunctionalDependency fd2) {
        //α → β1,β2 → γ(β1包含β2)  ==> α → γ
        if (contains(fd1.getRight(), fd2.getLeft())) {
            return new FunctionalDependency(fd1.getLeft(), fd2.getRight());
        }
        return null;
    }

2. 测试代码

    @Test
    public void test03() {
        System.out.println("传递律:");
        final FunctionalDependency fd1 = new FunctionalDependency("α", "β");//α → β
        final FunctionalDependency fd2 = new FunctionalDependency("β", "γ");//β → γ
        System.out.println(transitivity(fd1, fd2));//α→γ
    }

合并律(union)

1. 方法代码

    /**
     * 描述:合并律
     * @param fd1 FunctionalDependency  α → β
     * @param fd2 FunctionalDependency  α → γ
     * @return FunctionalDependency  α → βγ
     */
    default FunctionalDependency union(FunctionalDependency fd1, FunctionalDependency fd2) {
        if (fd1.getLeft().equals(fd2.getLeft())) {
            return new FunctionalDependency(fd1.getLeft(), "" + fd1.getRight() + fd2.getRight());
        }
        return null;
    }

2. 测试代码

@Test
    public void test03() {
        System.out.println("合并律:");
        final FunctionalDependency fd1 = new FunctionalDependency("α", "β");//α → β
        final FunctionalDependency fd2 = new FunctionalDependency("α", "γ");//α → γ
        System.out.println(union(fd1, fd2));//α→βγ
    }

分解律(decomposition)

1. 方法代码

    /**
     * 描述:分解律
     *
     * @param fd FunctionalDependency  α → βγ
     * @return FunctionalDependency    {α → β,α → γ}
     */
    default FunctionalDependency[] decomposition(FunctionalDependency fd) {
        final CharSequence right = fd.getRight();
        FunctionalDependency[] fds = new FunctionalDependency[right.length()];
        for (int i = 0; i < fds.length; i++) {
            fds[i] = new FunctionalDependency(fd.getLeft(), "" + right.charAt(i));
        }
        return fds;
    }

2. 测试代码

    @Test
    public void test05() {
        System.out.println("分解律:");
        final FunctionalDependency fd = new FunctionalDependency("α", "βγ");//α → βγ
        final FunctionalDependency[] decomposition = decomposition(fd);
        System.out.println(Arrays.toString(decomposition));//[α→β, α→γ]
    }

伪传递律(pseudoTransitivity)

1. 方法代码

    /**
     * 描述:伪传递律
     *
     * @param fd1 FunctionalDependency  α → β
     * @param fd2 FunctionalDependency  γβ → δ
     * @return FunctionalDependency     αγ → δ
     */
    default FunctionalDependency pseudoTransitivity(FunctionalDependency fd1, FunctionalDependency fd2) {
        if (contains(fd2.getLeft(), fd1.getRight())) {
            return new FunctionalDependency(("" + fd1.getLeft() + fd2.getLeft()).replace(fd1.getRight(), ""), fd2.getRight());
        }
        return null;
    }

2. 测试代码

    @Test
    public void test06() {
        System.out.println("伪传递律:");
        final FunctionalDependency fd1 = new FunctionalDependency("α", "β");//α → β
        final FunctionalDependency fd2 = new FunctionalDependency("βγ", "δ");//βγ → δ
        System.out.println(union(fd1, fd2));//αγ → δ
    }