Commit ee6a22a1 authored by javier's avatar javier
Browse files

Initial commit

parents
Pipeline #63 canceled with stages
<!--
Copyright 20018 Information Retrieval Lab, University of A Coruña
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>erisk-dummy-participant</artifactId>
<groupId>org.irlab.erisk</groupId>
<version>0.1</version>
<name>eRisk Dumy Participant</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.irlab.erisk.util.client.Client</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.7</version>
</dependency>
<!-- HTTPClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- LOGGING -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
/*******************************************************************************
* Copyright 20018 Information Retrieval Lab, University of A Coruña
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
package org.irlab.erisk.util.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.irlab.erisk.util.client.vo.Post;
import org.irlab.erisk.util.client.vo.TeamDecision;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Client {
private static final Logger logger = LoggerFactory.getLogger(Client.class);
private static String ENDPOINT_GET_WRITINGS = "http://erisk.irlab.org/challenge-service/getwritings/%s";
private static String ENDPOINT_SUBMIT_DECISIONS = "http://erisk.irlab.org/challenge-service/submit/%s/%d";
public static List<Post> getUserWritings(String teamToken) {
final ObjectMapper mapper = new ObjectMapper();
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpGet request = new HttpGet(String.format(ENDPOINT_GET_WRITINGS, teamToken));
logger.info("Requesting: {}", request);
request.addHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(request);
Post[] writings = mapper.readValue(response.getEntity().getContent(), Post[].class);
logger.debug("Subject object: {}", Arrays.toString(writings));
return Arrays.asList(writings);
} catch (IOException e) {
logger.info("IOError network: {}", e);
}
return new ArrayList<>();
}
public static void submitDecisions(List<Post> posts, String teamToken, int run) {
TeamDecision[] decisions = new TeamDecision[posts.size()];
for (int i = 0; i < decisions.length; i++) {
decisions[i] = new TeamDecision(posts.get(i).getNick(),
ThreadLocalRandom.current().nextInt(0, 2),
ThreadLocalRandom.current().nextDouble(0, 10));
}
String body = "";
final ObjectMapper mapper = new ObjectMapper();
try {
body = mapper.writeValueAsString(decisions);
} catch (IOException e1) {
logger.info("JSON mapper fail: {}", e1);
}
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost request = new HttpPost(
String.format(ENDPOINT_SUBMIT_DECISIONS, teamToken, run));
StringEntity params = new StringEntity(body);
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
TeamDecision[] acceptedDecissions = mapper.readValue(response.getEntity().getContent(),
TeamDecision[].class);
logger.debug("Accepted decissions: {}", Arrays.toString(acceptedDecissions));
} catch (IOException e) {
logger.info("IOError network: {}", e);
}
}
public static void main(String[] args) throws IOException {
String teamToken = "XASADSFASFAaSFAF";
int runs = 5;
List<Post> posts = getUserWritings(teamToken);
int sequence = 0;
while (!posts.isEmpty()) {
logger.info("Sequence {} with {} posts", sequence, posts.size());
for (int run = 0; run < runs; run++) {
submitDecisions(posts, teamToken, run);
}
posts = getUserWritings(teamToken);
sequence++;
}
}
}
/*******************************************************************************
* Copyright 20018 Information Retrieval Lab, University of A Coruña
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
package org.irlab.erisk.util.client.vo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Post {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.000+0000'");
private Integer id;
private Integer number;
private String nick;
private Integer redditor;
private String title;
private String content;
private Calendar date;
private static final Logger logger = LoggerFactory.getLogger(Post.class);
public Post() {
super();
}
public Post(Integer id, Integer number, String nick, Integer redditor, String title,
String content, String date) {
super();
this.id = id;
this.number = number;
this.nick = nick;
this.redditor = redditor;
this.title = title;
this.content = content;
this.date = Calendar.getInstance();
try {
this.date.setTime(dateFormat.parse(date));
} catch (ParseException e) {
logger.info("Date format exception: {}", e);
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Integer getRedditor() {
return redditor;
}
public void setRedditor(Integer redditor) {
this.redditor = redditor;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public Calendar getDate() {
return date;
}
public void setDate(String date) {
this.date = Calendar.getInstance();
try {
this.date.setTime(dateFormat.parse(date));
} catch (ParseException e) {
logger.info("Date format exception: {}", e);
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Post [dateFormat=" + dateFormat + ", id=" + id + ", number=" + number + ", nick="
+ nick + ", redditor=" + redditor + ", title=" + title + ", content=" + content
+ ", date=" + date + "]";
}
}
/*******************************************************************************
* Copyright 20018 Information Retrieval Lab, University of A Coruña
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
package org.irlab.erisk.util.client.vo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class TeamDecision {
private String nick;
private Integer decision;
private Double score;
public TeamDecision() {
super();
}
public TeamDecision(String nick, Integer decision, Double score) {
super();
this.nick = nick;
this.decision = decision;
this.score = score;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public Integer getDecision() {
return decision;
}
public void setDecision(Integer decision) {
this.decision = decision;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "TeamDecision [nick=" + nick + ", decision=" + decision + ", score=" + score + "]";
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment