Skip to content

Commit 32d6ce9

Browse files
authored
fix: j.*-namespace, when exporting with undefined namespace and creating class with max z index (#130, RDFA-543)
* dct, dcat, owl are now added as a known namespace by default * fixed bug, where new classes were not created with the maximum z index * creating an ontology now adds the necessary namespaces
1 parent d1cbfa7 commit 32d6ce9

7 files changed

Lines changed: 92 additions & 20 deletions

File tree

backend/src/main/java/org/rdfarchitect/services/dl/update/DiagramLayoutServiceUtils.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.rdfarchitect.dl.data.dto.relations.MRID;
2727
import org.rdfarchitect.dl.data.dto.relations.OrientationKind;
2828
import org.rdfarchitect.dl.data.dto.relations.XYZPosition;
29+
import org.rdfarchitect.dl.queries.select.DLObjectFetcher;
2930
import org.rdfarchitect.dl.queries.update.DLUpdates;
30-
import org.rdfarchitect.dl.rdf.resources.DL;
3131

3232
import java.util.UUID;
3333

@@ -81,32 +81,39 @@ public MRID insertDiagramObject(
8181
* Helper method for creating and inserting a {@link DiagramObjectPoint} into a given model.
8282
*
8383
* @param diagramLayoutModel the model into which the diagram object point is inserted
84+
* @param diagrammUUID the uuid of the diagram
8485
* @param diagramObjectMRID the mRID of the diagram object the point belongs to
8586
*/
86-
public void insertDiagramObjectPoint(Model diagramLayoutModel, MRID diagramObjectMRID) {
87-
insertDiagramObjectPoint(diagramLayoutModel, diagramObjectMRID, 0, 0);
87+
public void insertDiagramObjectPoint(
88+
Model diagramLayoutModel, UUID diagrammUUID, MRID diagramObjectMRID) {
89+
insertDiagramObjectPoint(diagramLayoutModel, diagramObjectMRID, diagrammUUID, 0, 0);
8890
}
8991

9092
/**
9193
* Helper method for creating and inserting a {@link DiagramObjectPoint} at a given position.
9294
*
9395
* @param diagramLayoutModel the model into which the diagram object point is inserted
9496
* @param diagramObjectMRID the mRID of the diagram object the point belongs to
97+
* @param diagrammUUID the UUID of the diagram
9598
* @param xPosition the x position of the diagram object point
9699
* @param yPosition the y position of the diagram object point
97100
*/
98101
public void insertDiagramObjectPoint(
99-
Model diagramLayoutModel, MRID diagramObjectMRID, float xPosition, float yPosition) {
102+
Model diagramLayoutModel,
103+
MRID diagramObjectMRID,
104+
UUID diagrammUUID,
105+
float xPosition,
106+
float yPosition) {
107+
108+
var existingPoints =
109+
DLObjectFetcher.fetchDiagramDOPPerClass(diagramLayoutModel, diagrammUUID);
110+
100111
var maxZPosition =
101-
diagramLayoutModel.listObjectsOfProperty(DL.zPosition).toSet().stream()
102-
.max(
103-
(o1, o2) -> {
104-
var z1 = o1.asLiteral().getInt();
105-
var z2 = o2.asLiteral().getInt();
106-
return Integer.compare(z1, z2);
107-
})
108-
.map(o -> o.asLiteral().getInt())
112+
existingPoints.values().stream()
113+
.map(dop -> dop.getPosition().getZ())
114+
.max(Integer::compare)
109115
.orElse(0);
116+
110117
var diagramObjectPoint =
111118
DiagramObjectPoint.builder()
112119
.mRID(new MRID(UUID.randomUUID()))

backend/src/main/java/org/rdfarchitect/services/dl/update/UpdateDiagramLayoutService.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public void createDiagramLayout(GraphIdentifier graphIdentifier) {
8383
cimClassOrEnum.getLabel().getValue(),
8484
cimClassOrEnum.getUuid());
8585
DiagramLayoutServiceUtils.insertDiagramObjectPoint(
86-
diagramLayoutModel, diagramObjectMRID);
86+
diagramLayoutModel,
87+
packageGraphFilter.getPackageUUID() == null
88+
? diagramLayout.getDefaultPackageMRID().getUuid()
89+
: UUID.fromString(packageGraphFilter.getPackageUUID()),
90+
diagramObjectMRID);
8791
}
8892

8993
// create DOs and DOPs for each frontend diagram
@@ -102,7 +106,7 @@ public void createDiagramLayout(GraphIdentifier graphIdentifier) {
102106
cimClassOrEnum.getLabel().getValue(),
103107
cimClassOrEnum.getUuid());
104108
DiagramLayoutServiceUtils.insertDiagramObjectPoint(
105-
diagramLayoutModel, diagramObjectMRID);
109+
diagramLayoutModel, cimPackage.getUuid(), diagramObjectMRID);
106110
}
107111
}
108112
}
@@ -173,7 +177,7 @@ private void ensureDiagramObjectsExist(
173177
cimClass.getLabel().getValue(),
174178
cimClass.getUuid());
175179
DiagramLayoutServiceUtils.insertDiagramObjectPoint(
176-
diagramLayoutModel, diagramObjectMRID);
180+
diagramLayoutModel, diagramUUID, diagramObjectMRID);
177181
}
178182
}
179183
}

