package fr.univ.nantes.alma.accecs.generator;

import fr.univ.nantes.alma.accecs.model.Machine;
import fr.univ.nantes.alma.accecs.model.Property;
import fr.univ.nantes.alma.accecs.model.Variable;
import org.jtwig.JtwigModel;
import org.jtwig.JtwigTemplate;

import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Logger;

/**
 *
 */
public class MachineGenerator implements IMachineGenerator {
    private static final Logger LOGGER = Logger.getLogger(MachineGenerator.class.getName());

    public void generate(Machine machine, File templateFile, OutputStream outputStream) {
        JtwigTemplate template = JtwigTemplate.fileTemplate(templateFile);
        JtwigModel model = JtwigModel.newModel();
        Collection<String> variables = new ArrayList<String>();
        Collection<String> invariants = new ArrayList<String>();
        Collection<String> initialisations = new ArrayList<String>();
        Collection<String> properties = new ArrayList<String>();

        for (Variable variable : machine.getVariables()) {
            variables.add(variable.getName());
            invariants.add(variable.getName()+" : "+variable.getType());
            if (variable.hasBounds()) {
                invariants.add(variable.getName()+" : "+variable.getLowerBound()+".."+variable.getUpperBound());
            }
            initialisations.add(variable.getName()+" := "+variable.getDefaultValue());
        }

        for (Property property : machine.getProperties()) {
            properties.add(property.getExpression());
        }

        LOGGER.info("machine = " + machine.toString());
        LOGGER.info("variables = " + variables.toString());
        LOGGER.info("invariants = " + invariants.toString());
        LOGGER.info("initialisation = " + initialisations.toString());

        model.with("name", machine.getName());
        model.with("variables", variables);
        model.with("invariants", invariants);
        model.with("initialisations", initialisations);
        model.with("properties", properties);

        template.render(model, outputStream);
    }
}

@@ -809,16 +809,234 @@
 ariables
+Input = new ArrayList%3CString%3E();%0A        Collection%3CString%3E variablesOutput = new ArrayList%3CString%3E();%0A        Collection%3CString%3E variablesControl = new ArrayList%3CString%3E();%0A        Collection%3CString%3E variablesInternal
  = new A
@@ -1331,42 +1331,527 @@
     
-variables.add(variable.getName());
+switch (variable.getCategory()) %7B%0A                case INPUT:%0A                    variablesInput.add(variable.getName());%0A                    break;%0A                case OUTPUT:%0A                    variablesOutput.add(variable.getName());%0A                    break;%0A                case CONTROL:%0A                    variablesControl.add(variable.getName());%0A                    break;%0A                case INTERNAL:%0A                    variablesInternal.add(variable.getName());%0A                    break;%0A            %7D
 %0A   
@@ -2322,34 +2322,26 @@
     
-LOGGER.info(%22machine = %22 +
+model.with(%22name%22,
  mac
@@ -2345,24 +2345,23 @@
 machine.
-toString
+getName
 ());%0A   
@@ -2365,27 +2365,26 @@
         
-LOGGER.info
+model.with
 (%22variab
@@ -2386,22 +2386,23 @@
 ariables
- = %22 +
+Input%22,
  variabl
@@ -2407,156 +2407,72 @@
 bles
-.toString());%0A        LOGGER.info(%22invariants = %22 + invariants.toString());%0A        LOGGER.info(%22initialisation = %22 + initialisations.toString()
+Input);%0A        model.with(%22variablesOutput%22, variablesOutput
 );%0A
-%0A
     
@@ -2491,32 +2491,43 @@
 th(%22
-name%22, machine.getName()
+variablesControl%22, variablesControl
 );%0A 
@@ -2554,16 +2554,24 @@
 ariables
+Internal
 %22, varia
@@ -2574,16 +2574,24 @@
 ariables
+Internal
 );%0A     

use variable category