• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 28123901609

24 Jun 2026 07:23PM UTC coverage: 27.824% (+1.2%) from 26.584%
28123901609

push

github

web-flow
Merge pull request #506 from knowledgepixels/fix/template-matcher-backtracking-505

fix: backtracking matcher so grouped shared-placeholder statements unify (#505)

1676 of 6929 branches covered (24.19%)

Branch coverage included in aggregate %.

3561 of 11893 relevant lines covered (29.94%)

4.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.32
src/main/java/com/knowledgepixels/nanodash/component/StatementItem.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.template.ContextType;
4
import com.knowledgepixels.nanodash.template.Template;
5
import com.knowledgepixels.nanodash.template.TemplateContext;
6
import com.knowledgepixels.nanodash.template.UnificationException;
7
import org.apache.wicket.AttributeModifier;
8
import org.apache.wicket.Component;
9
import org.apache.wicket.ajax.AjaxEventBehavior;
10
import org.apache.wicket.ajax.AjaxRequestTarget;
11
import org.apache.wicket.behavior.AttributeAppender;
12
import org.apache.wicket.markup.html.WebMarkupContainer;
13
import org.apache.wicket.markup.html.basic.Label;
14
import org.apache.wicket.markup.html.form.FormComponent;
15
import org.apache.wicket.markup.html.list.ListItem;
16
import org.apache.wicket.markup.html.list.ListView;
17
import org.apache.wicket.markup.html.panel.Panel;
18
import org.apache.wicket.model.IModel;
19
import org.eclipse.rdf4j.model.IRI;
20
import org.eclipse.rdf4j.model.Statement;
21
import org.eclipse.rdf4j.model.Value;
22
import org.eclipse.rdf4j.model.ValueFactory;
23
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
24
import org.nanopub.MalformedNanopubException;
25
import org.nanopub.NanopubAlreadyFinalizedException;
26
import org.nanopub.NanopubCreator;
27
import org.nanopub.vocabulary.NTEMPLATE;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

31
import java.io.Serializable;
32
import java.util.*;
33

34
/**
35
 * Represents a single item in a statement, which can be a subject, predicate, or object.
36
 */
37
public class StatementItem extends Panel {
38

39
    private TemplateContext context;
40
    private IRI statementId;
41
    private List<IRI> statementPartIds = new ArrayList<>();
15✔
42
    private List<WebMarkupContainer> viewElements = new ArrayList<>();
15✔
43
    private List<RepetitionGroup> repetitionGroups = new ArrayList<>();
15✔
44
    private boolean repetitionGroupsChanged = true;
9✔
45
    private Set<IRI> iriSet = new HashSet<>();
15✔
46
    private boolean isMatched = false;
9✔
47
    private static final Logger logger = LoggerFactory.getLogger(StatementItem.class);
9✔
48

49
    /**
50
     * Constructor for creating a StatementItem with a specific ID and statement ID.
51
     *
52
     * @param id          the Wicket component ID
53
     * @param statementId the IRI of the statement this item represents
54
     * @param context     the template context containing information about the template and its items
55
     */
56
    public StatementItem(String id, IRI statementId, TemplateContext context) {
57
        super(id);
9✔
58

59
        this.statementId = statementId;
9✔
60
        this.context = context;
9✔
61
        setOutputMarkupId(true);
12✔
62

63
        if (isGrouped()) {
9✔
64
            statementPartIds.addAll(getTemplate().getStatementIris(statementId));
27✔
65
        } else {
66
            statementPartIds.add(statementId);
15✔
67
        }
68

69
        addRepetitionGroup();
6✔
70

71
        ListView<WebMarkupContainer> v = new ListView<WebMarkupContainer>("statement-group", viewElements) {
48✔
72

73
            @Override
74
            protected void populateItem(ListItem<WebMarkupContainer> item) {
75
                item.add(item.getModelObject());
×
76
            }
×
77

78
        };
79
        v.setOutputMarkupId(true);
12✔
80
        add(v);
27✔
81
    }
3✔
82

83
    /**
84
     * Adds a new repetition group to this StatementItem with a default RepetitionGroup.
85
     */
86
    public void addRepetitionGroup() {
87
        addRepetitionGroup(new RepetitionGroup());
18✔
88
    }
3✔
89

90
    /**
91
     * Adds a new repetition group to this StatementItem.
92
     *
93
     * @param rg the RepetitionGroup to add
94
     */
95
    public void addRepetitionGroup(RepetitionGroup rg) {
96
        repetitionGroups.add(rg);
15✔
97
        repetitionGroupsChanged = true;
9✔
98
    }
3✔
99

100
    /**
101
     * {@inheritDoc}
102
     */
103
    @Override
104
    protected void onBeforeRender() {
105
        if (repetitionGroupsChanged) {
×
106
            updateViewElements();
×
107
            finalizeValues();
×
108
        }
109
        repetitionGroupsChanged = false;
×
110
        super.onBeforeRender();
×
111
    }
×
112

113
    private void updateViewElements() {
114
        viewElements.clear();
×
115
        boolean first = true;
×
116
        for (RepetitionGroup r : repetitionGroups) {
×
117
            if (isGrouped() && !first) {
×
118
                viewElements.add(new HorizontalLine("statement"));
×
119
            }
120
            viewElements.addAll(r.getStatementParts());
×
121
            boolean isOnly = repetitionGroups.size() == 1;
×
122
            boolean isLast = repetitionGroups.get(repetitionGroups.size() - 1) == r;
×
123
            r.addRepetitionButton.setVisible(!context.isReadOnly() && isRepeatable() && isLast);
×
124
            r.removeRepetitionButton.setVisible(!context.isReadOnly() && isRepeatable() && !isOnly);
×
125
            r.optionalMark.setVisible(isOnly);
×
126
            first = false;
×
127
        }
×
128
        String htmlClassString = "";
×
129
        if (!context.isReadOnly()) {
×
130
            if (isOptional()) {
×
131
                htmlClassString += "nanopub-optional ";
×
132
            }
133
            if (isAdvanced()) {
×
134
                htmlClassString += "advanced ";
×
135
            }
136
        }
137
        boolean singleItem = context.getStatementItems().size() == 1;
×
138
        boolean repeatableOrRepeated = (!context.isReadOnly() && isRepeatable()) || (context.isReadOnly() && getRepetitionCount() > 1);
×
139
        if ((isGrouped() || repeatableOrRepeated) && !singleItem) {
×
140
            htmlClassString += "nanopub-group ";
×
141
        }
142
        if (!htmlClassString.isEmpty()) {
×
143
            add(new AttributeModifier("class", htmlClassString));
×
144
        }
145
    }
×
146

147
    /**
148
     * Adds the triples of this statement item to the given NanopubCreator.
149
     *
150
     * @param npCreator the NanopubCreator to which the triples will be added
151
     * @throws org.nanopub.MalformedNanopubException        if the statement item is not properly set up
152
     * @throws org.nanopub.NanopubAlreadyFinalizedException if the NanopubCreator has already been finalized
153
     */
154
    public void addTriplesTo(NanopubCreator npCreator) throws MalformedNanopubException, NanopubAlreadyFinalizedException {
155
        if (hasEmptyElements()) {
×
156
            if (isOptional()) {
×
157
                return;
×
158
            } else {
159
                throw new MalformedNanopubException("Field of statement not set.");
×
160
            }
161
        }
162
        for (RepetitionGroup rg : repetitionGroups) {
×
163
            rg.addTriplesTo(npCreator);
×
164
        }
×
165
    }
×
166

167
    private Template getTemplate() {
168
        return context.getTemplate();
12✔
169
    }
170

171
    /**
172
     * Returns the number of the repetition groups for this statement item.
173
     *
174
     * @return the number of repetition groups
175
     */
176
    public int getRepetitionCount() {
177
        return repetitionGroups.size();
12✔
178
    }
179

180
    /**
181
     * Returns whether the statement is optional.
182
     */
183
    public boolean isOptional() {
184
        return repetitionGroups.size() == 1 && getTemplate().isOptionalStatement(statementId);
×
185
    }
186

187
    /**
188
     * Returns whether the statement is advanced.
189
     */
190
    public boolean isAdvanced() {
191
        return getTemplate().isAdvancedStatement(statementId);
×
192
    }
193

194
    /**
195
     * Checks if this statement item is grouped.
196
     *
197
     * @return true if the statement item is grouped, false otherwise
198
     */
199
    public boolean isGrouped() {
200
        return getTemplate().isGroupedStatement(statementId);
18✔
201
    }
202

203
    /**
204
     * Checks if this statement item is repeatable.
205
     *
206
     * @return true if the statement item is repeatable, false otherwise
207
     */
208
    public boolean isRepeatable() {
209
        return getTemplate().isRepeatableStatement(statementId);
18✔
210
    }
211

212
    /**
213
     * Checks if this statement item has empty elements.
214
     *
215
     * @return true if any of the repetition groups has empty elements, false otherwise
216
     */
217
    public boolean hasEmptyElements() {
218
        return repetitionGroups.get(0).hasEmptyElements();
×
219
    }
220

221
    /**
222
     * Returns the set of IRIs associated with this statement item.
223
     *
224
     * @return a set of IRIs
225
     */
226
    public Set<IRI> getIriSet() {
227
        return iriSet;
9✔
228
    }
229

230
    /**
231
     * Checks if this statement item will match any triple.
232
     *
233
     * @return true if it will match any triple, false otherwise
234
     */
235
    public boolean willMatchAnyTriple() {
236
        return repetitionGroups.get(0).matches(dummyStatementList);
×
237
    }
238

239
    /**
240
     * Fills this statement item with the provided list of statements, matching them against the repetition groups.
241
     *
242
     * @param statements the list of statements to match against
243
     * @throws com.knowledgepixels.nanodash.template.UnificationException if the statements cannot be unified with this statement item
244
     */
245
    public void fill(List<Statement> statements) throws UnificationException {
246
        if (isMatched) return;
9!
247
        if (repetitionGroups.size() == 1) {
15!
248
            RepetitionGroup rg = repetitionGroups.get(0);
18✔
249
            if (rg.matches(statements)) {
12✔
250
                rg.fill(statements);
12✔
251
            } else {
252
                return;
3✔
253
            }
254
        } else {
3✔
255
            return;
×
256
        }
257
        isMatched = true;
9✔
258
        if (!isRepeatable()) return;
12✔
259
        while (true) {
260
            Set<IRI> modelsBefore = new HashSet<>(context.getComponentModels().keySet());
24✔
261
            List<Statement> statementsBefore = new ArrayList<>(statements);
15✔
262
            RepetitionGroup newGroup = new RepetitionGroup();
15✔
263
            boolean filled;
264
            if (newGroup.matches(statements)) {
12✔
265
                try {
266
                    newGroup.fill(statements);
9✔
267
                    filled = true;
6✔
268
                } catch (UnificationException ex) {
×
269
                    // matches() validates unifiability statelessly, but fill() mutates shared
270
                    // placeholder models via unifyWith as it goes, so binding an earlier part can
271
                    // make a later one non-unifiable ("seemed to work but then didn't"). Treat this
272
                    // like end-of-repetitions rather than letting it abort the whole template fill:
273
                    // roll back the partial statement consumption and discard the trial group.
274
                    logger.warn("Repetition fill failed after matches() succeeded for {}; stopping repetitions of this statement", statementId, ex);
×
275
                    statements.clear();
×
276
                    statements.addAll(statementsBefore);
×
277
                    filled = false;
×
278
                }
3✔
279
            } else {
280
                filled = false;
6✔
281
            }
282
            if (!filled) {
6✔
283
                newGroup.disconnect();
6✔
284
                // The trial group's constructor registered fresh (empty) component
285
                // models for its narrow-scope placeholders (e.g. public-key__N). If
286
                // left behind, a later real repetition group at the same index reuses
287
                // the stale empty model and skips param seeding — the "derive new
288
                // introduction" empty-fields bug. Drop exactly the models this trial
289
                // group added; shared (wide-scope) models existed before and stay.
290
                context.getComponentModels().keySet().retainAll(modelsBefore);
21✔
291
                return;
3✔
292
            }
293
            addRepetitionGroup(newGroup);
9✔
294
        }
3✔
295
    }
296

297
    /**
298
     * Marks the filling of this statement item as finished, indicating that all values have been filled.
299
     */
300
    public void fillFinished() {
301
        for (RepetitionGroup rg : repetitionGroups) {
33✔
302
            rg.fillFinished();
6✔
303
        }
3✔
304
    }
3✔
305

306
    /**
307
     * Finalizes the values of all ValueItems in this statement item.
308
     */
309
    public void finalizeValues() {
310
        for (RepetitionGroup rg : repetitionGroups) {
33✔
311
            rg.finalizeValues();
6✔
312
        }
3✔
313
    }
3✔
314

315
    /**
316
     * Returns true if the statement item has been matched with a set of statements.
317
     *
318
     * @return true if matched, false otherwise
319
     */
320
    public boolean isMatched() {
321
        return isMatched;
×
322
    }
323

324
    /**
325
     * Checks if this statement item is empty, meaning it has no filled repetition groups.
326
     *
327
     * @return true if the statement item is empty, false otherwise
328
     */
329
    public boolean isEmpty() {
330
        return repetitionGroups.size() == 1 && repetitionGroups.get(0).isEmpty();
48✔
331
    }
332

333
    /**
334
     * Represents a group of repetitions for a statement item, containing multiple statement parts.
335
     */
336
    public class RepetitionGroup implements Serializable {
337

338
        private List<StatementPartItem> statementParts;
339
        private List<ValueItem> localItems = new ArrayList<>();
15✔
340
        private boolean filled = false;
9✔
341

342
        private List<ValueItem> items = new ArrayList<>();
15✔
343

344
        Label addRepetitionButton, removeRepetitionButton, optionalMark;
345

346
        /**
347
         * Constructor for creating a RepetitionGroup.
348
         */
349
        public RepetitionGroup() {
15✔
350
            statementParts = new ArrayList<>();
15✔
351
            for (IRI s : statementPartIds) {
33✔
352
                StatementPartItem statement = new StatementPartItem("statement",
18✔
353
                        makeValueItem("subj", getTemplate().getSubject(s), s),
24✔
354
                        makeValueItem("pred", getTemplate().getPredicate(s), s),
24✔
355
                        makeValueItem("obj", getTemplate().getObject(s), s)
21✔
356
                );
357
                statementParts.add(statement);
15✔
358

359
                // Some of the methods of StatementItem and RepetitionGroup don't work properly before this
360
                // object is fully instantiated:
361
                boolean isFirstGroup = repetitionGroups.isEmpty();
12✔
362
                boolean isFirstLine = statementParts.size() == 1;
27✔
363
                boolean isLastLine = statementParts.size() == statementPartIds.size();
33✔
364
                boolean isOptional = getTemplate().isOptionalStatement(statementId);
18✔
365

366
                if (statementParts.size() == 1 && !isFirstGroup) {
21✔
367
                    statement.add(new AttributeAppender("class", " separate-statement"));
39✔
368
                }
369

370
                // This code adds "advanced" marks similar to "optional":
371
//                if (!context.isReadOnly()) {
372
//                    if (isOptional && isLastLine) {
373
//                        if (isAdvanced()) {
374
//                            optionalMark = new Label("label", "(optional, advanced)");
375
//                        } else {
376
//                            optionalMark = new Label("label", "(optional)");
377
//                        }
378
//                    } else if (isAdvanced()) {
379
//                        optionalMark = new Label("label", "(advanced)");
380
//                    } else {
381
//                        optionalMark = new Label("label", "");
382
//                        optionalMark.setVisible(false);
383
//                    }
384
//                } else {
385
//                    optionalMark = new Label("label", "");
386
//                    optionalMark.setVisible(false);
387
//                }
388

389
                if (!context.isReadOnly() && isOptional && isLastLine) {
24!
390
                    optionalMark = new Label("label", "(optional)");
24✔
391
                } else {
392
                    optionalMark = new Label("label", "");
21✔
393
                    optionalMark.setVisible(false);
15✔
394
                }
395
                statement.add(optionalMark);
30✔
396
                if (isLastLine) {
6✔
397
                    addRepetitionButton = new Label("add-repetition", "+");
21✔
398
                    statement.add(addRepetitionButton);
30✔
399
                    addRepetitionButton.add(new AjaxEventBehavior("click") {
74✔
400

401
                        @Override
402
                        protected void onEvent(AjaxRequestTarget target) {
403
                            addRepetitionGroup(new RepetitionGroup());
×
404
                            target.add(StatementItem.this);
×
405
                            target.appendJavaScript("updateElements();");
×
406
                        }
×
407

408
                    });
409
                } else {
410
                    statement.add(new Label("add-repetition", "").setVisible(false));
45✔
411
                }
412
                if (isFirstLine) {
6✔
413
                    removeRepetitionButton = new Label("remove-repetition", "-");
21✔
414
                    statement.add(removeRepetitionButton);
30✔
415
                    removeRepetitionButton.add(new AjaxEventBehavior("click") {
74✔
416

417
                        @Override
418
                        protected void onEvent(AjaxRequestTarget target) {
419
                            RepetitionGroup.this.remove();
×
420
                            target.appendJavaScript("updateElements();");
×
421
                            target.add(StatementItem.this);
×
422
                        }
×
423

424
                    });
425
                } else {
426
                    statement.add(new Label("remove-repetition", "").setVisible(false));
45✔
427
                }
428
            }
3✔
429
        }
3✔
430

431
        private ValueItem makeValueItem(String id, Value value, IRI statementPartId) {
432
            if (isFirst() && value instanceof IRI) {
18✔
433
                iriSet.add((IRI) value);
21✔
434
            }
435
            ValueItem vi = new ValueItem(id, transform(value), statementPartId, this);
30✔
436
            localItems.add(vi);
15✔
437
            items.add(vi);
15✔
438
            return vi;
6✔
439
        }
440

441
        private void disconnect() {
442
            for (ValueItem vi : new ArrayList<>(localItems)) {
42✔
443
                // TODO These remove operations on list are slow. Improve:
444
                localItems.remove(vi);
15✔
445
                items.remove(vi);
15✔
446
                vi.removeFromContext();
6✔
447
            }
3✔
448
        }
3✔
449

450
        /**
451
         * Returns the statement parts.
452
         *
453
         * @return a list of StatementPartItem objects representing the statement parts
454
         */
455
        public List<StatementPartItem> getStatementParts() {
456
            return statementParts;
×
457
        }
458

459
        /**
460
         * Returns the index of this repetition group in the list of repetition groups.
461
         *
462
         * @return the index of this repetition group
463
         */
464
        public int getRepeatIndex() {
465
            if (!repetitionGroups.contains(this)) return repetitionGroups.size();
33✔
466
            return repetitionGroups.indexOf(this);
18✔
467
        }
468

469
        /**
470
         * Returns the total number of repetition groups for this statement.
471
         *
472
         * @return the number of repetition groups
473
         */
474
        public int getRepetitionCount() {
475
            return StatementItem.this.getRepetitionCount();
×
476
        }
477

478
        /**
479
         * Returns true if the repeat index if the first one.
480
         *
481
         * @return true if the repeat index is 0, false otherwise
482
         */
483
        public boolean isFirst() {
484
            return getRepeatIndex() == 0;
21✔
485
        }
486

487
        /**
488
         * Returns true if the repeat index is the last one.
489
         *
490
         * @return true if the repeat index is the last one, false otherwise
491
         */
492
        public boolean isLast() {
493
            return getRepeatIndex() == repetitionGroups.size() - 1;
×
494
        }
495

496
        private void remove() {
497
            String thisSuffix = getRepeatSuffix();
×
498
            for (IRI iriBase : iriSet) {
×
499
                IRI thisIri = vf.createIRI(iriBase + thisSuffix);
×
500
                if (context.getComponentModels().containsKey(thisIri)) {
×
501
                    IModel swapModel1 = (IModel) context.getComponentModels().get(thisIri);
×
502
                    for (int i = getRepeatIndex() + 1; i < repetitionGroups.size(); i++) {
×
503
                        IModel swapModel2 = (IModel) context.getComponentModels().get(vf.createIRI(iriBase + getRepeatSuffix(i)));
×
504
                        if (swapModel1 != null && swapModel2 != null) {
×
505
                            swapModel1.setObject(swapModel2.getObject());
×
506
                        }
507
                        // Drop any retained rawInput so the shifted model value is rendered
508
                        // instead of the user's previous (post-validation-error) entry.
509
                        clearInputForModel(swapModel1);
×
510
                        swapModel1 = swapModel2;
×
511
                    }
512
                    if (swapModel1 != null) {
×
513
                        swapModel1.setObject(null);
×
514
                        clearInputForModel(swapModel1);
×
515
                    }
516
                }
517
            }
×
518
            RepetitionGroup lastGroup = repetitionGroups.get(repetitionGroups.size() - 1);
×
519
            repetitionGroups.remove(lastGroup);
×
520
            for (ValueItem vi : lastGroup.items) {
×
521
                vi.removeFromContext();
×
522
            }
×
523
            repetitionGroupsChanged = true;
×
524
        }
×
525

526
        private void clearInputForModel(IModel<?> model) {
527
            if (model == null) return;
×
528
            for (Component c : context.getComponents()) {
×
529
                if (c instanceof FormComponent && c.getDefaultModel() == model) {
×
530
                    ((FormComponent<?>) c).clearInput();
×
531
                }
532
            }
×
533
        }
×
534

535
        private String getRepeatSuffix() {
536
            return getRepeatSuffix(getRepeatIndex());
15✔
537
        }
538

539
        private String getRepeatSuffix(int i) {
540
            if (i == 0) return "";
6!
541
            // TODO: Check that this double-underscore pattern isn't used otherwise:
542
            return "__" + i;
9✔
543
        }
544

545
        /**
546
         * Returns the template context associated.
547
         *
548
         * @return the TemplateContext
549
         */
550
        public TemplateContext getContext() {
551
            return context;
12✔
552
        }
553

554
        /**
555
         * Checks if this repetition group is optional.
556
         *
557
         * @return true if the repetition group is optional, false otherwise
558
         */
559
        public boolean isOptional() {
560
            if (!getTemplate().isOptionalStatement(statementId)) return false;
30✔
561
            if (repetitionGroups.size() == 0) return true;
21✔
562
            if (repetitionGroups.size() == 1 && repetitionGroups.get(0) == this) return true;
39!
563
            return false;
6✔
564
        }
565

566
        private Value transform(Value value) {
567
            if (!(value instanceof IRI)) {
9✔
568
                return value;
6✔
569
            }
570
            IRI iri = (IRI) value;
9✔
571
            String iriString = iri.stringValue();
9✔
572
            iriString = iriString.replaceAll("~~ARTIFACTCODE~~", "~~~ARTIFACTCODE~~~");
15✔
573
            // Only add "__N" to URI from second repetition group on; for the first group, information about
574
            // narrow scopes is not yet complete.
575
            if (getRepeatIndex() > 0 && context.hasNarrowScope(iri)) {
27✔
576
                if (context.getTemplate().isPlaceholder(iri) || context.getTemplate().isLocalResource(iri)) {
42!
577
                    iriString += getRepeatSuffix();
15✔
578
                }
579
            }
580
            return vf.createIRI(iriString);
12✔
581
        }
582

583
        /**
584
         * Adds the triples of this repetition group to the given NanopubCreator.
585
         *
586
         * @param npCreator the NanopubCreator to which the triples will be added
587
         * @throws org.nanopub.NanopubAlreadyFinalizedException if the NanopubCreator has already been finalized
588
         */
589
        public void addTriplesTo(NanopubCreator npCreator) throws NanopubAlreadyFinalizedException {
590
            Template t = getTemplate();
×
591
            for (IRI s : statementPartIds) {
×
592
                IRI subj = context.processIri((IRI) transform(t.getSubject(s)));
×
593
                IRI pred = context.processIri((IRI) transform(t.getPredicate(s)));
×
594
                Value obj = context.processValue(transform(t.getObject(s)));
×
595
                if (context.getType() == ContextType.ASSERTION) {
×
596
                    npCreator.addAssertionStatement(subj, pred, obj);
×
597
                } else if (context.getType() == ContextType.PROVENANCE) {
×
598
                    npCreator.addProvenanceStatement(subj, pred, obj);
×
599
                } else if (context.getType() == ContextType.PUBINFO) {
×
600
                    npCreator.addPubinfoStatement(subj, pred, obj);
×
601
                }
602
            }
×
603
            for (ValueItem vi : items) {
×
604
                if (vi.getComponent() instanceof GuidedChoiceItem) {
×
605
                    String value = ((GuidedChoiceItem) vi.getComponent()).getModel().getObject();
×
606
                    if (value != null && GuidedChoiceItem.getLabel(value) != null) {
×
607
                        String label = GuidedChoiceItem.getLabel(value);
×
608
                        if (label.length() > 1000) label = label.substring(0, 997) + "...";
×
609
                        try {
610
                            npCreator.addPubinfoStatement(vf.createIRI(value), NTEMPLATE.HAS_LABEL_FROM_API, vf.createLiteral(label));
×
611
                        } catch (IllegalArgumentException ex) {
×
612
                            logger.error("Could not create IRI from value: {}", value, ex);
×
613
                        }
×
614
                    }
615
                }
616
            }
×
617
        }
×
618

619
        private boolean hasEmptyElements() {
620
            for (IRI s : statementPartIds) {
×
621
                if (context.processIri((IRI) transform(getTemplate().getSubject(s))) == null) return true;
×
622
                if (context.processIri((IRI) transform(getTemplate().getPredicate(s))) == null) return true;
×
623
                if (context.processValue(transform(getTemplate().getObject(s))) == null) return true;
×
624
            }
×
625
            return false;
×
626
        }
627

628
        /**
629
         * Checks if this repetition group is empty, meaning it has no filled items.
630
         *
631
         * @return true if the repetition group is empty, false otherwise
632
         */
633
        public boolean isEmpty() {
634
            for (IRI s : statementPartIds) {
36✔
635
                Template t = getTemplate();
12✔
636
                IRI subj = t.getSubject(s);
12✔
637
                if (t.isPlaceholder(subj) && context.hasNarrowScope(subj) && context.processIri((IRI) transform(subj)) != null)
57!
638
                    return false;
6✔
639
                IRI pred = t.getPredicate(s);
12✔
640
                if (t.isPlaceholder(pred) && context.hasNarrowScope(pred) && context.processIri((IRI) transform(pred)) != null)
12!
641
                    return false;
×
642
                Value obj = t.getObject(s);
12✔
643
                if (obj instanceof IRI && t.isPlaceholder((IRI) obj) && context.hasNarrowScope((IRI) obj) && context.processValue(transform(obj)) != null)
69!
644
                    return false;
6✔
645
            }
3✔
646
            return true;
6✔
647
        }
648

649
        /**
650
         * Checks if this repetition group matches the provided list of statements.
651
         *
652
         * @param statements the list of statements to match against
653
         * @return true if the repetition group matches, false otherwise
654
         */
655
        public boolean matches(List<Statement> statements) {
656
            if (filled) return false;
9!
657
            // matches() must agree with fill(): because fill() binds shared placeholder models as it
658
            // goes, a valid assignment can only be confirmed by actually simulating those bindings.
659
            // We do exactly that (with backtracking) but on a copy and then restore the models, so
660
            // matches() stays side-effect free.
661
            Map<IRI, Object> snapshot = snapshotModels();
9✔
662
            try {
663
                return assignParts(0, new ArrayList<>(statements));
30✔
664
            } finally {
665
                restoreModels(snapshot);
9✔
666
            }
667
        }
668

669
        /**
670
         * Fills this repetition group with the provided list of statements, unifying them with the statement parts.
671
         *
672
         * @param statements the list of statements to match against
673
         * @throws UnificationException if the statements cannot be unified with this repetition group
674
         */
675
        public void fill(List<Statement> statements) throws UnificationException {
676
            if (filled) throw new UnificationException("Already filled");
9!
677
            // Backtracking assignment: a greedy first-match can bind a shared placeholder in a way
678
            // that blocks a later part even though a consistent assignment exists. assignParts tries
679
            // alternatives and rolls back the model bindings between attempts. On success the matched
680
            // statements are removed from the list and the winning bindings are kept.
681
            if (!assignParts(0, statements)) {
15!
682
                throw new UnificationException("Unification seemed to work but then didn't");
×
683
            }
684
            filled = true;
9✔
685
        }
3✔
686

687
        /**
688
         * Tries to assign each remaining statement part (from index {@code partIndex} on) to a distinct
689
         * statement in {@code available} such that all bindings of shared placeholder models are mutually
690
         * consistent. Uses backtracking: on a failed branch the model bindings are restored and the next
691
         * candidate is tried. On success the chosen statements are removed from {@code available} and the
692
         * winning bindings remain applied to the component models.
693
         *
694
         * @param partIndex the index of the next statement part to assign
695
         * @param available the statements still available for assignment (mutated in place)
696
         * @return true if all remaining parts could be assigned consistently
697
         */
698
        private boolean assignParts(int partIndex, List<Statement> available) {
699
            if (partIndex == statementParts.size()) return true;
21✔
700
            StatementPartItem p = statementParts.get(partIndex);
18✔
701
            for (int i = 0; i < available.size(); i++) {
24✔
702
                Statement s = available.get(i);
15✔
703
                Map<IRI, Object> snapshot = snapshotModels();
9✔
704
                if (unifyPart(p.getPredicate(), s.getPredicate())  // checking predicate first optimizes performance
27✔
705
                        && unifyPart(p.getSubject(), s.getSubject())
21✔
706
                        && unifyPart(p.getObject(), s.getObject())) {
15✔
707
                    available.remove(i);
12✔
708
                    if (assignParts(partIndex + 1, available)) return true;
27✔
709
                    available.add(i, s);
12✔
710
                }
711
                restoreModels(snapshot);
9✔
712
            }
713
            return false;
6✔
714
        }
715

716
        /**
717
         * Checks unifiability and, if unifiable, applies the binding. Returns false instead of throwing,
718
         * so the caller can backtrack. (A partial binding left behind here is rolled back by the caller's
719
         * model snapshot.)
720
         */
721
        private boolean unifyPart(ValueItem item, Value v) {
722
            if (!item.isUnifiableWith(v)) return false;
18✔
723
            try {
724
                item.unifyWith(v);
9✔
725
                return true;
6✔
726
            } catch (UnificationException ex) {
×
727
                return false;
×
728
            }
729
        }
730

731
        /**
732
         * Snapshots the current values of all shared component models, so a trial binding can be rolled back.
733
         */
734
        private Map<IRI, Object> snapshotModels() {
735
            Map<IRI, Object> snapshot = new HashMap<>();
12✔
736
            for (Map.Entry<IRI, IModel<?>> e : context.getComponentModels().entrySet()) {
42✔
737
                snapshot.put(e.getKey(), e.getValue().getObject());
30✔
738
            }
3✔
739
            return snapshot;
6✔
740
        }
741

742
        /**
743
         * Restores component model values captured by {@link #snapshotModels()}.
744
         */
745
        @SuppressWarnings("unchecked")
746
        private void restoreModels(Map<IRI, Object> snapshot) {
747
            Map<IRI, IModel<?>> models = context.getComponentModels();
15✔
748
            for (Map.Entry<IRI, Object> e : snapshot.entrySet()) {
33✔
749
                IModel<?> m = models.get(e.getKey());
18✔
750
                if (m != null) ((IModel<Object>) m).setObject(e.getValue());
18!
751
            }
3✔
752
        }
3✔
753

754
        /**
755
         * Marks the filling of this repetition group as finished, indicating that all values have been filled.
756
         */
757
        public void fillFinished() {
758
            for (ValueItem vi : items) {
33✔
759
                vi.fillFinished();
6✔
760
            }
3✔
761
        }
3✔
762

763
        /**
764
         * Finalizes the values of all ValueItems in this repetition group.
765
         */
766
        public void finalizeValues() {
767
            for (ValueItem vi : items) {
33✔
768
                vi.finalizeValues();
6✔
769
            }
3✔
770
        }
3✔
771

772
    }
773

774
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
6✔
775
    private static final List<Statement> dummyStatementList = new ArrayList<Statement>(Collections.singletonList(vf.createStatement(vf.createIRI("http://dummy.com/"), vf.createIRI("http://dummy.com/"), vf.createIRI("http://dummy.com/"))));
51✔
776

777
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc