Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import from Lionweb models not exported from Kolasu #67

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project from version 1.2.0 upwards are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [1.6.24] – Not yet released

### Fixed
- `assertASTsAreEqual` is now properly exported

## [1.6.23] – 2024-04-24

### Fixed
Expand Down
69 changes: 47 additions & 22 deletions src/interop/lionweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import {
Classifier,
Concept,
Containment,
Containment, deserializeChunk,
Feature as LWFeature,
Id,
InstantiationFacade,
Interface,
Language,
Node as LWNodeInterface
Node as LWNodeInterface,
SerializationChunk
} from "@lionweb/core";
import {NodeAdapter, Issue, Node, NodeDefinition, Position, Feature, Point, pos} from "..";
import {NodeAdapter, Issue, Node, NodeDefinition, Position, Feature, Point, pos, TraceNode} from "..";
import {STARLASU_LANGUAGE} from "./lionweb-starlasu-language";
export {STARLASU_LANGUAGE} from "./lionweb-starlasu-language";

Expand Down Expand Up @@ -77,25 +78,37 @@ export class LanguageMapping {
}
}

function isASTNode(classifier: Classifier) {
return (classifier instanceof Concept) &&
(classifier.key == ASTNode.key || (classifier.extends && isASTNode(classifier.extends)));
function isSpecialConcept(classifier: Classifier | null) {
return classifier?.key == PositionClassifier.key;
}

function importFeature(feature: LWFeature): Feature | undefined {
const def: Feature = { name: feature.name };
if (feature instanceof Containment) {
if (feature.type && isASTNode(feature.type)) {
if (!isSpecialConcept(feature.type)) {
def.child = true;
def.multiple = feature.multiple;
} else {
// TODO we assume that:
// 1) we're importing a StarLasu AST
// 2) containments in a StarLasu AST are either AST nodes or internal StarLasu objects like the position
// We don't import the containment because we handle it specially
return undefined;
}
}
return def
return def;
}

function importConcept(concept: NodeDefinition | undefined, classifier: Classifier) {
concept = {
name: classifier.name,
features: {},
resolved: true
};
allFeatures(classifier).forEach(f => {
const feature = importFeature(f);
if (feature) {
concept!.features[f.name] = feature;
}
});
return concept;
}

export class TylasuInstantiationFacade implements InstantiationFacade<TylasuWrapper> {
Expand Down Expand Up @@ -128,17 +141,7 @@ export class TylasuInstantiationFacade implements InstantiationFacade<TylasuWrap
if (!node) {
let concept = this.concepts.get(classifier);
if (!concept) {
concept = {
name: classifier.name,
features: {},
resolved: true
};
allFeatures(classifier).forEach(f => {
const feature = importFeature(f);
if (feature) {
concept!.features[f.name] = feature;
}
});
concept = importConcept(concept, classifier);
this.concepts.set(classifier, concept);
}
node = new LionwebNode(concept, {
Expand Down Expand Up @@ -281,3 +284,25 @@ export class LionwebNode extends NodeAdapter {
return other instanceof LionwebNode && other.lwnode == this.lwnode;
}
}

export function deserializeToTylasuNodes(
chunk: SerializationChunk,
languages: Language[],
languageMappings: LanguageMapping[] = [STARLASU_LANGUAGE_MAPPING],
dependentNodes: LWNodeInterface[] = []
): Node[] {
return deserializeChunk(
chunk, new TylasuInstantiationFacade(languageMappings), [STARLASU_LANGUAGE, ...languages], dependentNodes
)
.filter(n => n instanceof TylasuNodeWrapper)
.map(n => (n as TylasuNodeWrapper).node);
}

export function deserializeToTraceNodes(
chunk: SerializationChunk,
languages: Language[],
dependentNodes: LWNodeInterface[] = []
): TraceNode[] {
return deserializeToTylasuNodes(chunk, languages, [], dependentNodes).map(
n => new TraceNode(n as LionwebNode));
}
91 changes: 13 additions & 78 deletions tests/interop/fs-language.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -287,12 +282,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -630,12 +620,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -766,12 +751,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -1096,12 +1076,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -1224,12 +1199,7 @@
"version": "2023.1",
"key": "Link-type"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
}
],
"annotations": [],
Expand Down Expand Up @@ -1357,12 +1327,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -1604,12 +1569,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -1943,12 +1903,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -2083,12 +2038,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -2275,12 +2225,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -2467,12 +2412,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down Expand Up @@ -2667,12 +2607,7 @@
"version": "2023.1",
"key": "Concept-extends"
},
"targets": [
{
"resolveInfo": "ASTNode",
"reference": "com-strumenta-StarLasu-ASTNode-id"
}
]
"targets": []
},
{
"reference": {
Expand Down
Loading