backend/src/main/java/org/rdfarchitect/services/dl/update/classlayout/UpdateClassLayoutService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void createClassLayoutData(
7171
float xPosition = classLayoutPosition != null ? classLayoutPosition.getXPosition() : 0;
7272
float yPosition = classLayoutPosition != null ? classLayoutPosition.getYPosition() : 0;
7373
DiagramLayoutServiceUtils.insertDiagramObjectPoint(
74-
diagramLayoutModel, doMRID, xPosition, yPosition);
74+
diagramLayoutModel, doMRID, packageUUID, xPosition, yPosition);
7575
}
7676

7777
@Override

backend/src/main/java/org/rdfarchitect/services/update/ontology/UpdateOntologyService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
import org.apache.jena.query.TxnType;
2323
import org.apache.jena.rdf.model.ModelFactory;
24+
import org.apache.jena.shared.impl.PrefixMappingImpl;
25+
import org.apache.jena.vocabulary.DCAT;
26+
import org.apache.jena.vocabulary.DCTerms;
27+
import org.apache.jena.vocabulary.OWL2;
2428
import org.rdfarchitect.api.dto.ontology.OntologyDTO;
2529
import org.rdfarchitect.database.DatabasePort;
2630
import org.rdfarchitect.database.GraphIdentifier;
@@ -63,6 +67,19 @@ public void createOntology(GraphIdentifier graphIdentifier, OntologyDTO ontology
6367
}
6468
var ontology = new OntologyFacade(model);
6569
ontology.createOntology(ontologyDTO);
70+
var pm = new PrefixMappingImpl();
71+
pm.setNsPrefixes(databasePort.getPrefixMapping(graphIdentifier.datasetName()));
72+
if (pm.getNsURIPrefix(DCAT.getURI()) == null) {
73+
pm.setNsPrefix("dcat", DCAT.getURI());
74+
}
75+
if (pm.getNsURIPrefix(DCTerms.getURI()) == null) {
76+
pm.setNsPrefix("dct", DCTerms.getURI());
77+
}
78+
if (pm.getNsURIPrefix(OWL2.getURI()) == null) {
79+
pm.setNsPrefix("owl", OWL2.getURI());
80+
}
81+
databasePort.setPrefixMapping(graphIdentifier.datasetName(), pm);
82+
6683
graph.commit();
6784
} finally {
6885
if (graph != null) {

backend/src/main/resources/application-schema.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ schema:
99
uml: "http://iec.ch/TC57/NonStandard/UML#"
1010
cim: "http://iec.ch/TC57/CIM100#"
1111
cims: "http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#"
12+
dct: "http://purl.org/dc/terms/"
13+
dcat: "http://www.w3.org/ns/dcat#"

frontend/src/lib/rendering/svelteflow/svelteFlowWrapper.svelte

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,34 @@
141141
}
142142
143143
/**
144-
* Initializes nodeOrder from inputNodes on first load.
145-
* Sorts by position.z from the backend so persisted order is restored.
146-
* No need to handle add/remove since the diagram reloads in those cases.
144+
* Synchronizes nodeOrder with the current set of nodes.
145+
*
146+
* - On first load, initializes the order from the backend's persisted
147+
* position.z so the saved stacking order is restored.
148+
* - On subsequent updates, removes nodes that no longer exist and appends
149+
* newly added nodes at the end so they appear in front (highest zIndex).
147150
*/
148151
function syncNodeOrder(nextNodes) {
152+
const nextIds = new Set(nextNodes.map(n => n.id));
153+
149154
if (nodeOrder.length === 0) {
155+
// Initial load: sort by persisted z-position
150156
nodeOrder = [...nextNodes]
151157
.sort((a, b) => (a.position?.z ?? 0) - (b.position?.z ?? 0))
152158
.map(n => n.id);
159+
return;
153160
}
161+
162+
// Drop nodes that no longer exist from the order
163+
const existingOrder = nodeOrder.filter(id => nextIds.has(id));
164+
165+
// Append new nodes at the end -> they appear in front (highest zIndex)
166+
const knownIds = new Set(existingOrder);
167+
const newIds = nextNodes
168+
.filter(n => !knownIds.has(n.id))
169+
.map(n => n.id);
170+
171+
nodeOrder = [...existingOrder, ...newIds];
154172
}
155173
156174
function applyZIndicesFromOrder(diagramNodes) {

frontend/src/routes/mainpage/packageNavigation/ontology-editor-dialog/OntologyDialog.svelte

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@
4848
4949
const bec = new BackendConnection(fetch, PUBLIC_BACKEND_URL);
5050
51+
// Namespaces that the backend auto-adds when an ontology is created.
52+
// Added client-side here (without persisting) so they already appear
53+
// in the namespace selection while the dialog is open.
54+
const ONTOLOGY_DEFAULT_NAMESPACES = [
55+
{ substitutedPrefix: "dcat:", prefix: "http://www.w3.org/ns/dcat#" },
56+
{ substitutedPrefix: "dct:", prefix: "http://purl.org/dc/terms/" },
57+
{ substitutedPrefix: "owl:", prefix: "http://www.w3.org/2002/07/owl#" },
58+
];
59+
5160
let showAddKnownEntriesPopUp = $state(false);
5261
let showDiscardSaveConfirmDialog = $state(false);
5362
@@ -63,11 +72,26 @@
6372
6473
let disableSubmit = $derived(!hasChanges || !isValid);
6574
75+
/**
76+
* Returns the namespace list extended with the ontology default
77+
* namespaces (dcat, dct, owl) that are missing. A namespace counts as
78+
* present if its IRI (`prefix`) already exists. Nothing is persisted here;
79+
* the backend adds these on save when the ontology is created.
80+
*/
81+
function withOntologyDefaultNamespaces(currentNamespaces) {
82+
const existing = currentNamespaces ?? [];
83+
const missing = ONTOLOGY_DEFAULT_NAMESPACES.filter(
84+
defaultNs => !existing.some(ns => ns.prefix === defaultNs.prefix),
85+
);
86+
return [...existing, ...missing];
87+
}
88+
6689
async function onOpen() {
6790
if (!namespaces) {
6891
namespaces = await getNamespaces(dataset);
6992
}
7093
94+
namespaces = withOntologyDefaultNamespaces(namespaces);
7195
const resolvedNamespaces = namespaces ?? [];
7296
7397
if (!ontology) {
@@ -113,7 +137,7 @@
113137
ontology.uuid,
114138
ontology.namespace,
115139
ontology.entries,
116-
namespaces ?? [],
140+
namespaces ?? withOntologyDefaultNamespaces([]),
117141
);
118142
}
119143
ontologyObject.save();

0 commit comments

Comments
 (0)