• 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

32.17
src/main/java/com/knowledgepixels/nanodash/template/TemplateData.java
1
package com.knowledgepixels.nanodash.template;
2

3
import com.knowledgepixels.nanodash.QueryApiAccess;
4
import net.trustyuri.TrustyUriUtils;
5
import org.eclipse.rdf4j.model.IRI;
6
import org.eclipse.rdf4j.model.Statement;
7
import org.nanopub.Nanopub;
8
import org.nanopub.extra.services.ApiResponse;
9
import org.nanopub.extra.services.ApiResponseEntry;
10
import org.nanopub.vocabulary.NTEMPLATE;
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

14
import java.io.Serializable;
15
import java.util.*;
16

17
/**
18
 * Singleton class that manages templates data.
19
 */
20
public class TemplateData implements Serializable {
21

22
    private static final long serialVersionUID = 1L;
23
    private static final Logger logger = LoggerFactory.getLogger(TemplateData.class);
3✔
24

25
    private static TemplateData instance;
26

27
    /**
28
     * Refreshes the templates data by creating a new instance of TemplateData.
29
     */
30
    public static void refreshTemplates() {
31
        instance = new TemplateData();
4✔
32
    }
1✔
33

34
    /**
35
     * Ensures that the TemplateData instance is loaded.
36
     */
37
    public static synchronized void ensureLoaded() {
38
        if (instance == null) refreshTemplates();
3✔
39
    }
1✔
40

41
    /**
42
     * Gets the singleton instance of TemplateData.
43
     *
44
     * @return the TemplateData instance
45
     */
46
    public static TemplateData get() {
47
        ensureLoaded();
1✔
48
        return instance;
2✔
49
    }
50

51
    private List<ApiResponseEntry> assertionTemplates, provenanceTemplates, pubInfoTemplates;
52
    private Map<String, Template> templateMap;
53

54
    /**
55
     * Constructor to initialize the TemplateData instance.
56
     */
57
    public TemplateData() {
2✔
58
        assertionTemplates = new ArrayList<>();
5✔
59
        provenanceTemplates = new ArrayList<>();
5✔
60
        pubInfoTemplates = new ArrayList<>();
5✔
61
        templateMap = new HashMap<>();
5✔
62
        refreshTemplates(assertionTemplates, "get-assertion-templates");
5✔
63
        refreshTemplates(provenanceTemplates, "get-provenance-templates");
5✔
64
        refreshTemplates(pubInfoTemplates, "get-pubinfo-templates");
5✔
65
    }
1✔
66

67
    private void refreshTemplates(List<ApiResponseEntry> templates, String queryId) {
68
        ApiResponse templateEntries = QueryApiAccess.forcedGet(queryId);
3✔
69
        String previousId = null;
2✔
70
        logger.info("Loading templates...");
3✔
71
        for (ApiResponseEntry entry : templateEntries.getData()) {
11✔
72
            if ("true".equals(entry.get("unlisted"))) continue;
7✔
73
            if (!entry.get("np").equals(previousId)) {
6✔
74
                templates.add(entry);
4✔
75
            }
76
            previousId = entry.get("np");
4✔
77
        }
1✔
78
        Collections.sort(templates, templateComparator);
3✔
79
    }
1✔
80

81
    /**
82
     * Returns the list of assertion templates.
83
     *
84
     * @return a list of assertion templates
85
     */
86
    public List<ApiResponseEntry> getAssertionTemplates() {
87
        return assertionTemplates;
×
88
    }
89

90
    /**
91
     * Returns the list of provenance templates.
92
     *
93
     * @return a list of provenance templates
94
     */
95
    public List<ApiResponseEntry> getProvenanceTemplates() {
96
        return provenanceTemplates;
×
97
    }
98

99
    /**
100
     * Returns the list of publication information templates.
101
     *
102
     * @return a list of publication information templates
103
     */
104
    public List<ApiResponseEntry> getPubInfoTemplates() {
105
        return pubInfoTemplates;
×
106
    }
107

108
    /**
109
     * Returns a Template object for the given template ID.
110
     *
111
     * @param id the ID of the template
112
     * @return the Template object if found, or null if not found or invalid
113
     */
114
    public Template getTemplate(String id) {
115
        Template template = templateMap.get(id);
×
116
        if (template != null) return template;
×
117
        if (TrustyUriUtils.isPotentialTrustyUri(id)) {
×
118
            try {
119
                Template t = new Template(id);
×
120
                templateMap.put(id, t);
×
121
                return t;
×
122
            } catch (Exception ex) {
×
123
                logger.error("Exception: {}", ex.getMessage());
×
124
                return null;
×
125
            }
126
        }
127
        return null;
×
128
    }
129

130
    public Template getTemplate(Nanopub np) {
131
        IRI templateId = getTemplateId(np);
×
132
        if (templateId == null) return null;
×
133
        return getTemplate(templateId.stringValue());
×
134
    }
135

136
    /**
137
     * Returns a Template object for the provenance template of the given Nanopub.
138
     *
139
     * @param np the Nanopub from which to extract the provenance template
140
     * @return the Template object if found, or null if not found or invalid
141
     */
142
    public Template getProvenanceTemplate(Nanopub np) {
143
        IRI templateId = getProvenanceTemplateId(np);
×
144
        if (templateId == null) return null;
×
145
        return getTemplate(templateId.stringValue());
×
146
    }
147

148
    /**
149
     * Returns a set of Template objects for the publication information templates of the given Nanopub.
150
     *
151
     * @param np the Nanopub from which to extract the publication information templates
152
     * @return a set of Template objects
153
     */
154
    public Set<Template> getPubinfoTemplates(Nanopub np) {
155
        Set<Template> templates = new HashSet<>();
×
156
        for (IRI id : getPubinfoTemplateIds(np)) {
×
157
            templates.add(getTemplate(id.stringValue()));
×
158
        }
×
159
        return templates;
×
160
    }
161

162
    /**
163
     * Returns the template ID of the given Nanopub.
164
     *
165
     * @param nanopub the Nanopub from which to extract the template ID
166
     * @return the IRI of the template ID, or null if not found
167
     */
168
    public IRI getTemplateId(Nanopub nanopub) {
169
        for (Statement st : nanopub.getPubinfo()) {
×
170
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
171
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_TEMPLATE)) continue;
×
172
            if (!(st.getObject() instanceof IRI)) continue;
×
173
            return (IRI) st.getObject();
×
174
        }
