Multifilter of IFs on demand on-the-fly

General FreeBASIC programming questions.
Post Reply
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Multifilter of IFs on demand on-the-fly

Post by petan »

Hmm, one lotty's prog leads me to the question.A bit AI, or database query..
Can I create/inject on-the-fly multi IF block (encapsulated) in running FB program ?, as pure FB code ?
As only one code line it is

Code: Select all

IF (complexCondition) then ...
where 'complexCondition' is/has (in every call) a different contents of injected single (aka IF) tests joined with logic operators And/Or/etc..?
Even maybe new, immediate test condition added to the list of possible tests ?

In easy words - I want to add multifilter feature into my keno app.
e.g.

Code: Select all

IF a is in range AND (b is low or paused) then 
and sum of these simple Ifs in one multi IF block 'll goes up to 30 in max
Equal to

Code: Select all

IF (simpleTest) then ...
 IF (simpleTest) then ...
  IF (simpleTest) then ...
   IF (simpleTest) then ...
    (...)
     (...)
       etc,

   end if
  end if
 end if
end if
Having only foggy idea, howto do it
Any tips, guys ?
Thx for your replies.
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Multifilter of IFs on demand on-the-fly

Post by St_W »

That's no problem, nothing special. You don't need to "create" or "inject" anything at runtime, just use a method for returning the result, like this:

Code: Select all

const MAX_FILTERS = 10
type FilterType as integer

type FilterBase extends object
	declare abstract function getFilterResult(o as FilterType) as boolean
end type


type CompoundFilter extends FilterBase
	filters(0 to MAX_FILTERS-1) as FilterBase ptr
	filtersCnt as integer = 0
	declare destructor()
	declare sub addFilter(f as FilterBase ptr)
end type


type CompoundAndFilter extends CompoundFilter
	declare function getFilterResult(o as FilterType) as boolean
end type

type CompoundOrFilter extends CompoundFilter
	declare function getFilterResult(o as FilterType) as boolean
end type

type RangeFilter extends FilterBase
	minVal as FilterType
	maxVal as FilterType
	declare function getFilterResult(o as FilterType) as boolean
	declare constructor(minVal as FilterType, maxVal as FilterType)
end type

function CompoundAndFilter.getFilterResult(o as FilterType) as boolean
	for q as integer = 0 to filtersCnt-1
		if not filters(q)->getFilterResult(o) then return false
	next
	return true
end function

function CompoundOrFilter.getFilterResult(o as FilterType) as boolean
	for q as integer = 0 to filtersCnt-1
		if filters(q)->getFilterResult(o) then return true
	next
	return false
end function

function RangeFilter.getFilterResult(o as FilterType) as boolean
	if (o < minVal) then return false
	if (o > maxVal) then return false
	return true
end function

constructor RangeFilter(minVal as FilterType, maxVal as FilterType)
	this.minVal = minVal
	this.maxVal = maxVal
end constructor


sub CompoundFilter.addFilter(f as FilterBase ptr)
	if filtersCnt < MAX_FILTERS then
		filters(filtersCnt) = f
		filtersCnt += 1
	end if
end sub

destructor CompoundFilter()
	for q as integer = 0 to filtersCnt-1
		delete filters(q)
	next
end destructor



dim as CompoundFilter ptr filter
filter = new CompoundAndFilter
filter->addFilter(new RangeFilter(0,100))
filter->addFilter(new RangeFilter(0,200))
dim as CompoundFilter ptr subFilter = new CompoundOrFilter
subFilter->addFilter(new RangeFilter(0,10))
subFilter->addFilter(new RangeFilter(20,30))
subFilter->addFilter(new RangeFilter(40,50))
filter->addFilter(subFilter)

dim as FilterType enteredValue
input "enter number in range (0-10 or 20-30 or 40-50): ", enteredValue

if (filter->getFilterResult(enteredValue)) then
	print "value accepted"
else
	print "value rejected"
end if


delete filter
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Multifilter of IFs on demand on-the-fly

Post by petan »

Thanks for quick response, St_W; I'll play with your OOP solution.
Seems in final stage the multifilter code 'll needs also some kind of fuzzy logic to setup a decision's priorities for very complex testing expressions.

My starting point of thought was to use old non-OOP code with macros & recursions with simple IF tests.
Post Reply