Skip to content

Commit

Permalink
algoritmo de recomendação
Browse files Browse the repository at this point in the history
  • Loading branch information
AdsonEsteves committed Dec 5, 2020
1 parent 17d0cf6 commit e282676
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 50 deletions.
9 changes: 8 additions & 1 deletion ServerAgentsServices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->

<service>
<name>groupStudents</name>
<name>getStudentGroups</name>
<description>Agrupa os alunos em grupos</description>
<entity>GrouperAgent</entity>
<scope>mas</scope>
Expand Down Expand Up @@ -133,6 +133,13 @@
<scope>components</scope>
<organization>SACIP</organization>
</service>
<service>
<name>getContentByTags</name>
<description>traz conteudos selecionados a partir de tags enviadas</description>
<entity>DBConnection</entity>
<scope>components</scope>
<organization>SACIP</organization>
</service>
<service>
<name>deleteContent</name>
<description>remove um conteudo do banco de dados</description>
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/sacip/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.midas.as.AgentServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Launcher {
Expand All @@ -24,7 +25,18 @@ public static void main(String[] args) {
iniciandoAgentesServidor(localport+"", serverport, serverAddress);
localport++;
iniciandoAgentesUsuario(localport+"", serverport, serverAddress);
SpringApplication.run(Launcher.class, args);

// iniciandoAgentesContainerUnico(localport+"", serverport, serverAddress);

ApplicationContext ctx = SpringApplication.run(Launcher.class, args);

// System.out.println("Let's inspect the beans provided by Spring Boot:");

// String[] beanNames = ctx.getBeanDefinitionNames();
// //Arrays.sort(beanNames);
// for (String beanName : beanNames) {
// System.out.println(beanName);
// }

} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -57,4 +69,13 @@ public static void iniciandoAgentesServidor(String localport, String serverport,
ServerAgentsStructureXML = ServerAgentsStructureXML.replace("$localport", localport).replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("</name>", instancia+"</name>");
AgentServer.initialize(true, true, ServerAgentsStructureXML, ServerAgentsServicesXML);
}

public static void iniciandoAgentesContainerUnico(String localport, String serverport, String serverAddress) throws IOException
{
//inicializando Agentes do Servidor.
String ServerAgentsStructureXML = readFile("structure.xml");
String ServerAgentsServicesXML = readFile("services.xml");
ServerAgentsStructureXML = ServerAgentsStructureXML.replace("$localport", localport).replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("</name>", instancia+"</name>");
AgentServer.initialize(true, true, ServerAgentsStructureXML, ServerAgentsServicesXML);
}
}
45 changes: 30 additions & 15 deletions src/main/java/sacip/rest/RestTutor.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
package sacip.rest;

import org.springframework.web.bind.annotation.*;
import java.util.List;

import org.midas.as.AgentServer;
import org.midas.as.manager.execution.ServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/tutor")
public class RestTutor {

private String test = "HELLO WORLD";
private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);

@GetMapping("/requisitaConteudos")
public String getConteudosRecomendados(){
return "";
try {
ServiceWrapper wrapper = AgentServer.require("SACIP", "suggestContent");
List run = wrapper.run();
return run.toString();
} catch (Exception e) {
LOG.error("Falhou criar conta em REST Interface", e);
return "Falhou criar conta: \n"+e.getLocalizedMessage();
}
}

@GetMapping("/trilha/{user}")
public String getTrilhaUsuario(String name) {
return test;
}
// @GetMapping("/trilha/{user}")
// public String getTrilhaUsuario(String name) {
// return test;
// }

@GetMapping("/trilha")
public String getTrilhas(String name) {
return test;
}
// @GetMapping("/trilha")
// public String getTrilhas(String name) {
// return test;
// }

@GetMapping("/dicas")
public String getDicas(String name) {
return test;
}
// @GetMapping("/dicas")
// public String getDicas(String name) {
// return test;
// }

}

8 changes: 5 additions & 3 deletions src/main/java/sacip/sti/agents/GrouperAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
import org.midas.as.agent.templates.LifeCycleException;
import org.midas.as.agent.templates.ServiceException;

import sacip.sti.dataentities.Student;