175
        return null;
×
176
    }
177

178
    /**
179
     * Returns the provenance template ID of the given Nanopub.
180
     *
181
     * @param nanopub the Nanopub from which to extract the provenance template ID
182
     * @return the IRI of the provenance template ID, or null if not found
183
     */
184
    public IRI getProvenanceTemplateId(Nanopub nanopub) {
185
        for (Statement st : nanopub.getPubinfo()) {
×
186
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
187
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_PROVENANCE_TEMPLATE)) continue;
×
188
            if (!(st.getObject() instanceof IRI)) continue;
×
189
            return (IRI) st.getObject();
×
190
        }
191
        return null;
×
192
    }
193

194
    /**
195
     * Returns the set of publication information template IDs for the given Nanopub.
196
     *
197
     * @param nanopub the Nanopub from which to extract the publication information template IDs
198
     * @return a set of IRI objects representing the publication information template IDs
199
     */
200
    public Set<IRI> getPubinfoTemplateIds(Nanopub nanopub) {
201
        Set<IRI> iriSet = new HashSet<>();
×
202
        for (Statement st : nanopub.getPubinfo()) {
×
203
            if (!st.getSubject().equals(nanopub.getUri())) continue;
×
204
            if (!st.getPredicate().equals(NTEMPLATE.WAS_CREATED_FROM_PUBINFO_TEMPLATE)) continue;
×
205
            if (!(st.getObject() instanceof IRI)) continue;
×
206
            iriSet.add((IRI) st.getObject());
×
207
        }
×
208
        return iriSet;
×
209
    }
210

211

212
    private static final TemplateComparator templateComparator = new TemplateComparator();
5✔
213

214
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
215

216
        private static final long serialVersionUID = 1L;
217

218
        /**
219
         * Compares two Template objects based on their labels.
220
         *
221
         * @param o1 the first object to be compared.
222
         * @param o2 the second object to be compared.
223
         * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
224
         */
225
        @Override
226
        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
227
            return o1.get("label").compareTo(o2.get("label"));
8✔
228
        }
229

230
    }
231

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