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

knowledgepixels / nanopub-query / 26289965018

22 May 2026 01:15PM UTC coverage: 58.782% (+0.7%) from 58.112%
26289965018

push

github

web-flow
Merge pull request #108 from knowledgepixels/fix/spaces-inline-role-extraction

fix(SpacesExtractor): emit inline non-hasAdmin role RIs from gen:Space

494 of 928 branches covered (53.23%)

Branch coverage included in aggregate %.

1360 of 2226 relevant lines covered (61.1%)

9.4 hits per line

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

89.96
src/main/java/com/knowledgepixels/query/SpacesExtractor.java
1
package com.knowledgepixels.query;
2

3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Collections;
6
import java.util.Date;
7
import java.util.LinkedHashMap;
8
import java.util.LinkedHashSet;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Set;
12

13
import org.eclipse.rdf4j.model.IRI;
14
import org.eclipse.rdf4j.model.Literal;
15
import org.eclipse.rdf4j.model.Resource;
16
import org.eclipse.rdf4j.model.Statement;
17
import org.eclipse.rdf4j.model.ValueFactory;
18
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
19
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
20
import org.eclipse.rdf4j.model.vocabulary.RDF;
21
import org.nanopub.Nanopub;
22
import org.nanopub.NanopubUtils;
23
import org.nanopub.vocabulary.NPA;
24
import org.nanopub.vocabulary.NPX;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

28
import com.knowledgepixels.query.vocabulary.BackcompatRolePredicates;
29
import com.knowledgepixels.query.vocabulary.GEN;
30
import com.knowledgepixels.query.vocabulary.SpacesVocab;
31

32
import net.trustyuri.TrustyUriUtils;
33

34
/**
35
 * Pure-logic extractor from a loaded {@link Nanopub} to the add-only summary
36
 * triples destined for {@code npa:spacesGraph}. Implements the per-type schema
37
 * from {@code doc/design-space-repositories.md}.
38
 *
39
 * <p>Dispatch is by nanopub type — {@link NanopubUtils#getTypes(Nanopub)} returns
40
 * both {@code rdf:type} / {@code npx:hasNanopubType} declarations and, for
41
 * single-predicate-assertion nanopubs, the predicate itself. That means the
42
 * four predefined types ({@link GEN#SPACE}, {@link GEN#HAS_ROLE},
43
 * {@link GEN#SPACE_MEMBER_ROLE}, {@link GEN#ROLE_INSTANTIATION}) and all 14
44
 * {@link BackcompatRolePredicates backwards-compat predicates} can be detected
45
 * with a single type-set lookup.
46
 *
47
 * <p>Output: a list of RDF4J {@link Statement}s, all in the
48
 * {@link SpacesVocab#SPACES_GRAPH} named graph, that the caller writes into the
49
 * {@code spaces} repo. Deterministic and idempotent — the same nanopub always
50
 * produces the same statement set.
51
 */
