forked from windj007/uima-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphWriter.java
More file actions
115 lines (93 loc) · 4.53 KB
/
GraphWriter.java
File metadata and controls
115 lines (93 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package org.apache.uima.graph;
import org.apache.uima.UIMAException;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.JCasFactory;
import org.apache.uima.graph.exceptions.UIMAGraphExceptionBase;
import org.apache.uima.graph.impl.DefaultJCasWrapper;
import org.apache.uima.graph.impl.DummyExistenceChecker;
import org.apache.uima.graph.impl.ReflectUtils;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.uima.util.CasCopier;
import org.apache.uima.util.Level;
import org.apache.uima.util.TypeSystemUtil;
import com.github.windj.utils.factories.exceptions.CannotCreateFactory;
import com.tinkerpop.blueprints.Graph;
public class GraphWriter extends JCasAnnotator_ImplBase {
public static final String PARAM_GRAPH_FACTORY_NAME = "graphFactoryName";
public static final String PARAM_GRAPH_CONFIG_STRING = "graphConfigString";
public static final String PARAM_MAPPING_FACTORY_NAME = "mappingFactoryName";
public static final String PARAM_JCAS_WRAPPER_CLASS_NAME = "jcasWrapperClassName";
public static final String PARAM_EXISTENCE_CHECKER_CLASS_NAME = "existenceCheckerClassName";
@ConfigurationParameter(mandatory = false, description = "Name of factory to get from GraphMetaFactory to open connection to graph database")
private String graphFactoryName = GraphMetaFactory.DEFAULT_FACTORY_NAME;
@ConfigurationParameter(mandatory = true, description = "String to pass to the GraphFactory when opening connection")
private String graphConfigString = null;
@ConfigurationParameter(mandatory = false, description = "Name of factory to get mapping")
private String mappingFactoryName = MappingManagerMetaFactory.DEFAULT_FACTORY_NAME;
@ConfigurationParameter(mandatory = false, description = "Full name of IJCasWrapper implementor to instantiate to get serializable objects from JCas")
private String jcasWrapperClassName = DefaultJCasWrapper.class.getName();
@ConfigurationParameter(mandatory = false, description = "Full name of IExistenceChecker implementor to instantiate to check if a document needs to be written")
private String existenceCheckerClassName = DummyExistenceChecker.class.getName();
private IGraphFactory graphFactory = null;
private Graph workingGraph = null;
private IMappingManagerFactory mapperFactory = null;
private IMappingManager mapper = null;
private IJCasWrapper jcasWrapper = null;
private IExistenceChecker existenceChecker = null;
@Override
public void initialize(UimaContext context)
throws ResourceInitializationException {
super.initialize(context);
try {
jcasWrapper = ReflectUtils.tryLoad(
jcasWrapperClassName,
IJCasWrapper.class);
existenceChecker = ReflectUtils.tryLoad(
existenceCheckerClassName,
IExistenceChecker.class);
} catch (ReflectiveOperationException e) {
throw new ResourceInitializationException(e);
}
try {
graphFactory = GraphMetaFactory.Instance().getFactory(
graphFactoryName);
setWorkingGraph(graphFactory.createGraph(graphConfigString));
mapperFactory = MappingManagerMetaFactory.Instance().getFactory(
mappingFactoryName);
mapper = mapperFactory.createMappingProvider(getWorkingGraph());
} catch (UIMAGraphExceptionBase e) {
throw new ResourceInitializationException(e);
} catch (CannotCreateFactory e) {
throw new ResourceInitializationException(e);
}
}
@Override
public void process(JCas doc) throws AnalysisEngineProcessException {
try {
if (!existenceChecker.exists(doc, getWorkingGraph())) {
getLogger().log(Level.INFO, "Serializing a doc...");
TypeSystemDescription tsDesc = TypeSystemUtil.typeSystem2TypeSystemDescription(doc.getTypeSystem());
JCas copy = JCasFactory.createJCas(tsDesc);
CasCopier.copyCas(doc.getCas(), copy.getCas(), true);
mapper.enqueueObject(jcasWrapper.wrap(copy));
mapper.processQueue();
graphFactory.commit(getWorkingGraph());
getLogger().log(Level.INFO, "Doc serialized");
}
// TODO: add logging for else
} catch (UIMAException e) {
throw new AnalysisEngineProcessException(e);
}
}
protected Graph getWorkingGraph() {
return workingGraph;
}
protected void setWorkingGraph(Graph workingGraph) {
this.workingGraph = workingGraph;
}
}