Revision 39157
Added by Marek Horst over 8 years ago
AbstractActionManagerBasedExporterMapper.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.iis.export.actionmanager; |
2 | 2 |
|
3 |
import static eu.dnetlib.iis.common.WorkflowRuntimeParameters.EXPORT_ACTION_BUILDER_FACTORY_CLASSNAME; |
|
4 |
import static eu.dnetlib.iis.common.WorkflowRuntimeParameters.EXPORT_ACTION_SETID; |
|
5 |
import static eu.dnetlib.iis.common.WorkflowRuntimeParameters.EXPORT_ACTION_SET_PROPERTY_SEPARATOR; |
|
6 |
import static eu.dnetlib.iis.common.WorkflowRuntimeParameters.EXPORT_SKIP_AUTHORS; |
|
7 |
import static eu.dnetlib.iis.common.WorkflowRuntimeParameters.EXPORT_SKIP_ORIGINAL_METADATA; |
|
8 |
import static eu.dnetlib.iis.common.WorkflowRuntimeParameters.EXPORT_TRUST_LEVEL; |
|
3 |
import static eu.dnetlib.iis.export.actionmanager.ExportWorkflowRuntimeParameters.EXPORT_ACTION_BUILDER_FACTORY_CLASSNAME; |
|
4 |
import static eu.dnetlib.iis.export.actionmanager.ExportWorkflowRuntimeParameters.EXPORT_ACTION_SETID; |
|
5 |
import static eu.dnetlib.iis.export.actionmanager.ExportWorkflowRuntimeParameters.EXPORT_ALGORITHM_PROPERTY_SEPARATOR; |
|
6 |
import static eu.dnetlib.iis.export.actionmanager.ExportWorkflowRuntimeParameters.EXPORT_TRUST_LEVEL; |
|
7 |
import static eu.dnetlib.iis.export.actionmanager.ExportWorkflowRuntimeParameters.EXPORT_TRUST_LEVEL_THRESHOLD; |
|
9 | 8 |
|
10 | 9 |
import java.io.IOException; |
11 | 10 |
import java.lang.reflect.Constructor; |
... | ... | |
26 | 25 |
import eu.dnetlib.iis.export.actionmanager.cfg.StaticConfigurationProvider; |
27 | 26 |
import eu.dnetlib.iis.export.actionmanager.module.ActionBuilderFactory; |
28 | 27 |
import eu.dnetlib.iis.export.actionmanager.module.ActionBuilderModule; |
29 |
import eu.dnetlib.iis.export.actionmanager.module.ActionSetIdProvider; |
|
30 |
import eu.dnetlib.iis.export.actionmanager.module.ActionSetMappingNotDefinedException; |
|
28 |
import eu.dnetlib.iis.export.actionmanager.module.AlgorithmMapper; |
|
31 | 29 |
import eu.dnetlib.iis.export.actionmanager.module.AlgorithmName; |
30 |
import eu.dnetlib.iis.export.actionmanager.module.MappingNotDefinedException; |
|
31 |
import eu.dnetlib.iis.export.actionmanager.module.TrustLevelThresholdExceededException; |
|
32 | 32 |
|
33 | 33 |
/** |
34 | 34 |
* ActionManager service based exporter mapper. |
... | ... | |
38 | 38 |
public abstract class AbstractActionManagerBasedExporterMapper |
39 | 39 |
extends Mapper<AvroKey<? extends SpecificRecordBase>, NullWritable, NullWritable, NullWritable> { |
40 | 40 |
|
41 |
// private String predefinedTrust = SpecialTrust.NEUTRAL.name(); |
|
42 | 41 |
private String predefinedTrust = "0.9"; |
43 | 42 |
|
44 | 43 |
private ActionManagerServiceFacade actionManager; |
45 | 44 |
|
46 | 45 |
private ActionManagerConfigurationProvider configProvider; |
47 | 46 |
|
47 |
private ActionBuilderFactory<SpecificRecordBase> actionBuilderFactory; |
|
48 |
|
|
48 | 49 |
private ActionBuilderModule<SpecificRecordBase> actionBuilder; |
49 | 50 |
|
50 |
private ActionSetIdProvider actionSetIdProvider;
|
|
51 |
private AlgorithmMapper<String> actionSetIdProvider;
|
|
51 | 52 |
|
52 | 53 |
|
53 | 54 |
/** This is the place you can access map-reduce workflow node parameters */ |
... | ... | |
55 | 56 |
@Override |
56 | 57 |
protected void setup(Context context) throws IOException, |
57 | 58 |
InterruptedException { |
58 |
|
|
59 | 59 |
if (context.getConfiguration().get( |
60 | 60 |
EXPORT_TRUST_LEVEL)!=null) { |
61 | 61 |
this.predefinedTrust = context.getConfiguration().get( |
62 | 62 |
EXPORT_TRUST_LEVEL); |
63 | 63 |
} |
64 |
|
|
65 |
@SuppressWarnings("unused") |
|
66 |
boolean skipOriginalMetadata = true; |
|
67 |
if (context.getConfiguration().get( |
|
68 |
EXPORT_SKIP_ORIGINAL_METADATA)!=null) { |
|
69 |
skipOriginalMetadata = Boolean.valueOf(context.getConfiguration().get( |
|
70 |
EXPORT_SKIP_ORIGINAL_METADATA)); |
|
71 |
} |
|
72 |
|
|
73 |
@SuppressWarnings("unused") |
|
74 |
boolean skipExportingAuthors = true; |
|
75 |
if (context.getConfiguration().get( |
|
76 |
EXPORT_SKIP_AUTHORS)!=null) { |
|
77 |
skipExportingAuthors = Boolean.valueOf(context.getConfiguration().get( |
|
78 |
EXPORT_SKIP_AUTHORS)); |
|
79 |
} |
|
64 |
|
|
80 | 65 |
String moduleClassName = context.getConfiguration().get( |
81 | 66 |
EXPORT_ACTION_BUILDER_FACTORY_CLASSNAME); |
82 | 67 |
if (moduleClassName!=null) { |
83 | 68 |
try { |
84 |
actionSetIdProvider = provideAlgorithmToActionSetMap(context); |
|
69 |
actionSetIdProvider = provideAlgorithmToActionSetMapper(context);
|
|
85 | 70 |
Class<?> clazz = Class.forName(moduleClassName); |
86 | 71 |
Constructor<?> constructor = clazz.getConstructor(); |
87 |
ActionBuilderFactory<SpecificRecordBase> actionBuilderFactory = |
|
88 |
(ActionBuilderFactory<SpecificRecordBase>) constructor.newInstance(); |
|
72 |
actionBuilderFactory = (ActionBuilderFactory<SpecificRecordBase>) constructor.newInstance(); |
|
89 | 73 |
actionBuilder = actionBuilderFactory.instantiate( |
90 |
predefinedTrust, context.getConfiguration()); |
|
74 |
predefinedTrust, provideTrustLevelThreshold( |
|
75 |
context, actionBuilderFactory.getAlgorithName()), |
|
76 |
context.getConfiguration()); |
|
91 | 77 |
actionManager = buildActionManager(context); |
92 | 78 |
configProvider = new StaticConfigurationProvider( |
93 | 79 |
StaticConfigurationProvider.AGENT_DEFAULT, |
... | ... | |
107 | 93 |
} |
108 | 94 |
} |
109 | 95 |
|
110 |
private ActionSetIdProvider provideAlgorithmToActionSetMap(Context context) { |
|
96 |
/** |
|
97 |
* Provides trust level threshold if defined for given algorithm or globally. |
|
98 |
* @param context |
|
99 |
* @param algorithmName |
|
100 |
* @return trust level threshold or null if not defined |
|
101 |
*/ |
|
102 |
private Float provideTrustLevelThreshold(Context context, AlgorithmName algorithmName) { |
|
103 |
String algorithmTrustLevelThreshold = context.getConfiguration().get( |
|
104 |
EXPORT_TRUST_LEVEL_THRESHOLD + EXPORT_ALGORITHM_PROPERTY_SEPARATOR + |
|
105 |
algorithmName.name()); |
|
106 |
if (algorithmTrustLevelThreshold!=null && |
|
107 |
!WorkflowRuntimeParameters.UNDEFINED_NONEMPTY_VALUE.equals( |
|
108 |
algorithmTrustLevelThreshold)) { |
|
109 |
return Float.valueOf(algorithmTrustLevelThreshold); |
|
110 |
} |
|
111 |
String defaultTrustLevelThresholdStr = context.getConfiguration().get( |
|
112 |
EXPORT_TRUST_LEVEL_THRESHOLD); |
|
113 |
if (defaultTrustLevelThresholdStr!=null && |
|
114 |
!WorkflowRuntimeParameters.UNDEFINED_NONEMPTY_VALUE.equals( |
|
115 |
defaultTrustLevelThresholdStr)) { |
|
116 |
return Float.valueOf(defaultTrustLevelThresholdStr); |
|
117 |
} |
|
118 |
// fallback: threshold was not defined |
|
119 |
return null; |
|
120 |
} |
|
121 |
|
|
122 |
private AlgorithmMapper<String> provideAlgorithmToActionSetMapper(Context context) { |
|
111 | 123 |
String defaultActionSetId = null; |
112 | 124 |
if (context.getConfiguration().get(EXPORT_ACTION_SETID)!=null && |
113 | 125 |
!WorkflowRuntimeParameters.UNDEFINED_NONEMPTY_VALUE.equals( |
... | ... | |
117 | 129 |
} |
118 | 130 |
final Map<AlgorithmName, String> algoToActionSetMap = new HashMap<AlgorithmName, String>(); |
119 | 131 |
for (AlgorithmName currentAlgorithm : AlgorithmName.values()) { |
120 |
String propertyKey = EXPORT_ACTION_SETID + EXPORT_ACTION_SET_PROPERTY_SEPARATOR +
|
|
132 |
String propertyKey = EXPORT_ACTION_SETID + EXPORT_ALGORITHM_PROPERTY_SEPARATOR +
|
|
121 | 133 |
currentAlgorithm.name(); |
122 | 134 |
if (context.getConfiguration().get(propertyKey)!=null && |
123 | 135 |
!WorkflowRuntimeParameters.UNDEFINED_NONEMPTY_VALUE.equals( |
... | ... | |
129 | 141 |
defaultActionSetId); |
130 | 142 |
} |
131 | 143 |
} |
132 |
return new ActionSetIdProvider() {
|
|
144 |
return new AlgorithmMapper<String>() {
|
|
133 | 145 |
@Override |
134 |
public String getActionSetId(AlgorithmName algorithmName)
|
|
135 |
throws ActionSetMappingNotDefinedException {
|
|
146 |
public String getValue(AlgorithmName algorithmName)
|
|
147 |
throws MappingNotDefinedException { |
|
136 | 148 |
String actionSetId = algoToActionSetMap.get(algorithmName); |
137 | 149 |
if (actionSetId!=null && |
138 | 150 |
!WorkflowRuntimeParameters.UNDEFINED_NONEMPTY_VALUE.equals( |
139 | 151 |
actionSetId)) { |
140 | 152 |
return actionSetId; |
141 | 153 |
} else { |
142 |
throw new ActionSetMappingNotDefinedException(
|
|
154 |
throw new MappingNotDefinedException( |
|
143 | 155 |
"no action set identifier defined " |
144 | 156 |
+ "for algorithm: " + algorithmName.name()); |
145 | 157 |
} |
... | ... | |
161 | 173 |
* @return personBuilder of actions |
162 | 174 |
*/ |
163 | 175 |
protected List<AtomicAction> createActions(SpecificRecordBase datum) { |
164 |
return actionBuilder.build(datum, configProvider.provideAgent(), |
|
165 |
actionSetIdProvider.getActionSetId(actionBuilder.getAlgorithName())); |
|
176 |
try { |
|
177 |
return actionBuilder.build(datum, configProvider.provideAgent(), |
|
178 |
actionSetIdProvider.getValue(actionBuilderFactory.getAlgorithName())); |
|
179 |
} catch (TrustLevelThresholdExceededException e) { |
|
180 |
return null; |
|
181 |
} |
|
166 | 182 |
} |
167 | 183 |
|
168 | 184 |
@Override |
Also available in: Unified diff
merging trunk changes with IIS-CDH-5.3.0 branch