From 6007388dd576b480dd762860d291bcc3fc6219f9 Mon Sep 17 00:00:00 2001 From: Adsu Date: Mon, 16 Nov 2020 19:20:14 -0300 Subject: [PATCH] separando os agentes em containeres --- ServerAgentsServices.xml | 152 +++++++++++++ ServerAgentsStructure.xml | 45 ++++ UserAgentsServices.xml | 61 +++++ UserAgentsStructure.xml | 31 +++ services.xml | 36 ++- src/main/java/sacip/Launcher.java | 43 ++-- .../sacip/sti/agents/PedagogicalAgent.java | 34 +-- .../java/sacip/sti/agents/TrackingAgent.java | 212 +++++++++--------- .../sacip/sti/components/DBConnection.java | 10 +- 9 files changed, 446 insertions(+), 178 deletions(-) create mode 100644 ServerAgentsServices.xml create mode 100644 ServerAgentsStructure.xml create mode 100644 UserAgentsServices.xml create mode 100644 UserAgentsStructure.xml diff --git a/ServerAgentsServices.xml b/ServerAgentsServices.xml new file mode 100644 index 0000000..e5cca65 --- /dev/null +++ b/ServerAgentsServices.xml @@ -0,0 +1,152 @@ + + + + + + + + getAnswer + retorna informações do aluno usuário + InterfaceAgent + mas + SACIP + + + + getInterface + retorna informações do aluno usuário + InterfaceAgent + mas + SACIP + + + + createAccount + cria nova conta de usuário + InterfaceAgent + mas + SACIP + + + + + + + + getRecommendedContent + recomenda um exercicio ao remetente + RecommenderAgent + mas + SACIP + + + + + + + + groupStudents + Agrupa os alunos em grupos + GrouperAgent + mas + SACIP + + + groupStudents + Agrupa os alunos em grupos + GrouperAgent + mas + SACIP + + + + + + + + createStudent + registra um novo estudante no banco de dados + DBConnection + components + SACIP + + + findStudents + busca estudantes no banco de dados + DBConnection + components + SACIP + + + editStudent + edita um estudante no banco de dados + DBConnection + components + SACIP + + + getLogsDoAluno + retorna os conteudos usados do aluno + DBConnection + components + SACIP + + + deleteStudent + remove um estudante do banco de dados + DBConnection + components + SACIP + + + storeStudentUseData + guard os dados de uso do usuario no banco de dados + DBConnection + components + SACIP + + + editStudentListAttr + guard os dados de uso do usuario no banco de dados + DBConnection + components + SACIP + + + storeStudentContentUse + guard os dados de uso do usuario no banco de dados + DBConnection + components + SACIP + + + createContent + registra um novo conteudo no banco de dados + DBConnection + components + SACIP + + + findContents + busca conteudos no banco de dados + DBConnection + components + SACIP + + + editContent + edita um conteudo no banco de dados + DBConnection + components + SACIP + + + deleteContent + remove um conteudo do banco de dados + DBConnection + components + SACIP + + + + \ No newline at end of file diff --git a/ServerAgentsStructure.xml b/ServerAgentsStructure.xml new file mode 100644 index 0000000..0596178 --- /dev/null +++ b/ServerAgentsStructure.xml @@ -0,0 +1,45 @@ + + SACIP + + $localport + + $serverport + $serverAddress + + + + + SACIP + sacip.sti + + + + InterfaceAgent + agents + InterfaceAgent + native + + + RecommenderAgent + agents + RecommenderAgent + native + + + GrouperAgent + agents + GrouperAgent + native + + + + + DBConnection + components + DBConnection + native + + + + + \ No newline at end of file diff --git a/UserAgentsServices.xml b/UserAgentsServices.xml new file mode 100644 index 0000000..967544e --- /dev/null +++ b/UserAgentsServices.xml @@ -0,0 +1,61 @@ + + + + + + + + + + getAluno + retorna o estudante em uma classe Student + PedagogicalAgent + mas + SACIP + + + + + + + + storeData + Armazena os dados do usuário + TrackingAgent + mas + SACIP + + + storeSolvedExercise + Armazena os exercícios resolvidos do usuário + TrackingAgent + mas + SACIP + + + storeContentOnPath + Armazena os conteúdos na trilha usuário + TrackingAgent + mas + SACIP + + + storeStudentErrors + Armazena os erros do usuário + TrackingAgent + mas + SACIP + + + \ No newline at end of file diff --git a/UserAgentsStructure.xml b/UserAgentsStructure.xml new file mode 100644 index 0000000..fc687cc --- /dev/null +++ b/UserAgentsStructure.xml @@ -0,0 +1,31 @@ + + SACIP + + $localport + + $serverport + $serverAddress + + + + + SACIP + sacip.sti + + + + PedagogicalAgent + agents + PedagogicalAgent + native + + + TrackingAgent + agents + TrackingAgent + native + + + + + \ No newline at end of file diff --git a/services.xml b/services.xml index d7d7a2a..9b26bc4 100644 --- a/services.xml +++ b/services.xml @@ -4,9 +4,9 @@ - - identifyErrors - verifica erros no código + + - suggestContent - sugere um conteudo + getAluno + retorna o estudante em uma classe Student PedagogicalAgent mas SACIP - + @@ -155,6 +142,13 @@ components SACIP + + getLogsDoAluno + retorna os conteudos usados do aluno + DBConnection + components + SACIP + deleteStudent remove um estudante do banco de dados diff --git a/src/main/java/sacip/Launcher.java b/src/main/java/sacip/Launcher.java index 7c43af2..0658c64 100644 --- a/src/main/java/sacip/Launcher.java +++ b/src/main/java/sacip/Launcher.java @@ -11,32 +11,20 @@ @SpringBootApplication public class Launcher { - public static int instancia = 1; + public static String instancia = ""; public static void main(String[] args) { try { - String localport = "7101"; + int localport = 7101; String serverport = "7100"; String serverAddress = "127.0.0.1"; //String serverAddress = "35.192.97.232"; - String structureXMLtemplate = readFile("structure.xml"); - String servicesXMLtemplate = readFile("services.xml"); - String structureXML; - String servicesXML; - - structureXML = structureXMLtemplate.replace("$localport", localport).replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("", instancia+""); - servicesXML = servicesXMLtemplate.replace("", instancia+"").replace("", instancia+"").replace("", instancia+""); - AgentServer.initialize(true, true, structureXML, servicesXML); - - // instancia++; - - // structureXML = structureXMLtemplate.replace("$localport", "7102").replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("", instancia+""); - // servicesXML = servicesXMLtemplate.replace("", instancia+"").replace("", instancia+"").replace("", instancia+""); - // AgentServer.initialize(true, true, structureXML, servicesXML); - - // SpringApplication.run(Launcher.class, args); + iniciandoAgentesServidor(localport+"", serverport, serverAddress); + localport++; + iniciandoAgentesUsuario(localport+"", serverport, serverAddress); + SpringApplication.run(Launcher.class, args); } catch (IOException e) { e.printStackTrace(); @@ -50,4 +38,23 @@ public static String readFile(String file) throws IOException String actual = Files.readString(fileName); return actual; } + + public static void iniciandoAgentesUsuario(String localport, String serverport, String serverAddress) throws IOException + { + //Inicializando Agentes do Usuário + String UserAgentsStructureXML = readFile("UserAgentsStructure.xml"); + String UserAgentsServicesXML = readFile("UserAgentsServices.xml"); + UserAgentsStructureXML = UserAgentsStructureXML.replace("$localport", localport).replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("", instancia+""); + UserAgentsServicesXML = UserAgentsServicesXML.replace("", instancia+"").replace("", instancia+"").replace("", instancia+""); + AgentServer.initialize(true, true, UserAgentsStructureXML, UserAgentsServicesXML); + } + + public static void iniciandoAgentesServidor(String localport, String serverport, String serverAddress) throws IOException + { + //inicializando Agentes do Servidor. + String ServerAgentsStructureXML = readFile("ServerAgentsStructure.xml"); + String ServerAgentsServicesXML = readFile("ServerAgentsServices.xml"); + ServerAgentsStructureXML = ServerAgentsStructureXML.replace("$localport", localport).replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("", instancia+""); + AgentServer.initialize(true, true, ServerAgentsStructureXML, ServerAgentsServicesXML); + } } \ No newline at end of file diff --git a/src/main/java/sacip/sti/agents/PedagogicalAgent.java b/src/main/java/sacip/sti/agents/PedagogicalAgent.java index 58cd512..abf7475 100644 --- a/src/main/java/sacip/sti/agents/PedagogicalAgent.java +++ b/src/main/java/sacip/sti/agents/PedagogicalAgent.java @@ -15,12 +15,14 @@ import org.slf4j.LoggerFactory; import sacip.Launcher; +import sacip.sti.dataentities.Student; public class PedagogicalAgent extends Agent implements MessageListener{ List alunosOnline = new ArrayList<>(); - private int instancia; + private String instancia; private static Logger LOG = LoggerFactory.getLogger(AgentServer.class); + private Student student; public PedagogicalAgent() { super(); @@ -31,26 +33,9 @@ public PedagogicalAgent() { public void provide(String service, Map in, List out) throws ServiceException { // TODO Auto-generated method stub - if(service.equals("suggestExercise")) + if(service.equals("getAluno")) { - try - { - System.out.println("verificando informações do aluno"); - ServiceWrapper serviceWrapper = require("LocalAgents", "getAluno"); - List alunoInfo = serviceWrapper.run(); - - System.out.println("requisitando exercício"); - ServiceWrapper serviceWrapper2 = require("PublicAgents", "getRecommendedExercises"); - serviceWrapper2.addParameter("aluno", alunoInfo.get(0)); - List exercicio = serviceWrapper2.run(); - - System.out.println("apresentando exercício"); - } - catch(Exception e) - { - throw new ServiceException("Não foi possível pegar o exercício recomendado - PAgent",e); - } - + out.add(getAluno()); } } @@ -89,14 +74,9 @@ public void boardChanged(Message msg) { } - public List checkDicas(Object resposta) + private Student getAluno() { - return new ArrayList<>(); - } - - public void sendDicas(Object dicas) - { - + return this.student; } } diff --git a/src/main/java/sacip/sti/agents/TrackingAgent.java b/src/main/java/sacip/sti/agents/TrackingAgent.java index 6c9baef..b5693b3 100644 --- a/src/main/java/sacip/sti/agents/TrackingAgent.java +++ b/src/main/java/sacip/sti/agents/TrackingAgent.java @@ -35,7 +35,7 @@ public class TrackingAgent extends Agent implements MessageListener { private static Logger LOG = LoggerFactory.getLogger(AgentServer.class); - private int instancia; + private String instancia; public TrackingAgent() { super(); @@ -45,115 +45,34 @@ public TrackingAgent() { @Override public void provide(String service, Map in, List out) throws ServiceException { - if (service.equals("storeData")) { - JsonNode dados = (JsonNode) in.get("dados"); - if (dados.isObject()) { - ObjectNode jsonobject = (ObjectNode) dados; - String nome = jsonobject.get("nome").asText(); - - List dadosC = new ArrayList<>(); - List dadosO = new ArrayList<>(); - List dadosA = new ArrayList<>(); - List dadosE = new ArrayList<>(); - if (jsonobject.has("cliques")) { - if (jsonobject.get("cliques").isArray()) { - ArrayNode cliqueArray = (ArrayNode) jsonobject.get("cliques"); - for (JsonNode jsonNode : cliqueArray) { - String modulo = jsonNode.get("modulo").asText(); - switch (modulo) { - case "Exemplo": - dadosE.add(jsonNode.toString()); - break; - - case "Conteudo": - dadosC.add(jsonNode.toString()); - break; - - case "Ajuda": - dadosA.add(jsonNode.toString()); - break; - - case "OGPor": - dadosO.add(jsonNode.toString()); - break; - } - } - } + JsonNode dados = (JsonNode) in.get("dados"); + String nome = dados.get("nome").asText(); - Map dadoss = Map.of("Conteudo", dadosC, "OGPor", dadosO, "Ajuda", dadosA, - "Exemplos", dadosE); - - try { - ServiceWrapper wrapper = require("SACIP", "storeStudentUseData"); - wrapper.addParameter("name", nome); - wrapper.addParameter("data", dadoss); - out.add(wrapper.run().get(0)); - } catch (Exception e) { - out.add(e.getLocalizedMessage()); - LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e); - e.printStackTrace(); - } + switch (service) + { + case "storeData": + if (dados.has("cliques")) { + out.add(storeClicks(dados, nome)); } - if (jsonobject.has("conteudo")) { - try { - ServiceWrapper wrapper = require("SACIP", "storeStudentContentUse"); - wrapper.addParameter("name", nome); - wrapper.addParameter("content", jsonobject.get("conteudo")); - out.add(wrapper.run().get(0)); - } catch (Exception e) { - out.add(e.getLocalizedMessage()); - LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e); - e.printStackTrace(); - } + if (dados.has("conteudo")) { + out.add(storeUsedContent(dados, nome)); } - - } - - } else if (service.equals("storeSolvedExercise")) { - JsonNode dados = (JsonNode) in.get("dados"); - String nome = dados.get("nome").asText(); - - try { - ServiceWrapper wrapper = require("SACIP", "editStudentListAttr"); - wrapper.addParameter("name", nome); - wrapper.addParameter("attrName", "exerciciosResolvidos"); - wrapper.addParameter("newValue", dados.get("conteudo")); - out.add(wrapper.run().get(0)); - } catch (Exception e) { - out.add(e.getLocalizedMessage()); - LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e); - e.printStackTrace(); - } - } else if (service.equals("storeContentOnPath")) { - JsonNode dados = (JsonNode) in.get("dados"); - String nome = dados.get("nome").asText(); - - try { - ServiceWrapper wrapper = require("SACIP", "editStudentListAttr"); - wrapper.addParameter("name", nome); - wrapper.addParameter("attrName", "trilha"); - wrapper.addParameter("newValue", dados.get("conteudo")); - out.add(wrapper.run().get(0)); - } catch (Exception e) { - out.add(e.getLocalizedMessage()); - LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e); - e.printStackTrace(); - } - } else if (service.equals("storeStudentErrors" + this.instancia)) { - JsonNode dados = (JsonNode) in.get("dados"); - String nome = dados.get("nome").asText(); - - try { - ServiceWrapper wrapper = require("SACIP", "editStudentListAttr"); - wrapper.addParameter("name", nome); - wrapper.addParameter("attrName", "errosDoEstudante"); - wrapper.addParameter("newValue", dados.get("conteudo")); - out.add(wrapper.run().get(0)); - } catch (Exception e) { - out.add(e.getLocalizedMessage()); - LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e); - e.printStackTrace(); - } + break; + + case "storeSolvedExercise": + out.add(addStudentData(dados.get("conteudo"), nome, "exerciciosResolvidos")); + break; + + case "storeContentOnPath": + out.add(addStudentData(dados.get("conteudo"), nome, "trilha")); + break; + + case "storeStudentErrors": + out.add(addStudentData(dados.get("conteudo"), nome, "errosDoEstudante")); + break; + + default: + break; } } @@ -164,7 +83,7 @@ protected void lifeCycle() throws LifeCycleException, InterruptedException { while (alive) { LOG.info("INSTANCIA " + this.instancia + " viva"); - Thread.sleep(1000); + Thread.sleep(300000); try { ServiceWrapper wrapper = require("SACIP", "getAluno"); Student estudante = (Student) wrapper.run(); @@ -191,12 +110,87 @@ public void boardChanged(Message msg) { } + private Object storeClicks(JsonNode dados, String nome) + { + List dadosC = new ArrayList<>(); + List dadosO = new ArrayList<>(); + List dadosA = new ArrayList<>(); + List dadosE = new ArrayList<>(); + + if (dados.get("cliques").isArray()) { + ArrayNode cliqueArray = (ArrayNode) dados.get("cliques"); + for (JsonNode jsonNode : cliqueArray) { + String modulo = jsonNode.get("modulo").asText(); + switch (modulo) { + case "Exemplo": + dadosE.add(jsonNode.toString()); + break; + + case "Conteudo": + dadosC.add(jsonNode.toString()); + break; + + case "Ajuda": + dadosA.add(jsonNode.toString()); + break; + + case "OGPor": + dadosO.add(jsonNode.toString()); + break; + } + } + } + + Map dadoss = Map.of("Conteudo", dadosC, "OGPor", dadosO, "Ajuda", dadosA, + "Exemplos", dadosE); + + try { + ServiceWrapper wrapper = require("SACIP", "storeStudentUseData"); + wrapper.addParameter("name", nome); + wrapper.addParameter("data", dadoss); + return wrapper.run().get(0); + } catch (Exception e) { + LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e); + e.printStackTrace(); + return e.getLocalizedMessage(); + } + } + + private Object storeUsedContent(JsonNode dados, String nome) + { + try { + ServiceWrapper wrapper = require("SACIP", "storeStudentContentUse"); + wrapper.addParameter("name", nome); + wrapper.addParameter("content", dados.get("conteudo")); + return wrapper.run().get(0); + } catch (Exception e) { + LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS", e); + e.printStackTrace(); + return e.getLocalizedMessage(); + } + } + + private Object addStudentData(JsonNode conteudo, String nome, String attrName) + { + try { + ServiceWrapper wrapper = require("SACIP", "editStudentListAttr"); + wrapper.addParameter("name", nome); + wrapper.addParameter("attrName", attrName); + wrapper.addParameter("newValue", conteudo); + return wrapper.run().get(0); + } catch (Exception e) { + LOG.error("ERRO NO TRACKING AGENT AO ENVIAR DADOS de "+attrName, e); + e.printStackTrace(); + return e.getLocalizedMessage(); + } + } + private void descobrirTempoGastoPorTag(Student estudante) throws ServiceWrapperException, InterruptedException, ExecutionException, JsonMappingException, JsonProcessingException { //Fazer busca dos conteudos usados - ServiceWrapper wrapper = require("SACIP", "getConteudosAluno"); + ServiceWrapper wrapper = require("SACIP", "getLogsDoAluno"); wrapper.addParameter("name", estudante.getName()); wrapper.addParameter("type", "USE"); List resposta = wrapper.run(); diff --git a/src/main/java/sacip/sti/components/DBConnection.java b/src/main/java/sacip/sti/components/DBConnection.java index e5cac52..9adf05a 100644 --- a/src/main/java/sacip/sti/components/DBConnection.java +++ b/src/main/java/sacip/sti/components/DBConnection.java @@ -52,6 +52,10 @@ public void provide(String service, Map in, List out) throws ServiceException { case "editStudentListAttr": out.add(editUserListAttr((String)in.get("name"), (String)in.get("attrName"), (JsonNode)in.get("newValue"))); break; + + case "getLogsDoAluno": + out.add(getUserLogInformation((String)in.get("name"), (String)in.get("type"))); + break; case "deleteStudent": out.add(deleteUser((String) in.get("name"))); @@ -66,7 +70,7 @@ public void provide(String service, Map in, List out) throws ServiceException { break; case "getStudentsContentUse": - out.add(getUsersUseInformation((String)in.get("name"), (String)in.get("type"))); + out.add(getUserLogInformation((String)in.get("name"), (String)in.get("type"))); break; case "createContent": @@ -238,7 +242,7 @@ private Object addClickInformation(String name, Map cliqueReg){ } } - private Object getUsersUseInformation(String name, String type) + private Object getUserLogInformation(String name, String type) { try { if(type.equals("CLICK")) @@ -256,7 +260,7 @@ else if(type.equals("USE")) return result.toString(); } - throw new ServiceException("Usuario não possuem esse tipo de informação"); + throw new ServiceException("Usuario não possui esse tipo de informação"); } catch (Exception e) { LOG.error("Não foi possível registrar os cliques do estudante no banco", e);