Skip to content

Commit

Permalink
unique relation constraint (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesstoetzner authored Nov 9, 2024
1 parent ab58d47 commit 4ea4429
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/docs/variability4tosca/specification/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ The following options are used to configure constraints.
| unique_artifact_constraint | false | Boolean | false | Enable the constraint regarding unique artifact names. |
| unique_input_constraint | false | Boolean | false | Enable the constraint regarding unique deployment input names. |
| unique_output_constraint | false | Boolean | false | Enable the constraint regarding unique deployment output names. |
| unique_relation_constraint | false | Boolean | false | Enable the constraint regarding unique relation names. |
| required_artifact_constraint | false | Boolean | false | Enable the constraint regarding required artifact. |
| required_incoming_relation_constraint | false | Boolean | false | Enable the constraint regarding required incoming relation. |

Expand Down Expand Up @@ -346,6 +347,7 @@ unique_property_constraint: true
unique_artifact_constraint: true
unique_input_constraint: true
unique_output_constraint: true
unique_relation_constraint: true
required_artifact_constraint: true
relation_default_implied: true
checks: false
Expand Down
20 changes: 15 additions & 5 deletions src/enricher/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export class ConstraintEnricher {
}

/**
* If requests, then the manual condition of an element implies this element
* If modeled, then the manual condition of an element implies this element
* This ensures, e.g., that a relation is present under a given condition while using incoming-host
*
* This is most likely only relevant for relations.
* This is most likely only relevant for relations and properties.
* However, the method is still written in a generic way.
*/
private enrichImplications(element: Element) {
Expand Down Expand Up @@ -180,7 +180,7 @@ export class ConstraintEnricher {
}
}

// TODO: Ensure that inputs are consumed
// TODO: Ensure that inputs are consumed (not required due to pruning?)

/**
* Ensure that outputs are unique per name
Expand All @@ -191,8 +191,18 @@ export class ConstraintEnricher {
}
}

// TODO: Ensure that outputs are produced
// TODO: Ensure that outputs are produced (not required due to pruning?)

// TODO: Ensure that relations are unique per name
/**
* Ensure that relations are unique per name
*/
if (this.graph.options.constraints.uniqueRelation) {
for (const node of this.graph.nodes) {
for (const [_name, relations] of node.outgoingMap) {
const consequence = {amo: relations.map(it => it.id)}
this.graph.addConstraint({implies: [node.id, consequence]})
}
}
}
}
}
7 changes: 7 additions & 0 deletions src/graph/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ class ConstraintsOptions extends BaseOptions {
readonly uniqueArtifact: boolean
readonly uniqueInput: boolean
readonly uniqueOutput: boolean
readonly uniqueRelation: boolean

readonly requiredArtifact: boolean
readonly requiredIncomingRelation: boolean
Expand Down Expand Up @@ -890,6 +891,9 @@ class ConstraintsOptions extends BaseOptions {
this.uniqueOutput = this.raw.unique_output_constraint ?? this.constraints
assert.isBoolean(this.uniqueOutput)

this.uniqueRelation = this.raw.unique_relation_constraint ?? this.constraints
assert.isBoolean(this.uniqueRelation)

this.requiredIncomingRelation = this.raw.required_incoming_relation_constraint ?? this.constraints
assert.isBoolean(this.requiredIncomingRelation)
} else {
Expand All @@ -914,6 +918,9 @@ class ConstraintsOptions extends BaseOptions {
this.uniqueOutput = this.raw.unique_output_constraint ?? this.raw.constraints ?? true
assert.isBoolean(this.uniqueOutput)

this.uniqueRelation = this.raw.unique_relation_constraint ?? this.raw.constraints ?? true
assert.isBoolean(this.uniqueRelation)

this.requiredIncomingRelation =
this.raw.required_incoming_relation_constraint ?? this.raw.constraints ?? false
assert.isBoolean(this.requiredIncomingRelation)
Expand Down
1 change: 1 addition & 0 deletions src/specification/variability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type ConstraintsOptions = {
unique_artifact_constraint?: boolean
unique_input_constraint?: boolean
unique_output_constraint?: boolean
unique_relation_constraint?: boolean

required_artifact_constraint?: boolean
required_incoming_relation_constraint?: boolean
Expand Down
15 changes: 15 additions & 0 deletions tests/conformance/relationships/ambigous-disabled/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tosca_definitions_version: tosca_simple_yaml_1_3

topology_template:
node_templates:
one:
type: one
requirements:
- ambiguous_relation: two
- ambiguous_relation: three

two:
type: two

three:
type: three
23 changes: 23 additions & 0 deletions tests/conformance/relationships/ambigous-disabled/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
tosca_definitions_version: tosca_variability_1_0_rc_3

topology_template:
variability:
options:
unique_relation_constraint: false

node_templates:
one:
type: one
persistent: true
managed: false
requirements:
- ambiguous_relation: two
- ambiguous_relation: three

two:
type: two
managed: false

three:
type: three
managed: false
19 changes: 19 additions & 0 deletions tests/conformance/relationships/ambigous-throw/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
tosca_definitions_version: tosca_variability_1_0_rc_3

topology_template:
node_templates:
one:
type: one
persistent: true
managed: false
requirements:
- ambiguous_relation: two
- ambiguous_relation: three

two:
type: two
managed: false

three:
type: three
managed: false
1 change: 1 addition & 0 deletions tests/conformance/relationships/ambigous-throw/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: Could not solve

0 comments on commit 4ea4429

Please sign in to comment.