Skip to content

Commit

Permalink
Fix for bug #1774:
Browse files Browse the repository at this point in the history
- Better sorting of elements in Inst.instElementList by considering qualified crefs.
- Added testcase Modelica.StateGraph.Examples.ControlledTanks.
- Enabled the forgotten Modelica.Blocks.Continuous testcase.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12666 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Aug 24, 2012
1 parent bb341c9 commit 8f300e1
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 63 deletions.
36 changes: 22 additions & 14 deletions Compiler/FrontEnd/Inst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5619,28 +5619,18 @@ algorithm
String id;
ElementList all_el, accum_el;
tuple<SCode.Element, DAE.Mod> e;
Absyn.ComponentRef cref;

case ((exp as Absyn.CREF(componentRef = Absyn.CREF_IDENT(name = id)), (all_el, accum_el)))
case ((exp as Absyn.CREF(componentRef = cref), (all_el, accum_el)))
equation
id = Absyn.crefFirstIdent(cref);
// Try and delete the element with the given name from the list of all
// elements. If this succeeds, add it to the list of elements. This
// ensures that we don't add any dependency more than once.
(all_el, SOME(e)) = List.deleteMemberOnTrue(id, all_el, isElementNamed);
then
((exp, (all_el, e :: accum_el)));

/* this doesn't work yet as we don't know the structure of a component reference!
For example (false positive):
Error: Cyclically dependent constants or parameters found in scope Modelica.Electrical.Machines.Examples.TransformerTestbench: {transformer,transformerData}
case ((exp as Absyn.CREF(componentRef = Absyn.CREF_QUAL(name = id)), (all_el, accum_el)))
equation
// Try and delete the element with the given name from the list of all
// elements. If this succeeds, add it to the list of elements. This
// ensures that we don't add any dependency more than once.
(all_el, SOME(e)) = List.deleteMemberOnTrue(id, all_el, isElementNamed);
then
((exp, (all_el, e :: accum_el)));*/

else then inTuple;
end matchcontinue;
end getElementDependenciesTraverserEnter;
Expand Down Expand Up @@ -5735,12 +5725,14 @@ algorithm
list<list<String>> names;
list<String> cycles_strs;
String cycles_str, scope_str;
list<tuple<Element, list<Element>>> graph;

case ({}, _) then ();

case (_, _)
equation
{} = Graph.findCycles(inCycles, isElementEqual);
graph = Graph.filterGraph(inCycles, isElementParamOrConst);
{} = Graph.findCycles(graph, isElementEqual);
then
();

Expand All @@ -5758,6 +5750,22 @@ algorithm
end matchcontinue;
end checkCyclicalComponents;

protected function isElementParamOrConst
input tuple<SCode.Element, DAE.Mod> inElement;
output Boolean outIsParamOrConst;
algorithm
outIsParamOrConst := match(inElement)
local
SCode.Element elem;
SCode.Variability var;

case ((SCode.COMPONENT(attributes = SCode.ATTR(variability = var)), _))
then SCode.isParameterOrConst(var);

else false;
end match;
end isElementParamOrConst;

protected function elementName
"Returns the name of the given element."
input tuple<SCode.Element, DAE.Mod> inElement;
Expand Down
93 changes: 50 additions & 43 deletions Compiler/Util/Graph.mo
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ protected import Error;
protected import List;
protected import Util;


public
replaceable type NodeType subtypeof Any;
replaceable type ArgType subtypeof Any;

public function buildGraph
"This function will build a graph given a list of nodes, an edge function, and
an extra argument to the edge function. The edge function should generate a
Expand All @@ -55,9 +58,6 @@ public function buildGraph
input ArgType inEdgeArg;
output list<tuple<NodeType, list<NodeType>>> outGraph;

replaceable type NodeType subtypeof Any;
replaceable type ArgType subtypeof Any;

