Evaluate boolean expressions from arrayListCreate ArrayList from arrayHow do I call one constructor from another in Java?When to use LinkedList over ArrayList in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyCheck if at least two out of three booleans are trueConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] array

How do I repair my stair bannister?

Find last 3 digits of this monster number

Why is Arduino resetting while driving motors?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Is a model fitted to data or is data fitted to a model?

Can someone explain how this makes sense electrically?

Transformation of random variables and joint distributions

Customize circled numbers

Varistor? Purpose and principle

Drawing ramified coverings with tikz

Why has "pence" been used in this sentence, not "pences"?

Greatest common substring

Could the E-bike drivetrain wear down till needing replacement after 400 km?

Fly on a jet pack vs fly with a jet pack?

Journal losing indexing services

Longest common substring in linear time

Using a siddur to Daven from in a seforim store

Drawing a topological "handle" with Tikz

Is there a conventional notation or name for the slip angle?

What linear sensor for a keyboard?

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

Java - What do constructor type arguments mean when placed *before* the type?

If a character with the Alert feat rolls a crit fail on their Perception check, are they surprised?

We have a love-hate relationship



Evaluate boolean expressions from arrayList


Create ArrayList from arrayHow do I call one constructor from another in Java?When to use LinkedList over ArrayList in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyCheck if at least two out of three booleans are trueConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] array













1















I generate a truth table from any boolean expression and store it into a 2D array. As a first step, I store my boolean expression in an object arrayList. I count noumbers of variables to deduct numbers of columns(variables) and numbers of rows(combinaisons).



 nbrVariables = variables.size();
nbrCombinaisons = (int)Math.pow(2,variables.size());
truth_table = new int [nbrCombinaisons][nbrVariables+1];


I declare nbrVariables+1 because I need 1 column which represents my output. Using a loop, I generate each combinaisons of my truth table.



I have this in my console :



---- Equation : [soccer, +, food]
---- Variables : [soccer, food]

variables : 2
combinaisons : 4

header : [food, soccer]

[0, 0, null]
[0, 1, null]
[1, 0, null]
[1, 1, null]


As you can see, each elements of my last columns is null. To complete this column which represents my output, I need to evaluate my boolean expression for each of this combinaisons. I reiterate that my boolean expression is a arrayList of Objects.



To complete it, I use a string buffer to use a script that evaluate my boolean expression for each combinaisons(=each row).



public void getOutput() throws ScriptException

StringBuffer sbExpr = new StringBuffer();
temp= new ArrayList<Object>();
temp.addAll(equation);

for(int i=0; i<=nbrCombinaisons-1; i++)
for (int j=0; j<=nbrVariables-1 ; j++)
if(equation.contains(variables.get(j).getName()))
temp.set(equation.indexOf(variables.get(j).getName()), truth_table[i][j]);



truth_table[i][nbrVariables]= getResult(temp, sbExpr );




public int getResult(ArrayList<Object> temp, StringBuffer sbExpr) throws ScriptException

sbExpr.delete(0, sbExpr.length());
for(int i =0; i< temp.size(); i++)
if(temp.get(i).equals(1))
sbExpr.append("1");
else if(temp.get(i).equals(0))
sbExpr.append("0");
else if(temp.get(i).getClass().equals(String.class))
switch((String)temp.get(i))
");
break;
case ".":
sbExpr.append("&");
break;
case "¤":
sbExpr.append("^");
break;
case "!":
sbExpr.append("!");
break;
case "(":
sbExpr.append("(");
break;
case ")":
sbExpr.append(")");
break;
default:
System.out.println("no match");


// System.out.println("nsbExpr -----> " +sbExpr );

ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("JavaScript");
if (se.eval(sbExpr.toString()).toString().equals("1"))
return 1;
else
return 0;




Well this works fine.



[PCU, Fuel circuit]

[0, 0, 0]
[0, 1, 1]
[1, 0, 1]
[1, 1, 1]


But I would like to know if there is an other way to do it because this way is, for me, not very sexy. It is heavy and more I have variables, more it is long. Also, I am asking me if using script can be use in other computer if I want share my application.



Thank you for yout help










