Skip to content

Commit

Permalink
#135: Initial work to Group Schedule Table Inputs (#144)
Browse files Browse the repository at this point in the history
* WIP: update 'dat' structure

* Fix syntax error in dat subcomponents

* Fix typos in test template

* WIP: add debug for redeclaration modifiers

* WIP: add TODO for expanding 'Modification' class for redeclares

* Expand 'Modification' class to support redeclare modifiers

* Add additional typing for redeclaration structures

* WIP: add 'dat' specific test group

* Correct replaceable param in Testpackage

* WIP: Update how child options are populated

* Fix bug in how child options are fetched for replaceable inputs

* Add optionTree helper function

* WIP: refactor modification factory method, add additional types

* WIP: add mod unpacking helper method

* Add 'Import' parser element

* Handle package redeclarations

* Remove option count test

* WIP: split out schedule options from config options

* Fix order-of-operations error in condition

* Punch out on duplicate type references when splitting schedule options

* Prevent recursive extraction of schedule options

* Update system option tests

* Remove debugging logs
  • Loading branch information
darenkeck-dev committed Jul 29, 2022
1 parent ff0de06 commit 78527cf
Show file tree
Hide file tree
Showing 15 changed files with 359 additions and 137 deletions.
5 changes: 4 additions & 1 deletion server/scripts/parse-template-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {

loadPackage("Buildings");

const { options, scheduleOptions } = getOptions();

const data = {
templates: getTemplates(),
systemTypes: getSystemTypes(),
options: getOptions(),
options: options,
scheduleOptions: scheduleOptions,
};

const dest = path.resolve(
Expand Down
5 changes: 4 additions & 1 deletion server/scripts/parse-test-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ loadPackage(`${fullTempDirPath}/TestPackage`);
// const buildDir = `${process.cwd()}/build/modelica-json/json`;
// loadPackage(`${buildDir}/Buildings/Templates`);

const { options, scheduleOptions } = getOptions();

const data = {
templates: getTemplates(),
systemTypes: getSystemTypes(),
options: getOptions(),
options: options,
scheduleOptions: scheduleOptions,
};

const dest = path.resolve(
Expand Down
5 changes: 4 additions & 1 deletion server/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export function getSystemTypes(): templates.SystemTypeN[] {
return templates.getSystemTypes();
}

export function getOptions(): parser.OptionN[] {
export function getOptions(): {
options: parser.OptionN[];
scheduleOptions: templates.ScheduleOption[];
} {
return templates.getOptions();
}
156 changes: 107 additions & 49 deletions server/src/parser/modification.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ShortClassSpecifier } from "./parser";

/**
* Modifications are places where there is an assignment, e.g.
* 'my_param=5'. Modifications can also contain groups of modifications, e.g.
Expand All @@ -23,22 +25,38 @@

const modStore: Map<string, Modification> = new Map();

type RedeclarationMod = {
type ComponentDeclaration1 = {
declaration: DeclarationBlock;
description?: DescriptionBlock;
};

type ComponentClause1 = {
type_specifier: string; // Modelica Path
component_declaration1: ComponentDeclaration1;
};

type ShortClassDefinition = {
class_prefixes: string; // //(PARTIAL)? (CLASS | MODEL | (OPERATOR)? RECORD | BLOCK | (EXPANDABLE)? CONNECTOR | TYPE | PACKAGE | ((PURE | IMPURE))? (OPERATOR)? FUNCTION | OPERATOR),
short_class_specifier: ShortClassSpecifier; // from 'parser.ts'
};

type ElementReplaceable = {
component_clause1: ComponentClause1;
short_class_definition: ShortClassDefinition;
};

type RedeclareMod = {
element_redeclaration: {
element_replaceable: {
component_clause1: {
type_specifier: string; // Modelica Path
component_declaration1: {
declaration: DeclarationBlock;
description: DescriptionBlock;
};
};
}
each: boolean;
final: boolean;
short_class_definition?: ShortClassDefinition;
element_replaceable?: ElementReplaceable;
component_clause1?: ComponentClause1;
};
};

type ClassMod = {
class_modification: (WrappedMod | RedeclarationMod)[];
class_modification: (WrappedMod | RedeclareMod)[];
};

type Assignment = {
Expand All @@ -57,12 +75,12 @@ export type WrappedMod = {

export type Mod = {
name: string;
modification: ClassMod | WrappedMod | Assignment | RedeclarationMod;
modification: ClassMod | WrappedMod | Assignment | RedeclareMod;
};

export type DeclarationBlock = {
identifier: string;
modification?: ClassMod | WrappedMod | Assignment | RedeclarationMod;
modification?: ClassMod | WrappedMod | Assignment | RedeclareMod;
};

export type DescriptionBlock = {
Expand All @@ -79,25 +97,28 @@ export function getModificationList(
classMod: ClassMod,
modelicaPath: string,
name = "",
): Modification[] {
return classMod.class_modification.map((m) =>
createModification({
definition: m as WrappedMod,
basePath: modelicaPath,
name: name,
}),
);
) {
return classMod.class_modification
.map((m) =>
createModification({
definition: m as WrappedMod,
basePath: modelicaPath,
name: name,
}),
)
.filter((m) => m !== undefined) as Modification[];
}

interface ModificationBasics {
basePath?: string;
name?: string;
value?: any;
definition?: any;
type?: string;
}

interface ModificationWithDefinition extends ModificationBasics {
definition: WrappedMod | Mod | DeclarationBlock;
definition: WrappedMod | Mod | DeclarationBlock | RedeclareMod;
value?: never;
}

Expand All @@ -109,6 +130,64 @@ interface ModificationWithValue extends ModificationBasics {

type ModificationProps = ModificationWithDefinition | ModificationWithValue;

function unpackRedeclaration(props: ModificationProps) {
let { definition } = props;
const redeclaration = (definition as RedeclareMod).element_redeclaration;
if ("component_clause1" in redeclaration) {
const componentClause1 =
redeclaration.component_clause1 as ComponentClause1;
const type = componentClause1.type_specifier;
const redeclareDefinition =
componentClause1.component_declaration1.declaration;
const modProps = { ...props, type, definition: redeclareDefinition };
const redeclareMod = createModification(modProps);
return redeclareMod;
} else if ("short_class_definition" in redeclaration) {
} else if ("element_replaceable" in redeclaration) {
}
}

function unpackModblock(props: ModificationProps) {
let mods: Modification[] = [];
let value: string = "";
let { definition, basePath = "", name } = props as ModificationWithDefinition;

let modBlock = definition;

modBlock =
"element_modification_or_replaceable" in definition
? definition.element_modification_or_replaceable.element_modification
: definition;

if ("name" in modBlock) {
name = modBlock.name;
} else if ("identifier" in modBlock) {
name = modBlock.identifier;
}

let modelicaPath = basePath ? `${basePath}.${name}` : "";
const mod = (modBlock as Mod).modification;
if (mod) {
// test if an assignment
if ("equal" in mod) {
// simple_expression can potentially be an expression
// TODO be ready to feed that into Expression generator
value = (mod as Assignment).expression.simple_expression;
} else if (name == "choice") {
const choiceMod = (mod as ClassMod).class_modification[0] as RedeclareMod;
if (choiceMod.element_redeclaration) {
const replaceable = choiceMod.element_redeclaration
.element_replaceable as ElementReplaceable;
value = replaceable.component_clause1.type_specifier;
}
} else if ("class_modification" in mod) {
mods = getModificationList(mod as ClassMod, modelicaPath);
}
}

return new Modification(basePath, name, value, mods);
}

/**
* Factory method that can create a Modification from two approaches:
*
Expand All @@ -118,40 +197,19 @@ type ModificationProps = ModificationWithDefinition | ModificationWithValue;
* @param props: ModificationProps
* @returns Modification
*/
export function createModification(props: ModificationProps): Modification {
export function createModification(
props: ModificationProps,
): Modification | undefined {
let mods: Modification[] = [];
let { definition, value, basePath = "", name } = props;
let modelicaPath = basePath ? `${basePath}.${name}` : "";

if (definition) {
const modBlock =
"element_modification_or_replaceable" in definition
? definition.element_modification_or_replaceable.element_modification
: definition;

if ("name" in modBlock) {
name = modBlock.name;
} else if ("identifier" in modBlock) {
name = modBlock.identifier;
if ("element_redeclaration" in definition) {
return unpackRedeclaration(props);
}

const mod = modBlock.modification;
if (mod) {
// test if an assignment
if ("equal" in mod) {
// simple_expression can potentially be an expression
// TODO be ready to feed that into Expression generator
value = (mod as Assignment).expression.simple_expression;
} else if (name == "choice") {
const choiceMod = (mod as ClassMod)
.class_modification[0] as RedeclarationMod;
value =
choiceMod.element_redeclaration.element_replaceable.component_clause1.type_specifier;
} else if ("class_modification" in mod) {
// const type = "";
mods = getModificationList(mod as ClassMod, modelicaPath);
}
}
return unpackModblock(props);
}

return new Modification(basePath, name, value, mods);
Expand Down
Loading

0 comments on commit 78527cf

Please sign in to comment.