/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package RULES;
import CREATOR_CONDICTIONS_RULES.CreatorConditionD3;
import CREATOR_CONDICTIONS_RULES.CreatorProtocol;
import CREATOR_CONDICTIONS_RULES.CreatorBinaryRules;
import CREATOR_CONDICTIONS_RULES.CreatorCondition_D1_D2;
import CREATOR_CONDICTIONS_RULES.CreatorCondition_D1_D2;
import CREATOR_CONDICTIONS_RULES.CreatorCondition_D4;
import CREATOR_CONDICTIONS_RULES.CreatorCondition_D5;
import CREATOR_CONDICTIONS_RULES.CreatorCondition_D6;
import CREATOR_CONDICTIONS_RULES.CreatorCondition_D7;
import CREATOR_CONDICTIONS_RULES.CreatorProtocol;
import CREATOR_CONDICTIONS_RULES.CreatorBinaryRules;
import LOGIC.Componente;
import LOGIC.Contract;
import LOGIC.Instance;
import LOGIC.ObjectList;
import LOGIC.Protocols;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
/**
*
* @author forall
*/
public class CreatorRuleCommunicationComposition {
private LinkedList<Instance> instance;
private CreatorBinaryRules rule;
private Contract c;
private Componente component;
private CreatorProtocol createP;
private LinkedList<Protocols> protChannel;
private CreatorCondition_D1_D2 d1;
private CreatorCondition_D1_D2 d2;
private CreatorConditionD3 d3;
private CreatorCondition_D4 condition_d4;
private CreatorCondition_D5 condition_D5;
private CreatorCondition_D6 condition_D6;
private CreatorCondition_D7 condition_D7;
private String eventoprotocol1 = "";
private String eventoprotocol2 = "";
public CreatorRuleCommunicationComposition(LinkedList<Instance> i, LinkedList<Protocols> listProtocols, ObjectList lt) {
instance = i;
rule = new CreatorBinaryRules(i, lt, listProtocols);
c = new Contract();
c.setName("RuleCommunicationOf" + i.get(0).getName() + "_" + i.get(1).getName());
//component = new Componente();
//component.setName(i.get(0).getName() + "_" + i.get(1).getName() + "_Communication");
protChannel = listProtocols;
createP = new CreatorProtocol(i, listProtocols);
rule.setPegaComposicao(instance.get(0).getName() + "_" + instance.get(1).getName());
//enviando sepado prq na regra de feedback recebe apenas 1
d1 = new CreatorCondition_D1_D2(i.get(0), listProtocols.get(0));
d2 = new CreatorCondition_D1_D2(i.get(1), listProtocols.get(1));
d3 = new CreatorConditionD3(i, listProtocols);
condition_d4 = new CreatorCondition_D4(i, listProtocols);
condition_D5 = new CreatorCondition_D5(i, listProtocols);
condition_D6 = new CreatorCondition_D6(i, listProtocols);
condition_D7 = new CreatorCondition_D7(i, listProtocols);
//pegando o nome do evento que passei por paramentro no protocolo
int pr1, pr2, pr3, pr4;
pr1 = listProtocols.get(0).getProtocol().indexOf("(");
pr2 = listProtocols.get(0).getProtocol().lastIndexOf(")");
eventoprotocol1 = listProtocols.get(0).getProtocol().substring(pr1 + 1, pr2);
pr3 = listProtocols.get(1).getProtocol().indexOf("(");
pr4 = listProtocols.get(1).getProtocol().lastIndexOf(")");
eventoprotocol2 = listProtocols.get(1).getProtocol().substring(pr3 + 1, pr4);
}
public void creatorRule() {
rule.creatorBinaryCSP();
rule.setRule(rule.getRule() + createP.creatorProtocol_and_DualProtocol());
//rule.setPegaComposicao("ResultComposition");
rule.setRule(rule.getRule() + "\n\n");
rule.setRule(rule.getRule() + "--COMMUNICATION COMPOSITION");
rule.setRule(rule.getRule() + "\n\n");
//implementação da regra
rule.setRule(rule.getRule() + rule.getPegaComposicao() + " = ");
rule.setRule(rule.getRule() + "COMM(");
rule.setRule(rule.getRule() + instance.get(0).getName() + ", ");
rule.setRule(rule.getRule() + instance.get(1).getName() + ", ");
rule.setRule(rule.getRule() + eventoprotocol1 + ", ");
rule.setRule(rule.getRule() + eventoprotocol2 + ")");
rule.setRule(rule.getRule() + "\n");
//rule.setRule(rule.getRule() + "assert ");
//rule.setRule(rule.getRule() + rule.getPegaComposicao() + ":[deadlock free[FD]]");
rule.setRule(rule.getRule() + d1.implementation_Condition_D1());
rule.setRule(rule.getRule() + d2.implementation_Condition_D1());
rule.setRule(rule.getRule() + condition_d4.implementationCondiction_D4());
rule.setRule(rule.getRule() + condition_D5.implementationCondiction_D5());
rule.setRule(rule.getRule() + condition_D6.implementationCondition_D6_1());
rule.setRule(rule.getRule() + condition_D6.implementationCondition_D6_2());
rule.setRule(rule.getRule() + condition_D7.implementationCondition_D7());
//Finding a valid protocol implementation
//protocolos
//rule.setRule(rule.getRule() + "\n\n" + createP.creatorFunction() + "\n\n");
FileWriter rule2 = null;
try {
rule2 = new FileWriter(new File("cspFiles/" + c.getName() + ".csp"));
PrintWriter p = new PrintWriter(rule2);
p.flush();
// escreve no arquivo os tipos e canais que estão na string 'csp, em seguida escreve uma string com o
// comportamento que é recebido do comtrato
p.write(rule.getRule());
rule2.close();
p.close();
} catch (IOException ex) {
//Logger.getLogger(instance.getName()).log(Level.SEVERE, null, ex);
}
}
}