-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeMapper.cs
54 lines (43 loc) · 2.07 KB
/
EdgeMapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using QuikGraph;
namespace GraphRewriteEngine {
public class EdgeMapper: IMatcher { //Class for when pattern is a K2 graph
public List<Morphism> morphisms; //should this be a thing? Should mappers just compute and not store?
public EdgeMapper() {
this.morphisms = new List<Morphism>();
}
public void EdgeSearch(BidirectionalGraph<Node, LEdge> pattern, BidirectionalGraph<Node, LEdge> host, bool searchAll) {
LEdge A = pattern.Edges.First();
if (!searchAll) {
LEdge e = host.Edges.FirstOrDefault(x => x.IsEquivalent(A));
if (e != default(LEdge)) {
NodeMapping vm = new NodeMapping(new Dictionary<Node, Node>() {{A.Source, e.Source}, {A.Target, e.Target}});
EdgeMapping em = new EdgeMapping(new Dictionary<LEdge, LEdge>() {{A, e}});
morphisms.Add(new Morphism(vm, em));
}
return;
}
foreach(var e in host.Edges) {
if (e.IsEquivalent(A)) {
NodeMapping vm = new NodeMapping(new Dictionary<Node, Node>() {{A.Source, e.Source}, {A.Target, e.Target}});
EdgeMapping em = new EdgeMapping(new Dictionary<LEdge, LEdge>() {{A, e}});
morphisms.Add(new Morphism(vm, em));
}
}
}
public Morphism Find(BidirectionalGraph<Node, LEdge> pattern, BidirectionalGraph<Node, LEdge> host) {
EdgeSearch(pattern, host, false);
return morphisms.FirstOrDefault();
}
public IList<Morphism> Enumerate(BidirectionalGraph<Node, LEdge> pattern, BidirectionalGraph<Node, LEdge> host, int iter = 0) {
EdgeSearch(pattern, host, true);
return morphisms;
}
public bool Exists(BidirectionalGraph<Node, LEdge> pattern, BidirectionalGraph<Node, LEdge> host) {
EdgeSearch(pattern, host, false);
return morphisms.Count > 0;
}
}
}