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

Nanopublication / nanopub-java / 25657329499

11 May 2026 07:49AM UTC coverage: 53.252% (+1.1%) from 52.184%
25657329499

push

github

web-flow
Merge pull request #78 from Nanopublication/feature/setting-driven-query-instances

feat(services): discover query API instances from nanopub setting

1206 of 3196 branches covered (37.73%)

Branch coverage included in aggregate %.

5573 of 9534 relevant lines covered (58.45%)

8.19 hits per line

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

89.74
src/main/java/org/nanopub/extra/setting/NanopubSetting.java
1
package org.nanopub.extra.setting;
2

3
import org.eclipse.rdf4j.common.exception.RDF4JException;
4
import org.eclipse.rdf4j.model.IRI;
5
import org.eclipse.rdf4j.model.Statement;
6
import org.eclipse.rdf4j.model.ValueFactory;
7
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
8
import org.eclipse.rdf4j.model.vocabulary.RDF;
9
import org.eclipse.rdf4j.model.vocabulary.RDFS;
10
import org.eclipse.rdf4j.rio.RDFFormat;
11
import org.nanopub.MalformedNanopubException;
12
import org.nanopub.Nanopub;
13
import org.nanopub.NanopubImpl;
14
import org.nanopub.vocabulary.NPX;
15

16
import java.io.File;
17
import java.io.IOException;
18
import java.io.InputStream;
19
import java.io.Serializable;
20
import java.util.HashSet;
21
import java.util.Objects;
22
import java.util.Set;
23

24
/**
25
 * This class represents a nanopub setting, which includes various configurations.
26
 */
