-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from typing import List | ||
|
||
from hebg import HEBGraph, Action, FeatureCondition, Behavior | ||
|
||
|
||
class ReachForest(Behavior): | ||
"""Reach forest""" | ||
|
||
def __init__(self) -> None: | ||
"""Reach forest""" | ||
super().__init__("Reach forest") | ||
|
||
def build_graph(self) -> HEBGraph: | ||
graph = HEBGraph(self) | ||
is_in_other_zone = FeatureCondition("Is in other zone ?") | ||
graph.add_edge(is_in_other_zone, Behavior("Reach other zone"), index=False) | ||
graph.add_edge(is_in_other_zone, Action("> forest"), index=True) | ||
is_in_other_zone = FeatureCondition("Is in meadow ?") | ||
graph.add_edge(is_in_other_zone, Behavior("Reach meadow"), index=False) | ||
graph.add_edge(is_in_other_zone, Action("> forest"), index=True) | ||
return graph | ||
|
||
|
||
class ReachOtherZone(Behavior): | ||
"""Reach other zone""" | ||
|
||
def __init__(self) -> None: | ||
"""Reach other zone""" | ||
super().__init__("Reach other zone") | ||
|
||
def build_graph(self) -> HEBGraph: | ||
graph = HEBGraph(self) | ||
is_in_forest = FeatureCondition("Is in forest ?") | ||
graph.add_edge(is_in_forest, Behavior("Reach forest"), index=False) | ||
graph.add_edge(is_in_forest, Action("> other zone"), index=True) | ||
is_in_other_zone = FeatureCondition("Is in meadow ?") | ||
graph.add_edge(is_in_other_zone, Behavior("Reach meadow"), index=False) | ||
graph.add_edge(is_in_other_zone, Action("> other zone"), index=True) | ||
return graph | ||
|
||
|
||
class ReachMeadow(Behavior): | ||
"""Reach meadow""" | ||
|
||
def __init__(self) -> None: | ||
"""Reach meadow""" | ||
super().__init__("Reach meadow") | ||
|
||
def build_graph(self) -> HEBGraph: | ||
graph = HEBGraph(self) | ||
is_in_forest = FeatureCondition("Is in forest ?") | ||
graph.add_edge(is_in_forest, Behavior("Reach forest"), index=False) | ||
graph.add_edge(is_in_forest, Action("> meadow"), index=True) | ||
is_in_other_zone = FeatureCondition("Is in other zone ?") | ||
graph.add_edge(is_in_other_zone, Behavior("Reach other zone"), index=False) | ||
graph.add_edge(is_in_other_zone, Action("> meadow"), index=True) | ||
return graph | ||
|
||
|
||
def build_looping_behaviors() -> List[Behavior]: | ||
behaviors: List[Behavior] = [ | ||
ReachForest(), | ||
ReachOtherZone(), | ||
ReachMeadow(), | ||
] | ||
all_behaviors = {behavior.name: behavior for behavior in behaviors} | ||
for behavior in behaviors: | ||
behavior.graph.all_behaviors = all_behaviors | ||
return behaviors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
import pytest_check as check | ||
|
||
import networkx as nx | ||
from hebg import HEBGraph | ||
from hebg.unrolling import unroll_graph | ||
|
||
from tests.examples.behaviors.loop_without_alternative import build_looping_behaviors | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
|
||
class TestLoop: | ||
"""Tests for the loop example""" | ||
|
||
@pytest.fixture(autouse=True) | ||
def setup_method(self): | ||
( | ||
self.reach_forest, | ||
self.reach_other_zone, | ||
self.reach_meadow, | ||
) = build_looping_behaviors() | ||
|
||
@pytest.mark.xfail | ||
def test_unroll_reach_forest(self): | ||
draw = False | ||
unrolled_graph = unroll_graph( | ||
self.reach_forest.graph, | ||
add_prefix=True, | ||
cut_looping_alternatives=True, | ||
) | ||
if draw: | ||
_plot_graph(unrolled_graph) | ||
|
||
expected_graph = nx.DiGraph() | ||
check.is_true(nx.is_isomorphic(unrolled_graph, expected_graph)) | ||
|
||
|
||
def _plot_graph(graph: "HEBGraph"): | ||
_, ax = plt.subplots() | ||
pos = None | ||
if len(graph.roots) == 0: | ||
pos = nx.spring_layout(graph) | ||
graph.draw(ax, pos=pos) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters