Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

How to express boolean logic as objects?

Dear Experts,

I have a dumb question.  Assume I want to store three test questions, in which BOTH of the first two are true, OR the third is true.  For instance, "If ( (Birdie is blue) AND (Duckie is yellow) ) OR (Bear is purple)..."

(I said this was a dumb question...)  

I'd like to store each test as an object, and then manipulate them that way, probably in a tree.  But I can't immediately think of a way to express the nested AND/OR conditions.  How would you folks do it?

Thanks!
BrianMc1958
0
BrianMc1958
Asked:
BrianMc1958
  • 7
  • 7
1 Solution
 
CEHJCommented:
You could use a BitSet, or if you want to be more minimalistic, a byte ;-)
0
 
BrianMc1958Author Commented:
Well, I'm trying to express the boolean logic WITHIN the structure of the tree that holds the test objects, for instance.  I'm trying to avoid having a set of test objects in one place, and then having another structure someplace else that describes their relationship.
0
 
CEHJCommented:
You can't do that really. The purpose of a tree is to describe a hierarchy. If you want to express something else, you will have to capture that in another container class that links to the tree

Of course the nodes in your tree (questions and answers) could have BitSet and true/false attributes respectively
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BrianMc1958Author Commented:
That's sort of what I was imagining.  If each "test object" also held an enum of AND, OR, NOT, etc.  Then I would recur to the leafs, and roll up the boolean logic that way.  Sounds elegant, but I'm asking you because I really don't know what I'm talking about here...!
0
 
CEHJCommented:
What do you want to do? If you want to determine which of your answers is true or false, you can keep the BitSet as an attrtibute of the parent (question) node
0
 
BrianMc1958Author Commented:
I should have said that I won't know which tests, or their boolean relationships, in advance.  Basically, I'm building a UI that will allow the user to choose from a large list of tests, but then arrange them using boolean logic.  In principle, they can be infinitely nested, but in practice, three levels could definitely be set as a limit.

Given this, I can't see an elegant way to do this with a separate BitSet.

FYI, I am experimenting now with storing the "test" and an associated boolean value in a recursively defined XML element.  

Please wish me luck.
0
 
CEHJCommented:
Right, so they choose a test. How are they then arranged? I don't know what you mean by 'using boolean logic'
0
 
BrianMc1958Author Commented:
Logically (if whimsically) like this:

"If ( (Birdie is blue) AND (Duckie is yellow) ) OR (Bear is purple)..."

In XML, maybe something like this:

  <BOOL val=OR>
    <BOOL val=AND>
      <TEST type="Birdie is blue"/>
      <TEST type="Duckie is yellow"/>
    </BOOL>
      <TEST type="Bear is purple"/>
  </BOOL>
0
 
CEHJCommented:
Well i'm trying to see what your functional requirements are. If you don't know yet how to achieve their realisation, attempts to show me the former by illustrating the latter are not going to help
0
 
BrianMc1958Author Commented:
Functionally, I need to parse that, evaluate each test to true or false, and return a single true or false for the whole statement.
0
 
CEHJCommented:
0
 
BrianMc1958Author Commented:
I was afraid you were going to say that.  For the purposes of this project, I think that would be too involved.  Nevertheless, I think I've bothered you enough for one day, and I'll accept the answer.  From what I've read, a rules engine is the general-purpose solution here, but I don't have the time at the moment.

Thanks very much again.
0
 
CEHJCommented:
:-) Good luck. Don't forget that wheel reinvention (or what you might consider a wheel subset) could be more time and energy consuming
0
 
BrianMc1958Author Commented:
I know.  I know...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now