Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 15177bd

Browse files
adrpoOpenModelica-Hudson
authored andcommitted
[NF] overconstrained connection graph (OCG)
- creating and breaking of the OCG is working - replacement of broken connect equation with the equalityConstraint call is missing - update common Belonging to [master]: - #2578
1 parent c5bc6e5 commit 15177bd

File tree

16 files changed

+2350
-9
lines changed

16 files changed

+2350
-9
lines changed

Compiler/FrontEnd/SCodeUtil.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ algorithm
9090
System.setHasInnerOuterDefinitions(false);
9191
// set the external flag that signals the presence of expandable connectors in the model
9292
System.setHasExpandableConnectors(false);
93+
// set the external flag that signals the presence of overconstrained connectors in the model
94+
System.setHasOverconstrainedConnectors(false);
9395
// set the external flag that signals the presence of expandable connectors in the model
9496
System.setHasStreamConnectors(false);
9597

Compiler/NFFrontEnd/NFClass.mo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import Expression = NFExpression;
4646
protected
4747
import NFBinding.Binding;
4848
import ComplexType = NFComplexType;
49+
import System;
4950

5051
public
5152

@@ -499,6 +500,8 @@ uniontype Class
499500
algorithm
500501
try
501502
lookupElement("equalityConstraint", cls);
503+
// set the external flag that signals the presence of expandable connectors in the model
504+
System.setHasOverconstrainedConnectors(true);
502505
isOverdetermined := true;
503506
else
504507
isOverdetermined := false;

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,26 @@ public
551551
end match;
552552
end isEqual;
553553

554+
function isPrefix
555+
input ComponentRef cref1;
556+
input ComponentRef cref2;
557+
output Boolean isPrefix;
558+
algorithm
559+
if referenceEq(cref1, cref2) then
560+
isPrefix := true;
561+
return;
562+
end if;
563+
564+
isPrefix := match (cref1, cref2)
565+
case (CREF(), CREF())
566+
then
567+
if InstNode.name(cref1.node) == InstNode.name(cref2.node) then
568+
isEqual(cref1.restCref, cref2.restCref)
569+
else isEqual(cref1, cref2.restCref);
570+
else false;
571+
end match;
572+
end isPrefix;
573+
554574
function toDAE
555575
input ComponentRef cref;
556576
output DAE.ComponentRef dcref;
@@ -613,6 +633,13 @@ public
613633
end match;
614634
end toString;
615635

636+
function listToString
637+
input list<ComponentRef> crs;
638+
output String str;
639+
algorithm
640+
str := "{" + stringDelimitList(List.map(crs, toString), ",") + "}";
641+
end listToString;
642+
616643
function hash
617644
input ComponentRef cref;
618645
input Integer mod;

Compiler/NFFrontEnd/NFConnectionSets.mo

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@
3131

3232
encapsulated package NFConnectionSets
3333
import DisjointSets;
34+
import ComponentRef = NFComponentRef;
3435
import Connector = NFConnector;
3536
import Connection = NFConnection;
3637
import Connections = NFConnections;
38+
import BrokenEdges = NFConnections.BrokenEdges;
3739

3840
protected
3941
import Flags;
4042
import List;
43+
import NFOCConnectionGraph;
4144

4245
public
4346
package ConnectionSets
@@ -73,7 +76,7 @@ package ConnectionSets
7376
end if;
7477

7578
// Add the connections.
76-
sets := List.fold(connections.connections, addConnection, sets);
79+
sets := List.fold1(connections.connections, addConnection, connections.broken, sets);
7780
end fromConnections;
7881

7982
function addScalarConnector
@@ -95,6 +98,7 @@ package ConnectionSets
9598
"Adds a connection to the sets, which means merging the two sets that the
9699
connectors belong to, unless they already belong to the same set."
97100
input Connection connection;
101+
input BrokenEdges broken;
98102
input output ConnectionSets.Sets sets;
99103
protected
100104
Connector lhs, rhs, c2;
@@ -111,11 +115,41 @@ package ConnectionSets
111115
// when collecting the connections, but if the connectors themselves
112116
// contain connectors that have been deleted we need to remove them here.
113117
if not (Connector.isDeleted(c1) or Connector.isDeleted(c2)) then
114-
sets := merge(c1, c2, sets);
118+
if listEmpty(broken) then
119+
sets := merge(c1, c2, sets);
120+
elseif isBroken(c1, c2, broken) then
121+
// do nothing
122+
// print("Ignore broken: connect(" + Connector.toString(c1) + ", " + Connector.toString(c2) + ")\n");
123+
else
124+
sets := merge(c1, c2, sets);
125+
end if;
115126
end if;
116127
end for;
117128
end addConnection;
118129