27
public class NanopubSetting implements Serializable {
28

29
    private static ValueFactory vf = SimpleValueFactory.getInstance();
9✔
30

31
    /**
32
     * System property naming a TriG file with a custom setting nanopub. Overrides the
33
     * bundled default setting when set (env var {@code NANOPUB_SETTING_FILE} also accepted).
34
     */
35
    public static final String SETTING_FILE_PROPERTY = "nanopub.setting.file";
36

37
    /**
38
     * Environment variable equivalent of {@link #SETTING_FILE_PROPERTY}.
39
     */
40
    public static final String SETTING_FILE_ENV = "NANOPUB_SETTING_FILE";
41

42
    private static volatile NanopubSetting defaultSetting;
43

44
    /**
45
     * Retrieves the default nanopub setting, honoring the {@code nanopub.setting.file}
46
     * system property and the {@code NANOPUB_SETTING_FILE} environment variable as
47
     * overrides. When neither is set, falls back to {@link #getLocalSetting()}.
48
     *
49
     * @return the default NanopubSetting object
50
     * @throws org.eclipse.rdf4j.common.exception.RDF4JException if there is an error with RDF4J operations.
51
     * @throws org.nanopub.MalformedNanopubException             if the nanopub is malformed.
52
     * @throws java.io.IOException                               if there is an error reading the input stream.
53
     */
54
    public static NanopubSetting getDefaultSetting() throws RDF4JException, MalformedNanopubException, IOException {
55
        if (defaultSetting != null) return defaultSetting;
12✔
56
        synchronized (NanopubSetting.class) {
12✔
57
            if (defaultSetting != null) return defaultSetting;
6!
58
            String path = System.getProperty(SETTING_FILE_PROPERTY);
9✔
59
            if (path == null || path.isEmpty()) path = System.getenv(SETTING_FILE_ENV);
15!
60
            if (path != null && !path.isEmpty()) {
6!
61
                defaultSetting = new NanopubSetting(new NanopubImpl(new File(path)));
×
62
            } else {
63
                defaultSetting = getLocalSetting();
6✔
64
            }
65
            return defaultSetting;
12✔
66
        }
67
    }
68

69
    /**
70
     * Retrieves the default local nanopub setting.
71
     *
72
     * @return the NanopubSetting object for the default setting.
73
     * @throws org.eclipse.rdf4j.common.exception.RDF4JException if there is an error with RDF4J operations.
74
     * @throws org.nanopub.MalformedNanopubException             if the nanopub is malformed.
75
     * @throws java.io.IOException                               if there is an error reading the input stream.
76
     */
77
    public static NanopubSetting getLocalSetting() throws RDF4JException, MalformedNanopubException, IOException {
78
        return getLocalSetting(null);
9✔
79
    }
80

81
    /**
82
     * Retrieves the local nanopub setting by name.
83
     *
84
     * @param name the name of the setting, or "default" if null is provided.
85
     * @return the NanopubSetting object corresponding to the specified name.
86
     * @throws org.eclipse.rdf4j.common.exception.RDF4JException if there is an error with RDF4J operations.
87
     * @throws org.nanopub.MalformedNanopubException             if the nanopub is malformed.
88
     * @throws java.io.IOException                               if there is an error reading the input stream.
89
     */
90
    public static NanopubSetting getLocalSetting(String name) throws RDF4JException, MalformedNanopubException, IOException {
91
        if (name == null) name = "default";
12✔
92
        NanopubSetting setting = null;
6✔
93
        try (InputStream in = NanopubSetting.class.getResourceAsStream("/settings/" + name + ".trig")) {
15✔
94
            setting = new NanopubSetting(new NanopubImpl(in, RDFFormat.TRIG));
27✔
95
        }
96
        return setting;
6✔
97
    }
98

99
    private Nanopub nanopub;
100
    private IRI settingIri;
101
    private String name;
102
    private IRI agentIntroCollection;
103
    private IRI serviceIntroCollection;
104
    private Set<IRI> bootstrapServices = new HashSet<>();
15✔
105
    private IRI trustRangeAlgorithm;
106
    private IRI updateStrategy;
107

108
    /**
109
     * Constructs a NanopubSetting from a Nanopub object.
110
     *
111
     * @param nanopub the Nanopub object to create the setting from.
112
     */
113
    public NanopubSetting(Nanopub nanopub) {
6✔
114
        this.nanopub = nanopub;
9✔
115
        for (Statement st : nanopub.getAssertion()) {
33✔
116
            if (st.getPredicate().equals(RDF.TYPE) && st.getObject() instanceof IRI) {
27✔
117
                settingIri = (IRI) st.getSubject();
15✔
118
                break;
3✔
119
            }
120
        }
3✔
121
        if (settingIri == null) {
9✔
122
            throw new RuntimeException("No setting IRI found: " + nanopub.getUri());
24✔
123
        }
124
        for (Statement st : nanopub.getAssertion()) {
33✔
125
            if (!st.getSubject().equals(settingIri)) continue;
21✔
126
            IRI pred = st.getPredicate();
9✔
127
            if (pred.equals(RDFS.LABEL)) {
12✔
128
                name = st.getObject().stringValue();
15✔
129
                continue;
3✔
130
            }
131
            if (!(st.getObject() instanceof IRI obj)) continue;
27✔
132
            if (pred.equals(NPX.HAS_AGENTS)) {
12✔
133
                if (agentIntroCollection != null)
9✔
134
                    throw new RuntimeException("Two agent intro collections found: " + nanopub.getUri());
24✔
135
                agentIntroCollection = obj;
12✔
136
            } else if (pred.equals(NPX.HAS_SERVICES)) {
12✔
137
                if (serviceIntroCollection != null)
9✔
138
                    throw new RuntimeException("Two service intro collections found: " + nanopub.getUri());
24✔
139
                serviceIntroCollection = obj;
12✔
140
            } else if (pred.equals(NPX.HAS_BOOTSTRAP_SERVICE)) {
12✔
141
                bootstrapServices.add(obj);
18✔
142
            } else if (pred.equals(NPX.HAS_TRUST_RANGE_ALGORITHM)) {
12✔
143
                if (trustRangeAlgorithm != null)
9✔
144
                    throw new RuntimeException("Two trust range algorithms found: " + nanopub.getUri());
24✔
145
                trustRangeAlgorithm = obj;
12✔
146
            } else if (pred.equals(NPX.HAS_UPDATE_STRATEGY)) {
12✔
147
                if (updateStrategy != null)
9✔
148
                    throw new RuntimeException("Two update strategies found: " + nanopub.getUri());
24✔
149
                updateStrategy = obj;
9✔
150
            }
151
        }
3✔
152
    }
3✔
153

154
    /**
155
     * Returns the Nanopub object associated with this setting.
156
     *
157
     * @return the Nanopub object representing the setting.
158
     */
159
    public Nanopub getNanopub() {
160
        return nanopub;
9✔
161
    }
162

163
    /**
164
     * Returns the name of the setting.
165
     *
166
     * @return the name of the setting, or null if not set.
167
     */
168
    public String getName() {
169
        return name;
9✔
170
    }
171

172
    /**
173
     * Returns the IRI of the agent intro collection.
174
     *
175
     * @return the IRI of the agent intro collection, or null if not set.
176
     */
177
    public IRI getAgentIntroCollection() {
178
        return agentIntroCollection;
9✔
179
    }
180

181
    /**
182
     * Returns the IRI of the service intro collection.
183
     *
184
     * @return the IRI of the service intro collection, or null if not set.
185
     */
186
    public IRI getServiceIntroCollection() {
187
        return serviceIntroCollection;
9✔
188
    }
189

190
    /**
191
     * Returns a set of IRIs representing the bootstrap services.
192
     *
193
     * @return a set of IRIs for bootstrap services, which may be empty if none are defined.
194
     */
195
    public Set<IRI> getBootstrapServices() {
196
        return bootstrapServices;
9✔
197
    }
198

199
    /**
200
     * Returns the IRI of the trust range algorithm.
201
     *
202
     * @return the IRI of the trust range algorithm, or null if not set.
203
     */
204
    public IRI getTrustRangeAlgorithm() {
205
        return trustRangeAlgorithm;
9✔
206
    }
207

208
    /**
209
     * Returns the IRI of the update strategy.
210
     *
211
     * @return the IRI of the update strategy, or null if not set.
212
     */
213
    public IRI getUpdateStrategy() {
214
        return updateStrategy;
9✔
215
    }
216

217
    /**
218
     * {@inheritDoc}
219
     */
220
    @Override
221
    public boolean equals(Object o) {
222
        if (o == null || getClass() != o.getClass()) {
21!
223
            return false;
×
224
        }
225
        NanopubSetting that = (NanopubSetting) o;
9✔
226
        return Objects.equals(nanopub, that.nanopub);
18✔
227
    }
228

229
    /**
230
     * {@inheritDoc}
231
     */
232
    @Override
233
    public int hashCode() {
234
        return Objects.hash(nanopub);
×
235
    }
236

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