Skip to content

Commit 8869624

Browse files
author
MrsSima
committed
xx2
1 parent aebc6d1 commit 8869624

File tree

11 files changed

+141
-67
lines changed

11 files changed

+141
-67
lines changed

ConstraintsMatcher/ConstraintsMatcher.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35+
<Reference Include="QuickGraph">
36+
<HintPath>..\packages\QuickGraphPCL\lib\portable-win+net4+sl5+wp8+win8+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\QuickGraph.dll</HintPath>
37+
</Reference>
38+
<Reference Include="QuickGraph.Graphviz">
39+
<HintPath>..\packages\QuickGraphPCL\lib\portable-win+net4+sl5+wp8+win8+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\QuickGraph.Graphviz.dll</HintPath>
40+
</Reference>
3541
<Reference Include="System" />
3642
<Reference Include="System.Core" />
3743
<Reference Include="System.Xml.Linq" />

ConstraintsMatcher/Matcher.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace ConstraintsMatcher
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Text.RegularExpressions;
89

910
public class Matcher
1011
{
@@ -30,6 +31,7 @@ public Matcher(IModel model)
3031
public bool PreCheck(IModel constraintsModel)
3132
{
3233
var Nodes = new List<INode>();
34+
//Find the root
3335
foreach (var node in constraintsModel.Nodes)
3436
{
3537
var inEdges = constraintsModel.Edges.Where(x => x.To == node).ToList();
@@ -44,11 +46,13 @@ public bool PreCheck(IModel constraintsModel)
4446
return false;
4547
}
4648
this.root = Nodes.FirstOrDefault();
49+
// Check if root is correct
4750
if ((this.root.Class.Name == "NotNode") || (this.root.Class.Name == "OrNode") || (this.root.Class.Name == "NoNodes"))
4851
{
49-
this.ErrorMsg = "The root should be NodeType from modrl or AllNodes.";
52+
this.ErrorMsg = "The root should be NodeType from model or AllNodes.";
5053
return false;
5154
}
55+
// Check ther there is only one outcoming edge from notNode
5256
var notNodes = constraintsModel.Nodes.Where(x => x.Class.Name == "NotNode");
5357
foreach(var notNode in notNodes)
5458
{
@@ -57,13 +61,42 @@ public bool PreCheck(IModel constraintsModel)
5761
return false;
5862
}
5963
}
60-
//TODO check if tree
64+
// Check if this constraint is a tree
65+
var visited = new HashSet<int>();
66+
if (!this.CheckIffTree(constraintsModel, this.root, visited))
67+
{
68+
this.ErrorMsg = "Constraints graph should have a tree structure.";
69+
return false;
70+
}
71+
6172
return true;
6273
}
6374

75+
public bool CheckIffTree(IModel constraintsModel, INode root, HashSet<int> visited)
76+
{
77+
visited.Add(root.GetHashCode());
78+
var outgoingEdges = constraintsModel.Edges.Where(x => x.From == root);
79+
foreach(var edge in outgoingEdges)
80+
{
81+
if (visited.Any(x => x == edge.To.GetHashCode()))
82+
{
83+
return false;
84+
}
85+
if (!CheckIffTree(constraintsModel, (INode)edge.To, visited))
86+
{
87+
return false;
88+
}
89+
}
90+
return true;
91+
}
6492
public bool Check(INode originRoot, INode constraintsRoot, IModel constraintsModel)
6593
{
66-
return this.InnerCheck(originRoot, constraintsRoot, constraintsModel);
94+
return this.InnerCheck(originRoot, constraintsRoot, constraintsModel);
95+
}
96+
97+
public bool AttrCheck(INode originRoot, INode constraintsRoot)
98+
{
99+
return this.AttributesAreIdentic(originRoot, constraintsRoot);
67100
}
68101

