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

knowledgepixels / nanodash / 17375037963

01 Sep 2025 10:33AM UTC coverage: 11.982% (-0.04%) from 12.02%
17375037963

push

github

tkuhn
Merge branch 'master' of github.com:knowledgepixels/nanodash

330 of 3846 branches covered (8.58%)

Branch coverage included in aggregate %.

949 of 6828 relevant lines covered (13.9%)

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
import java.util.concurrent.ConcurrentHashMap;
17
import java.util.concurrent.ConcurrentMap;
18

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

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

27
    private static TemplateData instance;
28

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

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

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

53
    private List<ApiResponseEntry> assertionTemplates, provenanceTemplates, pubInfoTemplates;
54
    private ConcurrentMap<String, Template> templateMap;
55

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

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

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

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

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

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

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

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

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

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

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

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

213

214
    private static final TemplateComparator templateComparator = new TemplateComparator();
5✔
215

216
    private static class TemplateComparator implements Comparator<ApiResponseEntry>, Serializable {
217

218
        private static final long serialVersionUID = 1L;
219

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

232
    }
233

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