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

knowledgepixels / nanodash / 17380144000

01 Sep 2025 02:12PM UTC coverage: 12.03% (+0.05%) from 11.978%
17380144000

push

github

ashleycaselli
refactor: replace printStackTrace with logger.error for better error handling

330 of 3850 branches covered (8.57%)

Branch coverage included in aggregate %.

958 of 6857 relevant lines covered (13.97%)

0.62 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.Utils;
4
import com.knowledgepixels.nanodash.component.GuidedChoiceItem;
5
import com.knowledgepixels.nanodash.component.PublishForm.FillMode;
6
import org.eclipse.rdf4j.model.*;
7
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
8
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
9
import org.eclipse.rdf4j.model.vocabulary.RDFS;
10
import org.nanopub.Nanopub;
11
import org.nanopub.vocabulary.NPX;
12
import org.nanopub.vocabulary.NTEMPLATE;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

16
import java.util.ArrayList;
17
import java.util.List;
18
import java.util.Set;
19

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

25
    private static ValueFactory vf = SimpleValueFactory.getInstance();
2✔
26

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

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

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

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

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

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

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

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

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

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

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

186
    Value transform(Value v) {
187
        if (fillNp.getUri().equals(v)) {
6✔
188
            return vf.createIRI("local:nanopub");
4✔
189
//                        return Template.NANOPUB_PLACEHOLDER;
190
        } else if (fillNp.getAssertionUri().equals(v)) {
6✔
191
            return vf.createIRI("local:assertion");
4✔
192
//                        return Template.ASSERTION_PLACEHOLDER;
193
        } else if (v instanceof IRI iri && formMode) {
9!
194
            if (!Utils.getIntroducedIriIds(fillNp).contains(iri.stringValue()) || fillMode != FillMode.SUPERSEDE) {
7!
195
                if (v.stringValue().startsWith(fillNp.getUri().stringValue())) {
8!
196
                    return vf.createIRI("local:" + Utils.getUriPostfix(v.stringValue()));
×
197
                }
198
            }
199
        }
200
        return v;
2✔
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