69102
private bool InnerCheck(INode targetRoot, INode constraintsRoot, IModel constraintsModel)
@@ -104,7 +137,7 @@ private bool InnerCheck(INode targetRoot, INode constraintsRoot, IModel constrai
104137
var constraintsToRoot = (INode)outConstraintsEdges.FirstOrDefault().To;
105138
var targetParentRoot = (INode)this.targetModel.Edges.Where(x => x.To == targetRoot).FirstOrDefault().From;
106139
var outParentTargetEdges = this.targetModel.Edges.Where(x => x.From == targetParentRoot);
107-
var innerCheck = outParentTargetEdges.Any(x => ElementsAreIdentic(constraintParentEdge, x) && this.InnerCheck((INode)x.To, constraintsToRoot, constraintsModel));
140+
var innerCheck = outParentTargetEdges.Any(x => ElementsAreIdentic(x, constraintParentEdge) && this.InnerCheck((INode)x.To, constraintsToRoot, constraintsModel));
108141
return !innerCheck;
109142
}
110143
if ((outConstraintsEdges.Count() > 0)&&(outConstraintsEdges.FirstOrDefault().To.Class.Name == "NoNodes"))
@@ -161,5 +194,19 @@ public bool ElementsAreIdentic(IElement firstElement, IElement secondElement)
161194
}
162195
return true;
163196
}
197+
198+
public bool AttributesAreIdentic(IElement targetElement, IElement constraintsElement)
199+
{
200+
foreach (var targetAttr in targetElement.Attributes)
201+
{
202+
var constraintsAttr = constraintsElement.Attributes.Where(x => x.Name == targetAttr.Name).FirstOrDefault();
203+
Regex regEx = new Regex(constraintsAttr.StringValue);
204+
if (!regEx.IsMatch(targetAttr.StringValue))
205+
{
206+
return false;
207+
}
208+
}
209+
return true;
210+
}
164211
}
165212
}

src/AirSim/View/Pictures/not.png

5.51 KB
Loading

src/EditorPluginInterfaces/PluginConfig.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ public class PluginConfig
7272
public Func FuncCreateConstraintsModel;
7373
public Func OnMainModelChangedFunction;
7474

75-
public delegate void Func1(bool modelName);
75+
public delegate void Func1(bool isVisible);
7676
public Func1 FuncChangeSelectorVisibility;
7777

78-
7978
public Action<String> OnMainModelChanged;
8079
/// <summary>
8180
/// Initializes a new instance of <see cref="PluginConfig"/>

src/Repo/ModelBuilders/ConstraintsMetamodelBuilder.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ type ConstraintsMetamodelBuilder() =
9393

9494
let allNodes = +("AllNodes", "View/Pictures/allNodes.png", false)
9595
let anyNodes = +("AnyNodes", "View/Pictures/timerBlock.png", false)
96-
let noNodes = +("NoNodes", "View/Pictures/timerBlock.png", false)
97-
let andNode = +("AndNode", "View/Pictures/timerBlock.png", false)
98-
let orNode = +("OrNode", "View/Pictures/timerBlock.png", false)
99-
let notNode = +("NotNode", "View/Pictures/timerBlock.png", false)
96+
let noNodes = +("NoNodes", "View/Pictures/no.png", false)
97+
let andNode = +("AndNode", "View/Pictures/and.png", false)
98+
let orNode = +("OrNode", "View/Pictures/or.png", false)
99+
let notNode = +("NotNode", "View/Pictures/not.png", false)
100100

101101
let link = abstractNode ---> (abstractNode, "target", "Link")
102102
infrastructure.Element.AddAttribute link "guard" "AttributeKind.String" ""

src/Repo/ModelBuilders/ConstraintsTestModelBuilder.fs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,11 @@ type ConstraintsTestModelBuilder() =
4545
let model = repo.CreateModel("ConstraintsTestModel", metamodel)
4646

4747
//let initialNode = infrastructure.Instantiate model metamodelInitialNode
48-
let finalNode = infrastructure.Instantiate model metamodelFinalNode
4948