52
public final class SpacesExtractor {
53

54
    private static final Logger log = LoggerFactory.getLogger(SpacesExtractor.class);
9✔
55

56
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
6✔
57

58
    private static final IRI GRAPH = SpacesVocab.SPACES_GRAPH;
6✔
59

60
    /**
61
     * The set of nanopub-level type/predicate IRIs that make a nanopub "space-relevant"
62
     * — i.e., that dispatch to one of the per-shape extractors in {@link #extract}.
63
     * Shared with {@link NanopubLoader} so the spaces-load gate and the invalidation
64
     * propagation paths agree on a single definition of "space-relevant" without
65
     * needing to re-run the extractor.
66
     *
67
     * <p>Membership is checked against {@link NanopubUtils#getTypes(Nanopub)}, which
68
     * includes both {@code rdf:type} / {@code npx:hasNanopubType} declarations and,
69
     * for single-predicate-assertion nanopubs, the predicate itself — so predicate
70
     * markers like {@link GEN#HAS_ROLE} and {@link GEN#IS_MAINTAINED_BY} can appear
71
     * as types here.
72
     */
73
    public static final Set<IRI> TRIGGER_TYPES;
74
    static {
75
        Set<IRI> s = new LinkedHashSet<>();
12✔
76
        s.add(GEN.SPACE);
12✔
77
        s.add(GEN.HAS_ROLE);
12✔
78
        s.add(GEN.SPACE_MEMBER_ROLE);
12✔
79
        s.add(GEN.ROLE_INSTANTIATION);
12✔
80
        s.add(GEN.IS_SUB_SPACE_OF);
12✔
81
        s.add(GEN.MAINTAINED_RESOURCE);
12✔
82
        s.add(GEN.IS_MAINTAINED_BY);
12✔
83
        s.addAll(BackcompatRolePredicates.ALL);
12✔
84
        TRIGGER_TYPES = Collections.unmodifiableSet(s);
9✔
85
    }
3✔
86

87
    private SpacesExtractor() {
88
    }
89

90
    /**
91
     * Returns {@code true} iff at least one of {@code types} is in
92
     * {@link #TRIGGER_TYPES} — i.e., the nanopub carries a type that dispatches
93
     * to one of the extractor branches. Callers should typically pass
94
     * {@code NanopubUtils.getTypes(np)} so that single-predicate-assertion
95
     * auto-typing is included.
96
     */
97
    public static boolean isSpaceRelevant(Set<IRI> types) {
98
        for (IRI t : types) {
30✔
99
            if (TRIGGER_TYPES.contains(t)) return true;
18✔
100
        }
3✔
101
        return false;
6✔
102
    }
103

104
    /**
105
     * Bundles the information a single extraction needs beyond the nanopub itself.
106
     *
107
     * @param artifactCode trusty-URI artifact code of {@code np} (used for minting
108
     *                     {@code npari:}/{@code npara:}/{@code npard:}/{@code npadef:}
109
     *                     subject IRIs).
110
     * @param signedBy     signer agent IRI from pubinfo, or {@code null} if absent.
111
     * @param pubkeyHash   hash of the signing public key, or {@code null} if absent.
112
     * @param createdAt    creation timestamp, or {@code null} if the nanopub lacks one.
113
     */
114
    public record Context(String artifactCode, IRI signedBy, String pubkeyHash, Date createdAt) {
45✔
115
    }
116

117
    /**
118
     * Runs the extractor on a loaded nanopub. Returns an empty list if the nanopub is
119
     * not space-relevant.
120
     *
121
     * @param np  the nanopub to inspect
122
     * @param ctx the extraction context
123
     * @return statements to write into {@code npa:spacesGraph}
124
     */
125
    public static List<Statement> extract(Nanopub np, Context ctx) {
126
        Set<IRI> types = NanopubUtils.getTypes(np);
9✔
127
        if (!isSpaceRelevant(types)) return Collections.emptyList();
15✔
128

129
        List<Statement> out = new ArrayList<>();
12✔
130

131
        boolean isSpace = types.contains(GEN.SPACE);
12✔
132
        boolean isHasRole = types.contains(GEN.HAS_ROLE);
12✔
133
        boolean isSpaceMemberRole = types.contains(GEN.SPACE_MEMBER_ROLE);
12✔
134
        boolean isRoleInstantiation = types.contains(GEN.ROLE_INSTANTIATION)
18!
135
                || anyMatch(types, BackcompatRolePredicates.ALL);
18✔
136
        boolean isSubSpaceOf = types.contains(GEN.IS_SUB_SPACE_OF);
12✔
137
        // Maintained-resource nanopubs use either the resource-class marker
138
        // (gen:MaintainedResource — what Nanodash currently writes) or the
139
        // predicate marker (gen:isMaintainedBy — single-predicate-assertion
140
        // auto-typing or explicit npx:hasNanopubType). Both shapes carry the
141
        // same <r> gen:isMaintainedBy <s> triple in the assertion.
142
        boolean isMaintainedResource = types.contains(GEN.MAINTAINED_RESOURCE)
18✔
143
                || types.contains(GEN.IS_MAINTAINED_BY);
18✔
144

145
        if (isSpace) extractSpace(np, ctx, out);
18✔
146
        if (isHasRole) extractHasRole(np, ctx, out);
18✔
147
        if (isSpaceMemberRole) extractSpaceMemberRole(np, ctx, out);
18✔
148
        if (isRoleInstantiation) extractRoleInstantiation(np, ctx, out);
18✔
149
        if (isSubSpaceOf) extractSubSpaceOf(np, ctx, out);
18✔
150
        if (isMaintainedResource) extractIsMaintainedBy(np, ctx, out);
18✔
151

152
        return out;
6✔
153
    }
154

155
    // ---------------- gen:Space ----------------
156

157
    private static void extractSpace(Nanopub np, Context ctx, List<Statement> out) {
158
        // A single gen:Space nanopub may declare multiple Space IRIs, each via its own
159
        // gen:hasRootDefinition triple. We emit one SpaceRef + SpaceDefinition per
160
        // Space IRI. A nanopub missing any hasRootDefinition is accepted as its own
161
        // root for every Space IRI it declares (transition backcompat).
162
        Set<IRI> handled = new LinkedHashSet<>();
12✔
163
        List<IRI> adminAgents = collectAdminAgents(np);
9✔
164

165
        // Rooted case: gen:hasRootDefinition explicitly declared.
166
        for (Statement st : np.getAssertion()) {
33✔
167
            if (!st.getPredicate().equals(GEN.HAS_ROOT_DEFINITION)) continue;
18✔
168
            if (!(st.getSubject() instanceof IRI spaceIri)) continue;
27!
169
            if (!(st.getObject() instanceof IRI rootUri)) continue;
27!
170
            String rootNanopubId = TrustyUriUtils.getArtifactCode(rootUri.stringValue());
12✔
171
            if (rootNanopubId == null || rootNanopubId.isEmpty()) {
15!
172
                log.warn("Ignoring space {}: gen:hasRootDefinition target is not a trusty URI: {}",
×
173
                        spaceIri, rootUri);
174
                continue;
×
175
            }
176
            if (!handled.add(spaceIri)) continue;
12!
177
            emitSpaceEntry(np, ctx, spaceIri, rootUri, rootNanopubId, adminAgents, out);
24✔
178
        }
3✔
179

180
        // Rootless transition case: any Space IRI in the assertion that didn't get a
181
        // hasRootDefinition triple is treated as if it were its own root. Detect by
182
        // looking for triples that reference a Space IRI we haven't handled yet —
183
        // typically via gen:hasAdmin subjects or the rdf:type gen:Space triple on a
184
        // blank-node assertion subject. The common template publishes the Space IRI
185
        // as the subject of at least one triple in the assertion, so we scan for that.
186
        for (Statement st : np.getAssertion()) {
33✔
187
            if (!(st.getSubject() instanceof IRI spaceIri)) continue;
27!
188
            if (handled.contains(spaceIri)) continue;
15✔
189
            // Skip IRIs that clearly aren't Space IRIs (role IRIs embedded in this nanopub).
190
            if (spaceIri.stringValue().startsWith(np.getUri().stringValue())) continue;
21!
191
            // Require at least one structural signal that this is a Space IRI:
192
            // an rdf:type gen:Space, or a gen:hasAdmin triple with this as subject.
193
            if (!looksLikeSpaceIri(np, spaceIri)) continue;
15✔
194
            handled.add(spaceIri);
12✔
195
            String rootNanopubId = TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
15✔
196
            if (rootNanopubId == null || rootNanopubId.isEmpty()) continue;
15!
197
            emitSpaceEntry(np, ctx, spaceIri, np.getUri(), rootNanopubId, adminAgents, out);
27✔
198
        }
3✔
199
    }
3✔
200

201
    private static void emitSpaceEntry(Nanopub np, Context ctx, IRI spaceIri, IRI rootUri,
202
                                       String rootNanopubId, List<IRI> adminAgents,
203
                                       List<Statement> out) {
204
        String spaceRef = rootNanopubId + "_" + Utils.createHash(spaceIri);
15✔
205
        IRI refIri = SpacesVocab.forSpaceRef(spaceRef);
9✔
206
        IRI defIri = SpacesVocab.forSpaceDefinition(ctx.artifactCode());
12✔
207

208
        // Aggregate entry: contributor-independent, reinforced on every contribution.
209
        out.add(vf.createStatement(refIri, RDF.TYPE, SpacesVocab.SPACE_REF, GRAPH));
27✔
210
        out.add(vf.createStatement(refIri, SpacesVocab.SPACE_IRI, spaceIri, GRAPH));
27✔
211
        out.add(vf.createStatement(refIri, SpacesVocab.ROOT_NANOPUB, rootUri, GRAPH));
27✔
212

213
        // Identity-derived path-prefix enumeration powering the URL-prefix sub-space
214
        // fallback in the materializer. Same triples on every contributor (RDF set
215
        // semantics dedups them).
216
        for (IRI prefix : enumerateIdPrefixes(spaceIri)) {
33✔
217
            out.add(vf.createStatement(refIri, SpacesVocab.HAS_ID_PREFIX, prefix, GRAPH));
27✔
218
        }
3✔
219

220
        // Embedded gen:isSubSpaceOf triples in this gen:Space nanopub: emit one
221
        // SubSpaceDeclaration per (spaceIri, parentIri) pair. Same shape as the
222
        // standalone path; downstream rules don't distinguish them.
223
        emitSubSpaceDeclarations(np, ctx, spaceIri, out);
15✔
224

225
        // Embedded gen:isMaintainedBy triples in this gen:Space nanopub: emit one
226
        // MaintainedResourceDeclaration per (resourceIri, spaceIri) pair where the
227
        // object equals the Space being defined. Same shape as the standalone path.
228
        emitMaintainedResourceDeclarations(np, ctx, spaceIri, out);
15✔
229

230
        // Per-contributor entry: signer, pubkey, created-at, link back to nanopub.
231
        out.add(vf.createStatement(defIri, RDF.TYPE, SpacesVocab.SPACE_DEFINITION, GRAPH));
27✔
232
        out.add(vf.createStatement(defIri, SpacesVocab.FOR_SPACE_REF, refIri, GRAPH));
27✔
233
        out.add(vf.createStatement(defIri, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
234
        addProvenance(defIri, ctx, out);
12✔
235

236
        // Trust seed: this is the root nanopub iff rootUri equals the nanopub's own URI.
237
        boolean isOwnRoot = rootUri.equals(np.getUri());
15✔
238
        if (isOwnRoot) {
6✔
239
            for (IRI adminAgent : adminAgents) {
30✔
240
                out.add(vf.createStatement(defIri, SpacesVocab.HAS_ROOT_ADMIN, adminAgent, GRAPH));
27✔
241
            }
3✔
242
        }
243

244
        // gen:RoleInstantiation entry for the admins asserted in this gen:Space nanopub,
245
        // so admins show up in the same SPARQL pattern as ordinary admin instantiations.
246
        if (!adminAgents.isEmpty()) {
9!
247
            IRI riIri = SpacesVocab.forRoleInstantiation(ctx.artifactCode());
12✔
248
            out.add(vf.createStatement(riIri, RDF.TYPE, GEN.ROLE_INSTANTIATION, GRAPH));
27✔
249
            out.add(vf.createStatement(riIri, SpacesVocab.FOR_SPACE, spaceIri, GRAPH));
27✔
250
            out.add(vf.createStatement(riIri, SpacesVocab.INVERSE_PROPERTY, GEN.HAS_ADMIN, GRAPH));
27✔
251
            for (IRI adminAgent : adminAgents) {
30✔
252
                out.add(vf.createStatement(riIri, SpacesVocab.FOR_AGENT, adminAgent, GRAPH));
27✔
253
            }
3✔
254
            out.add(vf.createStatement(riIri, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
255
            addProvenance(riIri, ctx, out);
12✔
256
        }
257

258
        // Inline non-hasAdmin role triples: a gen:Space nanopub may also assert
259
        // <space> <pred> <agent> (INVERSE) or <agent> <pred> <space> (REGULAR)
260
        // for any of the back-compat role predicates (has-event-facilitator,
261
        // participatedAsParticipantIn, …). Without an extraction path here those
262
        // are silently dropped because gen:Space nanopubs are not auto-typed
263
        // with back-compat predicates (only single-triple-assertion nanopubs are).
264
        // Emit one RoleInstantiation per distinct predicate found, grouping
265
        // multi-agent like the admin case. The subject is disambiguated by a
266
        // hash of the predicate IRI so multiple predicates in one nanopub don't
267
        // collide on the same npari:<artifactCode> subject as the admin RI.
268
        emitInlineRoleInstantiations(np, ctx, spaceIri, out);
15✔
269
    }
3✔
270

271
    /**
272
     * Scans the assertion of a {@code gen:Space} nanopub for inline role triples
273
     * (excluding {@code gen:hasAdmin}, which is handled separately as the trust
274
     * seed), grouping by predicate and emitting one {@link GEN#ROLE_INSTANTIATION}
275
     * per (predicate, direction) pair with multi-valued {@code npa:forAgent}.
276
     */
277
    private static void emitInlineRoleInstantiations(Nanopub np, Context ctx, IRI spaceIri,
278
                                                     List<Statement> out) {
279
        Map<IRI, BackcompatRolePredicates.Direction> directionByPred = new LinkedHashMap<>();
12✔
280
        Map<IRI, Set<IRI>> agentsByPred = new LinkedHashMap<>();
12✔
281
        for (Statement st : np.getAssertion()) {
33✔
282
            IRI predicate = st.getPredicate();
9✔
283
            if (GEN.HAS_ADMIN.equals(predicate)) continue; // already emitted above
15✔
284
            BackcompatRolePredicates.Direction direction = BackcompatRolePredicates.DIRECTIONS.get(predicate);
15✔
285
            if (direction == null) continue;
9✔
286
            if (!(st.getSubject() instanceof IRI subjIri)) continue;
27!
287
            if (!(st.getObject() instanceof IRI objIri)) continue;
27!
288
            IRI agent;
289
            if (direction == BackcompatRolePredicates.Direction.INVERSE) {
9✔
290
                if (!spaceIri.equals(subjIri)) continue;
12!
291
                agent = objIri;
9✔
292
            } else {
293
                if (!spaceIri.equals(objIri)) continue;
12!
294
                agent = subjIri;
6✔
295
            }
296
            directionByPred.put(predicate, direction);
15✔
297
            agentsByPred.computeIfAbsent(predicate, k -> new LinkedHashSet<>()).add(agent);
36✔
298
        }
3✔
299
        for (Map.Entry<IRI, Set<IRI>> entry : agentsByPred.entrySet()) {
33✔
300
            IRI predicate = entry.getKey();
12✔
301
            BackcompatRolePredicates.Direction direction = directionByPred.get(predicate);
15✔
302
            String predHash = Utils.createHash(predicate.stringValue());
12✔
303
            IRI riIri = SpacesVocab.forRoleInstantiation(ctx.artifactCode(), predHash);
15✔
304
            out.add(vf.createStatement(riIri, RDF.TYPE, GEN.ROLE_INSTANTIATION, GRAPH));
27✔
305
            out.add(vf.createStatement(riIri, SpacesVocab.FOR_SPACE, spaceIri, GRAPH));
27✔
306
            IRI directionPredicate = (direction == BackcompatRolePredicates.Direction.REGULAR)
9✔
307
                    ? SpacesVocab.REGULAR_PROPERTY
6✔
308
                    : SpacesVocab.INVERSE_PROPERTY;
6✔
309
            out.add(vf.createStatement(riIri, directionPredicate, predicate, GRAPH));
27✔
310
            for (IRI agent : entry.getValue()) {
36✔
311
                out.add(vf.createStatement(riIri, SpacesVocab.FOR_AGENT, agent, GRAPH));
27✔
312
            }
3✔
313
            out.add(vf.createStatement(riIri, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
314
            addProvenance(riIri, ctx, out);
12✔
315
        }
3✔
316
    }
3✔
317

318
    /**
319
     * Heuristic: does {@code candidate} look like a Space IRI in {@code np}'s assertion,
320
     * independent of any {@code gen:hasRootDefinition} triple? We accept it if the
321
     * assertion contains {@code candidate rdf:type gen:Space} or
322
     * {@code candidate gen:hasAdmin ?x}.
323
     */
324
    private static boolean looksLikeSpaceIri(Nanopub np, IRI candidate) {
325
        for (Statement st : np.getAssertion()) {
33✔
326
            if (!candidate.equals(st.getSubject())) continue;
18✔
327
            if (st.getPredicate().equals(RDF.TYPE) && GEN.SPACE.equals(st.getObject())) return true;
36!
328
            if (st.getPredicate().equals(GEN.HAS_ADMIN)) return true;
15!
329
        }
3✔
330
        return false;
6✔
331
    }
332

333
    private static List<IRI> collectAdminAgents(Nanopub np) {
334
        Set<IRI> agents = new LinkedHashSet<>();
12✔
335
        for (Statement st : np.getAssertion()) {
33✔
336
            if (!st.getPredicate().equals(GEN.HAS_ADMIN)) continue;
18✔
337
            if (!(st.getObject() instanceof IRI agent)) continue;
27!
338
            agents.add(agent);
12✔
339
        }
3✔
340
        return new ArrayList<>(agents);
15✔
341
    }
342

343
    // ---------------- gen:hasRole (role attachment) ----------------
344

345
    private static void extractHasRole(Nanopub np, Context ctx, List<Statement> out) {
346
        // A gen:hasRole nanopub asserts <space> gen:hasRole <role>.
347
        for (Statement st : np.getAssertion()) {
33!
348
            if (!st.getPredicate().equals(GEN.HAS_ROLE)) continue;
15!
349
            if (!(st.getSubject() instanceof IRI spaceIri)) continue;
27!
350
            if (!(st.getObject() instanceof IRI roleIri)) continue;
27!
351
            IRI subject = SpacesVocab.forRoleAssignment(ctx.artifactCode());
12✔
352
            out.add(vf.createStatement(subject, RDF.TYPE, GEN.ROLE_ASSIGNMENT, GRAPH));
27✔
353
            out.add(vf.createStatement(subject, SpacesVocab.FOR_SPACE, spaceIri, GRAPH));
27✔
354
            out.add(vf.createStatement(subject, GEN.HAS_ROLE, roleIri, GRAPH));
27✔
355
            out.add(vf.createStatement(subject, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
356
            addProvenance(subject, ctx, out);
12✔
357
            // One attachment per nanopub — the subject IRI is derived from the nanopub
358
            // artifact code so multiple hasRole triples in the same nanopub would collide.
359
            // If that case shows up in practice, we'll refine the subject-minting scheme.
360
            return;
3✔
361
        }
362
    }
×
363

364
    // ---------------- gen:SpaceMemberRole (role declaration) ----------------
365

366
    private static void extractSpaceMemberRole(Nanopub np, Context ctx, List<Statement> out) {
367
        // The role IRI is embedded in this nanopub, so look for an assertion statement
368
        // of the shape <roleIri> rdf:type gen:SpaceMemberRole where <roleIri> starts
369
        // with the nanopub IRI (valid embedded mint).
370
        IRI roleIri = null;
6✔
371
        for (Statement st : np.getAssertion()) {
33✔
372
            if (!st.getPredicate().equals(RDF.TYPE)) continue;
15!
373
            if (!GEN.SPACE_MEMBER_ROLE.equals(st.getObject())) continue;
18✔
374
            if (!(st.getSubject() instanceof IRI candidate)) continue;
27!
375
            if (!candidate.stringValue().startsWith(np.getUri().stringValue())) continue;
24✔
376
            roleIri = candidate;
6✔
377
            break;
3✔
378
        }
379
        if (roleIri == null) return;
9✔
380

381
        IRI roleType = findRoleTier(np, roleIri);
12✔
382
        List<IRI> regulars = collectRolePredicate(np, roleIri, GEN.HAS_REGULAR_PROPERTY);
15✔
383
        List<IRI> inverses = collectRolePredicate(np, roleIri, GEN.HAS_INVERSE_PROPERTY);
15✔
384

385
        IRI subject = SpacesVocab.forRoleDeclaration(ctx.artifactCode());
12✔
386
        out.add(vf.createStatement(subject, RDF.TYPE, SpacesVocab.ROLE_DECLARATION, GRAPH));
27✔
387
        out.add(vf.createStatement(subject, SpacesVocab.ROLE, roleIri, GRAPH));
27✔
388
        out.add(vf.createStatement(subject, SpacesVocab.HAS_ROLE_TYPE, roleType, GRAPH));
27✔
389
        for (IRI reg : regulars) {
30✔
390
            out.add(vf.createStatement(subject, GEN.HAS_REGULAR_PROPERTY, reg, GRAPH));
27✔
391
        }
3✔
392
        for (IRI inv : inverses) {
30✔
393
            out.add(vf.createStatement(subject, GEN.HAS_INVERSE_PROPERTY, inv, GRAPH));
27✔
394
        }
3✔
395
        out.add(vf.createStatement(subject, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
396
        if (ctx.createdAt() != null) {
9!
397
            out.add(vf.createStatement(subject, DCTERMS.CREATED, vf.createLiteral(ctx.createdAt()), GRAPH));
36✔
398
        }
399
    }
3✔
400

401
    /**
402
     * Looks for a tier rdf:type ({@code gen:MaintainerRole} / {@code gen:MemberRole} /
403
     * {@code gen:ObserverRole}) on the role IRI in the assertion; defaults to
404
     * {@code gen:ObserverRole} if none is declared.
405
     */
406
    private static IRI findRoleTier(Nanopub np, IRI roleIri) {
407
        for (Statement st : np.getAssertion()) {
33✔
408
            if (!roleIri.equals(st.getSubject())) continue;
15!
409
            if (!st.getPredicate().equals(RDF.TYPE)) continue;
15!
410
            if (!(st.getObject() instanceof IRI type)) continue;
27!
411
            if (GEN.MAINTAINER_ROLE.equals(type) || GEN.MEMBER_ROLE.equals(type)
30!
412
                    || GEN.OBSERVER_ROLE.equals(type)) {
6!
413
                return type;
6✔
414
            }
415
        }
3✔
416
        return GEN.OBSERVER_ROLE;
6✔
417
    }
418

419
    private static List<IRI> collectRolePredicate(Nanopub np, IRI roleIri, IRI predicate) {
420
        List<IRI> out = new ArrayList<>();
12✔
421
        for (Statement st : np.getAssertion()) {
33✔
422
            if (!roleIri.equals(st.getSubject())) continue;
15!
423
            if (!predicate.equals(st.getPredicate())) continue;
18✔
424
            if (!(st.getObject() instanceof IRI obj)) continue;
27!
425
            out.add(obj);
12✔
426
        }
3✔
427
        return out;
6✔
428
    }
429

430
    // ---------------- gen:RoleInstantiation (and backcompat) ----------------
431

432
    private static void extractRoleInstantiation(Nanopub np, Context ctx, List<Statement> out) {
433
        // Find the assignment triple. Directionality (matches the publisher convention
434
        // used by gen:hasRegularProperty / gen:hasInverseProperty in role-definition
435
        // nanopubs):
436
        //   REGULAR: <agent> <predicate> <space>  → npa:regularProperty.
437
        //   INVERSE: <space> <predicate> <agent>  → npa:inverseProperty.
438
        // gen:hasAdmin is hardcoded INVERSE (space-centric: <space> hasAdmin <agent>).
439
        // The 14 backwards-compat predicates are classified in
440
        // {@link BackcompatRolePredicates#DIRECTIONS}. User-defined role predicates from
441
        // gen:SpaceMemberRole nanopubs aren't resolvable here without the role-declaration
442
        // registry; FIXME: the materializer in PR 2 should refine direction for the
443
        // typed-but-unknown-predicate case. For now we emit only triples whose predicate
444
        // we know the direction of.
445
        for (Statement st : np.getAssertion()) {
33!
446
            IRI predicate = st.getPredicate();
9✔
447
            BackcompatRolePredicates.Direction direction = directionFor(predicate);
9✔
448
            if (direction == null) continue;
6!
449
            if (!(st.getSubject() instanceof IRI subjIri)) continue;
27!
450
            if (!(st.getObject() instanceof IRI objIri)) continue;
27!
451

452
            IRI spaceSide;
453
            IRI agentSide;
454
            if (direction == BackcompatRolePredicates.Direction.REGULAR) {
9✔
455
                agentSide = subjIri;
6✔
456
                spaceSide = objIri;
9✔
457
            } else {
458
                spaceSide = subjIri;
6✔
459
                agentSide = objIri;
6✔
460
            }
461

462
            // Deduplicate against the (possibly already emitted) admin instantiation
463
            // from the gen:Space path — a single nanopub can be typed gen:Space AND
464
            // have a gen:hasAdmin triple that the backcompat list also catches. The
465
            // subject IRI is the same (derived from artifact code) and the payload
466
            // would conflict if re-emitted. Skip if we already have a RoleInstantiation
467
            // entry on this subject.
468
            IRI subject = SpacesVocab.forRoleInstantiation(ctx.artifactCode());
12✔
469
            Statement typeSt = vf.createStatement(subject, RDF.TYPE, GEN.ROLE_INSTANTIATION, GRAPH);
21✔
470
            if (out.contains(typeSt)) return;
12!
471

472
            out.add(typeSt);
12✔
473
            out.add(vf.createStatement(subject, SpacesVocab.FOR_SPACE, spaceSide, GRAPH));
27✔
474
            IRI directionPredicate = (direction == BackcompatRolePredicates.Direction.REGULAR)
9✔
475
                    ? SpacesVocab.REGULAR_PROPERTY
6✔
476
                    : SpacesVocab.INVERSE_PROPERTY;
6✔
477
            out.add(vf.createStatement(subject, directionPredicate, predicate, GRAPH));
27✔
478
            out.add(vf.createStatement(subject, SpacesVocab.FOR_AGENT, agentSide, GRAPH));
27✔
479
            out.add(vf.createStatement(subject, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
480
            addProvenance(subject, ctx, out);
12✔
481
            return;
3✔
482
        }
483
    }
×
484

485
    private static BackcompatRolePredicates.Direction directionFor(IRI predicate) {
486
        if (GEN.HAS_ADMIN.equals(predicate)) return BackcompatRolePredicates.Direction.INVERSE;
12!
487
        return BackcompatRolePredicates.DIRECTIONS.get(predicate);
15✔
488
    }
489

490
    // ---------------- gen:isSubSpaceOf (standalone path) ----------------
491

492
    /**
493
     * Standalone {@code gen:isSubSpaceOf} nanopub: every
494
     * {@code <childIri> gen:isSubSpaceOf <parentIri>} triple in the assertion emits one
495
     * {@code npa:SubSpaceDeclaration}. Multi-triple assertions are allowed; one entry
496
     * per pair. Self-loops ({@code <X> gen:isSubSpaceOf <X>}) are rejected.
497
     */
498
    private static void extractSubSpaceOf(Nanopub np, Context ctx, List<Statement> out) {
499
        for (Statement st : np.getAssertion()) {
33✔
500
            if (!st.getPredicate().equals(GEN.IS_SUB_SPACE_OF)) continue;
15!
501
            if (!(st.getSubject() instanceof IRI childIri)) continue;
27!
502
            if (!(st.getObject() instanceof IRI parentIri)) continue;
27!
503
            emitSubSpaceDeclaration(np, ctx, childIri, parentIri, out);
18✔
504
        }
3✔
505
    }
3✔
506

507
    /**
508
     * Embedded path: scan a {@code gen:Space} nanopub's assertion for
509
     * {@code <spaceIri> gen:isSubSpaceOf <parentIri>} triples (subject must equal the
510
     * Space IRI we're emitting an entry for, so the subspace declaration is bound to
511
     * this particular Space). Self-loops are rejected.
512
     */
513
    private static void emitSubSpaceDeclarations(Nanopub np, Context ctx, IRI spaceIri,
514
                                                 List<Statement> out) {
515
        for (Statement st : np.getAssertion()) {
33✔
516
            if (!st.getPredicate().equals(GEN.IS_SUB_SPACE_OF)) continue;
18✔
517
            if (!spaceIri.equals(st.getSubject())) continue;
18✔
518
            if (!(st.getObject() instanceof IRI parentIri)) continue;
27!
519
            emitSubSpaceDeclaration(np, ctx, spaceIri, parentIri, out);
18✔
520
        }
3✔
521
    }
3✔
522

523
    /**
524
     * Emits one {@code npa:SubSpaceDeclaration} entry, keyed by
525
     * {@code (artifactCode, parentHash)} so a single nanopub can declare multiple
526
     * parents without subject collision. Self-loops are silently dropped.
527
     */
528
    private static void emitSubSpaceDeclaration(Nanopub np, Context ctx, IRI childIri,
529
                                                IRI parentIri, List<Statement> out) {
530
        if (childIri.equals(parentIri)) {
12✔
531
            log.debug("Ignoring self-loop sub-space declaration on {} in {}", childIri, np.getUri());
18✔
532
            return;
3✔
533
        }
534
        String parentHash = Utils.createHash(parentIri);
9✔
535
        IRI subject = SpacesVocab.forSubSpaceDeclaration(ctx.artifactCode(), parentHash);
15✔
536

537
        // Idempotence: the embedded and standalone paths can both fire on the same
538
        // (np, child, parent) combination if a gen:Space nanopub somehow ends up typed
539
        // gen:isSubSpaceOf as well. Skip if we've already emitted the type triple for
540
        // this subject.
541
        Statement typeSt = vf.createStatement(subject, RDF.TYPE, SpacesVocab.SUB_SPACE_DECLARATION, GRAPH);
21✔
542
        if (out.contains(typeSt)) return;
12!
543

544
        out.add(typeSt);
12✔
545
        out.add(vf.createStatement(subject, SpacesVocab.CHILD_SPACE, childIri, GRAPH));
27✔
546
        out.add(vf.createStatement(subject, SpacesVocab.PARENT_SPACE, parentIri, GRAPH));
27✔
547
        out.add(vf.createStatement(subject, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
548
        addProvenance(subject, ctx, out);
12✔
549
    }
3✔
550

551
    // ---------------- gen:isMaintainedBy ----------------
552

553
    /**
554
     * Standalone {@code gen:isMaintainedBy} nanopub: every
555
     * {@code <resourceIri> gen:isMaintainedBy <spaceIri>} triple in the assertion emits
556
     * one {@code npa:MaintainedResourceDeclaration}. Multi-triple assertions are
557
     * allowed; one entry per pair. Self-loops ({@code <X> gen:isMaintainedBy <X>}) are
558
     * rejected.
559
     */
560
    private static void extractIsMaintainedBy(Nanopub np, Context ctx, List<Statement> out) {
561
        for (Statement st : np.getAssertion()) {
33✔
562
            if (!st.getPredicate().equals(GEN.IS_MAINTAINED_BY)) continue;
18✔
563
            if (!(st.getSubject() instanceof IRI resourceIri)) continue;
27!
564
            if (!(st.getObject() instanceof IRI spaceIri)) continue;
27!
565
            emitMaintainedResourceDeclaration(np, ctx, resourceIri, spaceIri, out);
18✔
566
        }
3✔
567
    }
3✔
568

569
    /**
570
     * Embedded path: scan a {@code gen:Space} nanopub's assertion for
571
     * {@code <resourceIri> gen:isMaintainedBy <spaceIri>} triples (object must equal
572
     * the Space IRI we're emitting an entry for, so the maintained-resource
573
     * declaration is bound to this particular Space). Self-loops are rejected.
574
     */
575
    private static void emitMaintainedResourceDeclarations(Nanopub np, Context ctx, IRI spaceIri,
576
                                                           List<Statement> out) {
577
        for (Statement st : np.getAssertion()) {
33✔
578
            if (!st.getPredicate().equals(GEN.IS_MAINTAINED_BY)) continue;
18✔
579
            if (!spaceIri.equals(st.getObject())) continue;
18✔
580
            if (!(st.getSubject() instanceof IRI resourceIri)) continue;
27!
581
            emitMaintainedResourceDeclaration(np, ctx, resourceIri, spaceIri, out);
18✔
582
        }
3✔
583
    }
3✔
584

585
    /**
586
     * Emits one {@code npa:MaintainedResourceDeclaration} entry, keyed by
587
     * {@code (artifactCode, resourceHash)} so a single nanopub can declare multiple
588
     * maintained resources without subject collision. Self-loops are silently dropped.
589
     */
590
    private static void emitMaintainedResourceDeclaration(Nanopub np, Context ctx, IRI resourceIri,
591
                                                          IRI spaceIri, List<Statement> out) {
592
        if (resourceIri.equals(spaceIri)) {
12✔
593
            log.debug("Ignoring self-loop maintained-resource declaration on {} in {}",
15✔
594
                    resourceIri, np.getUri());
3✔
595
            return;
3✔
596
        }
597
        String resourceHash = Utils.createHash(resourceIri);
9✔
598
        IRI subject = SpacesVocab.forMaintainedResourceDeclaration(ctx.artifactCode(), resourceHash);
15✔
599

600
        // Idempotence: the embedded (gen:Space) and standalone (gen:isMaintainedBy)
601
        // paths can both fire on the same (np, resource, space) combination if a
602
        // gen:Space nanopub somehow ends up typed gen:isMaintainedBy as well. Skip if
603
        // we've already emitted the type triple for this subject.
604
        Statement typeSt = vf.createStatement(subject, RDF.TYPE,
21✔
605
                SpacesVocab.MAINTAINED_RESOURCE_DECLARATION, GRAPH);
606
        if (out.contains(typeSt)) return;
12!
607

608
        out.add(typeSt);
12✔
609
        out.add(vf.createStatement(subject, SpacesVocab.RESOURCE_IRI, resourceIri, GRAPH));
27✔
610
        out.add(vf.createStatement(subject, SpacesVocab.MAINTAINER_SPACE, spaceIri, GRAPH));
27✔
611
        out.add(vf.createStatement(subject, SpacesVocab.VIA_NANOPUB, np.getUri(), GRAPH));
30✔
612
        addProvenance(subject, ctx, out);
12✔
613
    }
3✔
614

615
    // ---------------- ID-prefix enumeration ----------------
616

617
    /**
618
     * Returns the immediate URL-path parent of a Space IRI, after normalisation,
619
     * for the URL-prefix sub-space fallback. Strips query / fragment / trailing
620
     * slash, then drops the last path segment after the {@code ://} scheme
621
     * separator. Returns at most one IRI; empty for inputs without a scheme
622
     * separator or without any path beyond the host.
623
     *
624
     * <p>Direct-parent-only semantics matches Nanodash's existing
625
     * {@code SpaceRepository.findSubspaces(...)} URL-regex behaviour. Multi-level
626
     * containment queries should use SPARQL property paths
627
     * ({@code <ancestor> npa:hasSubSpace+ ?descendant}) which walk the chain
628
     * transitively, so deeper descendants remain reachable as long as the
629
     * intermediate Spaces exist.
630
     *
631
     * <p>Examples:
632
     * <pre>
633
     *   https://example.org/a/b/c/space  →  [https://example.org/a/b/c]
634
     *   https://example.org/space        →  [https://example.org]   (single segment → host)
635
     *   https://example.org/x/           →  [https://example.org]   (trailing slash stripped)
636
     *   https://example.org/a/space?q=1  →  [https://example.org/a] (query stripped)
637
     *   https://example.org              →  []                       (no path to strip)
638
     * </pre>
639
     */
640
    static List<IRI> enumerateIdPrefixes(IRI spaceIri) {
641
        String s = spaceIri.stringValue();
9✔
642
        int hash = s.indexOf('#');
12✔
643
        if (hash >= 0) s = s.substring(0, hash);
21✔
644
        int qmark = s.indexOf('?');
12✔
645
        if (qmark >= 0) s = s.substring(0, qmark);
21✔
646
        while (s.endsWith("/")) s = s.substring(0, s.length() - 1);
39✔
647

648
        int schemeEnd = s.indexOf("://");
12✔
649
        if (schemeEnd < 0) return Collections.emptyList();
12✔
650
        int hostStart = schemeEnd + 3;
12✔
651
        int hostEnd = s.indexOf('/', hostStart);
15✔
652
        if (hostEnd < 0) return Collections.emptyList();   // host-only, nothing to strip
12✔
653

654
        // Drop the last path segment. If that strips us back to the host (single-
655
        // segment path), return the host-only IRI as the immediate parent.
656
        int lastSlash = s.lastIndexOf('/');
12✔
657
        String parent = (lastSlash <= hostEnd) ? s.substring(0, hostEnd) : s.substring(0, lastSlash);
39✔
658
        return List.of(vf.createIRI(parent));
15✔
659
    }
660

661
    // ---------------- shared helpers ----------------
662

663
    private static void addProvenance(Resource subject, Context ctx, List<Statement> out) {
664
        if (ctx.signedBy() != null) {
9!
665
            out.add(vf.createStatement(subject, NPX.SIGNED_BY, ctx.signedBy(), GRAPH));
30✔
666
        }
667
        if (ctx.pubkeyHash() != null) {
9!
668
            out.add(vf.createStatement(subject, SpacesVocab.PUBKEY_HASH,
27✔
669
                    vf.createLiteral(ctx.pubkeyHash()), GRAPH));
9✔
670
        }
671
        if (ctx.createdAt() != null) {
9!
672
            Literal ts = vf.createLiteral(ctx.createdAt());
15✔
673
            out.add(vf.createStatement(subject, DCTERMS.CREATED, ts, GRAPH));
27✔
674
        }
675
    }
3✔
676

677
    private static boolean anyMatch(Set<IRI> types, Set<IRI> candidates) {
678
        for (IRI c : candidates) {
30✔
679
            if (types.contains(c)) return true;
18✔
680
        }
3✔
681
        return false;
6✔
682
    }
683

684
    // ---------------- load-number stamping ----------------
685

686
    /**
687
     * Stamps {@code <thisNP> npa:hasLoadNumber <N>} on the given nanopub. Intended to
688
     * be called by the loader once per nanopub, in the same transaction as the
689
     * extraction writes. Also bumps {@code npa:thisRepo npa:currentLoadCounter <N>}
690
     * in the admin graph so the materializer's delta cycles know the horizon.
691
     *
692
     * @param npId        nanopub IRI
693
     * @param loadNumber  the load counter value
694
     * @return two statements: load-number stamp + current-load-counter value
695
     */
696
    public static List<Statement> loadCounterStatements(IRI npId, long loadNumber) {
697
        List<Statement> out = new ArrayList<>(2);
15✔
698
        Literal lit = vf.createLiteral(loadNumber);
12✔
699
        out.add(vf.createStatement(npId, NPA.HAS_LOAD_NUMBER, lit, NPA.GRAPH));
27✔
700
        out.add(vf.createStatement(NPA.THIS_REPO, SpacesVocab.CURRENT_LOAD_COUNTER, lit, NPA.GRAPH));
27✔
701
        return out;
6✔
702
    }
703

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