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

knowledgepixels / nanodash / 28106580844

24 Jun 2026 02:38PM UTC coverage: 26.527% (+0.02%) from 26.511%
28106580844

push

github

web-flow
Merge pull request #502 from knowledgepixels/fix/repeated-local-resource-labels

fix: distinguish repeated local resources by index in form labels

1557 of 6925 branches covered (22.48%)

Branch coverage included in aggregate %.

3429 of 11871 relevant lines covered (28.89%)

4.25 hits per line

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

52.35
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;
×
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
            RepetitionGroup newGroup = new RepetitionGroup();
15✔
262
            if (newGroup.matches(statements)) {
12✔
263
                newGroup.fill(statements);
9✔
264
                addRepetitionGroup(newGroup);
12✔
265
            } else {
266
                newGroup.disconnect();
6✔
267
                // The trial group's constructor registered fresh (empty) component
268
                // models for its narrow-scope placeholders (e.g. public-key__N). If
269
                // left behind, a later real repetition group at the same index reuses
270
                // the stale empty model and skips param seeding — the "derive new
271
                // introduction" empty-fields bug. Drop exactly the models this trial
272
                // group added; shared (wide-scope) models existed before and stay.
273
                context.getComponentModels().keySet().retainAll(modelsBefore);
21✔
274
                return;
3✔
275
            }
276
        }
3✔
277
    }
278

279
    /**
280
     * Marks the filling of this statement item as finished, indicating that all values have been filled.
281
     */
282
    public void fillFinished() {
283
        for (RepetitionGroup rg : repetitionGroups) {
33✔
284
            rg.fillFinished();
6✔
285
        }
3✔
286
    }
3✔
287

288
    /**
289
     * Finalizes the values of all ValueItems in this statement item.
290
     */
291
    public void finalizeValues() {
292
        for (RepetitionGroup rg : repetitionGroups) {
33✔
293
            rg.finalizeValues();
6✔
294
        }
3✔
295
    }
3✔
296

297
    /**
298
     * Returns true if the statement item has been matched with a set of statements.
299
     *
300
     * @return true if matched, false otherwise
301
     */
302
    public boolean isMatched() {
303
        return isMatched;
×
304
    }
305

306
    /**
307
     * Checks if this statement item is empty, meaning it has no filled repetition groups.
308
     *
309
     * @return true if the statement item is empty, false otherwise
310
     */
311
    public boolean isEmpty() {
312
        return repetitionGroups.size() == 1 && repetitionGroups.get(0).isEmpty();
48✔
313
    }
314

315
    /**
316
     * Represents a group of repetitions for a statement item, containing multiple statement parts.
317
     */