50-
let motorsForward = infrastructure.Instantiate model metamodelMotorsForward
51-
infrastructure.Element.SetAttributeValue motorsForward "ports" "M3, M4"
52-
infrastructure.Element.SetAttributeValue motorsForward "power" "100"
49+
let motorsForw = infrastructure.Instantiate model metamodelMotorsForward
50+
infrastructure.Element.SetAttributeValue motorsForw "ports" "M3, M4"
51+
infrastructure.Element.SetAttributeValue motorsForw "power" "10*"
5352

54-
let motorsForward2 = infrastructure.Instantiate model metamodelMotorsForward
55-
infrastructure.Element.SetAttributeValue motorsForward2 "ports" "M3, M4"
56-
infrastructure.Element.SetAttributeValue motorsForward2 "power" "100"
57-
58-
let timer = infrastructure.Instantiate model metamodelTimer
59-
infrastructure.Element.SetAttributeValue timer "delay" "3000"
60-
61-
let notNode = infrastructure.Instantiate model metamodelNot
62-
63-
let orNodes = infrastructure.Instantiate model metamodelOr
64-
65-
let timer2 = infrastructure.Instantiate model metamodelTimer
66-
infrastructure.Element.SetAttributeValue timer2 "delay" "3000"
67-
68-
let timer3 = infrastructure.Instantiate model metamodelTimer
69-
infrastructure.Element.SetAttributeValue timer3 "delay" "3000"
70-
7153

7254
let (-->) (src: IElement) dst =
7355
let aLink = infrastructure.Instantiate model link :?> IAssociation
@@ -76,6 +58,5 @@ type ConstraintsTestModelBuilder() =
7658
dst
7759

7860
//initialNode -->
79-
finalNode --> timer --> motorsForward --> notNode --> timer2 |> ignore
80-
timer --> timer3 --> orNodes --> motorsForward2 |> ignore
61+
motorsForw |> ignore
8162
()

src/Repo/ModelBuilders/RobotsTestModelBuilder.fs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ type RobotsTestModelBuilder() =
4343
infrastructure.Element.SetAttributeValue motorsForward "ports" "M3, M4"
4444
infrastructure.Element.SetAttributeValue motorsForward "power" "100"
4545

46-
let motorsForward2 = infrastructure.Instantiate model metamodelMotorsForward
47-
infrastructure.Element.SetAttributeValue motorsForward2 "ports" "M3, M4"
48-
infrastructure.Element.SetAttributeValue motorsForward2 "power" "100"
49-
5046
let timer = infrastructure.Instantiate model metamodelTimer
5147
infrastructure.Element.SetAttributeValue timer "delay" "3000"
5248

@@ -66,7 +62,7 @@ type RobotsTestModelBuilder() =
6662
dst
6763

6864
//initialNode -->
69-
finalNode --> timer --> motorsForward |> ignore
65+
motorsForward |> ignore
7066

7167
//timer --> timer2 --> motorsForward --> finalNode |> ignore
7268
//motorsForward --> timer3 --> motorsForward2 |> ignore

src/WpfEditor/View/MainWindow.xaml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal partial class MainWindow : INotifyPropertyChanged
3939

4040
public event PropertyChangedEventHandler PropertyChanged;
4141

42-
public event Action<String> OnModelChanged;
42+
public event Action<string> OnModelChanged;
4343

4444
public AppConsoleViewModel Console { get; } = new AppConsoleViewModel();
4545

@@ -114,7 +114,7 @@ private void SelectModel(string modelName)
114114
this.model.ModelName = modelName;
115115
this.palette.InitPalette(this.model.ModelName);
116116
this.scene.Reload();
117-
this.OnModelChanged?.Invoke(modelName);
117+
this.OnModelChanged?.Invoke(this.model.ModelName);
118118
}
119119

120120
private void InitToolbar()
@@ -137,7 +137,6 @@ private void InitAndLaunchPlugins()
137137
{
138138
var dirs = new List<string>(System.IO.Directory.GetDirectories(plugindir + "/bin"));
139139
var config = new PluginConfig(this.model, this.scene, null, this.Console, this.temporaryElementProvider, this.constraintsGrid);
140-
//config.AddGrids(this.rightPanel, this.sceneGrid, this.paletteGrid);
141140
config.FuncChangeSelectorVisibility = (x) => { this.modelSelector.SelectorVisibility = x; };
142141
config.FuncCreateConstraintsModel = this.SelectModel;
143142
foreach (var dir in dirs)

src/plugins/ConstraintsPlugin/ConstraintsCheckSystem.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,69 @@ namespace ConstraintsPlugin
1010
class ConstraintsCheckSystem
1111
{
1212
// nodeName and modelName
13-
private List<Tuple<INode, IModel>> constraints;
13+
private List<Constraint> constraints;
1414

1515
public Repo.IRepo Repo { get; private set; }
1616
private Model targetModel;
17+
private IModel repoModel;
1718
private Matcher matcher;
1819
private Graph graph;
1920
public ConstraintsCheckSystem(Model targetModel, Graph graph)
2021
{
21-
this.constraints = new List<Tuple<INode, IModel>>();
22+
this.constraints = new List<Constraint>();
2223
this.Repo = global::Repo.RepoFactory.Create();
2324
this.targetModel = targetModel;
25+
this.repoModel = this.Repo.Model(targetModel.ModelName);
2426
this.graph = graph;
25-
this.matcher = new Matcher(this.Repo.Model(targetModel.ModelName));
27+
this.matcher = new Matcher(repoModel);
2628
}
2729

28-
public string AddConstraint(string constraintModelName)
30+
public string AddConstraint(string constraintModelName, int unitHash)
2931
{
3032
var constraintModel = this.Repo.Model(constraintModelName);
3133
if (!matcher.PreCheck(constraintModel))
3234
{
3335
throw new Exception(matcher.ErrorMsg);
3436
}
35-
this.constraints.Add(new Tuple<INode, IModel>(matcher.root, constraintModel));
37+
this.constraints.Add(new Constraint { Root = matcher.root, Tree = constraintModel, UnitHash = unitHash });
3638
return matcher.root.Name;
3739
}
3840

41+
public void DeleteConstraint(string constraintModelName, int unitHash)
42+
{
43+
var constraintModel = this.Repo.Model(constraintModelName);
44+
this.constraints.RemoveAll(x => x.UnitHash == unitHash);
45+
}
3946
public bool Check()
4047
{
4148
var result = true;
42-
foreach (var element in this.graph.DataGraph.Vertices)
49+
foreach (var node in this.repoModel.Nodes) //foreach (var element in this.graph.DataGraph.Vertices)
4350
{
44-
var node = element.Node;
51+
//var node = element.Node;
4552
foreach (var constraint in this.constraints)
4653
{
47-
if (this.matcher.ElementsAreIdentic(constraint.Item1, node) || constraint.Item1.Class.Name == "AllNodes")
54+
if (this.matcher.ElementsAreIdentic(constraint.Root, node) || constraint.Root.Class.Name == "AllNodes")
4855
{
49-
var isAllowed = matcher.Check(node, constraint.Item1, constraint.Item2);
50-
this.targetModel.SetElementAllowed(node, isAllowed);
56+
var isAllowed = matcher.Check(node, constraint.Root, constraint.Tree);
57+
//this.targetModel.SetElementAllowed(node, isAllowed);
58+
result = result && isAllowed;
59+
}
60+
if ((node.Class.Name == constraint.Root.Class.Name)&&(constraint.Tree.Nodes.Count() == 1))
61+
{
62+
var isAllowed = matcher.AttrCheck(node, constraint.Root);
63+
//this.targetModel.SetElementAllowed(node, isAllowed);
5164
result = result && isAllowed;
5265
}
5366
}
5467
}
5568
return result;
5669
}
5770
}
71+
72+
struct Constraint
73+
{
74+
public INode Root { get; set; }
75+
public IModel Tree { get; set; }
76+
public int UnitHash { get; set; }
77+
}
5878
}

0 commit comments

Comments
 (0)