/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package CSP_CREATOR;
import GUI.TelaInicial;
import LOGIC.Channel;
import LOGIC.Componente;
import LOGIC.Contract;
import LOGIC.Instance;
import LOGIC.ObjectList;
import LOGIC.Protocols;
import java.io.Serializable;
import java.util.LinkedList;
/**
*
* @author sarah
*/
public class CreatorNewComponent extends Instance implements Serializable {
private Contract c;
private Componente component;
LinkedList<Instance> instance;
private LinkedList<Protocols> prot;
private String rename;
public CreatorNewComponent(LinkedList<Instance> i, LinkedList<Protocols> listProt) {
component = new Componente();
instance = i;
component.setInstance1(instance.get(0));
component.setInstance2(instance.get(1));
c = new Contract();
prot = listProt;
rename = "";
}
private Componente setarNewInstance() {
// lista de canais
//setando canais
for (Instance instance1 : instance) {
for (int i = 0; i < instance1.getChannel().size(); i++) {
setChannelInInstanceList(instance1.getChannel().get(i));
}
}
//setando os eventos de in
for (Instance instance1 : instance) {
for (int i = 0; i < instance1.getIn().size(); i++) {
component.getIn().add(instance1.getIn().get(i));
}
}
//setando os eventos de out
for (Instance instance1 : instance) {
for (int i = 0; i < instance1.getOut().size(); i++) {
component.getOut().add(instance1.getOut().get(i));
}
}
//lista de protocolos
// component.setProtocolos(instance.get(0).getProtocolos());
// component.getProtocolos().addAll(instance.get(1).getProtocolos());
//setando protocolo
for (Instance instance1 : instance) {
for (int i = 0; i < instance1.getProtocolos().size(); i++) {
setCCInProtList(instance1.getProtocolos().get(i));
}
}
//nome contrato
c.setName(instance.get(0).getName() + "_"
+ instance.get(1).getName());
//setar canais de contrato
for (Instance instance1 : instance) {
for (int i = 0; i < instance1.getContrato().getChannel().size(); i++) {
//c.getChannel().add(instance1.getContrato().getChannel().get(i));
setChannelInContractList(instance1.getContrato().getChannel().get(i));
//c.setChannel(instance1.getContrato().getChannel());
//System.out.println("Canais do contrato da instancia:" +
// instance1.getContrato().getChannel() + "- " +c.getChannel());
}
}
//component.setContrato(c);
return component;
}
//criei um método para cada componente prq tanto comportamento como nome mudam
public Componente creatorNewComponentIterleave() {
component.setName(instance.get(0).getName() + "_" + instance.get(1).getName() + "_INTER");
component.setFlag(true);
setarNewInstance();
//setando comportamento do novo componente
//o comportamento é setado no contrato
/*if (instance.get(0).getContrato().getBehavior().equals(instance.get(1).getContrato().getBehavior())) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else {
c.setBehavior(instance.get(0).getContrato().getBehavior() + "\n" +
instance.get(1).getContrato().getBehavior());
}*/
int st1 = instance.get(0).getContrato().getBehavior().indexOf("=");
int st2 = instance.get(1).getContrato().getBehavior().indexOf("=");
String s1 = instance.get(0).getContrato().getBehavior().substring(0, st1 - 1);
String s2 = instance.get(1).getContrato().getBehavior().substring(0, st2 - 1);
if (instance.get(0).getContrato().getBehavior().equals(instance.get(1).getContrato().getBehavior())) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else if (s1.equals(s2)) {
if (instance.get(0).getContrato().getBehavior().length()
> instance.get(1).getContrato().getBehavior().length()) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else {
c.setBehavior(instance.get(1).getContrato().getBehavior());
}
} else if (instance.get(0).getContrato().getBehavior().contains(instance.get(1).getContrato().getBehavior())) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else if (instance.get(1).getContrato().getBehavior().contains(instance.get(0).getContrato().getBehavior())) {
c.setBehavior(instance.get(1).getContrato().getBehavior());
} else {
c.setBehavior(instance.get(0).getContrato().getBehavior() + "\n\n"
+ instance.get(1).getContrato().getBehavior());
}
//usando a função rename para colocar no comportamento do novo comp
for (Instance instance1 : instance) {
//c = instance1.getContrato();
rename = rename + "Inst_" + instance1.getName() + " = <";
for (int i = 0; i < instance1.getChannel().size(); i++) {
if (i == instance1.getChannel().size() - 1) {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ ")";
} else {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ "),";
}
}
rename = rename + "> \n";
rename = rename + instance1.getName() + " = rename(" + instance1.getContrato().getName()
+ ", Inst_" + instance1.getName() + ") \n\n";
}
c.setBehavior(c.getBehavior() + "\n" + instance.get(0).getName() + "_"
+ instance.get(1).getName() + " = INTER(" + instance.get(0).getName() + ", "
+ instance.get(1).getName() + ")" + "\n"
+ "\n\n" + rename);
component.setContrato(c);
return component;
}
//criei um método para cada componente prq tanto comportamento como nome mudam
public Componente creatorNewComponentComunication() {
component.setName(instance.get(0).getName() + "_" + instance.get(1).getName() + "_COMM");
component.setFlag(true);
setarNewInstance();
//setando comportamento do novo componente
//o comportamento é setado no contrato
int st1 = instance.get(0).getContrato().getBehavior().indexOf("=");
int st2 = instance.get(1).getContrato().getBehavior().indexOf("=");
String s1 = instance.get(0).getContrato().getBehavior().substring(0, st1 - 1);
String s2 = instance.get(1).getContrato().getBehavior().substring(0, st2 - 1);
if (instance.get(0).getContrato().getBehavior().equals(instance.get(1).getContrato().getBehavior())) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else if (s1.equals(s2)) {
if (instance.get(0).getContrato().getBehavior().length()
> instance.get(1).getContrato().getBehavior().length()) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else {
c.setBehavior(instance.get(1).getContrato().getBehavior());
}
}/*else if(instance.get(1).getBehavior().contains(s1)){
int v = instance.get(1).getContrato().getBehavior().indexOf(instance.get(1).getName());
String apaga = instance.get(0).getContrato().getBehavior().substring(st1, v);
}
*/ else if (instance.get(0).getContrato().getBehavior().contains(instance.get(1).getContrato().getBehavior())) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else if (instance.get(1).getContrato().getBehavior().contains(instance.get(0).getContrato().getBehavior())) {
c.setBehavior(instance.get(1).getContrato().getBehavior());
} else {
c.setBehavior(instance.get(0).getContrato().getBehavior()
+ instance.get(1).getContrato().getBehavior());
}
//usando a função rename para colocar no comportamento do novo comp
for (Instance instance1 : instance) {
//c = instance1.getContrato();
rename = rename + "Inst_" + instance1.getName() + " = <";
for (int i = 0; i < instance1.getChannel().size(); i++) {
if (i == instance1.getChannel().size() - 1) {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ ")";
} else {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ "),";
}
}
rename = rename + "> \n";
rename = rename + instance1.getName() + " = rename(" + instance1.getContrato().getName()
+ ", Inst_" + instance1.getName() + ") \n\n";
}
//COMM(P,Q,c1,c2)
c.setBehavior(c.getBehavior() + "\n" + instance.get(0).getName() + "_"
+ instance.get(1).getName() + " = COMM(" + instance.get(0).getName() + ", "
+ instance.get(1).getName() + "," + prot.get(0).getCc() + ","
+ prot.get(1).getCc() + ")" + "\n"
+ "\n\n" + rename);
component.setContrato(c);
return component;
}
//criei um método para cada componente prq tanto comportamento como nome mudam
public Componente creatorNewComponentFeedBack() {
component.setName(instance.get(0).getName() + "_" + instance.get(1).getName() + "_FEED");
component.setFlag(true);
setarNewInstance();
//setando comportamento do novo componente
//o comportamento é setado no contrato
if (instance.get(0).getContrato().getBehavior().equals(instance.get(1).getContrato().getBehavior())) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else {
c.setBehavior(instance.get(0).getContrato().getBehavior() + "\n\n" + instance.get(1).getContrato().getBehavior());
}
//usando a função rename para colocar no comportamento do novo comp
for (Instance instance1 : instance) {
//c = instance1.getContrato();
rename = rename + "Inst_" + instance1.getName() + " = <";
for (int i = 0; i < instance1.getChannel().size(); i++) {
if (i != instance1.getChannel().size() - 1) {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ "),";
} else {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ ")";
}
}
rename = rename + "> \n";
rename = rename + instance1.getName() + " = rename(" + instance1.getContrato().getName()
+ ", Inst_" + instance1.getName() + ") \n\n";
}
//FEED(P,c1,c2)
c.setBehavior(c.getBehavior() + "\n" + instance.get(0).getName() + "_"
+ instance.get(1).getName() + " = FEED(" + instance.get(0).getName() + ", "
+ prot.get(0).getCc() + ","
+ prot.get(1).getCc() + ")" + "\n"
+ "\n\n" + rename);
component.setContrato(c);
return component;
}
//criei um método para cada componente prq tanto comportamento como nome mudam
public Componente creatorNewComponentReflexive() {
component.setName(instance.get(0).getName() + "_" + instance.get(1).getName() + "_REF");
component.setFlag(true);
setarNewInstance();
//setando comportamento do novo componente
//o comportamento é setado no contrato
if (instance.get(0).getContrato().getBehavior().equals(instance.get(1).getContrato().getBehavior())) {
c.setBehavior(instance.get(0).getContrato().getBehavior());
} else {
c.setBehavior(instance.get(0).getContrato().getBehavior() + "\n" + instance.get(1).getContrato().getBehavior());
}
//usando a função rename para colocar no comportamento do novo comp
for (Instance instance1 : instance) {
//c = instance1.getContrato();
rename = rename + "Inst_" + instance1.getName() + " = <";
for (int i = 0; i < instance1.getChannel().size(); i++) {
if (i == instance1.getChannel().size() - 1) {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ ")";
} else {
rename = rename + "(" + instance1.getContrato().getChannel().get(i).getName()
+ "," + instance1.getChannel().get(i).getName()
+ "),";
}
}
rename = rename + "> \n";
rename = rename + instance1.getName() + " = rename(" + instance1.getContrato().getName()
+ ", Inst_" + instance1.getName() + ") \n\n";
}
//REF(P,c1,c2)
c.setBehavior(c.getBehavior() + "\n" + instance.get(0).getName() + "_"
+ instance.get(1).getName() + " = REF(" + instance.get(0).getName() + ", "
+ prot.get(0).getCc() + ","
+ prot.get(1).getCc() + ")" + "\n"
+ "\n\n" + rename);
component.setContrato(c);
return component;
}
public void setChannelInInstanceList(Channel c) {
if (isntChannelInInstanceList(c)) {
component.getChannel().add(c);
}
}
public boolean isntChannelInInstanceList(Channel c) {
for (int i = 0; i < component.getChannel().size(); i++) {
if (c == component.getChannel().get(i)) {
return false;
}
}
return true;
}
public boolean isntChannelInContractList(Channel channel) {
for (int i = 0; i < c.getChannel().size(); i++) {
if (channel == c.getChannel().get(i)) {
return false;
}
}
return true;
}
public void setChannelInContractList(Channel channel) {
if (isntChannelInContractList(channel)) {
c.getChannel().add(channel);
}
}
public boolean isntCCInProtList(Protocols p) {
for (int i = 0; i < component.getProtocolos().size(); i++) {
if (p.getCc().equals(component.getProtocolos().get(i).getCc())) {
return false;
}
}
return true;
}
public void setCCInProtList(Protocols p) {
int i = 0;
if (isntCCInProtList(p)) {
//component.getProtocolos().add(p);
component.getProtocolos().add(p);
}
}
}