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

knowledgepixels / nanodash / 17320701451

29 Aug 2025 09:55AM UTC coverage: 12.02% (+0.01%) from 12.007%
17320701451

push

github

ashleycaselli
refactor: minor code style update

330 of 3842 branches covered (8.59%)

Branch coverage included in aggregate %.

950 of 6807 relevant lines covered (13.96%)

0.61 hits per line

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

33.12
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

14
import java.util.ArrayList;
15
import java.util.List;
16
import java.util.Set;
17

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

23
    private static ValueFactory vf = SimpleValueFactory.getInstance();
3✔
24

25
    private Nanopub fillNp;
26
    private FillMode fillMode;
27
    private List<Statement> unusedStatements = new ArrayList<>();
5✔
28
    private int initialSize;
29
    private boolean formMode;
30

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

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

74
    /**
75
     * Fills the TemplateContext with the unused statements.
76
     *
77
     * @param context the TemplateContext to fill
78
     */
79
    public void fill(TemplateContext context) {
80
        try {
81
            context.fill(unusedStatements);
×
82
        } catch (UnificationException ex) {
×
83
            ex.printStackTrace();
×
84
        }
×
85
    }
×
86

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

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

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

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

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

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

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

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

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