share|improve this question






















  • Why do you generate a script rather than evaluating the expression directly?

    – Alexandre Dupriez
    Mar 8 at 7:47











  • Because I do not know how to do it from an ArrayList... :/

    – Miigui_08
    Mar 8 at 8:12











  • Why not use an ArrayList<Boolean>?

    – Nicholas K
    Mar 8 at 8:38











  • Even if I use an ArrayList<Boolean> , I will need to parse my expression boolean which is into my ArrayList<Object> and identify which columns are which variables. Well, to be honset, at the beginning, my 2D array was typed HashMap<String, Boolean> . But I was due to create for each element, an HashMap. Too gourmet. So I prefer work with Integer and try to identify it. Furthermore, I want implements Quine Mc Cluskey method to minmise any boolean expression. To do that, I will use an 2D array with integer to work easly. But thats is an other issue ahaha. Thanks

    – Miigui_08
    Mar 8 at 8:46
















1















I generate a truth table from any boolean expression and store it into a 2D array. As a first step, I store my boolean expression in an object arrayList. I count noumbers of variables to deduct numbers of columns(variables) and numbers of rows(combinaisons).



 nbrVariables = variables.size();
nbrCombinaisons = (int)Math.pow(2,variables.size());
truth_table = new int [nbrCombinaisons][nbrVariables+1];


I declare nbrVariables+1 because I need 1 column which represents my output. Using a loop, I generate each combinaisons of my truth table.



I have this in my console :



---- Equation : [soccer, +, food]
---- Variables : [soccer, food]

variables : 2
combinaisons : 4

header : [food, soccer]

[0, 0, null]
[0, 1, null]
[1, 0, null]
[1, 1, null]


As you can see, each elements of my last columns is null. To complete this column which represents my output, I need to evaluate my boolean expression for each of this combinaisons. I reiterate that my boolean expression is a arrayList of Objects.



To complete it, I use a string buffer to use a script that evaluate my boolean expression for each combinaisons(=each row).



public void getOutput() throws ScriptException

StringBuffer sbExpr = new StringBuffer();
temp= new ArrayList<Object>();
temp.addAll(equation);

for(int i=0; i<=nbrCombinaisons-1; i++)
for (int j=0; j<=nbrVariables-1 ; j++)
if(equation.contains(variables.get(j).getName()))
temp.set(equation.indexOf(variables.get(j).getName()), truth_table[i][j]);



truth_table[i][nbrVariables]= getResult(temp, sbExpr );




public int getResult(ArrayList<Object> temp, StringBuffer sbExpr) throws ScriptException

sbExpr.delete(0, sbExpr.length());
for(int i =0; i< temp.size(); i++)
if(temp.get(i).equals(1))
sbExpr.append("1");
else if(temp.get(i).equals(0))
sbExpr.append("0");
else if(temp.get(i).getClass().equals(String.class))
switch((String)temp.get(i))
");
break;
case ".":
sbExpr.append("&");
break;
case "¤":
sbExpr.append("^");
break;
case "!":
sbExpr.append("!");
break;
case "(":
sbExpr.append("(");
break;
case ")":
sbExpr.append(")");
break;
default:
System.out.println("no match");


// System.out.println("nsbExpr -----> " +sbExpr );

ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("JavaScript");
if (se.eval(sbExpr.toString()).toString().equals("1"))
return 1;
else
return 0;




Well this works fine.



[PCU, Fuel circuit]

[0, 0, 0]
[0, 1, 1]
[1, 0, 1]
[1, 1, 1]


But I would like to know if there is an other way to do it because this way is, for me, not very sexy. It is heavy and more I have variables, more it is long. Also, I am asking me if using script can be use in other computer if I want share my application.



Thank you for yout help










share|improve this question






















  • Why do you generate a script rather than evaluating the expression directly?

    – Alexandre Dupriez
    Mar 8 at 7:47











  • Because I do not know how to do it from an ArrayList... :/

    – Miigui_08
    Mar 8 at 8:12











  • Why not use an ArrayList<Boolean>?

    – Nicholas K
    Mar 8 at 8:38











  • Even if I use an ArrayList<Boolean> , I will need to parse my expression boolean which is into my ArrayList<Object> and identify which columns are which variables. Well, to be honset, at the beginning, my 2D array was typed HashMap<String, Boolean> . But I was due to create for each element, an HashMap. Too gourmet. So I prefer work with Integer and try to identify it. Furthermore, I want implements Quine Mc Cluskey method to minmise any boolean expression. To do that, I will use an 2D array with integer to work easly. But thats is an other issue ahaha. Thanks

    – Miigui_08
    Mar 8 at 8:46














1












1








1


1






I generate a truth table from any boolean expression and store it into a 2D array. As a first step, I store my boolean expression in an object arrayList. I count noumbers of variables to deduct numbers of columns(variables) and numbers of rows(combinaisons).



 nbrVariables = variables.size();
