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

knowledgepixels / nanodash / 27622721129

16 Jun 2026 01:55PM UTC coverage: 26.963% (+6.3%) from 20.697%
27622721129

Pull #483

github

web-flow
Merge 73a4d0fe1 into 663f14f46
Pull Request #483: Space/resource About pages, ref-aware spaces, and magic query params

1542 of 6717 branches covered (22.96%)

Branch coverage included in aggregate %.

3407 of 11638 relevant lines covered (29.27%)

4.31 hits per line

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

52.45
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 true if the repeat index if the first one.
453
         *
454
         * @return true if the repeat index is 0, false otherwise
455
         */
456
        public boolean isFirst() {
457
            return getRepeatIndex() == 0;
21✔
458
        }
459

460
        /**
461
         * Returns true if the repeat index is the last one.
462
         *
463
         * @return true if the repeat index is the last one, false otherwise
464
         */
465
        public boolean isLast() {
466
            return getRepeatIndex() == repetitionGroups.size() - 1;
×
467
        }
468

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

499
        private void clearInputForModel(IModel<?> model) {
500
            if (model == null) return;
×
501
            for (Component c : context.getComponents()) {
×
502
                if (c instanceof FormComponent && c.getDefaultModel() == model) {
×
503
                    ((FormComponent<?>) c).clearInput();
×
504
                }
505
            }
×
506
        }
×
507

508
        private String getRepeatSuffix() {
509
            return getRepeatSuffix(getRepeatIndex());
15✔
510
        }
511

512
        private String getRepeatSuffix(int i) {
513
            if (i == 0) return "";
6!
514
            // TODO: Check that this double-underscore pattern isn't used otherwise:
515
            return "__" + i;
9✔
516
        }
517

518
        /**
519
         * Returns the template context associated.
520
         *
521
         * @return the TemplateContext
522
         */
523
        public TemplateContext getContext() {
524
            return context;
12✔
525
        }
526

527
        /**
528
         * Checks if this repetition group is optional.
529
         *
530
         * @return true if the repetition group is optional, false otherwise
531
         */
532
        public boolean isOptional() {
533
            if (!getTemplate().isOptionalStatement(statementId)) return false;
30✔
534
            if (repetitionGroups.size() == 0) return true;
21✔
535
            if (repetitionGroups.size() == 1 && repetitionGroups.get(0) == this) return true;
39!
536
            return false;
6✔
537
        }
538

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

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

592
        private boolean hasEmptyElements() {
593
            for (IRI s : statementPartIds) {
×
594
                if (context.processIri((IRI) transform(getTemplate().getSubject(s))) == null) return true;
×
595
                if (context.processIri((IRI) transform(getTemplate().getPredicate(s))) == null) return true;
×
596
                if (context.processValue(transform(getTemplate().getObject(s))) == null) return true;
×
597
            }
×
598
            return false;
×
599
        }
600

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

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

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

682
        /**
683
         * Marks the filling of this repetition group as finished, indicating that all values have been filled.
684
         */
685
        public void fillFinished() {
686
            for (ValueItem vi : items) {
33✔
687
                vi.fillFinished();
6✔
688
            }
3✔
689
        }
3✔
690

691
        /**
692
         * Finalizes the values of all ValueItems in this repetition group.
693
         */
694
        public void finalizeValues() {
695
            for (ValueItem vi : items) {
33✔
696
                vi.finalizeValues();
6✔
697
            }
3✔
698
        }
3✔
699

700
    }
701

702
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
6✔
703
    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✔
704

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