1
|
package eu.dnetlib.data.collective.transformation.rulelanguage;
|
2
|
|
3
|
import static org.junit.Assert.*;
|
4
|
|
5
|
import java.io.StringReader;
|
6
|
import java.util.Iterator;
|
7
|
import java.util.List;
|
8
|
import java.util.Set;
|
9
|
|
10
|
import org.junit.Test;
|
11
|
import org.svenson.JSONParser;
|
12
|
|
13
|
import eu.dnetlib.data.collective.transformation.engine.functions.IdentifierExtract;
|
14
|
import eu.dnetlib.data.collective.transformation.engine.functions.Lookup;
|
15
|
import eu.dnetlib.data.collective.transformation.engine.functions.RegularExpression;
|
16
|
import eu.dnetlib.data.collective.transformation.rulelanguage.Argument.Type;
|
17
|
import eu.dnetlib.data.collective.transformation.rulelanguage.parser.ASTMyScript.SCRIPTTYPE;
|
18
|
import eu.dnetlib.data.collective.transformation.rulelanguage.visitor.RuleLanguageVisitor;
|
19
|
|
20
|
|
21
|
public class RuleLanguageTest {
|
22
|
|
23
|
private static final String declareScript = "declare_script \"abc\";\r\n";
|
24
|
private static final String endScript = "end";
|
25
|
private StringBuilder scriptBuilder;
|
26
|
|
27
|
private RuleLanguageVisitor parseValid(String stmt) {
|
28
|
RuleLanguageParser rlp = new RuleLanguageParser();
|
29
|
StringReader r = new StringReader(stmt);
|
30
|
rlp.parse(r);
|
31
|
return rlp.getVisitor();
|
32
|
}
|
33
|
|
34
|
@Test
|
35
|
public void parseMinimalScript(){
|
36
|
scriptBuilder = new StringBuilder();
|
37
|
scriptBuilder.append(declareScript);
|
38
|
scriptBuilder.append(endScript);
|
39
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
40
|
assertEquals("abc", v.getScriptName());
|
41
|
assertEquals(SCRIPTTYPE.MAINSCRIPT, v.getScriptType());
|
42
|
}
|
43
|
|
44
|
@Test
|
45
|
public void parseNameSpace(){
|
46
|
final String ns_decl = "declare_ns somePrefix = \"http://someHost/somePath/someVersion/1.0/\";\r\n";
|
47
|
scriptBuilder = new StringBuilder();
|
48
|
scriptBuilder.append(declareScript);
|
49
|
scriptBuilder.append(ns_decl);
|
50
|
scriptBuilder.append(endScript);
|
51
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
52
|
assertEquals("http://someHost/somePath/someVersion/1.0/", v.getNamespaceDeclarations().get("somePrefix"));
|
53
|
}
|
54
|
|
55
|
@Test
|
56
|
public void parsePreprocessing(){
|
57
|
final String preproc = "preprocess abc = dblookup(\"select * from xyz\");\r\n";
|
58
|
scriptBuilder = new StringBuilder();
|
59
|
scriptBuilder.append(declareScript);
|
60
|
scriptBuilder.append(preproc);
|
61
|
scriptBuilder.append(endScript);
|
62
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
63
|
assertTrue(v.getPreprocessings().get(0).containsKey("dblookup"));
|
64
|
}
|
65
|
|
66
|
@Test
|
67
|
public void parsePreprocessingBlacklist(){
|
68
|
final String preproc = "blacklist(\"//RESOURCE_IDENTIFIER/@value\");\r\n";
|
69
|
scriptBuilder = new StringBuilder();
|
70
|
scriptBuilder.append(declareScript);
|
71
|
scriptBuilder.append(preproc);
|
72
|
scriptBuilder.append(endScript);
|
73
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
74
|
assertTrue(v.getPreprocessings().get(0).containsKey("blacklist"));
|
75
|
assertEquals("//RESOURCE_IDENTIFIER/@value", v.getPreprocessings().get(0).get("blacklist"));
|
76
|
Argument argXpath = new Argument(Type.INPUTFIELD, v.getPreprocessings().get(0).get("blacklist"));
|
77
|
assertEquals("//RESOURCE_IDENTIFIER/@value", argXpath.getArgument());
|
78
|
}
|
79
|
|
80
|
@Test
|
81
|
public void testLookup(){
|
82
|
final String rule = "dc:relation = lookup(xpath:\"//proc:provenance\", \"name\");\r\n";
|
83
|
scriptBuilder = new StringBuilder();
|
84
|
scriptBuilder.append(declareScript);
|
85
|
scriptBuilder.append(rule);
|
86
|
scriptBuilder.append(endScript);
|
87
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
88
|
Set<IRule> s = v.getElementMappingRules().get("dc:relation");
|
89
|
assertNotNull(s);
|
90
|
assertEquals(1, s.size());
|
91
|
assertEquals("lookup", (s.toArray(new Rules[0]))[0].getFunctionCall().getExternalFunctionName());
|
92
|
assertEquals("//proc:provenance", (s.toArray(new Rules[0]))[0].getFunctionCall().getParameters().get(Lookup.paramExprIdentifier));
|
93
|
assertEquals("name", (s.toArray(new Rules[0]))[0].getFunctionCall().getParameters().get(Lookup.paramExprProperty));
|
94
|
}
|
95
|
|
96
|
@Test
|
97
|
public void testLookupVar(){
|
98
|
// final String rule = "$var = ";
|
99
|
final String rule = "dc:relation = lookup($var0, \"name\");\r\n";
|
100
|
scriptBuilder = new StringBuilder();
|
101
|
scriptBuilder.append(declareScript);
|
102
|
scriptBuilder.append(rule);
|
103
|
scriptBuilder.append(endScript);
|
104
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
105
|
Set<IRule> s = v.getElementMappingRules().get("dc:relation");
|
106
|
assertNotNull(s);
|
107
|
assertEquals(1, s.size());
|
108
|
assertEquals("lookup", (s.toArray(new Rules[0]))[0].getFunctionCall().getExternalFunctionName());
|
109
|
assertEquals("$var0", (s.toArray(new Rules[0]))[0].getFunctionCall().getParameters().get(Lookup.paramExprIdentifier));
|
110
|
assertEquals("name", (s.toArray(new Rules[0]))[0].getFunctionCall().getParameters().get(Lookup.paramExprProperty));
|
111
|
}
|
112
|
|
113
|
@Test
|
114
|
public void parseAssignXpathRule(){
|
115
|
final String ruleAssignWithXpathExpr = "dri:title = xpath:\"//dc:title\";\r\n";
|
116
|
final String comment = "// some comment\r\n";
|
117
|
scriptBuilder = new StringBuilder();
|
118
|
scriptBuilder.append(declareScript);
|
119
|
scriptBuilder.append(comment);
|
120
|
scriptBuilder.append(ruleAssignWithXpathExpr);
|
121
|
scriptBuilder.append(endScript);
|
122
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
123
|
Set<IRule> s = v.getElementMappingRules().get("dri:title");
|
124
|
assertNotNull(s);
|
125
|
assertEquals(1, s.size());
|
126
|
assertEquals("//dc:title", (s.toArray(new Rules[0]))[0].getXpath());
|
127
|
}
|
128
|
|
129
|
@Test
|
130
|
public void parseConstantRule(){
|
131
|
final String ruleConstant = "dri:CobjMDformats = \"oai_dc\";\r\n";
|
132
|
scriptBuilder = new StringBuilder();
|
133
|
scriptBuilder.append(declareScript);
|
134
|
scriptBuilder.append(ruleConstant);
|
135
|
scriptBuilder.append(endScript);
|
136
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
137
|
assertEquals(1, v.getElementMappingRules().size());
|
138
|
Set<IRule> s = v.getElementMappingRules().get("dri:CobjMDformats");
|
139
|
for (IRule r: s){
|
140
|
assertEquals("oai_dc", ((Rules)r).getConstant());
|
141
|
}
|
142
|
}
|
143
|
|
144
|
@Test
|
145
|
public void parseFuncGetValueRule(){
|
146
|
final String ruleFuncGetvalue = "static dri:repositoryName = getValue(profileField,[$job.recordprefix, def]);\r\n";
|
147
|
scriptBuilder = new StringBuilder();
|
148
|
scriptBuilder.append(declareScript);
|
149
|
scriptBuilder.append(ruleFuncGetvalue);
|
150
|
scriptBuilder.append(endScript);
|
151
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
152
|
assertEquals(1, v.getElementMappingRules().size());
|
153
|
Set<IRule> s = v.getElementMappingRules().get("dri:repositoryName");
|
154
|
for (IRule r: s){
|
155
|
assertEquals("getValue", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
156
|
//assertEquals("dnetExt:getValue(profileField)", r.getFunction());
|
157
|
assertEquals(2, ((Rules)r).getFunctionCall().getArguments().size());
|
158
|
assertEquals("$job.recordprefix", ((Rules)r).getFunctionCall().getArguments().get(0).getArgument());
|
159
|
assertEquals(true, ((Rules)r).isStatic());
|
160
|
}
|
161
|
}
|
162
|
|
163
|
@Test
|
164
|
public void parseFuncGetValueXpathArgsRule(){
|
165
|
final String ruleFuncGetvalue = "dri:repositoryName = getValue(profileField,[xpath:\"//node1\", xpath:\"//node2\"]);\r\n";
|
166
|
scriptBuilder = new StringBuilder();
|
167
|
scriptBuilder.append(declareScript);
|
168
|
scriptBuilder.append(ruleFuncGetvalue);
|
169
|
scriptBuilder.append(endScript);
|
170
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
171
|
assertEquals(1, v.getElementMappingRules().size());
|
172
|
Set<IRule> s = v.getElementMappingRules().get("dri:repositoryName");
|
173
|
for (IRule r: s){
|
174
|
assertEquals("getValue", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
175
|
//assertEquals("dnetExt:getValue(profileField)", r.getFunction());
|
176
|
assertEquals(2, ((Rules)r).getFunctionCall().getArguments().size());
|
177
|
assertEquals(false, ((Rules)r).isStatic());
|
178
|
Argument arg1 = ((Rules)r).getFunctionCall().getArguments().get(0);
|
179
|
assertTrue(arg1.isInputField());
|
180
|
assertEquals("//node1", arg1.getArgument());
|
181
|
Argument arg2 = ((Rules)r).getFunctionCall().getArguments().get(1);
|
182
|
assertTrue(arg2.isInputField());
|
183
|
assertEquals("//node2", arg2.getArgument());
|
184
|
}
|
185
|
}
|
186
|
|
187
|
@Test
|
188
|
public void parseFuncConvert(){
|
189
|
final String ruleFuncConvert_withPrefixes = "somePrefix:lv2 = Convert(xpath:\"/dc:metadata/dc:language\",LangVocab);\r\n";
|
190
|
scriptBuilder = new StringBuilder();
|
191
|
scriptBuilder.append(declareScript);
|
192
|
scriptBuilder.append(ruleFuncConvert_withPrefixes);
|
193
|
scriptBuilder.append(endScript);
|
194
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
195
|
assertEquals(1, v.getElementMappingRules().size());
|
196
|
Set<IRule> s = v.getElementMappingRules().get("somePrefix:lv2");
|
197
|
for (IRule r: s){
|
198
|
assertEquals("convert", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
199
|
//assertEquals("dnetExt:convert(/metadata/language,LangVocab)", r.getFunction());
|
200
|
}
|
201
|
}
|
202
|
|
203
|
@Test
|
204
|
public void parseFuncExtract(){
|
205
|
final String ruleFuncExtract = "somePrefix:lv = Extract(Language);\r\n";
|
206
|
scriptBuilder = new StringBuilder();
|
207
|
scriptBuilder.append(declareScript);
|
208
|
scriptBuilder.append(ruleFuncExtract);
|
209
|
scriptBuilder.append(endScript);
|
210
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
211
|
assertEquals(1, v.getElementMappingRules().size());
|
212
|
Set<IRule> s = v.getElementMappingRules().get("somePrefix:lv");
|
213
|
for (IRule r: s){
|
214
|
assertEquals("extract", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
215
|
assertEquals("Language", ((Rules)r).getFunctionCall().getParameters().get("feature"));
|
216
|
}
|
217
|
}
|
218
|
|
219
|
@Test
|
220
|
public void parseFuncConcat(){
|
221
|
final String ruleFuncConcat = "sp:lv = concat(\"value abc\", $var1);\r\n";
|
222
|
scriptBuilder = new StringBuilder();
|
223
|
scriptBuilder.append(declareScript);
|
224
|
scriptBuilder.append(ruleFuncConcat);
|
225
|
scriptBuilder.append(endScript);
|
226
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
227
|
assertEquals(1, v.getElementMappingRules().size());
|
228
|
Set<IRule> s = v.getElementMappingRules().get("sp:lv");
|
229
|
for (IRule r: s){
|
230
|
assertEquals("concat", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
231
|
assertEquals(2, ((Rules)r).getFunctionCall().getParamList().size());
|
232
|
}
|
233
|
}
|
234
|
|
235
|
@Test
|
236
|
public void parseFuncSplit(){
|
237
|
final String ruleFuncSplit = "%template1 = split(xpath:\"/dc:creator\", \"dc:creator\", \";\");\r\n";
|
238
|
scriptBuilder = new StringBuilder();
|
239
|
scriptBuilder.append(declareScript);
|
240
|
scriptBuilder.append(ruleFuncSplit);
|
241
|
scriptBuilder.append(endScript);
|
242
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
243
|
assertEquals(1, v.getTemplateMappingRules().size());
|
244
|
IRule r = v.getTemplateMappingRules().get("%template1");
|
245
|
assertEquals("split", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
246
|
|
247
|
}
|
248
|
|
249
|
@Test
|
250
|
public void parseFuncConvertWithAttr(){
|
251
|
final String ruleFuncConvert_withAttribute = "somePrefix:lv3 = Convert(xpath:\"/dc:metadata/language/@attr\",LangVocab);\r\n";
|
252
|
scriptBuilder = new StringBuilder();
|
253
|
scriptBuilder.append(declareScript);
|
254
|
scriptBuilder.append(ruleFuncConvert_withAttribute);
|
255
|
scriptBuilder.append(endScript);
|
256
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
257
|
assertEquals(1, v.getElementMappingRules().size());
|
258
|
Set<IRule> s = v.getElementMappingRules().get("somePrefix:lv3");
|
259
|
for (IRule r: s){
|
260
|
assertEquals("convert", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
261
|
//assertEquals("dnetExt:convert(/dc:metadata/language[@attr],LangVocab)", r.getFunction());
|
262
|
}
|
263
|
}
|
264
|
|
265
|
@Test
|
266
|
public void parseFuncConvertWithOptParams(){
|
267
|
final String ruleFuncConvert_withAttribute = "somePrefix:lv3 = Convert(xpath:\"/dc:metadata/dc:date\",DateISO, \"YYYY-MM-DD\", \"MIN\" );\r\n";
|
268
|
scriptBuilder = new StringBuilder();
|
269
|
scriptBuilder.append(declareScript);
|
270
|
scriptBuilder.append(ruleFuncConvert_withAttribute);
|
271
|
scriptBuilder.append(endScript);
|
272
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
273
|
assertEquals(1, v.getElementMappingRules().size());
|
274
|
Set<IRule> s = v.getElementMappingRules().get("somePrefix:lv3");
|
275
|
for (IRule r: s){
|
276
|
assertEquals("convert", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
277
|
//assertEquals("dnetExt:convert(/dc:metadata/language[@attr],LangVocab)", r.getFunction());
|
278
|
}
|
279
|
}
|
280
|
|
281
|
@Test
|
282
|
public void parseFuncIdentifierExtract(){
|
283
|
final String ruleFuncIdentiferExtract = "pref:elem = identifierExtract('[\"dc:identifer\", \"dc:relation\"]', xpath:\"/\", 'regExpr');\r\n";
|
284
|
scriptBuilder = new StringBuilder();
|
285
|
scriptBuilder.append(declareScript);
|
286
|
scriptBuilder.append(ruleFuncIdentiferExtract);
|
287
|
scriptBuilder.append(endScript);
|
288
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
289
|
assertEquals(1, v.getElementMappingRules().size());
|
290
|
Set<IRule> s = v.getElementMappingRules().get("pref:elem");
|
291
|
for (IRule r: s){
|
292
|
assertEquals("identifierExtract", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
293
|
String xpathJsonString = ((Rules)r).getFunctionCall().getParameters().get(IdentifierExtract.paramXpathExprJson);
|
294
|
assertEquals("[\"dc:identifer\", \"dc:relation\"]", xpathJsonString);
|
295
|
List<String> xpathExprList = JSONParser.defaultJSONParser().parse(List.class, xpathJsonString);
|
296
|
assertEquals(2, xpathExprList.size());
|
297
|
}
|
298
|
}
|
299
|
|
300
|
@Test
|
301
|
public void parseConditionalRuleWithDistinctTargetElements(){
|
302
|
final String ruleConditional = "if xpath:\"//dc:metadata/dc:language\" somePrefix:a = \"primaryRule\"; else somePrefix:b = \"secondaryRule\";\r\n";
|
303
|
scriptBuilder = new StringBuilder();
|
304
|
scriptBuilder.append(declareScript);
|
305
|
scriptBuilder.append(ruleConditional);
|
306
|
scriptBuilder.append(endScript);
|
307
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
308
|
assertEquals(2, v.getElementMappingRules().size());
|
309
|
Set<IRule> s1 = v.getElementMappingRules().get("somePrefix:a");
|
310
|
assertEquals(1, s1.size());
|
311
|
Set<IRule> s2 = v.getElementMappingRules().get("somePrefix:b");
|
312
|
assertEquals(1, s2.size());
|
313
|
for (IRule rCon: s1){
|
314
|
Rules r = (Rules)rCon;
|
315
|
assertEquals("//dc:metadata/dc:language", r.getCondition().getConditionExpression());
|
316
|
assertTrue(r.hasCondition());
|
317
|
assertTrue(r.getCondition().isPrimary(r));
|
318
|
}
|
319
|
for (IRule rCon: s2){
|
320
|
Rules r = (Rules)rCon;
|
321
|
assertEquals("//dc:metadata/dc:language", r.getCondition().getConditionExpression());
|
322
|
assertTrue(r.hasCondition());
|
323
|
if (r.getCondition().isPrimary(r)){
|
324
|
assertNotSame(r.getCondition().getSecondaryRule(), r);
|
325
|
assertEquals(r.getCondition().getPrimaryRule(), r);
|
326
|
}
|
327
|
assertNotSame(r.getCondition().getPrimaryRule().getUniqueName(), r.getCondition().getSecondaryRule().getUniqueName());
|
328
|
}
|
329
|
}
|
330
|
|
331
|
@Test
|
332
|
public void parseConditionalRuleWithParamAsConditionExpression(){
|
333
|
final String ruleConditional = "if xpath:\"$var1='Aggregator'\" somePrefix:a = \"primaryRule\"; else somePrefix:b = \"secondaryRule\";\r\n";
|
334
|
scriptBuilder = new StringBuilder();
|
335
|
scriptBuilder.append(declareScript);
|
336
|
scriptBuilder.append(ruleConditional);
|
337
|
scriptBuilder.append(endScript);
|
338
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
339
|
assertEquals(2, v.getElementMappingRules().size());
|
340
|
Set<IRule> s1 = v.getElementMappingRules().get("somePrefix:a");
|
341
|
assertEquals(1, s1.size());
|
342
|
Set<IRule> s2 = v.getElementMappingRules().get("somePrefix:b");
|
343
|
assertEquals(1, s2.size());
|
344
|
for (IRule rCon: s1){
|
345
|
Rules r = (Rules)rCon;
|
346
|
assertEquals("$var1='Aggregator'", r.getCondition().getConditionExpression());
|
347
|
assertTrue(r.hasCondition());
|
348
|
assertTrue(r.getCondition().isPrimary(r));
|
349
|
}
|
350
|
for (IRule rCon: s2){
|
351
|
Rules r = (Rules)rCon;
|
352
|
assertEquals("$var1='Aggregator'", r.getCondition().getConditionExpression());
|
353
|
assertTrue(r.hasCondition());
|
354
|
if (r.getCondition().isPrimary(r)){
|
355
|
assertNotSame(r.getCondition().getSecondaryRule(), r);
|
356
|
assertEquals(r.getCondition().getPrimaryRule(), r);
|
357
|
}
|
358
|
assertNotSame(r.getCondition().getPrimaryRule().getUniqueName(), r.getCondition().getSecondaryRule().getUniqueName());
|
359
|
}
|
360
|
}
|
361
|
|
362
|
@Test
|
363
|
public void parseConditionalRuleWithSameTargetElements(){
|
364
|
final String ruleConditional = "if xpath:\"//dc:metadata/dc:language\" somePrefix:a = \"primaryRule\"; else somePrefix:a = \"secondaryRule\";\r\n";
|
365
|
scriptBuilder = new StringBuilder();
|
366
|
scriptBuilder.append(declareScript);
|
367
|
scriptBuilder.append(ruleConditional);
|
368
|
scriptBuilder.append(endScript);
|
369
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
370
|
assertEquals(1, v.getElementMappingRules().size());
|
371
|
// the output element 'somePrefix:a' has two alternative rules
|
372
|
Set<IRule> s = v.getElementMappingRules().get("somePrefix:a");
|
373
|
assertEquals(2, s.size());
|
374
|
for (IRule rCon: s){
|
375
|
Rules r = (Rules)rCon;
|
376
|
assertEquals("//dc:metadata/dc:language", r.getCondition().getConditionExpression());
|
377
|
if (r.getCondition().isPrimary(r)){
|
378
|
assertEquals(r.getCondition().getPrimaryRule(), r);
|
379
|
}else{
|
380
|
assertEquals(r.getCondition().getSecondaryRule(), r);
|
381
|
}
|
382
|
assertNotSame(r.getCondition().getPrimaryRule(), r.getCondition().getSecondaryRule());
|
383
|
assertEquals(r.getCondition().getPrimaryRule().getUniqueName(), r.getCondition().getSecondaryRule().getUniqueName());
|
384
|
}
|
385
|
}
|
386
|
|
387
|
@Test
|
388
|
public void parseConditionalRuleWithApplyOnField(){
|
389
|
final String rule = "apply xpath:\"//dc:identifier\" if xpath:\"starts-with(., 'http:')\" dr:CobjIdentifier = xpath:\".\"; else dc:identifier = xpath:\".\";\r\n";
|
390
|
scriptBuilder = new StringBuilder();
|
391
|
scriptBuilder.append(declareScript);
|
392
|
scriptBuilder.append(rule);
|
393
|
scriptBuilder.append(endScript);
|
394
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
395
|
assertEquals(2, v.getElementMappingRules().size());
|
396
|
Set<IRule> s1 = v.getElementMappingRules().get("dr:CobjIdentifier");
|
397
|
for (IRule rCon: s1){
|
398
|
Rules r = (Rules)rCon;
|
399
|
assertEquals("//dc:identifier", r.getCondition().getApplyExpression());
|
400
|
}
|
401
|
}
|
402
|
|
403
|
@Test
|
404
|
public void testCopy(){
|
405
|
final String rule = "dc:title = copy(\"dc:title\", \"//dc:title\", \"@*|node()\");\r\n";
|
406
|
scriptBuilder = new StringBuilder();
|
407
|
scriptBuilder.append(declareScript);
|
408
|
scriptBuilder.append(rule);
|
409
|
scriptBuilder.append(endScript);
|
410
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
411
|
Set<IRule> s = v.getElementMappingRules().get("dc:title");
|
412
|
assertNotNull(s);
|
413
|
assertEquals("dc:title", (s.toArray(new Rules[0]))[0].getTemplateMatch());
|
414
|
}
|
415
|
|
416
|
@Test
|
417
|
public void testSkip(){
|
418
|
final String rule = "dc:title = skipRecord();\r\n";
|
419
|
scriptBuilder = new StringBuilder();
|
420
|
scriptBuilder.append(declareScript);
|
421
|
scriptBuilder.append(rule);
|
422
|
scriptBuilder.append(endScript);
|
423
|
|
424
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
425
|
assertNotNull(v);
|
426
|
assertEquals(1, v.getElementMappingRules().size());
|
427
|
Set<IRule> s1 = v.getElementMappingRules().get("dc:title");
|
428
|
assertNotNull(s1);
|
429
|
for (IRule rSkip: s1){
|
430
|
Rules r = (Rules)rSkip;
|
431
|
assertNotNull(r);
|
432
|
assertFalse(r.hasSet());
|
433
|
assertTrue(r.isSkip());
|
434
|
}
|
435
|
|
436
|
}
|
437
|
|
438
|
@Test
|
439
|
public void parseSetRule(){
|
440
|
final String rule = "dc:identifier = set(\"CONST\" , @type = \"ABC\"; , @lang = \"ger\";);";
|
441
|
scriptBuilder = new StringBuilder();
|
442
|
scriptBuilder.append(declareScript);
|
443
|
scriptBuilder.append(rule);
|
444
|
scriptBuilder.append(endScript);
|
445
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
446
|
assertEquals(1, v.getElementMappingRules().size());
|
447
|
Iterator<String> it = v.getElementMappingRules().keySet().iterator();
|
448
|
while (it.hasNext()){
|
449
|
System.out.println("key: " + it.next());
|
450
|
}
|
451
|
Set<IRule> s1 = v.getElementMappingRules().get("dc:identifier");
|
452
|
for (IRule rSet: s1){
|
453
|
Rules r = (Rules)rSet;
|
454
|
assertTrue(r.hasSet());
|
455
|
List<Rules> pendingRules = r.getRulesSet().getPendingRules();
|
456
|
assertEquals(2,pendingRules.size());
|
457
|
for (Rules pRule: pendingRules){
|
458
|
assertTrue(pRule.getAttribute().equals("type") ||
|
459
|
pRule.getAttribute().equals("lang"));
|
460
|
}
|
461
|
|
462
|
}
|
463
|
}
|
464
|
|
465
|
@Test
|
466
|
public void parseRuleWithVariable(){
|
467
|
final String ruleLine = "$abc = xpath:\"//dc:creator\";\r\n";
|
468
|
scriptBuilder = new StringBuilder();
|
469
|
scriptBuilder.append(declareScript);
|
470
|
scriptBuilder.append(ruleLine);
|
471
|
scriptBuilder.append(endScript);
|
472
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
473
|
assertEquals(0, v.getElementMappingRules().size());
|
474
|
assertEquals(1, v.getVariableMappingRules().size());
|
475
|
}
|
476
|
|
477
|
@Test
|
478
|
public void parseRuleWithStaticVariable(){
|
479
|
final String ruleLine = "static $abc = RegExpr(xpath:\"//someExpr1\", xpath:\"//someExpr2\", \"s/[x1]/[x2]\");\r\n";
|
480
|
scriptBuilder = new StringBuilder();
|
481
|
scriptBuilder.append(declareScript);
|
482
|
scriptBuilder.append(ruleLine);
|
483
|
scriptBuilder.append(endScript);
|
484
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
485
|
assertEquals(0, v.getElementMappingRules().size());
|
486
|
assertEquals(1, v.getVariableMappingRules().size());
|
487
|
assertEquals(true, ((Rules)v.getVariableMappingRules().get("$abc")).isStatic());
|
488
|
}
|
489
|
|
490
|
// @Test
|
491
|
public void parseRegExpr(){
|
492
|
final String ruleLine = "somePrefix:someElement = RegExpr(xpath:\"//someExpr1\", xpath:\"//someExpr2\", \"s/[x1]/[x2]\");\r\n";
|
493
|
scriptBuilder = new StringBuilder();
|
494
|
scriptBuilder.append(declareScript);
|
495
|
scriptBuilder.append(ruleLine);
|
496
|
scriptBuilder.append(endScript);
|
497
|
@SuppressWarnings("unused")
|
498
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
499
|
}
|
500
|
|
501
|
@Test
|
502
|
public void parseRegExpr2(){
|
503
|
final String ruleLine = "somePrefix:someElement = RegExpr($job.recordidentifier, xpath:\"//someExpr2\", \"s/[x1]/[x2]\");\r\n";
|
504
|
scriptBuilder = new StringBuilder();
|
505
|
scriptBuilder.append(declareScript);
|
506
|
scriptBuilder.append(ruleLine);
|
507
|
scriptBuilder.append(endScript);
|
508
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
509
|
assertEquals(1, v.getElementMappingRules().size());
|
510
|
Set<IRule> s = v.getElementMappingRules().get("somePrefix:someElement");
|
511
|
for (IRule r: s){
|
512
|
assertEquals("regExpr", ((Rules)r).getFunctionCall().getExternalFunctionName());
|
513
|
//assertEquals("dnetExt:getValue(profileField)", r.getFunction());
|
514
|
assertEquals("$job.recordidentifier", ((Rules)r).getFunctionCall().getParameters().get(RegularExpression.paramExpr1));
|
515
|
// assertEquals("$job.recordprefix", ((Rules)r).getFunctionCall().getArguments().get(0).getArgument());
|
516
|
// assertEquals(true, ((Rules)r).isStatic());
|
517
|
}
|
518
|
}
|
519
|
|
520
|
@Test
|
521
|
public void parseDc2DmfScript(){
|
522
|
scriptBuilder = new StringBuilder();
|
523
|
scriptBuilder.append("declare_script \"MainSample\";\r\n");
|
524
|
scriptBuilder.append("declare_ns dr = \"http://www.driver-repository.eu/namespace/dr\";\r\n");
|
525
|
scriptBuilder.append("declare_ns dri = \"http://www.driver-repository.eu/namespace/dri\";\r\n");
|
526
|
scriptBuilder.append("declare_ns dc = \"http://purl.org/dc/elements/1.1/\";\r\n");
|
527
|
|
528
|
scriptBuilder.append("dr:objectIdentifier = xpath:\"//dri:objIdentifier\";\r\n");
|
529
|
scriptBuilder.append("dr:dateOfCollection = getValue(CURRENTDATE, []);\r\n");
|
530
|
scriptBuilder.append("dr:CobjContentSynthesis = empty;\r\n");
|
531
|
scriptBuilder.append("dr:CobjTypology = \"Textual\";\r\n");
|
532
|
scriptBuilder.append("dr:CobjModel = \"OAI\";\r\n");
|
533
|
scriptBuilder.append("dr:CobjMdFormats = \"oai_dc\";\r\n");
|
534
|
scriptBuilder.append("dr:CobjDescriptionSynthesis = empty;\r\n");
|
535
|
scriptBuilder.append("//dr:aggregatorName = getValue(PROFILEFIELD, [\"transformationmanager-service-profile-id\", xpath:\"//PROPERTY/@key='name'\"]);\r\n");
|
536
|
scriptBuilder.append("dr:aggregatorInstitution = empty;\r\n");
|
537
|
scriptBuilder.append("dr:repositoryName = getValue(PROFILEFIELD, [xpath:\"//dri:repositoryId\", xpath:\"//CONFIGURATION/OFFICIAL_NAME\"]);\r\n");
|
538
|
scriptBuilder.append("dr:repositoryLink = getValue(PROFILEFIELD, [xpath:\"//dri:repositoryId\", xpath:\"//REPOSITORY_WEBPAGE\"]);\r\n");
|
539
|
scriptBuilder.append("dr:repositoryCountry = getValue(PROFILEFIELD, [xpath:\"//dri:repositoryId\", xpath:\"//COUNTRY\"]);\r\n");
|
540
|
scriptBuilder.append("dr:repositoryInstitution = getValue(PROFILEFIELD, [xpath:\"//dri:repositoryId\", xpath:\"//REPOSITORY_INSTITUTION\"]);\r\n");
|
541
|
scriptBuilder.append("dc:creator = xpath:\"//dc:creator\";\r\n");
|
542
|
scriptBuilder.append("dc:title = xpath:\"//dc:title\";\r\n");
|
543
|
scriptBuilder.append("dc:subject = xpath:\"//dc:subject\";\r\n");
|
544
|
// scriptBuilder.append("dr:CobjCategory = Convert(xpath:\"//dc:type\", TextTypologies);\r\n");
|
545
|
// scriptBuilder.append("dc:language = Convert(xpath:\"//dc:language\", Languages);\r\n");
|
546
|
// scriptBuilder.append("dc:dateAccepted = Convert(xpath:\"//dc:date\", DateISO8601);\r\n");
|
547
|
// scriptBuilder.append("dc:language = Convert(xpath:\"//dc:language\", Languages);\r\n");
|
548
|
scriptBuilder.append("apply xpath:\"//dc:identifier\" if xpath:\"starts-with(., 'http')\" dc:identifier = xpath:\".\"; else dr:CobjIdentifier = xpath:\".\";\r\n");
|
549
|
scriptBuilder.append("apply xpath:\"//dc:relation\" if xpath:\"starts-with(., 'http')\" dc:identifier = Convert(xpath:\"//dc:type\", TextTypologies); else dr:CobjIdentifier = xpath:\".\";\r\n");
|
550
|
scriptBuilder.append("dc:publisher = xpath:\"//dc:publisher\";\r\n");
|
551
|
scriptBuilder.append("dc:source = xpath:\"//dc:source\";\r\n");
|
552
|
scriptBuilder.append("dc:contributor = xpath:\"//dc:contributor\";\r\n");
|
553
|
scriptBuilder.append("dc:relation = xpath:\"//dc:relation\";\r\n");
|
554
|
scriptBuilder.append("dc:description = xpath:\"//dc:description\";\r\n");
|
555
|
scriptBuilder.append("end\r\n");
|
556
|
@SuppressWarnings("unused")
|
557
|
RuleLanguageVisitor v = parseValid(scriptBuilder.toString());
|
558
|
}
|
559
|
}
|