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

knowledgepixels / nanodash / 20268226543

16 Dec 2025 12:39PM UTC coverage: 14.107% (-1.3%) from 15.358%
20268226543

push

github

ashleycaselli
refactor: replace VocabUtils with the ones defined in the nanopub-java library

526 of 4946 branches covered (10.63%)

Branch coverage included in aggregate %.

1452 of 9075 relevant lines covered (16.0%)

2.11 hits per line

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

33.54
src/main/java/com/knowledgepixels/nanodash/template/ValueFiller.java
1
package com.knowledgepixels.nanodash.template;
2

3
import com.knowledgepixels.nanodash.LocalUri;
4
import com.knowledgepixels.nanodash.Utils;
5
import com.knowledgepixels.nanodash.component.GuidedChoiceItem;
6
import com.knowledgepixels.nanodash.component.PublishForm.FillMode;
7
import org.eclipse.rdf4j.model.*;
8
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
9
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
10
import org.eclipse.rdf4j.model.vocabulary.RDFS;
11
import org.nanopub.Nanopub;
12
import org.nanopub.NanopubUtils;
13
import org.nanopub.vocabulary.NPX;
14
import org.nanopub.vocabulary.NTEMPLATE;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

18
import java.util.ArrayList;
19
import java.util.List;
20
import java.util.Set;
21

22
/**
23
 * ValueFiller is a utility class that processes a Nanopub and fills a TemplateContext.
24
 */