partial function EdgeFunc
input NodeType inNode;
input ArgType inArg;
Expand All @@ -83,8 +83,6 @@ public function topologicalSort
output list<NodeType> outNodes;
output list<tuple<NodeType, list<NodeType>>> outRemainingGraph;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand All @@ -110,8 +108,6 @@ protected function topologicalSort2
output list<NodeType> outNodes;
output list<tuple<NodeType, list<NodeType>>> outRemainingGraph;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -163,8 +159,6 @@ protected function hasOutgoingEdges
"Returns true if the given node has no outgoing edges, otherwise false."
input tuple<NodeType, list<NodeType>> inNode;
output Boolean outHasOutEdges;

replaceable type NodeType subtypeof Any;
algorithm
outHasOutEdges := match(inNode)
case ((_, {})) then false;
Expand All @@ -180,8 +174,6 @@ protected function removeEdge
input EqualFunc inEqualFunc;
output tuple<NodeType, list<NodeType>> outNode;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -211,8 +203,6 @@ public function findCycles
input EqualFunc inEqualFunc;
output list<list<NodeType>> outCycles;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand All @@ -230,8 +220,6 @@ public function findCycles2
input EqualFunc inEqualFunc;
output list<list<NodeType>> outCycles;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -282,8 +270,6 @@ protected function findCycleForNode
input EqualFunc inEqualFunc;
output Option<list<NodeType>> outCycle;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -332,8 +318,6 @@ protected function findCycleForNode2
input EqualFunc inEqualFunc;
output list<NodeType> outCycle;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -375,8 +359,6 @@ protected function findNodeInGraph
input EqualFunc inEqualFunc;
output tuple<NodeType, list<NodeType>> outNode;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -411,8 +393,6 @@ protected function findIndexofNodeInGraph
input Integer inIndex;
output Integer outIndex;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -446,8 +426,6 @@ protected function removeNodesFromGraph
input EqualFunc inEqualFunc;
output list<tuple<NodeType, list<NodeType>>> outGraph;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -489,8 +467,6 @@ public function transposeGraph
input EqualFunc inEqualFunc;
output list<tuple<NodeType, list<NodeType>>> outGraph;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -529,8 +505,6 @@ protected function insertNodetoGraph
input list<tuple<NodeType, list<NodeType>>> inGraph;
output list<tuple<NodeType, list<NodeType>>> outGraph;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -568,8 +542,6 @@ public function allReachableNodes
input EqualFunc inEqualFunc;
output list<NodeType> reachableNodes;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -645,9 +617,6 @@ color[ui ] <- min{c > 0 : forbiddenColors[c] = ui }
input list<NodeType> inNode1;
input String inName;
end PrintFunc;

replaceable type NodeType subtypeof Any;

algorithm
outColored := matchcontinue(toColorNodes, inforbiddenColor, inColors, inGraph, inGraphT, inColored, inEqualFunc, inPrintFunc)
local
Expand Down Expand Up @@ -683,8 +652,6 @@ protected function addForbiddenColors
input PrintFunc inPrintFunc;
output array<Option<list<NodeType>>> outForbiddenColor;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -785,8 +752,6 @@ protected function arrayFindMinColorIndex
input PrintFunc inPrintFunc;
output Integer outColor;

replaceable type NodeType subtypeof Any;

partial function EqualFunc
"Given two nodes, returns true if they are equal, otherwise false."
input NodeType inNode1;
Expand Down Expand Up @@ -831,8 +796,6 @@ public function printGraph
input NodeToString inPrintFunc;
output String outString;

replaceable type NodeType subtypeof Any;

partial function NodeToString
input NodeType inNode;
output String outString;
Expand All @@ -846,8 +809,6 @@ public function printNode
input NodeToString inPrintFunc;
output String outString;

replaceable type NodeType subtypeof Any;

partial function NodeToString
input NodeType inNode;
output String outString;
Expand Down Expand Up @@ -1082,5 +1043,51 @@ algorithm
end matchcontinue;
end arrayFindMinColorIndexInt;