nbrCombinaisons = (int)Math.pow(2,variables.size());
truth_table = new int [nbrCombinaisons][nbrVariables+1];


I declare nbrVariables+1 because I need 1 column which represents my output. Using a loop, I generate each combinaisons of my truth table.



I have this in my console :



---- Equation : [soccer, +, food]
---- Variables : [soccer, food]

variables : 2
combinaisons : 4

header : [food, soccer]

[0, 0, null]
[0, 1, null]
[1, 0, null]
[1, 1, null]


As you can see, each elements of my last columns is null. To complete this column which represents my output, I need to evaluate my boolean expression for each of this combinaisons. I reiterate that my boolean expression is a arrayList of Objects.



To complete it, I use a string buffer to use a script that evaluate my boolean expression for each combinaisons(=each row).



public void getOutput() throws ScriptException

StringBuffer sbExpr = new StringBuffer();
temp= new ArrayList<Object>();
temp.addAll(equation);

for(int i=0; i<=nbrCombinaisons-1; i++)
for (int j=0; j<=nbrVariables-1 ; j++)
if(equation.contains(variables.get(j).getName()))
temp.set(equation.indexOf(variables.get(j).getName()), truth_table[i][j]);



truth_table[i][nbrVariables]= getResult(temp, sbExpr );




public int getResult(ArrayList<Object> temp, StringBuffer sbExpr) throws ScriptException

sbExpr.delete(0, sbExpr.length());
for(int i =0; i< temp.size(); i++)
if(temp.get(i).equals(1))
sbExpr.append("1");
else if(temp.get(i).equals(0))
sbExpr.append("0");
else if(temp.get(i).getClass().equals(String.class))
switch((String)temp.get(i))
");
break;
case ".":
sbExpr.append("&");
break;
case "¤":
sbExpr.append("^");
break;
case "!":
sbExpr.append("!");
break;
case "(":
sbExpr.append("(");
break;
case ")":
sbExpr.append(")");
break;
default:
System.out.println("no match");


// System.out.println("nsbExpr -----> " +sbExpr );

ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("JavaScript");
if (se.eval(sbExpr.toString()).toString().equals("1"))
return 1;
else
return 0;




Well this works fine.



[PCU, Fuel circuit]

[0, 0, 0]
[0, 1, 1]
[1, 0, 1]
[1, 1, 1]


But I would like to know if there is an other way to do it because this way is, for me, not very sexy. It is heavy and more I have variables, more it is long. Also, I am asking me if using script can be use in other computer if I want share my application.



Thank you for yout help










share|improve this question














I generate a truth table from any boolean expression and store it into a 2D array. As a first step, I store my boolean expression in an object arrayList. I count noumbers of variables to deduct numbers of columns(variables) and numbers of rows(combinaisons).



 nbrVariables = variables.size();
nbrCombinaisons = (int)Math.pow(2,variables.size());
truth_table = new int [nbrCombinaisons][nbrVariables+1];


I declare nbrVariables+1 because I need 1 column which represents my output. Using a loop, I generate each combinaisons of my truth table.



I have this in my console :



---- Equation : [soccer, +, food]
---- Variables : [soccer, food]

variables : 2
combinaisons : 4

header : [food, soccer]

[0, 0, null]
[0, 1, null]
[1, 0, null]
[1, 1, null]


As you can see, each elements of my last columns is null. To complete this column which represents my output, I need to evaluate my boolean expression for each of this combinaisons. I reiterate that my boolean expression is a arrayList of Objects.



To complete it, I use a string buffer to use a script that evaluate my boolean expression for each combinaisons(=each row).



public void getOutput() throws ScriptException

StringBuffer sbExpr = new StringBuffer();
temp= new ArrayList<Object>();
temp.addAll(equation);

for(int i=0; i<=nbrCombinaisons-1; i++)
for (int j=0; j<=nbrVariables-1 ; j++)
if(equation.contains(variables.get(j).getName()))
temp.set(equation.indexOf(variables.get(j).getName()), truth_table[i][j]);



truth_table[i][nbrVariables]= getResult(temp, sbExpr );




public int getResult(ArrayList<Object> temp, StringBuffer sbExpr) throws ScriptException

sbExpr.delete(0, sbExpr.length());
for(int i =0; i< temp.size(); i++)
if(temp.get(i).equals(1))
sbExpr.append("1");
else if(temp.get(i).equals(0))
sbExpr.append("0");
else if(temp.get(i).getClass().equals(String.class))
switch((String)temp.get(i))
");
break;
case ".":
sbExpr.append("&");
break;
case "¤":
sbExpr.append("^");
break;
case "!":
sbExpr.append("!");
break;
case "(":
sbExpr.append("(");
break;
case ")":
sbExpr.append(")");
break;
default:
System.out.println("no match");