25
public class ValueFiller {
26

27
    private static ValueFactory vf = SimpleValueFactory.getInstance();
6✔
28

29
    private Nanopub fillNp;
30
    private FillMode fillMode;
31
    private List<Statement> unusedStatements = new ArrayList<>();
15✔
32
    private int initialSize;
33
    private boolean formMode;
34
    private static final Logger logger = LoggerFactory.getLogger(ValueFiller.class);
12✔
35

36
    /**
37
     * Constructor for ValueFiller.
38
     *
39
     * @param fillNp      the Nanopub to fill
40
     * @param contextType the type of context to fill
41
     * @param formMode    if true, the filler will adapt to form mode, filtering out certain statements
42
     */
43
    public ValueFiller(Nanopub fillNp, ContextType contextType, boolean formMode) {
44
        this(fillNp, contextType, formMode, null);
18✔
45
    }
3✔
46

47
    /**
48
     * Constructor for ValueFiller with specified fill mode.
49
     *
50
     * @param fillNp      the Nanopub to fill
51
     * @param contextType the type of context to fill
52
     * @param formMode    if true, the filler will adapt to form mode, filtering out certain statements
53
     * @param fillMode    the fill mode to use, can be null for default behavior
54
     */
55
    public ValueFiller(Nanopub fillNp, ContextType contextType, boolean formMode, FillMode fillMode) {
6✔
56
        this.fillNp = fillNp;
9✔
57
        this.formMode = formMode;
9✔
58
        this.fillMode = fillMode;
9✔
59
        Set<Statement> statements;
60
        if (contextType == ContextType.ASSERTION) {
9!
61
            statements = fillNp.getAssertion();
12✔
62
        } else if (contextType == ContextType.PROVENANCE) {
×
63
            statements = fillNp.getProvenance();
×
64
        } else {
65
            statements = fillNp.getPubinfo();
×
66
        }
67
        for (Statement st : statements) {
30✔
68
            Statement stT = transform(st);
12✔
69
            if (stT != null) unusedStatements.add(stT);
21!
70
        }
3✔
71
        unusedStatements.sort((st1, st2) -> {
12✔
72
            String st1s = st1.getSubject() + " " + st1.getPredicate() + " " + st1.getObject();
×
73
            String st2s = st2.getSubject() + " " + st2.getPredicate() + " " + st2.getObject();
×
74
            return st1s.compareTo(st2s);
×
75
        });
76
        initialSize = unusedStatements.size();
15✔
77
    }
3✔
78

79
    /**
80
     * Fills the TemplateContext with the unused statements.
81
     *
82
     * @param context the TemplateContext to fill
83
     */
84
    public void fill(TemplateContext context) {
85
        try {
86
            context.fill(unusedStatements);
×
87
        } catch (UnificationException ex) {
×
88
            logger.error("Could not fill template context", ex);
×
89
        }
×
90
    }
×
91

92
    /**
93
     * Checks if the Nanopub has any statements.
94
     *
95
     * @return true if there are statements, false otherwise
96
     */
97
    public boolean hasStatements() {
98
        return initialSize > 0;
18!
99
    }
100

101
    /**
102
     * Checks if there are any used statements in the Nanopub.
103
     *
104
     * @return true if there are used statements, false otherwise
105
     */
106
    public boolean hasUsedStatements() {
107
        return unusedStatements.size() < initialSize;
24!
108
    }
109

110
    /**
111
     * Checks if there are any unused statements in the Nanopub.
112
     *
113
     * @return true if there are unused statements, false otherwise
114
     */
115
    public boolean hasUnusedStatements() {
116
        return !unusedStatements.isEmpty();
21!
117
    }
118

119
    /**
120
     * Returns the list of unused statements in the Nanopub.
121
     *
122
     * @return the list of unused statements
123
     */
124
    public List<Statement> getUnusedStatements() {
125
        return unusedStatements;
9✔
126
    }
127

128
    /**
129
     * Removes a specific unused statement from the list.
130
     *
131
     * @param st the statement to remove
132
     */
133
    public void removeUnusedStatement(Statement st) {
134
        unusedStatements.remove(st);
×
135
    }
×
136

137
    /**
138
     * Removes unused statements based on the specified subject, predicate, and object.
139
     *
140
     * @param subj the subject to match, can be null
141
     * @param pred the predicate to match, can be null
142
     * @param obj  the object to match, can be null
143
     */
144
    public void removeUnusedStatements(IRI subj, IRI pred, Value obj) {
145
        for (Statement st : new ArrayList<>(unusedStatements)) {
×
146
            if (subj != null && !st.getSubject().equals(subj)) continue;
×
147
            if (pred != null && !st.getPredicate().equals(pred)) continue;
×
148
            if (obj != null && !st.getObject().equals(obj)) continue;
×
149
            unusedStatements.remove(st);
×
150
        }
×
151
    }
×
152

153
    Statement transform(Statement st) {
154
        if (formMode && st.getContext().equals(fillNp.getPubinfoUri())) {
30!
155
            IRI pred = st.getPredicate();
×
156
            // TODO We might want to filter some of these out afterwards in PublishForm, to be more precise:
157
            if (st.getSubject().equals(fillNp.getUri())) {
×
158
                if (pred.equals(DCTERMS.CREATED)) return null;
×
159
                if (pred.equals(NPX.SUPERSEDES)) return null;
×
160
                if (pred.equals(RDFS.LABEL)) return null;
×
161
                if (pred.equals(NPX.INTRODUCES)) return null;
×
162
                if (pred.equals(NPX.EMBEDS)) return null;
×
163
                if (pred.equals(NPX.WAS_CREATED_AT)) return null;
×
164
                if (pred.equals(NTEMPLATE.WAS_CREATED_FROM_TEMPLATE)) return null;
×
165
                if (pred.equals(NTEMPLATE.WAS_CREATED_FROM_PROVENANCE_TEMPLATE)) return null;
×
166
                if (pred.equals(NTEMPLATE.WAS_CREATED_FROM_PUBINFO_TEMPLATE)) return null;
×
167
            }
168
            if (pred.equals(NPX.HAS_ALGORITHM)) return null;
×
169
            if (pred.equals(NPX.HAS_PUBLIC_KEY)) return null;
×
170
            if (pred.equals(NPX.HAS_SIGNATURE)) return null;
×
171
            if (pred.equals(NPX.HAS_SIGNATURE_TARGET)) return null;
×
172
            if (pred.equals(NPX.SIGNED_BY)) return null;
×
173
            if (pred.equals(NTEMPLATE.HAS_LABEL_FROM_API)) {
×
174
                GuidedChoiceItem.setLabel(st.getSubject().stringValue(), st.getObject().stringValue());
×
175
                return null;
×
176
            }
177
            if (pred.equals(RDFS.LABEL)) {
×
178
                GuidedChoiceItem.setLabel(st.getSubject().stringValue(), st.getObject().stringValue());
×
179
            }
180
        }
181
        return vf.createStatement(
15✔
182
                (Resource) transform(st.getSubject()),
15✔
183
                (IRI) transform(st.getPredicate()),
15✔
184
                transform(st.getObject()),
12✔
185
                (Resource) transform(st.getContext()));
9✔
186
    }
187

188
    Value transform(Value v) {
189
        if (fillNp.getUri().equals(v)) {
18✔
190
            return LocalUri.of("nanopub");
9✔
191
        } else if (fillNp.getAssertionUri().equals(v)) {
18✔
192
            return LocalUri.of("assertion");
9✔
193
        } else if (v instanceof IRI iri && formMode) {
27!
194
            if (!NanopubUtils.getIntroducedIriIds(fillNp).contains(iri.stringValue()) || fillMode != FillMode.SUPERSEDE) {
21!
195
                if (v.stringValue().startsWith(fillNp.getUri().stringValue())) {
24!
196
                    return LocalUri.of(Utils.getUriPostfix(v.stringValue()));
×
197
                }
198
            }
199
        }
200
        return v;
6✔
201
    }
202

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