Solved

How to express boolean logic as objects?

Posted on 2007-11-27
14
226 Views
Last Modified: 2010-03-30
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
Comment
Question by:BrianMc1958
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20357989
You could use a BitSet, or if you want to be more minimalistic, a byte ;-)
0
 

Author Comment

by:BrianMc1958
ID: 20358078
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20358188
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:BrianMc1958
ID: 20358231
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20359960
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
 

Author Comment

by:BrianMc1958
ID: 20360235
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20360283
Right, so they choose a test. How are they then arranged? I don't know what you mean by 'using boolean logic'
0
 

Author Comment

by:BrianMc1958
ID: 20360607
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20360672
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
 

Author Comment

by:BrianMc1958
ID: 20360730
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 20360773
0
 

Author Comment

by:BrianMc1958
ID: 20360909
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20360953
:-) Good luck. Don't forget that wheel reinvention (or what you might consider a wheel subset) could be more time and energy consuming
0
 

Author Comment

by:BrianMc1958
ID: 20360983
I know.  I know...
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question