public function filterGraph
"Removes any node for which the given function evaluates to false, as well as
any edge pointing at that node."
input list<tuple<NodeType, list<NodeType>>> inGraph;
input CondFunc inCondFunc;
output list<tuple<NodeType, list<NodeType>>> outGraph;

partial function CondFunc
input NodeType inNode;
output Boolean outCond;
end CondFunc;
algorithm
outGraph := List.accumulateMapAccum1(inGraph, filterGraph2, inCondFunc, {});
end filterGraph;

protected function filterGraph2
"Helper function to filterGraph."
input tuple<NodeType, list<NodeType>> inNode;
input CondFunc inCondFunc;
input list<tuple<NodeType, list<NodeType>>> inAccumGraph;
output list<tuple<NodeType, list<NodeType>>> outNode;

partial function CondFunc
input NodeType inNode;
output Boolean outCond;
end CondFunc;
algorithm
outNode := matchcontinue(inNode, inCondFunc, inAccumGraph)
local
NodeType node;
list<NodeType> edges;

case ((node, _), _, _)
equation
false = inCondFunc(node);
then
inAccumGraph;

case ((node, edges), _, _)
equation
edges = List.filterOnTrue(edges, inCondFunc);
then
(node, edges) :: inAccumGraph;

end matchcontinue;
end filterGraph2;

end Graph;
48 changes: 42 additions & 6 deletions Compiler/Util/List.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6462,7 +6462,8 @@ algorithm
end filter1rOnTrue_tail;

public function removeOnTrue
"Go through a list and when the given function is true, remove that element."
"Goes through a list and removes all elements which are equal to the given
value, using the given comparison function."
input ValueType inValue;
input CompFunc inCompFunc;
input list<ElementType> inList;
Expand Down Expand Up @@ -7362,11 +7363,13 @@ public function accumulateMap
output list<ElementOutType> outList;
end MapFunc;
algorithm
outList := accumulateMap_impl(inList, inMapFunc, {});
outList := accumulateMapAccum(inList, inMapFunc, {});
end accumulateMap;

protected function accumulateMap_impl
"The actual implementation of accumulateMap."
public function accumulateMapAccum
"Takes a list, a function and a result list. The function is applied to each
element of the list, and the function is itself responsible for adding
elements to the result list."
input list<ElementInType> inList;
input MapFunc inMapFunc;
input list<ElementOutType> inAccumList;
Expand All @@ -7388,10 +7391,43 @@ algorithm
equation
accum = inMapFunc(e, accum);
then
accumulateMap_impl(rest_e, inMapFunc, accum);
accumulateMapAccum(rest_e, inMapFunc, accum);

case ({}, _, _) then inAccumList;
end match;
end accumulateMap_impl;
end accumulateMapAccum;

public function accumulateMapAccum1
"Takes a list, a function, an extra argument, and a result list. The function
is applied to each element of the list, and the function is itself responsible
for adding elements to the result list."
input list<ElementInType> inList;
input MapFunc inMapFunc;
input ArgType1 inArg;
input list<ElementOutType> inAccumList;
output list<ElementOutType> outList;

partial function MapFunc
input ElementInType inElement;
input ArgType1 inArg;
input list<ElementOutType> inAccumList;
output list<ElementOutType> outList;
end MapFunc;
algorithm
outList := match(inList, inMapFunc, inArg, inAccumList)
local
ElementInType e;
list<ElementInType> rest_e;
list<ElementOutType> accum;

case (e :: rest_e, _, _, accum)
equation
accum = inMapFunc(e, inArg, accum);
then
accumulateMapAccum1(rest_e, inMapFunc, inArg, accum);

case ({}, _, _, _) then inAccumList;
end match;
end accumulateMapAccum1;

end List;

0 comments on commit 8f300e1

Please sign in to comment.