318
    public class RepetitionGroup implements Serializable {
319

320
        private List<StatementPartItem> statementParts;
321
        private List<ValueItem> localItems = new ArrayList<>();
15✔
322
        private boolean filled = false;
9✔
323

324
        private List<ValueItem> items = new ArrayList<>();
15✔
325

326
        Label addRepetitionButton, removeRepetitionButton, optionalMark;
327

328
        /**
329
         * Constructor for creating a RepetitionGroup.
330
         */
331
        public RepetitionGroup() {
15✔
332
            statementParts = new ArrayList<>();
15✔
333
            for (IRI s : statementPartIds) {
33✔
334
                StatementPartItem statement = new StatementPartItem("statement",
18✔
335
                        makeValueItem("subj", getTemplate().getSubject(s), s),
24✔
336
                        makeValueItem("pred", getTemplate().getPredicate(s), s),
24✔
337
                        makeValueItem("obj", getTemplate().getObject(s), s)
21✔
338
                );
339
                statementParts.add(statement);
15✔
340

341
                // Some of the methods of StatementItem and RepetitionGroup don't work properly before this
342
                // object is fully instantiated:
343
                boolean isFirstGroup = repetitionGroups.isEmpty();
12✔
344
                boolean isFirstLine = statementParts.size() == 1;
27✔
345
                boolean isLastLine = statementParts.size() == statementPartIds.size();
33✔
346
                boolean isOptional = getTemplate().isOptionalStatement(statementId);
18✔
347

348
                if (statementParts.size() == 1 && !isFirstGroup) {
21✔
349
                    statement.add(new AttributeAppender("class", " separate-statement"));
39✔
350
                }
351

352
                // This code adds "advanced" marks similar to "optional":
353
//                if (!context.isReadOnly()) {
354
//                    if (isOptional && isLastLine) {
355
//                        if (isAdvanced()) {
356
//                            optionalMark = new Label("label", "(optional, advanced)");
357
//                        } else {
358
//                            optionalMark = new Label("label", "(optional)");
359
//                        }
360
//                    } else if (isAdvanced()) {
361
//                        optionalMark = new Label("label", "(advanced)");
362
//                    } else {
363
//                        optionalMark = new Label("label", "");
364
//                        optionalMark.setVisible(false);
365
//                    }
366
//                } else {
367
//                    optionalMark = new Label("label", "");
368
//                    optionalMark.setVisible(false);
369
//                }
370

371
                if (!context.isReadOnly() && isOptional && isLastLine) {
24!
372
                    optionalMark = new Label("label", "(optional)");
24✔
373
                } else {
374
                    optionalMark = new Label("label", "");
21✔
375
                    optionalMark.setVisible(false);
15✔
376
                }
377
                statement.add(optionalMark);
30✔
378
                if (isLastLine) {
6✔
379
                    addRepetitionButton = new Label("add-repetition", "+");
21✔
380
                    statement.add(addRepetitionButton);
30✔
381
                    addRepetitionButton.add(new AjaxEventBehavior("click") {
74✔
382

383
                        @Override
384
                        protected void onEvent(AjaxRequestTarget target) {
385
                            addRepetitionGroup(new RepetitionGroup());
×
386
                            target.add(StatementItem.this);
×
387
                            target.appendJavaScript("updateElements();");
×
388
                        }
×
389

390
                    });
391
                } else {
392
                    statement.add(new Label("add-repetition", "").setVisible(false));
45✔
393
                }
394
                if (isFirstLine) {
6✔
395
                    removeRepetitionButton = new Label("remove-repetition", "-");
21✔
396
                    statement.add(removeRepetitionButton);
30✔
397
                    removeRepetitionButton.add(new AjaxEventBehavior("click") {
74✔
398

399
                        @Override
400
                        protected void onEvent(AjaxRequestTarget target) {
401
                            RepetitionGroup.this.remove();
×
402
                            target.appendJavaScript("updateElements();");
×
403
                            target.add(StatementItem.this);
×
404
                        }
×
405

406
                    });
407
                } else {
408
                    statement.add(new Label("remove-repetition", "").setVisible(false));
45✔
409
                }
410
            }
3✔
411
        }
3✔
412

413
        private ValueItem makeValueItem(String id, Value value, IRI statementPartId) {
414
            if (isFirst() && value instanceof IRI) {
18✔
415
                iriSet.add((IRI) value);
21✔
416
            }
417
            ValueItem vi = new ValueItem(id, transform(value), statementPartId, this);
30✔
418
            localItems.add(vi);
15✔
419
            items.add(vi);
15✔
420
            return vi;
6✔
421
        }
422

423
        private void disconnect() {
424
            for (ValueItem vi : new ArrayList<>(localItems)) {
42✔
425
                // TODO These remove operations on list are slow. Improve:
426
                localItems.remove(vi);
15✔
427
                items.remove(vi);
15✔
428
                vi.removeFromContext();
6✔
429
            }
3✔
430
        }
3✔
431

432
        /**
433
         * Returns the statement parts.
434
         *
435
         * @return a list of StatementPartItem objects representing the statement parts
436
         */
437
        public List<StatementPartItem> getStatementParts() {
438
            return statementParts;
×
439
        }
440

441
        /**
442
         * Returns the index of this repetition group in the list of repetition groups.
443
         *
444
         * @return the index of this repetition group
445
         */
446
        public int getRepeatIndex() {
447
            if (!repetitionGroups.contains(this)) return repetitionGroups.size();
33✔
448
            return repetitionGroups.indexOf(this);
18✔
449
        }
450

451
        /**
452
         * Returns the total number of repetition groups for this statement.
453
         *
454
         * @return the number of repetition groups
455
         */
456
        public int getRepetitionCount() {
457
            return StatementItem.this.getRepetitionCount();
×
458
        }
459

460
        /**
461
         * Returns true if the repeat index if the first one.
462
         *
463
         * @return true if the repeat index is 0, false otherwise
464
         */
465
        public boolean isFirst() {
466
            return getRepeatIndex() == 0;
21✔
467
        }
468

469
        /**
470
         * Returns true if the repeat index is the last one.
471
         *
472
         * @return true if the repeat index is the last one, false otherwise
473
         */
474
        public boolean isLast() {
475
            return getRepeatIndex() == repetitionGroups.size() - 1;
×
476
        }
477

478
        private void remove() {
479
            String thisSuffix = getRepeatSuffix();
×
480
            for (IRI iriBase : iriSet) {
×
481
                IRI thisIri = vf.createIRI(iriBase + thisSuffix);
×
482
                if (context.getComponentModels().containsKey(thisIri)) {
×
483
                    IModel swapModel1 = (IModel) context.getComponentModels().get(thisIri);
×
484
                    for (int i = getRepeatIndex() + 1; i < repetitionGroups.size(); i++) {
×
485
                        IModel swapModel2 = (IModel) context.getComponentModels().get(vf.createIRI(iriBase + getRepeatSuffix(i)));
×
486
                        if (swapModel1 != null && swapModel2 != null) {
×
487
                            swapModel1.setObject(swapModel2.getObject());
×
488
                        }
489
                        // Drop any retained rawInput so the shifted model value is rendered
490
                        // instead of the user's previous (post-validation-error) entry.
491
                        clearInputForModel(swapModel1);
×
492
                        swapModel1 = swapModel2;
×
493
                    }
494
                    if (swapModel1 != null) {
×
495
                        swapModel1.setObject(null);
×
496
                        clearInputForModel(swapModel1);
×
497
                    }
498
                }
499
            }
×
500
            RepetitionGroup lastGroup = repetitionGroups.get(repetitionGroups.size() - 1);
×
501
            repetitionGroups.remove(lastGroup);
×
502
            for (ValueItem vi : lastGroup.items) {
×
503
                vi.removeFromContext();
×
504
            }
×
505
            repetitionGroupsChanged = true;
×
506
        }
×
507

508
        private void clearInputForModel(IModel<?> model) {
509
            if (model == null) return;
×
510
            for (Component c : context.getComponents()) {
×
511
                if (c instanceof FormComponent && c.getDefaultModel() == model) {
×
512
                    ((FormComponent<?>) c).clearInput();
×
513
                }
514
            }
×
515
        }
×
516

517
        private String getRepeatSuffix() {
518
            return getRepeatSuffix(getRepeatIndex());
15✔
519
        }
520

521
        private String getRepeatSuffix(int i) {
522
            if (i == 0) return "";
6!
523
            // TODO: Check that this double-underscore pattern isn't used otherwise:
524
            return "__" + i;
9✔
525
        }
526

527
        /**
528
         * Returns the template context associated.
529
         *
530
         * @return the TemplateContext
531
         */
532
        public TemplateContext getContext() {
533
            return context;
12✔
534
        }
535

536
        /**
537
         * Checks if this repetition group is optional.
538
         *
539
         * @return true if the repetition group is optional, false otherwise
540
         */
541
        public boolean isOptional() {
542
            if (!getTemplate().isOptionalStatement(statementId)) return false;
30✔
543
            if (repetitionGroups.size() == 0) return true;
21✔
544
            if (repetitionGroups.size() == 1 && repetitionGroups.get(0) == this) return true;
39!
545
            return false;
6✔
546
        }
547

548
        private Value transform(Value value) {
549
            if (!(value instanceof IRI)) {
9✔
550
                return value;
6✔
551
            }
552
            IRI iri = (IRI) value;
9✔
553
            String iriString = iri.stringValue();
9✔
554
            iriString = iriString.replaceAll("~~ARTIFACTCODE~~", "~~~ARTIFACTCODE~~~");
15✔
555
            // Only add "__N" to URI from second repetition group on; for the first group, information about
556
            // narrow scopes is not yet complete.
557
            if (getRepeatIndex() > 0 && context.hasNarrowScope(iri)) {
27✔
558
                if (context.getTemplate().isPlaceholder(iri) || context.getTemplate().isLocalResource(iri)) {
42!
559
                    iriString += getRepeatSuffix();
15✔
560
                }
561
            }
562
            return vf.createIRI(iriString);
12✔
563
        }
564

565
        /**
566
         * Adds the triples of this repetition group to the given NanopubCreator.
567
         *
568
         * @param npCreator the NanopubCreator to which the triples will be added
569
         * @throws org.nanopub.NanopubAlreadyFinalizedException if the NanopubCreator has already been finalized
570
         */
571
        public void addTriplesTo(NanopubCreator npCreator) throws NanopubAlreadyFinalizedException {
572
            Template t = getTemplate();
×
573
            for (IRI s : statementPartIds) {
×
574
                IRI subj = context.processIri((IRI) transform(t.getSubject(s)));
×
575
                IRI pred = context.processIri((IRI) transform(t.getPredicate(s)));
×
576
                Value obj = context.processValue(transform(t.getObject(s)));
×
577
                if (context.getType() == ContextType.ASSERTION) {
×
578
                    npCreator.addAssertionStatement(subj, pred, obj);
×
579
                } else if (context.getType() == ContextType.PROVENANCE) {
×
580
                    npCreator.addProvenanceStatement(subj, pred, obj);
×
581
                } else if (context.getType() == ContextType.PUBINFO) {
×
582
                    npCreator.addPubinfoStatement(subj, pred, obj);
×
583
                }
584
            }
×
585
            for (ValueItem vi : items) {
×
586
                if (vi.getComponent() instanceof GuidedChoiceItem) {
×
587
                    String value = ((GuidedChoiceItem) vi.getComponent()).getModel().getObject();
×
588
                    if (value != null && GuidedChoiceItem.getLabel(value) != null) {
×
589
                        String label = GuidedChoiceItem.getLabel(value);
×
590
                        if (label.length() > 1000) label = label.substring(0, 997) + "...";
×
591
                        try {
592
                            npCreator.addPubinfoStatement(vf.createIRI(value), NTEMPLATE.HAS_LABEL_FROM_API, vf.createLiteral(label));
×
593
                        } catch (IllegalArgumentException ex) {
×
594
                            logger.error("Could not create IRI from value: {}", value, ex);
×
595
                        }
×
596
                    }
597
                }
598
            }
×
599
        }
×
600

601
        private boolean hasEmptyElements() {
602
            for (IRI s : statementPartIds) {
×
603
                if (context.processIri((IRI) transform(getTemplate().getSubject(s))) == null) return true;
×
604
                if (context.processIri((IRI) transform(getTemplate().getPredicate(s))) == null) return true;
×
605
                if (context.processValue(transform(getTemplate().getObject(s))) == null) return true;
×
606
            }
×
607
            return false;
×
608
        }
609

610
        /**
611
         * Checks if this repetition group is empty, meaning it has no filled items.
612
         *
613
         * @return true if the repetition group is empty, false otherwise
614
         */
615
        public boolean isEmpty() {
616
            for (IRI s : statementPartIds) {
36✔
617
                Template t = getTemplate();
12✔
618
                IRI subj = t.getSubject(s);
12✔
619
                if (t.isPlaceholder(subj) && context.hasNarrowScope(subj) && context.processIri((IRI) transform(subj)) != null)
57!
620
                    return false;
6✔
621
                IRI pred = t.getPredicate(s);
12✔
622
                if (t.isPlaceholder(pred) && context.hasNarrowScope(pred) && context.processIri((IRI) transform(pred)) != null)
12!
623
                    return false;
×
624
                Value obj = t.getObject(s);
12✔
625
                if (obj instanceof IRI && t.isPlaceholder((IRI) obj) && context.hasNarrowScope((IRI) obj) && context.processValue(transform(obj)) != null)
69!
626
                    return false;
6✔
627
            }
3✔
628
            return true;
6✔
629
        }
630

631
        /**
632
         * Checks if this repetition group matches the provided list of statements.
633
         *
634
         * @param statements the list of statements to match against
635
         * @return true if the repetition group matches, false otherwise
636
         */
637
        public boolean matches(List<Statement> statements) {
638
            if (filled) return false;
9!
639
            List<Statement> st = new ArrayList<>(statements);
15✔
640
            for (StatementPartItem p : statementParts) {
33✔
641
                Statement matchedStatement = null;
6✔
642
                for (Statement s : st) {
30✔
643
                    if (
3✔
644
                            p.getPredicate().isUnifiableWith(s.getPredicate()) &&  // checking predicate first optimizes performance
18✔
645
                            p.getSubject().isUnifiableWith(s.getSubject()) &&
18!
646
                            p.getObject().isUnifiableWith(s.getObject())) {
15!
647
                        matchedStatement = s;
6✔
648
                        break;
3✔
649
                    }
650
                }
3✔
651
                if (matchedStatement == null) {
6✔
652
                    return false;
6✔
653
                } else {
654
                    st.remove(matchedStatement);
12✔
655
                }
656
            }
3✔
657
            return true;
6✔
658
        }
659

660
        /**
661
         * Fills this repetition group with the provided list of statements, unifying them with the statement parts.
662
         *
663
         * @param statements the list of statements to match against
664
         * @throws UnificationException if the statements cannot be unified with this repetition group
665
         */
666
        public void fill(List<Statement> statements) throws UnificationException {
667
            if (filled) throw new UnificationException("Already filled");
9!
668
            for (StatementPartItem p : statementParts) {
33✔
669
                Statement matchedStatement = null;
6✔
670
                for (Statement s : statements) {
30!
671
                    if (
3✔
672
                            p.getPredicate().isUnifiableWith(s.getPredicate()) &&  // checking predicate first optimizes performance
18✔
673
                            p.getSubject().isUnifiableWith(s.getSubject()) &&
18!
674
                            p.getObject().isUnifiableWith(s.getObject())) {
15!
675
                        p.getPredicate().unifyWith(s.getPredicate());
15✔
676
                        p.getSubject().unifyWith(s.getSubject());
15✔
677
                        p.getObject().unifyWith(s.getObject());
15✔
678
                        matchedStatement = s;
6✔
679
                        break;
3✔
680
                    }
681
                }
3✔
682
                if (matchedStatement == null) {
6!
683
                    throw new UnificationException("Unification seemed to work but then didn't");
×
684
                } else {
685
                    statements.remove(matchedStatement);
12✔
686
                }
687
                filled = true;
9✔
688
            }
3✔
689
        }
3✔
690

691
        /**
692
         * Marks the filling of this repetition group as finished, indicating that all values have been filled.
693
         */
694
        public void fillFinished() {
695
            for (ValueItem vi : items) {
33✔
696
                vi.fillFinished();
6✔
697
            }
3✔
698
        }
3✔
699

700
        /**
701
         * Finalizes the values of all ValueItems in this repetition group.
702
         */
703
        public void finalizeValues() {
704
            for (ValueItem vi : items) {
33✔
705
                vi.finalizeValues();
6✔
706
            }
3✔
707
        }
3✔
708

709
    }
710

711
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
6✔
712
    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✔
713

714
}
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