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
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
add a comment |
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
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 anArrayList<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
add a comment |
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
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
java optimization boolean-operations javascript-engine
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 anArrayList<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
add a comment |
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 anArrayList<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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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