All Downloads are FREE. Search and download functionalities are using the official Maven repository.

example.Predicates Maven / Gradle / Ivy

/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (c) 2016-2021 Barry DeZonia All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution.
 * 
 * Neither the name of the  nor the names of its contributors may
 * be used to endorse or promote products derived from this software without specific
 * prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package example;

import nom.bdezonia.zorbage.algebra.G;
import nom.bdezonia.zorbage.algorithm.CreateMask;
import nom.bdezonia.zorbage.algorithm.Fill;
import nom.bdezonia.zorbage.algorithm.StablePartition;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;
import nom.bdezonia.zorbage.function.Function1;
import nom.bdezonia.zorbage.predicate.AndPredicate;
import nom.bdezonia.zorbage.predicate.Equal;
import nom.bdezonia.zorbage.predicate.EqualConstant;
import nom.bdezonia.zorbage.predicate.GreaterThan;
import nom.bdezonia.zorbage.predicate.GreaterThanConstant;
import nom.bdezonia.zorbage.predicate.GreaterThanEqual;
import nom.bdezonia.zorbage.predicate.GreaterThanEqualConstant;
import nom.bdezonia.zorbage.predicate.LessThan;
import nom.bdezonia.zorbage.predicate.LessThanConstant;
import nom.bdezonia.zorbage.predicate.LessThanEqual;
import nom.bdezonia.zorbage.predicate.LessThanEqualConstant;
import nom.bdezonia.zorbage.predicate.NandPredicate;
import nom.bdezonia.zorbage.predicate.NorPredicate;
import nom.bdezonia.zorbage.predicate.NotEqual;
import nom.bdezonia.zorbage.predicate.NotEqualConstant;
import nom.bdezonia.zorbage.predicate.NotPredicate;
import nom.bdezonia.zorbage.predicate.OrPredicate;
import nom.bdezonia.zorbage.predicate.XnorPredicate;
import nom.bdezonia.zorbage.predicate.XorPredicate;
import nom.bdezonia.zorbage.tuple.Tuple2;
import nom.bdezonia.zorbage.type.integer.int1.UnsignedInt1Member;
import nom.bdezonia.zorbage.type.integer.int16.SignedInt16Algebra;
import nom.bdezonia.zorbage.type.integer.int16.SignedInt16Member;
import nom.bdezonia.zorbage.type.real.float64.Float64Member;

/**
 * @author Barry DeZonia
 */
class Predicates {

	/* 
	 * A predicate is a logical function that tests an input and returns a boolean.
	 * You can also think of it as a condition. Some conditions include "number is
	 * odd", "number is greater than 100.0", "a string is lexicographically less
	 * than another string".
	 */
	
	/*
	 * Some basic predicates are already defined
	 */
	
	@SuppressWarnings("unused")
	void exmaple0() {

		boolean result;
		
		// basic one input predicates
		
		SignedInt16Member value = new SignedInt16Member(56);
		
		EqualConstant eqC = new EqualConstant<>(G.INT16, value);
		NotEqualConstant neqC = new NotEqualConstant<>(G.INT16, value);
		LessThanConstant lessC = new LessThanConstant<>(G.INT16, value);
		LessThanEqualConstant lessEqC = new LessThanEqualConstant<>(G.INT16, value);
		GreaterThanConstant greatC = new GreaterThanConstant<>(G.INT16, value);
		GreaterThanEqualConstant greatEqC = new GreaterThanEqualConstant<>(G.INT16, value);

		SignedInt16Member value2 = new SignedInt16Member(105);
		
		result = eqC.call(value2);
		result = neqC.call(value2);
		result = lessC.call(value2);
		result = lessEqC.call(value2);
		result = greatC.call(value2);
		result = greatEqC.call(value2);
		
		// basic two input predicates
		
		Equal eq = new Equal<>(G.INT16);
		NotEqual neq = new NotEqual<>(G.INT16);
		LessThan less = new LessThan<>(G.INT16);
		LessThanEqual lessEq = new LessThanEqual<>(G.INT16);
		GreaterThan great = new GreaterThan<>(G.INT16);
		GreaterThanEqual greatEq = new GreaterThanEqual<>(G.INT16);
		
		SignedInt16Member a = new SignedInt16Member(45);
		SignedInt16Member b = new SignedInt16Member(104);
		
		Tuple2 tuple = new Tuple2<>(a,b);
		
		result = eq.call(tuple);
		result = neq.call(tuple);
		result = less.call(tuple);
		result = lessEq.call(tuple);
		result = great.call(tuple);
		result = greatEq.call(tuple);
		
	}
	
