Łukasiewicz predicate logic?

Is there a way to emulate ukasiewicz three-digit logic with the Java API?

More specifically, I want to imitate Łukasiewicz ' "M" ("this is not a lie ...") .

For example, I need to build this Predicate<String>one that gives the following results:

predicate.test("0") = false
predicate.test(null) = true
predicate.test("1") = true

which, when denied, gives:

predicate.negate().test("0") = true
predicate.negate().test(null) = true
predicate.negate().test("1") = false

If the value is for the test A, my predicate calculates MA. What it negate()does is compute ¬MA, while I want it to compute M¬A.

PS Do not bother developing a three-digit replacement answer with a three- Predicatedigit replacement boolean, because I know quite well how to do it myself. I ask this question to find out if this is possible with pure Java8 with minimal extra coding effort.

+4
source share
2 answers

Looks like this is what you want:

static <T> Predicate<T> p(Predicate<? super T> o){ //(I'm not sure what to name this)
    return new Predicate<T>(){
        @Override
        public boolean test(T t) {
            return t == null || o.test(t);
        }
        @Override
        public Predicate<T> negate(){
            return p(o.negate());
        }
    };
}
....
Predicate<String> predicate = p(t -> !"0".equals(t));

If the value for the test is null, we return true, otherwise we will use the original predicate. To deny, we simply deny the original predicate.


If null is not always the third value you could pass in another predicate to check for this:

static <T> Predicate<T> p(Predicate<? super T> isUnknown, Predicate<? super T> isTrue){
    return new Predicate<T>(){
        @Override
        public boolean test(T t) {
            return isUnknown.test(t) || isTrue.test(t);
        }
        @Override
        public Predicate<T> negate(){
            return p(isUnknown, isTrue.negate());
        }
    };
}
+2
source

, negate() Predicate: , , . , , predicate.test(null) predicate.negate().test(null) true.

. negate boolean. trueValue falseValue. thirdValue true. , , t210 > ( , ).

public static void main(String... args) {
    Predicate<String> predicate = lukasiewicz("1", "0", null);

    System.out.println(predicate.test("0"));
    System.out.println(predicate.test(null));
    System.out.println(predicate.test("1"));

    System.out.println(predicate.negate().test("0"));
    System.out.println(predicate.negate().test(null));
    System.out.println(predicate.negate().test("1"));
}

private static <T> Predicate<T> lukasiewicz(T trueValue, T falseValue, T thirdValue) {
    class LukasiewiczPredicate implements Predicate<T> {

        private boolean negate; 

        private LukasiewiczPredicate(boolean negate) {
            this.negate = negate;
        }

        @Override
        public boolean test(T t) {
            if (Objects.equals(trueValue, t)) return negate;
            if (Objects.equals(falseValue, t)) return !negate;
            if (Objects.equals(thirdValue, t)) return true;
            return false;
        }

        @Override
        public Predicate<T> negate() {
            return new LukasiewiczPredicate(!negate);
        }

    };
    return new LukasiewiczPredicate(true);
}
+1

Source: https://habr.com/ru/post/1617888/