Jump to content

Help me parse textfiles booleans to simulate them in autoit.


Go to solution Solved by Nine,

Recommended Posts

Hi.

I have a file that contains boolean expressions:

...
  IF  ((KV = TRUE) AND (MH = FALSE)) OR (TV = TRUE) THEN
    GUN(GUN1, GunOn, PT5, TR5)
  END_IF 
...

I would like to read the file, parse those imporant bits, and save those in some data structure that allows me to "simulate a run" trough that file with all combinations of variables.

I have trouble comming up with an idea on how to save the data in a way I can than use later. How do I model my data structure so that I can then assign "random" values(true/false) to vars (KV, MH..), and see if, given those variable values, the IF condition is satisfied or not (so whether Gun line gets read given set of variables).

Theres gonna be max 10 IFs per file, and the maximum amount of variables is about 10, so bruteforcing seems like the easiest way (I will just try all combinations one after another) for simulation. But how do I code up the "solver" of these regular expressions?

Edited by MaximusCZ
Link to comment
Share on other sites

Lookup the 3-SAT problem: it's NP-complete.  Hence 10-SAT isn't going to be any easier than NP.  If fortunately you don't have to process gazillions of terms, you can still do something.

To get your hands wet, try DPLL.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I am not really looking for efficient way to do it, as I said I think bruteforcing is completely acceptable approach here. I just do a pass on the file for each combination of inputs.

The issue I have is "how" to parse and store the data in codeblock, so that it can then simulate executing the IF properly.

Link to comment
Share on other sites

You can do everything in a single run :

1- parse all statements between IF ... THEN

2- parse all variables inside a statement (alpha chars before = sign)

3- Add $ sign before each variable inside a statement

4- Assign those vars to False / True in alternance

5- Execute the statement for each combination and show result

Link to comment
Share on other sites

Already have 1. and 2. finished. 

I am not sure I follow the rest? Currently I store the vars in an array (KV, MH..), do I just do a pass on the array adding "$" in front of every element? 
How am I supposed to simulate the boolean logic on the whole statement?

Link to comment
Share on other sites

  • Solution
Posted (edited)

Execute function (see help file) will do the boolean logic for the whole statement

Assign function (see help file) will set each variable to True/False

StringReplace function will replace each var with $var inside a statement

You need to create a loop to assign in alternance each var to true/false

edit :

Local $aVar = ["KV", "MH", "TV"]
Local $sStatement = "((KV = TRUE) AND (MH = FALSE)) OR (TV = TRUE)"
Local $nVar = UBound($aVar), $sList, $bRes

$sStatement = StringRegExpReplace($sStatement, "([A-Z]+ =)", "\$\1")
ConsoleWrite($sStatement & @CRLF)

For $i = 0 To (2 ^ $nVar) - 1
  $sList = ""
  For $j = 0 To $nVar - 1
    Assign($aVar[$j], BitAND($i, 2 ^ $j) ? True : False)
    $sList &= $aVar[$j] & " = " & Eval($aVar[$j]) & @TAB
  Next
  $bRes = Execute($sStatement)
  ConsoleWrite($sList & ":: " & $bRes & @CRLF)
Next

 

Edited by Nine
change replace approach
Link to comment
Share on other sites

Posted (edited)

Absolutely amazing, thanks! Had no idea about Execute and Assign function existing at all :), now it make sense why you made them Bold in your original post :)

 

 

Edited by MaximusCZ
Link to comment
Share on other sites

Posted (edited)
9 hours ago, Jos said:

Care to explain what this is all about?

Sure thing!

I am reading files containing movement/application programs for atomotive industrial paintshop robots. Each file consists of points and triggers. Points for actual robot movement, and triggers for events like opening/closing the main needle (GunOn/Off), changing application parameters (flow amount of color/air etc) and possibly much more. 

image.thumb.png.bb96e17c59cf29c5fb53781269ccf802.png

In this case the IFs are for panel repairs, making the robot paint only the areas that are marked by the factory to be repainted, hence the GunOn/Off events in IFs. The problem I am facing is that if those conditions are made the easy way, theres a high chance a robot will encounter two GunOns in a row (without GunOff in between), and for some stupid reason this raises an alarm. So every condition must be carefully crafted to only trigger precisely as needed, making sure that all condition states from before are considered. Its clunky, its unintuitive, but its reality. 

I am writing a macro capable of reading these programs, simulating "going trough" the program in sequence for all possible combinations of variables (usually less than 5 in a single module), and making sure such things cant happen. 

No need to worry, I am not writing a game bot, I am aware of the rules :) Thanks for taking care of AutoIt and its users so consistently over the years, its really helping.

Also 69th comment, nice..

Edited by MaximusCZ
Link to comment
Share on other sites

  • Developers
1 hour ago, MaximusCZ said:

No need to worry, I am not writing a game bot, I am aware of the rules :) Thanks for taking care of AutoIt and its users so consistently over the years, its really helping.

Thanks for explaining and sorry to be a pain, but I am trying to avoid some pain we experienced in the old days. :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...