Skip to content

Commit 6f1e730

Browse files
committed
Added Tensorflow
Needs an updated libc6
1 parent 38be19b commit 6f1e730

File tree

14 files changed

+447
-7
lines changed

14 files changed

+447
-7
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ branches:
55
language: java
66
jdk:
77
- oraclejdk8
8+
dist: trusty
89
sudo: false
10+
group: beta
911
cache:
1012
yarn: true
1113
directories:
@@ -15,6 +17,7 @@ cache:
1517
addons:
1618
apt:
1719
packages:
20+
- libc6
1821
- graphviz
1922
- ca-certificates
2023
ssh_known_hosts:

jee-domain/src/test/java/net/trajano/jee/domain/dao/test/BaseIntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public abstract class BaseIntegrationTest {
4646
public static void setupInfrastructure() throws SecurityException,
4747
IOException {
4848

49-
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("basejpatest-logging.properties")) {
49+
try (final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("basejpatest-logging.properties")) {
5050
LogManager.getLogManager().readConfiguration(is);
5151
}
5252

nlp/pom.xml

+39-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
<packaging>ejb</packaging>
1111
<name>Natural Language Processing backend</name>
1212
<description>This is an advanced EJB implementation that utilizes Java 8 runtime with CoreNLP and Kotlin as the primary language for tests and the API rather than Java. Because of debugging purposes, the implementation itself is written in Java.</description>
13-
<properties>
14-
<maven.javadoc.skip>true</maven.javadoc.skip>
15-
</properties>
1613
<dependencies>
1714
<dependency>
1815
<groupId>${project.groupId}</groupId>
@@ -83,9 +80,9 @@
8380
</exclusions>
8481
</dependency>
8582
<dependency>
86-
<groupId>org.jetbrains.kotlin</groupId>
87-
<artifactId>kotlin-stdlib</artifactId>
88-
<version>1.1.2-3</version>
83+
<groupId>org.tensorflow</groupId>
84+
<artifactId>tensorflow</artifactId>
85+
<version>1.1.0</version>
8986
</dependency>
9087
<dependency>
9188
<groupId>org.slf4j</groupId>
@@ -104,6 +101,42 @@
104101
<artifactId>junit</artifactId>
105102
<scope>test</scope>
106103
</dependency>
104+
<dependency>
105+
<groupId>org.eclipse.persistence</groupId>
106+
<artifactId>eclipselink</artifactId>
107+
<version>2.6.4</version>
108+
<scope>test</scope>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.glassfish</groupId>
112+
<artifactId>javax.el</artifactId>
113+
<version>3.0.0</version>
114+
<scope>test</scope>
115+
</dependency>
116+
<dependency>
117+
<groupId>org.hibernate</groupId>
118+
<artifactId>hibernate-entitymanager</artifactId>
119+
<version>5.2.10.Final</version>
120+
<scope>test</scope>
121+
</dependency>
122+
<dependency>
123+
<groupId>org.hibernate</groupId>
124+
<artifactId>hibernate-validator</artifactId>
125+
<version>5.4.1.Final</version>
126+
<scope>test</scope>
127+
</dependency>
128+
<dependency>
129+
<groupId>org.jboss.weld.se</groupId>
130+
<artifactId>weld-se</artifactId>
131+
<version>2.4.3.Final</version>
132+
<scope>test</scope>
133+
</dependency>
134+
<dependency>
135+
<groupId>org.jetbrains.kotlin</groupId>
136+
<artifactId>kotlin-stdlib</artifactId>
137+
<version>1.1.2-3</version>
138+
<scope>test</scope>
139+
</dependency>
107140
<dependency>
108141
<groupId>org.mockito</groupId>
109142
<artifactId>mockito-core</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.trajano.jee.nlp.impl;
2+
3+
import javax.enterprise.context.ApplicationScoped;
4+
import javax.enterprise.inject.Produces;
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
8+
@ApplicationScoped
9+
public class JpaProvider {
10+
11+
private EntityManager em;
12+
13+
@Produces
14+
public EntityManager getEntityManager() {
15+
16+
return em;
17+
}
18+
19+
/**
20+
* Sets/injects the entity manager.
21+
*
22+
* @param em
23+
* entity manager
24+
*/
25+
@PersistenceContext
26+
public void setEntityManager(final EntityManager em) {
27+
28+
this.em = em;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package net.trajano.jee.nlp.impl;
2+
3+
import java.util.logging.Logger;
4+
5+
import javax.annotation.PostConstruct;
6+
import javax.annotation.PreDestroy;
7+
import javax.ejb.Lock;
8+
import javax.ejb.LockType;
9+
import javax.ejb.Schedule;
10+
import javax.ejb.Singleton;
11+
import javax.ejb.Startup;
12+
import javax.enterprise.context.ApplicationScoped;
13+
import javax.enterprise.context.Dependent;
14+
import javax.enterprise.inject.Produces;
15+
import javax.inject.Inject;
16+
import javax.persistence.EntityManager;
17+
18+
import org.tensorflow.Graph;
19+
import org.tensorflow.OperationBuilder;
20+
import org.tensorflow.Session;
21+
22+
import net.trajano.jee.nlp.jpa.TensorGraph;
23+
24+
/**
25+
* This is a tensor graph provider. It will periodically save it's data to the
26+
* database type.
27+
*
28+
* @author Archimedes Trajano
29+
*/
30+
@Singleton
31+
@Startup
32+
@ApplicationScoped
33+
public class TensorGraphProvider {
34+
35+
private static final long GRAPH_ID = 1L;
36+
37+
private static final Logger LOG = Logger.getLogger("net.trajano.jee.nlp");
38+
39+
private EntityManager em;
40+
41+
private Graph graph;
42+
43+
@Lock(LockType.WRITE)
44+
@PreDestroy
45+
public void closeGraph() {
46+
47+
graph.close();
48+
}
49+
50+
/**
51+
* This will load the current graph from the database or construct a new one
52+
* if it is not found.
53+
*/
54+
@PostConstruct
55+
public void loadFromDatabase() {
56+
57+
TensorGraph dbData = em.find(TensorGraph.class, GRAPH_ID);
58+
if (dbData == null) {
59+
graph = new Graph();
60+
dbData = new TensorGraph(GRAPH_ID);
61+
dbData.setGraphDef(graph.toGraphDef());
62+
em.persist(dbData);
63+
} else {
64+
graph.importGraphDef(dbData.getGraphDef());
65+
}
66+
67+
}
68+
69+
/**
70+
* Please ensure that the session is closed after use.
71+
*/
72+
@Lock(LockType.READ)
73+
@Produces
74+
@Dependent
75+
public Session newSession() {
76+
77+
LOG.warning("new session created");
78+
return new Session(graph);
79+
}
80+
81+
public OperationBuilder opBuilder(final String type,
82+
final String name) {
83+
84+
return graph.opBuilder(type, name);
85+
}
86+
87+
@Schedule(minute = "*/10",
88+
hour = "*")
89+
@Lock(LockType.READ)
90+
public void persistCurrentGraph() {
91+
92+
final TensorGraph dbData = em.find(TensorGraph.class, GRAPH_ID);
93+
dbData.setGraphDef(graph.toGraphDef());
94+
em.merge(dbData);
95+
}
96+
97+
@Inject
98+
public void setEm(final EntityManager em) {
99+
100+
this.em = em;
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.trajano.jee.nlp.jpa;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.Lob;
9+
import javax.persistence.PrePersist;
10+
import javax.persistence.PreUpdate;
11+
import javax.persistence.Temporal;
12+
import javax.persistence.TemporalType;
13+
14+
@Entity
15+
public class TensorGraph {
16+
17+
@Lob
18+
@Column(length = Integer.MAX_VALUE,
19+
nullable = false)
20+
private byte[] graphDef;
21+
22+
/**
23+
* Primary key. These are internal IDs and are not exposed to the user. The
24+
* ID will not be generated, instead it is actually set by the constructor
25+
* for new objects.
26+
*/
27+
@Id
28+
@Column(nullable = false,
29+
updatable = false)
30+
private Long id;
31+
32+
@Temporal(TemporalType.TIMESTAMP)
33+
@Column(nullable = false)
34+
private Date lastUpdatedOn;
35+
36+
/**
37+
* Constructs TensorGraph. Required for JPA do not use.
38+
*/
39+
TensorGraph() {
40+
}
41+
42+
public TensorGraph(final long id) {
43+
this.id = id;
44+
}
45+
46+
public byte[] getGraphDef() {
47+
48+
return graphDef;
49+
}
50+
51+
public void setGraphDef(final byte[] graph) {
52+
53+
graphDef = graph;
54+
}
55+
56+
@PrePersist
57+
@PreUpdate
58+
public void updateLastUpdateOn() {
59+
60+
lastUpdatedOn = new Date();
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* JPA entities used to store Tensorflow data
3+
*
4+
* @author Archimedes Trajano
5+
*/
6+
package net.trajano.jee.nlp.jpa;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="annotated"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
3+
<persistence-unit name="default">
4+
<jta-data-source>java:app/ds</jta-data-source>
5+
</persistence-unit>
6+
</persistence>

0 commit comments

Comments
 (0)