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

knowledgepixels / nanodash / 17302137193

28 Aug 2025 04:35PM UTC coverage: 11.965% (-0.4%) from 12.355%
17302137193

Pull #244

github

web-flow
Merge 4e969b0ee into 3323a35f1
Pull Request #244: Use vocabularies with latest version of `nanopub-java`

331 of 3840 branches covered (8.62%)

Branch coverage included in aggregate %.

943 of 6808 relevant lines covered (13.85%)

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.*;
15

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

21
    private static ValueFactory vf = SimpleValueFactory.getInstance();
3✔
22

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

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

40
    /**
41
     * Constructor for ValueFiller with specified fill mode.
42
     *
43
     * @param fillNp      the Nanopub to fill
44
     * @param contextType the type of context to fill
45
     * @param formMode    if true, the filler will adapt to form mode, filtering out certain statements
46
     * @param fillMode    the fill mode to use, can be null for default behavior
47
     */
48
    public ValueFiller(Nanopub fillNp, ContextType contextType, boolean formMode, FillMode fillMode) {
2✔
49
        this.fillNp = fillNp;
3✔
50
        this.formMode = formMode;
3✔
51
        this.fillMode = fillMode;
3✔
52
        Set<Statement> statements;
53
        if (contextType == ContextType.ASSERTION) {
3!
54
            statements = fillNp.getAssertion();
4✔
55
        } else if (contextType == ContextType.PROVENANCE) {
×
56
            statements = fillNp.getProvenance();
×
57
        } else {
58
            statements = fillNp.getPubinfo();
×
59
        }
60
        for (Statement st : statements) {
10✔
61
            Statement stT = transform(st);
4✔
62
            if (stT != null) unusedStatements.add(stT);
7!
63
        }
1✔
64
        Collections.sort(unusedStatements, new Comparator<Statement>() {
10✔
65
            @Override
66
            public int compare(Statement st1, Statement st2) {
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
        });
72
        initialSize = unusedStatements.size();
5✔
73
    }
1✔
74

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

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

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

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

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

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

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

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

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

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