130+
function isBroken
131+
input Connector c1, c2;
132+
input BrokenEdges broken;
133+
output Boolean b = false;
134+
protected
135+
ComponentRef lhs, rhs, cr1, cr2;
136+
algorithm
137+
cr1 := Connector.name(c1);
138+
cr2 := Connector.name(c2);
139+
// print("Check: connect(" + ComponentRef.toString(cr1) + ", " + ComponentRef.toString(cr2) + ")\n");
140+
141+
for c in broken loop
142+
((lhs, rhs, _)) := c;
143+
144+
if ComponentRef.isPrefix(lhs, cr1) and ComponentRef.isPrefix(rhs, cr2) or
145+
ComponentRef.isPrefix(lhs, cr2) and ComponentRef.isPrefix(rhs, cr1)
146+
then
147+
b := true;
148+
break;
149+
end if;
150+
end for;
151+
end isBroken;
152+
119153
annotation(__OpenModelica_Interface="frontend");
120154
end ConnectionSets;
121155

Compiler/NFFrontEnd/NFConnections.mo

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ encapsulated uniontype NFConnections
3333
import Connection = NFConnection;
3434
import Connector = NFConnector;
3535
import FlatModel = NFFlatModel;
36+
import ComponentRef = NFComponentRef;
37+
import Equation = NFEquation;
3638

3739
protected
38-
import Equation = NFEquation;
39-
import ComponentRef = NFComponentRef;
4040
import Connections = NFConnections;
4141
import NFComponent.Component;
4242
import NFInstNode.InstNode;
@@ -47,13 +47,17 @@ protected
4747
import ExpandExp = NFExpandExp;
4848

4949
public
50+
type BrokenEdge = tuple<ComponentRef, ComponentRef, list<Equation>>;
51+
type BrokenEdges = list<BrokenEdge>;
52+
5053
record CONNECTIONS
5154
list<Connection> connections;
5255
list<Connector> flows;
56+
BrokenEdges broken;
5357
end CONNECTIONS;
5458

5559
function new
56-
output Connections conns = CONNECTIONS({}, {});
60+
output Connections conns = CONNECTIONS({}, {}, {});
5761
end new;
5862

5963
function addConnection
@@ -70,6 +74,13 @@ public
7074
conns.flows := conn :: conns.flows;
7175
end addFlow;
7276

77+
function addBroken
78+
input BrokenEdges broken;
79+
input output Connections conns;
80+
algorithm
81+
conns.broken := broken;
82+
end addBroken;
83+
7384
function collect
7485
input output FlatModel flatModel;
7586
output Connections conns = new();

Compiler/NFFrontEnd/NFConnector.mo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ public
129129
conn1.face == conn2.face;
130130
end isEqual;
131131

132+
function isPrefix
133+
input Connector conn1;
134+
input Connector conn2;
135+
output Boolean isPrefix = ComponentRef.isPrefix(conn1.name, conn2.name);
136+
end isPrefix;
137+
132138
function isOutside
133139
input Connector conn;
134140
output Boolean isOutside;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import NFClassTree.ClassTree;
6262
import NFComponent.Component;
6363
import NFModifier.Modifier;
6464
import Sections = NFSections;
65+
import NFOCConnectionGraph;
6566
import Prefixes = NFPrefixes;
6667
import NFPrefixes.Visibility;
6768
import RangeIterator = NFRangeIterator;
@@ -870,6 +871,7 @@ algorithm
870871
end addElementSourceArrayPrefix;
871872

