Skip to content

Commit a6f7688

Browse files
authored
Merge pull request #108 from BlockScience/docs
Docs
2 parents 7e375b5 + 6c09bca commit a6f7688

2 files changed

Lines changed: 117 additions & 4 deletions

File tree

docs/Functions & Validation Rules/Processor Functions.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ $$\text{isPrimitive}: \text{Processor} \rightarrow \text{Bool}$$
1717

1818
### Python Implementation
1919

20+
```python
21+
class Processor:
22+
...
23+
def is_primitive(self):
24+
return self.subsystem is None
25+
```
26+
2027
## Get System
2128

2229
$$\text{getSystem}: \text{Processor} \rightarrow \text{System}$$
@@ -28,6 +35,17 @@ $$\text{getSystem}: \text{Processor} \rightarrow \text{System}$$
2835

2936
### Python Implementation
3037

38+
```python
39+
class Processor:
40+
...
41+
def get_system(self):
42+
if self.is_primitive():
43+
return None
44+
else:
45+
return self.subsystem
46+
```
47+
48+
3149
## Get Shape
3250

3351
$$\text{getShape}: \text{Processor} \rightarrow \text{Block}$$
@@ -38,8 +56,6 @@ $$\text{getShape}: \text{Processor} \rightarrow \text{Block}$$
3856

3957
### Python Implementation
4058

41-
- The following is implemented on the python client's Processor class:
42-
4359
```python
4460
class Processor:
4561
...

docs/Functions & Validation Rules/System Functions.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ $$\text{isValid}: \text{system} \rightarrow \text{Bool}$$
2222

2323
### Python Implementation
2424

25+
- The python implementation assumes certain conditions, such as all inputs adhering to the schema, during loading. If these assumptions were incorrect, an error would already occur at that stage, making additional checks in this function redundant.
26+
27+
```python
28+
class System:
29+
...
30+
def is_valid(self):
31+
condition1 = len(self.get_open_ports()) == 0
32+
condition2 = self.is_connected()
33+
return condition1 and condition2
34+
```
35+
2536
## Is Directed
2637

2738
$$\text{isDirected}: \text{system} \rightarrow \text{Bool}$$
@@ -32,25 +43,84 @@ $$\text{isDirected}: \text{system} \rightarrow \text{Bool}$$
3243

3344
### Python Implementation
3445

46+
```python
47+
class System:
48+
...
49+
def is_directed(self):
50+
processors = set([x.id for x in self.processors])
51+
while len(processors) > 0:
52+
q = [processors.pop()]
53+
visited = []
54+
while len(q) > 0:
55+
cur = q.pop()
56+
visited.append(cur)
57+
cur = self.processors_map[cur]
58+
wires = [x for x in self.wires if x.source["Processor"].id == cur.id]
59+
for x in wires:
60+
x = x.target["Processor"].id
61+
if x in processors:
62+
q.append(x)
63+
processors.remove(x)
64+
if x in visited:
65+
return False
66+
return True
67+
```
68+
3569
## Is Connected
3670

3771
$$\text{isConnected}: \text{system} \rightarrow \text{Bool}$$
3872

3973
### Description
40-
- A function which determines if there is a path between any two nodes, in the weakly connected sense
74+
75+
- A function which determines if there is a path between any two nodes, in the weakly connected sense, i.e. either A->B or B->A would suffice for A and be being connected.
4176

4277
### Python Implementation
4378

79+
```python
80+
class System:
81+
...
82+
def is_connected(self):
83+
processors = set([x.id for x in self.processors])
84+
85+
q = [processors.pop()]
86+
while len(q) > 0:
87+
cur = q.pop()
88+
cur = self.processors_map[cur]
89+
wires = [
90+
x
91+
for x in self.wires
92+
if x.source["Processor"].id == cur.id
93+
or x.target["Processor"].id == cur.id
94+
]
95+
for y in wires:
96+
x = y.target["Processor"].id
97+
if x in processors:
98+
q.append(x)
99+
processors.remove(x)
100+
x = y.source["Processor"].id
101+
if x in processors:
102+
q.append(x)
103+
processors.remove(x)
104+
return len(processors) == 0
105+
```
106+
44107
## Is Dynamical
45108

46109
$$\text{isDynamical}: \text{system} \rightarrow \text{Bool}$$
47110

48111
### Description
49112

50-
- [NOTE]: Is this just the opposite of is directed?
113+
- The opposite of `IsDirected` in the sense that there is looping behavior in the system
51114

52115
### Python Implementation
53116

117+
```python
118+
class System:
119+
...
120+
def is_dynamical(self):
121+
return not self.is_directed()
122+
```
123+
54124
## Get Open Ports
55125

56126
$$\text{getOpenPorts}: \text{system} \rightarrow \text{List[Port]} = \text{List}[\{\text{Processor, Index, Space}\}]$$
@@ -62,6 +132,18 @@ $$\text{getOpenPorts}: \text{system} \rightarrow \text{List[Port]} = \text{List}
62132

63133
### Python Implementation
64134

135+
```python
136+
class System:
137+
...
138+
def get_open_ports(self):
139+
out = []
140+
for processor in self.processor_ports_map:
141+
for i, port_list in enumerate(self.processor_ports_map[processor]):
142+
if len(port_list) == 0:
143+
out.append([processor, i, processor.ports[i]])
144+
return out
145+
```
146+
65147
## Get Available Terminals
66148

67149
$$\text{getAvailableTerminals}: \text{system} \rightarrow \text{List[Terminal]} = \text{List}[\{\text{Processor, Index, Space}\}]$$
@@ -76,6 +158,21 @@ $$\text{getAvailableTerminals}: \text{system} \rightarrow \text{List[Terminal]}
76158

77159
### Python Implementation
78160

161+
```python
162+
class System:
163+
...
164+
def get_available_terminals(self, open_only=False):
165+
out = []
166+
for processor in self.processor_terminals_map:
167+
for i, terminal_list in enumerate(self.processor_terminals_map[processor]):
168+
if open_only:
169+
if len(terminal_list) == 0:
170+
out.append([processor, i, processor.terminals[i]])
171+
else:
172+
out.append([processor, i, processor.terminals[i]])
173+
return out
174+
```
175+
79176
## Get Connected Components
80177

81178
$$\text{getConnectedComponents}: \text{system} \rightarrow \text{List[Processor]}$$

0 commit comments

Comments
 (0)