// System.out.println("nsbExpr -----> " +sbExpr );

ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("JavaScript");
if (se.eval(sbExpr.toString()).toString().equals("1"))
return 1;
else
return 0;




Well this works fine.



[PCU, Fuel circuit]

[0, 0, 0]
[0, 1, 1]
[1, 0, 1]
[1, 1, 1]


But I would like to know if there is an other way to do it because this way is, for me, not very sexy. It is heavy and more I have variables, more it is long. Also, I am asking me if using script can be use in other computer if I want share my application.



Thank you for yout help







java optimization boolean-operations javascript-engine






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 7:10









Miigui_08Miigui_08

154




154












  • Why do you generate a script rather than evaluating the expression directly?

    – Alexandre Dupriez
    Mar 8 at 7:47











  • Because I do not know how to do it from an ArrayList... :/

    – Miigui_08
    Mar 8 at 8:12











  • Why not use an ArrayList<Boolean>?

    – Nicholas K
    Mar 8 at 8:38











  • Even if I use an ArrayList<Boolean> , I will need to parse my expression boolean which is into my ArrayList<Object> and identify which columns are which variables. Well, to be honset, at the beginning, my 2D array was typed HashMap<String, Boolean> . But I was due to create for each element, an HashMap. Too gourmet. So I prefer work with Integer and try to identify it. Furthermore, I want implements Quine Mc Cluskey method to minmise any boolean expression. To do that, I will use an 2D array with integer to work easly. But thats is an other issue ahaha. Thanks

    – Miigui_08
    Mar 8 at 8:46


















  • Why do you generate a script rather than evaluating the expression directly?

    – Alexandre Dupriez
    Mar 8 at 7:47











  • Because I do not know how to do it from an ArrayList... :/

    – Miigui_08
    Mar 8 at 8:12











  • Why not use an ArrayList<Boolean>?

    – Nicholas K
    Mar 8 at 8:38











  • Even if I use an ArrayList<Boolean> , I will need to parse my expression boolean which is into my ArrayList<Object> and identify which columns are which variables. Well, to be honset, at the beginning, my 2D array was typed HashMap<String, Boolean> . But I was due to create for each element, an HashMap. Too gourmet. So I prefer work with Integer and try to identify it. Furthermore, I want implements Quine Mc Cluskey method to minmise any boolean expression. To do that, I will use an 2D array with integer to work easly. But thats is an other issue ahaha. Thanks

    – Miigui_08
    Mar 8 at 8:46

















Why do you generate a script rather than evaluating the expression directly?

– Alexandre Dupriez
Mar 8 at 7:47





Why do you generate a script rather than evaluating the expression directly?

– Alexandre Dupriez
Mar 8 at 7:47













Because I do not know how to do it from an ArrayList... :/

– Miigui_08
Mar 8 at 8:12





Because I do not know how to do it from an ArrayList... :/

– Miigui_08
Mar 8 at 8:12













Why not use an ArrayList<Boolean>?

– Nicholas K
Mar 8 at 8:38





Why not use an ArrayList<Boolean>?

– Nicholas K
Mar 8 at 8:38













Even if I use an ArrayList<Boolean> , I will need to parse my expression boolean which is into my ArrayList<Object> and identify which columns are which variables. Well, to be honset, at the beginning, my 2D array was typed HashMap<String, Boolean> . But I was due to create for each element, an HashMap. Too gourmet. So I prefer work with Integer and try to identify it. Furthermore, I want implements Quine Mc Cluskey method to minmise any boolean expression. To do that, I will use an 2D array with integer to work easly. But thats is an other issue ahaha. Thanks

– Miigui_08
Mar 8 at 8:46






Even if I use an ArrayList<Boolean> , I will need to parse my expression boolean which is into my ArrayList<Object> and identify which columns are which variables. Well, to be honset, at the beginning, my 2D array was typed HashMap<String, Boolean> . But I was due to create for each element, an HashMap. Too gourmet. So I prefer work with Integer and try to identify it. Furthermore, I want implements Quine Mc Cluskey method to minmise any boolean expression. To do that, I will use an 2D array with integer to work easly. But thats is an other issue ahaha. Thanks

– Miigui_08
Mar 8 at 8:46













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55058372%2fevaluate-boolean-expressions-from-arraylist%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55058372%2fevaluate-boolean-expressions-from-arraylist%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page