872873
function resolveConnections
874+
"Generates the connect equations and adds them to the equation list"
873875
input output FlatModel flatModel;
874876
input String name;
875877
protected
@@ -878,17 +880,31 @@ protected
878880
ConnectionSets.Sets csets;
879881
array<list<Connector>> csets_array;
880882
CardinalityTable.Table ctable;
883+
Connections.BrokenEdges broken = {};
881884
algorithm
882-
// Generate the connect equations and add them to the equation list.
885+
// handle overconstrained connections
886+
// - build the graph
887+
// - evaluate the Connections.* operators
888+
// - generate the equations to replace the broken connects
889+
// - return the broken connects + the equations
890+
if System.getHasOverconstrainedConnectors() then
891+
(flatModel, broken) := NFOCConnectionGraph.handleOverconstrainedConnections(flatModel, name);
892+
end if;
893+
// get the connections from the model
883894
(flatModel, conns) := Connections.collect(flatModel);
895+
// add the broken connections
896+
conns := Connections.addBroken(broken, conns);
897+
// build the sets, check the broken connects
884898
csets := ConnectionSets.fromConnections(conns);
885899
csets_array := ConnectionSets.extractSets(csets);
900+
// generated the equations
886901
conn_eql := ConnectEquations.generateEquations(csets_array);
902+
// add the equations to the flat model
887903
flatModel.equations := listAppend(conn_eql, flatModel.equations);
888904
ctable := CardinalityTable.fromConnections(conns);
889905

890906
// Evaluate any connection operators if they're used.
891-
if System.getHasStreamConnectors() or System.getUsesCardinality() then
907+
if System.getHasStreamConnectors() or System.getUsesCardinality() then
892908
flatModel := evaluateConnectionOperators(flatModel, csets, csets_array, ctable);
893909
end if;
894910

Compiler/NFFrontEnd/NFHashTable.mo

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the Open Source Modelica
17+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
18+
* from OSMC, either from the above address,
19+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20+
* http://www.openmodelica.org, and in the OpenModelica distribution.
21+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22+
*
23+
* This program is distributed WITHOUT ANY WARRANTY; without
24+
* even the implied warranty of MERCHANTABILITY or FITNESS
25+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27+
*
28+
* See the full OSMC Public License conditions for more details.
29+
*
30+
*/
31+
32+
encapsulated package NFHashTable
33+
34+
/* Below is the instance specific code. For each hashtable the user must define:
35+
36+
Key - The key used to uniquely define elements in a hashtable
37+
Value - The data to associate with each key
38+
hashFunc - A function that maps a key to a positive integer.
39+
keyEqual - A comparison function between two keys, returns true if equal.
40+
*/
41+
42+
/* NFHashTable instance specific code */
43+
44+
public import BaseHashTable;
45+
import ComponentRef = NFComponentRef;
46+
47+
public type Key = ComponentRef;
48+
public type Value = Integer;
49+
50+
public type HashTableCrefFunctionsType = tuple<FuncHashCref, FuncCrefEqual, FuncCrefStr, FuncExpStr>;
51+
public type HashTable = tuple<array<list<tuple<Key, Integer>>>,
52+
tuple<Integer, Integer, array<Option<tuple<Key, Value>>>>,
53+
Integer,
54+
HashTableCrefFunctionsType>;
55+
56+
partial function FuncHashCref
57+
input Key cr;
58+
input Integer mod;
59+
output Integer res;
60+
end FuncHashCref;
61+
62+
partial function FuncCrefEqual
63+
input Key cr1;
64+
input Key cr2;
65+
output Boolean res;
66+
end FuncCrefEqual;
67+
68+
partial function FuncCrefStr
69+
input Key cr;
70+
output String res;
71+
end FuncCrefStr;
72+
73+
partial function FuncExpStr
74+
input Value exp;
75+
output String res;
76+
end FuncExpStr;
77+
78+
public function emptyHashTable
79+
"
80+
Returns an empty HashTable.
81+
Using the default bucketsize..
82+
"
83+
output HashTable hashTable;
84+
algorithm
85+
hashTable := emptyHashTableSized(BaseHashTable.defaultBucketSize);
86+
end emptyHashTable;
87+
88+
public function emptyHashTableSized
89+
"Returns an empty HashTable.
90+
Using the bucketsize size"
91+
input Integer size;
92+
output HashTable hashTable;
93+
algorithm
94+
hashTable := BaseHashTable.emptyHashTableWork(size,(ComponentRef.hash,ComponentRef.isEqual,ComponentRef.toString,intString));
95+
end emptyHashTableSized;
96+
97+
annotation(__OpenModelica_Interface="frontend");
98+
end NFHashTable;

0 commit comments

Comments
 (0)