2 * SubCircuit -- An implementation of the Ullmann Subgraph Isomorphism
3 * algorithm for coarse grain logic networks
5 * Copyright (C) 2013 Claire Xenia Wolf <claire@yosyshq.com>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 int nodeIdx
, portIdx
, bitIdx
;
38 BitRef(int nodeIdx
= -1, int portIdx
= -1, int bitIdx
= -1) : nodeIdx(nodeIdx
), portIdx(portIdx
), bitIdx(bitIdx
) { };
39 bool operator < (const BitRef
&other
) const;
43 std::set
<BitRef
> portBits
;
46 Edge() : constValue(0), isExtern(false) { };
51 PortBit() : edgeIdx(-1) { };
57 std::vector
<PortBit
> bits
;
58 Port() : minWidth(-1) { };
62 std::string nodeId
, typeId
;
63 std::map
<std::string
, int> portMap
;
64 std::vector
<Port
> ports
;
67 Node() : userData(NULL
), shared(false) { };
71 std::map
<std::string
, int> nodeMap
;
72 std::vector
<Node
> nodes
;
73 std::vector
<Edge
> edges
;
76 Graph() : allExtern(false) { };
77 Graph(const Graph
&other
, const std::vector
<std::string
> &otherNodes
);
79 void createNode(std::string nodeId
, std::string typeId
, void *userData
= NULL
, bool shared
= false);
80 void createPort(std::string nodeId
, std::string portId
, int width
= 1, int minWidth
= -1);
81 void createConnection(std::string fromNodeId
, std::string fromPortId
, int fromBit
, std::string toNodeId
, std::string toPortId
, int toBit
, int width
= 1);
82 void createConnection(std::string fromNodeId
, std::string fromPortId
, std::string toNodeId
, std::string toPortId
);
83 void createConstant(std::string toNodeId
, std::string toPortId
, int toBit
, int constValue
);
84 void createConstant(std::string toNodeId
, std::string toPortId
, int constValue
);
85 void markExtern(std::string nodeId
, std::string portId
, int bit
= -1);
89 friend class SolverWorker
;
95 struct ResultNodeMapping
{
96 std::string needleNodeId
, haystackNodeId
;
97 void *needleUserData
, *haystackUserData
;
98 std::map
<std::string
, std::string
> portMapping
;
101 std::string needleGraphId
, haystackGraphId
;
102 std::map
<std::string
, ResultNodeMapping
> mappings
;
105 struct MineResultNode
{
111 int totalMatchesAfterLimits
;
112 std::map
<std::string
, int> matchesPerGraph
;
113 std::vector
<MineResultNode
> nodes
;
117 SolverWorker
*worker
;
120 virtual bool userCompareNodes(const std::string
&needleGraphId
, const std::string
&needleNodeId
, void *needleUserData
,
121 const std::string
&haystackGraphId
, const std::string
&haystackNodeId
, void *haystackUserData
, const std::map
<std::string
, std::string
> &portMapping
);
123 virtual std::string
userAnnotateEdge(const std::string
&graphId
, const std::string
&fromNodeId
, void *fromUserData
, const std::string
&toNodeId
, void *toUserData
);
125 virtual bool userCompareEdge(const std::string
&needleGraphId
, const std::string
&needleFromNodeId
, void *needleFromUserData
, const std::string
&needleToNodeId
, void *needleToUserData
,
126 const std::string
&haystackGraphId
, const std::string
&haystackFromNodeId
, void *haystackFromUserData
, const std::string
&haystackToNodeId
, void *haystackToUserData
);
128 virtual bool userCheckSolution(const Result
&result
);
130 friend class SolverWorker
;
137 void addGraph(std::string graphId
, const Graph
&graph
);
138 void addCompatibleTypes(std::string needleTypeId
, std::string haystackTypeId
);
139 void addCompatibleConstants(int needleConstant
, int haystackConstant
);
140 void addSwappablePorts(std::string needleTypeId
, std::string portId1
, std::string portId2
, std::string portId3
= std::string(), std::string portId4
= std::string());
141 void addSwappablePorts(std::string needleTypeId
, std::set
<std::string
> ports
);
142 void addSwappablePortsPermutation(std::string needleTypeId
, std::map
<std::string
, std::string
> portMapping
);
144 void solve(std::vector
<Result
> &results
, std::string needleGraphId
, std::string haystackGraphId
, bool allowOverlap
= true, int maxSolutions
= -1);
145 void solve(std::vector
<Result
> &results
, std::string needleGraphId
, std::string haystackGraphId
,
146 const std::map
<std::string
, std::set
<std::string
>> &initialMapping
, bool allowOverlap
= true, int maxSolutions
= -1);
148 void mine(std::vector
<MineResult
> &results
, int minNodes
, int maxNodes
, int minMatches
, int limitMatchesPerGraph
= -1);
150 void clearOverlapHistory();
155 #endif /* SUBCIRCUIT_H */