|
| 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 | +} |
0 commit comments