public class GrouperAgent extends Agent{

@Override
public void provide(String service, Map in, List out) throws ServiceException {
// TODO Auto-generated method stub
if(service.equals("classifyStudents"))
if(service.equals("getStudentGroups"))
{
System.out.println("Buscando alunos no banco");
System.out.println("Classificando alunos");

String aluno = (String) in.get("aluno");
Student aluno = (Student) in.get("estudante");

System.out.println("Selecionando cluster de aluno "+aluno+" requisitado");
System.out.println("Selecionando cluster de aluno "+aluno.getName()+" requisitado");
System.out.println("retornando grupo");
}
}
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/sacip/sti/agents/InterfaceAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonFormat;

import org.midas.as.AgentServer;
import org.midas.as.agent.board.Message;
import org.midas.as.agent.board.MessageListener;
Expand All @@ -20,10 +22,12 @@
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import sacip.sti.dataentities.Content;
import sacip.sti.dataentities.Student;

@RestController
@RequestMapping("/interface")
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
public class InterfaceAgent extends Agent implements MessageListener{

private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);
Expand Down Expand Up @@ -51,7 +55,7 @@ public String criaNovaConta(@RequestBody Student conta) {

try {
ServiceWrapper wrapper = AgentServer.require("SACIP", "createStudent");
wrapper.addParameter("novaConta", conta);
wrapper.addParameter("conta", conta);
List run = wrapper.run();
return run.toString();
} catch (Exception e) {
Expand All @@ -69,6 +73,21 @@ public String fazLogin() {
public String editarConta(String name) {
return "test";
}

@PostMapping("/conteudos")
@ResponseBody
public String criaNovoConteudo(@RequestBody Content conteudo) {

try {
ServiceWrapper wrapper = AgentServer.require("SACIP", "createContent");
wrapper.addParameter("conteudo", conteudo);
List run = wrapper.run();
return run.toString();
} catch (Exception e) {
LOG.error("Falhou criar conteudo em REST Interface", e);
return "Falhou criar conteudo: \n"+e.getLocalizedMessage();
}
}

@GetMapping("/pergunta")
public String buscaPergunta()
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/sacip/sti/agents/PedagogicalAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public class PedagogicalAgent extends Agent implements MessageListener{
public PedagogicalAgent() {
super();
this.instancia = Launcher.instancia;
List<String> preferencias = new ArrayList<>();
preferencias.add("Animação");
preferencias.add("Filmes");
this.student = new Student("Adson", "123456", "link", "masculino", 24, "bacharelado", preferencias);
}

@Override
public void provide(String service, Map in, List out) throws ServiceException {
// TODO Auto-generated method stub

switch (service) {
case "getAluno":
out.add(getAluno());
Expand Down Expand Up @@ -63,16 +65,20 @@ private String suggestContent()
try
{
//Pegar grupo de alunos
ServiceWrapper wrapper = require("SACIP", "getStudentGroups");
wrapper.addParameter("estudante", getAluno());
List grupo = wrapper.run();

//Pedir recomendação para o recomendador
ServiceWrapper wrapper2 = require("SACIP", "getRecommendedContent");
wrapper2.addParameter("estudante", getAluno());
wrapper2.addParameter("grupo", grupo);
ServiceWrapper servicoGetGroups = require("SACIP", "getStudentGroups");
servicoGetGroups.addParameter("estudante", getAluno());
List grupo = servicoGetGroups.run();

String recomendacoes = (String) wrapper.run().get(0);
//Pedir recomendação para o recomendador
ServiceWrapper servicoGetContent = require("SACIP", "getRecommendedContent");
servicoGetContent.addParameter("estudante", getAluno());
servicoGetContent.addParameter("grupo", grupo);
List resultado = servicoGetContent.run();
if(resultado.isEmpty())
{
return "vazio";
}
String recomendacoes = (String) resultado.get(0);

return recomendacoes;

Expand Down
75 changes: 66 additions & 9 deletions src/main/java/sacip/sti/agents/RecommenderAgent.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package sacip.sti.agents;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.midas.as.AgentServer;
import org.midas.as.agent.templates.Agent;
import org.midas.as.agent.templates.LifeCycleException;
import org.midas.as.agent.templates.ServiceException;
import org.midas.as.manager.execution.ServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sacip.sti.dataentities.Content;
import sacip.sti.dataentities.Student;

public class RecommenderAgent extends Agent {
Expand All @@ -32,11 +37,12 @@ public void provide(String service, Map in, List out) throws ServiceException {

@Override
protected void lifeCycle() throws LifeCycleException, InterruptedException {
// TODO Auto-generated method stub
//Board.setContextAttribute("eventState", "checkErrors");
}

private String getConteudosRecomendados(List<Student> grupo, Student aluno) {

List<String> preferenciasAluno = aluno.getPreferencias();
try
{
List<String> caracteristicas = new ArrayList<>();
Expand All @@ -49,18 +55,44 @@ private String getConteudosRecomendados(List<Student> grupo, Student aluno) {
}
else
{
caracteristicas.add(aluno.getPreferenciasAsString());
caracteristicas.addAll(preferenciasAluno);
}

System.out.println("buscando caracteristicas em conteudos");


//Fazer chamada ao banco
ServiceWrapper wrapper = require("SACIP", "getContentByTags");
wrapper.addParameter("tags", caracteristicas);
List resultado = wrapper.run();
if(resultado.get(0)==null || resultado.get(0) instanceof String)
{
return "não há conteúdos";
}
List<Content> conteudos = (List<Content>) resultado.get(0);

List<Content> sortedContent = new ArrayList<Content>(){
@Override
public boolean add(Content e) {
super.add(e);
Collections.sort(this, new Comparator<Content>(){
@Override
public int compare(Content o1, Content o2) {
return o2.pontos-o1.pontos;
}
});
return true;
}
};

System.out.println("retornando conteudos");

String exercicio = "";
for (Content content : conteudos) {
content.pontos += calculateTagPoints(content.getTags(), preferenciasAluno);
content.pontos += calculateDistancePoints(aluno.getTrilha(), content);

sortedContent.add(content);
}

//retornando conteudos
String exercicio = sortedContent.toString();

return exercicio;
return exercicio;
}
catch (Exception e)
{
Expand All @@ -70,4 +102,29 @@ private String getConteudosRecomendados(List<Student> grupo, Student aluno) {
}
}

private int calculateTagPoints(List<String> tagsConteudo, List<String> preferenciasAluno)
{
int pontos = 0;

for (String tagConteudo : tagsConteudo) {
if(preferenciasAluno.contains(tagConteudo))
{
pontos++;
}
}

return pontos;
}

private int calculateDistancePoints(List<String> trilha, Content conteudo)
{
int pontos = 0;
ListIterator li = trilha.listIterator(trilha.size());

while(li.hasPrevious())
{
String contTrilha = (String) li.previous();
}
return pontos;
}
}
Loading

0 comments on commit e282676

Please sign in to comment.