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))
                {
                    case "+": 
                        sbExpr.append("|");
                        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

  • Why do you generate a script rather than evaluating the expression directly? – Alexandre Dupriez Mar 08 '19 at 07:47
  • Because I do not know how to do it from an ArrayList... :/ –  Mar 08 '19 at 08:12
  • Why not use an `ArrayList`? – Nicholas K Mar 08 '19 at 08:38
  • Even if I use an **ArrayList** , I will need to parse my expression boolean which is into my ArrayList and identify which columns are which variables. Well, to be honset, at the beginning, my 2D array was typed HashMap . 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 –  Mar 08 '19 at 08:46

0 Answers0