Skip to content

Commit

Permalink
create connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Polický committed Dec 11, 2023
1 parent 513f416 commit 65deec4
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ export const EntityCatalogue = () => {
.flat()
.map((cls) => [cls.cls.id, cls])
)
// [...models.keys()]
// .map((modelId) =>
// Object.values(models.get(modelId)!.getEntities())
// .filter(isSemanticModelClass)
// .map((c) => ({ cls: c, origin: modelId }))
// )
// .flat()
);
setRelationships(
[...models.keys()]
Expand Down Expand Up @@ -108,8 +101,7 @@ export const EntityCatalogue = () => {

const handleOpenDetail = (cls: SemanticModelClass) => {
console.log("in handle open detail for semantic model class", cls);
setEntityDetailSelected(cls);
openEntityDetailDialog();
openEntityDetailDialog(cls);
};

const handleAddConcept = () => {
Expand All @@ -121,15 +113,7 @@ export const EntityCatalogue = () => {

const handleOpenModification = (cls: SemanticModelClass) => {
console.log("in handle open modification for semantic model class", cls);
setEntityDetailSelected(cls);
openModifyEntityDialog();
};

const handleModifyConcept = (cls: SemanticModelClass, entity: Partial<Omit<SemanticModelClass, "type" | "id">>) => {
const resultSuccess = modifyClassInLocalModel(cls.id, entity); //{ cs: getRandomName(5), en: getRandomName(5) }, undefined);
if (!resultSuccess) {
alert("FIXME: something went wrong, class not added to local model");
}
openModifyEntityDialog(cls);
};

// classes from model
Expand Down Expand Up @@ -173,7 +157,7 @@ export const EntityCatalogue = () => {
<div className="flex flex-row justify-between whitespace-nowrap">
<span onClick={props.toggleHandler}>
{allowedClasses.includes(cls.id) ? "✅ " : "❌ "}
{getNameOf(cls)}
{getNameOf(cls).t}
</span>
<div className="ml-2 flex flex-row bg-teal-300 px-1">
<button onClick={() => handleOpenDetail(cls)}>Detail</button>
Expand All @@ -190,15 +174,15 @@ export const EntityCatalogue = () => {
};
const NonExpandableRow = (props: { cls: SemanticModelClassWithOrigin }) => (
<div className="flex flex-row justify-between whitespace-nowrap">
{getNameOf(props.cls.cls)}
{getNameOf(props.cls.cls).t}
<button className="ml-2 bg-teal-300 px-1" onClick={() => handleOpenDetail(props.cls.cls)}>
Detail
</button>
</div>
);
const ModifiableRow = (props: { cls: SemanticModelClassWithOrigin }) => (
<div className="flex flex-row justify-between whitespace-nowrap">
{getNameOf(props.cls.cls)}
{getNameOf(props.cls.cls).t}
<div className="bg-teal-300 px-1">
<button className="ml-0.5" onClick={() => handleOpenModification(props.cls.cls)}>
Modify
Expand Down Expand Up @@ -234,15 +218,8 @@ export const EntityCatalogue = () => {

<ul>{[...models.keys()].map((modelId) => getClassesFromModel(modelId))}</ul>
</div>
{isEntityDetailDialogOpen && <EntityDetailDialog cls={entityDetailSelected} />}
{isModifyEntityDialogOpen && (
<ModifyEntityDialog
cls={entityDetailSelected}
save={(entity: Partial<Omit<SemanticModelClass, "type" | "id">>) =>
handleModifyConcept(entityDetailSelected, entity)
}
/>
)}
{isEntityDetailDialogOpen && <EntityDetailDialog />}
{isModifyEntityDialogOpen && <ModifyEntityDialog />}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import {
SemanticModelClass,
SemanticModelGeneralization,
SemanticModelRelationship,
SemanticModelRelationshipEnd,
} from "@dataspecer/core-v2/semantic-model/concepts";
import { type InMemorySemanticModel } from "@dataspecer/core-v2/semantic-model/in-memory";
import { InMemorySemanticModel } from "@dataspecer/core-v2/semantic-model/in-memory";
import {
createGeneralization,
createRelationship,
modifyGeneralization,
modifyRelation,
} from "@dataspecer/core-v2/semantic-model/operations";
import React, { useContext } from "react";
import { ConnectionType } from "../util/connection";
import { AssociationConnectionType, ConnectionType, GeneralizationConnectionType } from "../util/connection";
import { useModelGraphContext } from "./graph-context";
import { LOCAL_MODEL_ID } from "../util/constants";

export type ClassesContextType = {
classes: Map<string, SemanticModelClassWithOrigin>; // was an array, [classId, classWithOrigin]
Expand Down Expand Up @@ -43,26 +44,27 @@ export const useClassesContext = () => {
generalizations,
setGeneralizations,
} = useContext(ClassesContext);
const { models } = useModelGraphContext();

const createConnection = (model: InMemorySemanticModel, connection: ConnectionType) => {
if (connection.connectionType == "association") {
const result = model.executeOperation(createRelationship({ ends: connection.ends }));
const createConnection = (connection: ConnectionType) => {
const model = models.get(LOCAL_MODEL_ID);
if (!model || !(model instanceof InMemorySemanticModel)) {
alert(`local model [${LOCAL_MODEL_ID}] not found or is not of type InMemoryLocal`);
return;
}
if (connection.type == "association") {
const conn = connection as AssociationConnectionType;
const result = model.executeOperation(createRelationship({ ...conn }));
console.log("create association operation done: ", model);
return result.success;
} else {
const result = model.executeOperation(
createGeneralization({ child: connection.childEntityId, parent: connection.parentEntityId })
);
} else if (connection.type == "generalization") {
const conn = connection as GeneralizationConnectionType;
const result = model.executeOperation(createGeneralization({ ...conn }));
console.log("create generalization operation done: ", model);
return result.success;
}
};

const modifyConnection = (model: InMemorySemanticModel, relationId: string, connection: ConnectionType) => {
if (connection.connectionType == "association") {
return model.executeOperation(modifyRelation(relationId, { ends: connection.ends })).success;
} else {
return model.executeOperation(
modifyGeneralization(relationId, { child: connection.childEntityId, parent: connection.parentEntityId })
).success;
alert(`classes-context: create-connection: unknown type ${connection}`);
return false;
}
};

Expand Down
Loading

0 comments on commit 65deec4

Please sign in to comment.