	/*
	 *  Some classes are already predicate aware. Many of the C++ STL methods use predicates.
	 */
	
	void example1() {

		IndexedDataSource data =
				nom.bdezonia.zorbage.storage.Storage.allocate(G.DBL.construct(),250);
		
		Fill.compute(G.DBL, G.DBL.random(), data);
		
		Function1 condition =
				new Function1()
		{
			@Override
			public Boolean call(Float64Member value) {
				return value.v() > 7;
			}
		};
		
		StablePartition.compute(G.DBL, condition, data);
	}

	/*
	 * You can write your own predicates : 1 input
	 */
	
	@SuppressWarnings("unused")
	void example2() {
	
		Function1 lessThan100 =
				new Function1()
		{
			@Override
			public Boolean call(SignedInt16Member value) {
				return value.v() < 100;
			}
		};
		
		SignedInt16Member v = new SignedInt16Member(22);
		
		boolean result = lessThan100.call(v);
	}
	
	/*
	 * You can write your own predicates : 2 inputs
	 */
	
	@SuppressWarnings("unused")
	void example3() {
	
		Function1> notEqual =
				new Function1>()
		{
			@Override
			public Boolean call(Tuple2 value) {
				
				return value.a().v() != value.b().v();
			}
		};
		
		SignedInt16Member v1 = new SignedInt16Member(22);
		
		SignedInt16Member v2 = new SignedInt16Member(25);
		
		Tuple2 tuple = new Tuple2<>(v1,v2);

		boolean result = notEqual.call(tuple);
	}
	
	/*
	 * Zorbage provides a number of ways to combine other predicates into more
	 * sophisticated predicates.
	 */
	
	@SuppressWarnings("unused")
	void example4() {
	
		Function1 eq7Cond = new EqualConstant<>(G.INT16, new SignedInt16Member(7));

		Function1 lessThan5Cond =  new LessThanConstant<>(G.INT16, new SignedInt16Member(5));

		// Here are the seven basic logical combinations of predicates
		
		AndPredicate cond1 = new AndPredicate(eq7Cond, lessThan5Cond);
	
		OrPredicate cond2 = new OrPredicate(eq7Cond, lessThan5Cond);
		
		NandPredicate cond3 = new NandPredicate(eq7Cond, lessThan5Cond);
		
		NorPredicate cond4 = new NorPredicate(eq7Cond, lessThan5Cond);
		
		NotPredicate cond5 = new NotPredicate(lessThan5Cond);
		
		XorPredicate cond6 = new XorPredicate(eq7Cond, lessThan5Cond);
		
		XnorPredicate cond7 = new XnorPredicate(eq7Cond, lessThan5Cond);
		
		SignedInt16Member num = new SignedInt16Member(-13);
		
		boolean result;
		
		result = cond1.call(num);
		result = cond2.call(num);
		result = cond3.call(num);
		result = cond4.call(num);
		result = cond5.call(num);
		result = cond6.call(num);
		result = cond7.call(num);
	}
	
	/*
	 * You can use predicates to create a mask from another data set
	 */
	
	@SuppressWarnings("unused")
	void example() {
		
		IndexedDataSource data =
				nom.bdezonia.zorbage.storage.Storage.allocate(G.DBL.construct(),250);
		
		Fill.compute(G.DBL, G.DBL.random(), data);
		
		Function1 condition =
				new Function1()
		{
			@Override
			public Boolean call(Float64Member value) {
				return (value.v() >= 0 && value.v() < 0.3);
			}
		};
		
		IndexedDataSource mask = CreateMask.compute(G.DBL, condition, data);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy