logic tree expression problem

InstructionWhich7142

Registered User.
Local time
Today, 14:43
Joined
Feb 24, 2010
Messages
203
I have a logic tree, but I can't express it in code?

The problem is some branches of the tree rejoin further down!


Maybe these two images from google explain it better:

What i'm trying to do looks like this, see how the true's feed back into each other on the left
http://www.emeraldinsight.com/content_images/fig/2860130202003.png

More "normal" logic tree's i know how to program look like this:
http://www.christianapologetic.org/images/accura10.gif
everything branches out, nothing branches back

Please could someone let me know what i'm missing
 
Depends on how you look at it. If you follow the path from the root to a leaf, the first diagram just tells you in a very concise way, that you can arrive at logically equivalent leaves - here depicted as the same physical leaf - via different paths. In the second diagram that would be depicted by some branches being entirely similar, so you again would end up at a logically equivalent leaf via different paths to physically different leaves.
 
Here's my take on it:

Code:
    If FailureWillAffectObligations() Or FailureWillAffectBusiness() Or MultipleFailuresWillCauseSignificantLoss() Then
        If EffectiveConditionMonitoring() Then
            PredictiveMaintenance()
        Else
            If EffectiveIntervalMonitoring() Then
                PreventiveMaintenance()
            Else
                RedesignSystem()
            End If
        End If
    Else
        CorrectiveMaintenance()
    End If
 
Depends on how you look at it. If you follow the path from the root to a leaf, the first diagram just tells you in a very concise way, that you can arrive at logically equivalent leaves - here depicted as the same physical leaf - via different paths. In the second diagram that would be depicted by some branches being entirely similar, so you again would end up at a logically equivalent leaf via different paths to physically different leaves.

So when writing code I need to think of it like this:
"In the second diagram that would be depicted by some branches being entirely similar"

Instead of the over simplified diagram where "here depicted as the same physical leaf - via different paths"
?
 
I am not sure as to what exactly you are asking.

But the second diagram has unique nodes. From each higher-level node you can go to specific lower-level nodes only. If you take the info from diagram 1 and spread it on diagram 2, then that means that you can get to the same logical end-node (=leaf) via more than one physical path.

If we take each node as a question, then you can arrive at the same question via different paths. But once there, what happens does not depend on the prior path. So structurally, each node has a given set of optional paths to lower-level nodes, and those paths do not depend on how you got there.

Does this gobbledygook help? If too abstract ,then go with VilaRestals example, where he has hard-coded the relations.
 
you can arrive at the same question via different paths. But once there, what happens does not depend on the prior path. So structurally, each node has a given set of optional paths to lower-level nodes, and those paths do not depend on how you got there.

This is what I'm trying to do, get to the same question (e.g: "IF" statement in my program) by taking different routes through the logic before it :)
 
So what is the specific problem? If you have no problem coding Diagram 2, then in Diagram 1 some of the nodes you get to (after a "rejoin") , are just the the same ones for all paths to them.
 
Err, that's where I get stuck!
what do you mean by:

"then in Diagram 1 some of the nodes you get to (after a "rejoin") , are just the the same ones for all paths to them."

After a rejoin can I get to the same line of code from multiple paths?

Or just a logically identical duplicate?
 
I don't know how else to explain it. Draw diagram 2, each node on a Post It. Write some ID in each Post It. On one branch, write the same ID as on another Branch. Now remove those Post It's, and put them on top of the Post It's with the same IDs in the other branch. When you add an arrow from the amputated branch to the branch containing the added Post-It's then now you have diagram 1.
 
I got kinda lost in the postit discription,

I've attached a kinda sketchy visio drawing of the logic "Tree" I need to code, let me know what you think,


Back to the postit's - by Nodes do you mean what i've labeled A B C D E etc? and the Branches are the lines? so the postit's are mean to have A B C D E? or am i meant to number the Branches(lines)?
 

Attachments

  • LOGIC.png
    LOGIC.png
    23.9 KB · Views: 108
I think if you understand logic gates and how they would appear in such a diagram then it's easy.

I attach an image that shows. I don't want anyone to feel that I'm being condescending by posting that. It's probably something you know already but hadn't applied to such a diagram.

With that in mind, all the top levels of conditions reduce to a simple one line statement with ORs except the AND between "Will accumulative failures.." and "Is the economic loss..", which I joined into one function in my 'code'.

After that there's just a couple of trivial conditions on the 'True' path.

I can't think of any other way than to advise you to look at that. Convert it to code: encapsulate the conditions in easy to identify functions, reuse those functions where you can and join pairs into ORs and ANDs where appropriate. Personally, I find the code easier to read than the diagram.
 

Attachments

  • LogicGates.png
    LogicGates.png
    6.9 KB · Views: 108
In the diagram you just posted chris there's a loop between F and G. I hope they are methods and not simple comparisons.

Apart from that just write out the code:

Code:
If A Then
    If B Or C Then
        If D Or E Then
            'F/G Loop
        Else
            H
        End If
    Else
       H
    End If
Else
    H
End If
 
You need to create procedures (subroutines or functions) that do the common elements of the task. That will allow you to branch out and then back in.

Code:
If A
    Do A1
    Do A2
    Do A3
Else
    If B
        Do B1
        Do A2
    Else
        If C
            Do A1
            Do B1
            Do C1
       End If
    Else
        Do C1
End If
 
I printed everything out and sat down with it all over lunch, made a lot more sense when I could really compare everything at the same time :) also I'd never really thought about it as logic gates, or about how a result part way down could be effectively reached by a single statement.

I think I was really struggling because I wanted to avoid having to repeat sections (e.g. H)

I wanted to avoid repeats because I don't think I can put much of it into functions, the whole tree I posted is wrapped within a Loop which is stepping down a recordset, and all of A-G compare to other open recordsets (up to 6 i think) and then perform calculations/comparisons/write backs before moving on.
 
Would I be better off doing this with GoTo statements? (I know these are frowned upon but I guess they are fast processing wise and save a lot of repetition?)
 
In a word no! (imo and many others' i'm sure)

Split it into separate subs and exit them early or jump to a later sub.

Achieves the same thing and is easier to read and adapt.
 
Hehe, wondered if that might be the response :)

How do I jump to a later sub?

Also will the RecordSets I open in the main function still be open for the subs to work on? Also will I get a ton of "variable not defined" errors for the subs?
 
See Attachements

.PNG is the messy tree again (I renumbered 1-9 and put T/F on the lines and a basic logic statement for me to refer to as well) [there are functions peformed after each T/F that change the value of "dem" etc]

.JPG is kinda how I'd picture it with nested If's

Edit: small fail in the .JPG there shoundn't be 9's after 6's, the loop just loops! :P
 

Attachments

  • LOGIC.jpg
    LOGIC.jpg
    77.4 KB · Views: 78
  • LOGIC1.png
    LOGIC1.png
    30.1 KB · Views: 72
Last edited:
How do I jump to a later sub?

By saying it's name :o

Also will the RecordSets I open in the main function still be open for the subs to work on? Also will I get a ton of "variable not defined" errors for the subs?

You can pass the recordset (or any other object) by reference

E.g.:

Code:
Private Sub Main()
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("Table1")
    'Do stuff with rs
    CloseRecordset rs
End Sub

Private Sub CloseRecordset(ByRef rs As DAO.Recordset)
    rs.Close
    Set rs = Nothing
End Sub
 
And within the diagram you posted (LOGIC.jpg), you'll notice certain blocks can be reused. In particular the 6 block is always the same where it happens. You'd put that in a sub and reuse it. The same goes for the 4 block (which contains two 6 blocks).
Secondly, notice that every possible path ends in 9 so you'd just put that at the end (it runs whatever happens before it).
Really, it isn't very complicated at all. There's certainly no need to use a single GoTo.
 

Users who are viewing this thread

Back
Top Bottom