Skip to content

Commit f8bffab

Browse files
committed
initial target link from WG
1 parent 0e9a9bd commit f8bffab

File tree

4 files changed

+215
-393
lines changed

4 files changed

+215
-393
lines changed

docs/Logic.md

Lines changed: 98 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The table below illustrates that:
4545

4646
 
4747

48-
## GenAI Natural Language Logic
48+
## GenAI: Natural Language Logic
4949

5050
You can use Natural Language to create logic during project creation, or for existing projects. For example: `The Customer's balance is the sum of the Order amount_total where date_shipped is null`.
5151

@@ -58,7 +58,7 @@ Think of Natural Language Logic as a translation process down onto underlying ru
5858
5959
 
6060

61-
## With Code Completion
61+
## IDE: With Code Completion
6262

6363
You can also use your IDE with Code Completion to add rules, and their arguments.
6464

@@ -81,7 +81,7 @@ Similarly, you can change rules without worrying about the order of execution.
8181

8282
Inside the larger process above, here is the best way to learn how to use rules:
8383

84-
1. **Rule Summary**: review the table above; there are a small number of rules, since their power lies in chaining
84+
1. **Rule Summary**: review the Rule Types table above; there are a small number of rules, since their power lies in chaining
8585

8686
* **Alert:** Logic consists of rules and Python. You will quickly learn to use logic events; focus on the *rules as the preferred* approach, using Python (events, etc) as a *fallback*.
8787

@@ -95,9 +95,10 @@ Inside the larger process above, here is the best way to learn how to use rules:
9595
9696
  
9797

98-
9998
## Rule Patterns
10099

100+
Rules support *chaining:* a rule may change a value that triggers other rules, including across tables. Mastering such ***multi-table logic*** is the key to using rules effectively. The most typical examples are described below.
101+
101102
| Pattern | Notes | Example
102103
| :------------- | :-----| :---- |
103104
| **Chain Up** | parent sums and counts mean that child row changes can ***adjust*** parents | [Derive Balance](Behave-Logic-Report.md/#scenario-good-order-custom-service){:target="_blank" rel="noopener"} |
@@ -109,26 +110,9 @@ Inside the larger process above, here is the best way to learn how to use rules:
109110
| **Ready Flag** | Multi-session editing, then , when ready...<br>adjust related data / enforce constraints | [Make Order Ready](Behave-Logic-Report.md/#scenario-order-made-ready){:target="_blank" rel="noopener"} |
110111
| **Events for Lib Access** | Events enable Python, use of standard libs (e.g., Kafka) | [Ship Order](Behave-Logic-Report.md#scenario-good-order-custom-service){:target="_blank" rel="noopener"} |
111112

112-
113-
## Logic Patterns
114-
115-
WebGenAI was trained to understand the Natural Language Logic problems shown below. Please see [Natural Language Logic](WebGenAI-CLI.md#natural-language-logic){:target="_blank" rel="noopener"}.
116-
117-
| Example | Notes |
118-
| :------------- | :------------- |
119-
| Airport - at least 10 tables<br>A flight's passengers must be less than its Airplane's seating capacity |
120-
| System for Departments and Employees.<br>Sum of employee salaries cannot exceed department budget |
121-
| Create a system with Employees and their Skills.<br>More than One Employee can have the same Skill.<br>EmployeeSkill.rating = Skill.rating<br>An Employee's skill-rating is the sum of the Employee Skills rating, plus 2 * years of service. |
122-
| Students have probations and sick days.<br>Signal an error if a Student's can-graduate is True, and there are more 2 probations, or more than 100 sick days.
123-
| Applicant have felonies and bankruptcies.<br>Signal error if is-hirable is true and there are more than 3 bankruptcies, or 2 felonies.
124-
| Students have Grades and belong to Clubs.<br>Copy the name from Club to Student Club<br>The student's service activity is the count of Student Clubs where name contains 'service'. <br>Signal error if student is eligible for the honor society == True, and their grade point average is under 3.5, or they have less than 2 service activities
125-
| Products have Notices, with severity 0-5.<br>Raise and error if product is orderable == True and there are any severity 5 Notices, or more than 3 Notices.
126-
| Create a system with customers, orders, items and products.<br>Include a notes field for orders.<br><br>Use Case: enforce the Check Credit for ready orders:<br>1. Customer.balance <= credit_limit<br>2. Customer.balance = Sum(Order.amount_total where date_shipped is null and ready is True)<br>3. Order.amount_total = Sum(Item.amount)<br>4. Item.amount = quantity * unit_price<br>5. Store the Item.unit_price as a copy from Product.unit_price<br><br>Use Case: Compute Products ordered<br>1. Item.ready = Order.ready<br>2. Product.total_ordered = sum(Item.quantity) where ready == True<br>3. Product.reorder_required = quantity_on_hand <= total_ordered<br><br>Use Case: No Empty Orders<br>1. Order.item_count = Count(Items)<br>2. When setting the date_shipped, item_count must be > 0. | Ready Flag
127-
| Teachers, courses which have offerings, and students who have offerings.<br><br>Use Case: capacity<br>teachers cannot be assigned to more than 5 courses<br>students cannot have more enrollments than 6<br><br>Use Case: budget control<br>courses have a charge, which is copied to enrollments charge<br>a student's total enrollment charges cannot exceed their budget
128-
129113
&nbsp;
130114

131-
## Case Study
115+
## Rules Case Study
132116

133117
The best way to learn the rules is by a Case Study approach:
134118

@@ -154,3 +138,95 @@ The best way to learn the rules is by a Case Study approach:
154138

155139
* Reveal the solution: open the disclosure box: "Tests - and their logic - are transparent.. click to see Logic"
156140

141+
142+
&nbsp;
143+
144+
# Learning Natural Language
145+
146+
As noted above, it is important to be clear on the rules generated for logic. Use the examples below to test your understanding.
147+
148+
WebGenAI provides the [Logic Editor](WebGenAI-logic-editor.md) so you can see/edit the translation:
149+
150+
![logic Editor](images/web_genai/logic/logic-editor.png)
151+
152+
## Natural Language Patterns
153+
| Pattern | Notes | Example
154+
| :------------- | :-----| :---- |
155+
| Formal vs Informal | You can: *Customer.balance = Sum(Order.amount_total where date_shipped is null)* | Or, more simply: *The Customer's balance is the sum of the Order amount_total where date_shipped is null*
156+
| Integration Logic | Kafka | *Send the Order to Kafka topic 'order_shipping' if the date_shipped is not None*
157+
| Multi-rule Logic | See Multi-rule Logic - Generatd Rules, below | *Sum of employee salaries cannot exceed department budget*
158+
| Conditional Derivations | See Conditional Derivation - Generated Rules, below | Provide a 10% discount when buying more than 10 carbon neutral products<br>The Item carbon neutral is copied from the Product carbon neutral
159+
| Cardinality Patterns<br>- *Qualified Any* | See Cardinality Patterns - Generated Rules, below | Products have Notices, with severity 0-5.<br>Raise and error if product is orderable == True and there are any severity 5 Notices, or more than 3 Notices.
160+
161+
162+
<details markdown>
163+
164+
<summary> Multi-rule Logic - Generated Rules </summary>
165+
```python title='Logic Recognizes "conditional derivations"'
166+
# Aggregate the total salaries of employees for each department.
167+
Rule.sum(derive=Department.total_salaries, as_sum_of=Employee.salary)
168+
169+
# Ensure the sum of employee salaries does not exceed the department budget
170+
Rule.constraint(validate=Department, as_condition=lambda row: row.total_salaries <= row.budget, error_msg="xxx")
171+
# End Logic from GenAI
172+
```
173+
</details>
174+
175+
&nbsp;
176+
177+
<details markdown>
178+
179+
<summary> Conditional Derivation - Generated Rules </summary>
180+
```python title='Logic Recognizes "conditional derivations"'
181+
# Provide a 10% discount when buying more than 10 carbon neutral products.
182+
Rule.formula(derive=Item.amount,
183+
as_expression=lambda row: 0.9 * row.unit_price * row.quantity \
184+
if row.Product.is_carbon_neutral and row.quantity > 10
185+
else row.unit_price * row.quantity)
186+
# End Logic from GenAI
187+
```
188+
</details>
189+
190+
&nbsp;
191+
<details markdown>
192+
193+
<summary> Cardinality Patterns - Generated Rules </summary>
194+
```python title='Logic Recognizes "qualified any"'
195+
# Logic from GenAI: (or, use your IDE w/ code completion)
196+
197+
# Derive product notice count from related notices.
198+
Rule.count(derive=Product.notice_count, as_count_of=Notice)
199+
200+
# Derive count of severity 5 notices for products.
201+
Rule.count(derive=Product.class_5_notice_count, as_count_of=Notice, where=lambda row: row.severity == 5)
202+
203+
# Ensure product is not orderable if conditions on notices are met.
204+
Rule.constraint(validate=Product,
205+
as_condition=lambda row: not (row.orderable and (row.class_5_notice_count > 0 or row.notice_count > 3)),
206+
error_msg="Orderable product contains severity 5 or excessive notices.")
207+
208+
# End Logic from GenAI
209+
```
210+
</details>
211+
212+
&nbsp;
213+
214+
## Natural Language Examples
215+
216+
WebGenAI was trained to understand the Natural Language Logic problems shown below. These automate many of the rule patters described above.
217+
218+
Please see [Natural Language Logic](WebGenAI-CLI.md#natural-language-logic){:target="_blank" rel="noopener"}.
219+
220+
| Example | Notes |
221+
| :------------- | :------------- |
222+
| Airport - at least 10 tables<br>A flight's passengers must be less than its Airplane's seating capacity |
223+
| System for Departments and Employees.<br>Sum of employee salaries cannot exceed department budget |
224+
| Create a system with Employees and their Skills.<br>More than One Employee can have the same Skill.<br>EmployeeSkill.rating = Skill.rating<br>An Employee's skill-rating is the sum of the Employee Skills rating, plus 2 * years of service. |
225+
| Students have probations and sick days.<br>Signal an error if a Student's can-graduate is True, and there are more 2 probations, or more than 100 sick days.
226+
| Applicant have felonies and bankruptcies.<br>Signal error if is-hirable is true and there are more than 3 bankruptcies, or 2 felonies.
227+
| Students have Grades and belong to Clubs.<br>Copy the name from Club to Student Club<br>The student's service activity is the count of Student Clubs where name contains 'service'. <br>Signal error if student is eligible for the honor society == True, and their grade point average is under 3.5, or they have less than 2 service activities
228+
| Products have Notices, with severity 0-5.<br>Raise and error if product is orderable == True and there are any severity 5 Notices, or more than 3 Notices.
229+
| Create a system with customers, orders, items and products.<br>Include a notes field for orders.<br><br>Use Case: enforce the Check Credit for ready orders:<br>1. Customer.balance <= credit_limit<br>2. Customer.balance = Sum(Order.amount_total where date_shipped is null and ready is True)<br>3. Order.amount_total = Sum(Item.amount)<br>4. Item.amount = quantity * unit_price<br>5. Store the Item.unit_price as a copy from Product.unit_price<br><br>Use Case: Compute Products ordered<br>1. Item.ready = Order.ready<br>2. Product.total_ordered = sum(Item.quantity) where ready == True<br>3. Product.reorder_required = quantity_on_hand <= total_ordered<br><br>Use Case: No Empty Orders<br>1. Order.item_count = Count(Items)<br>2. When setting the date_shipped, item_count must be > 0. | Ready Flag
230+
| Teachers, courses which have offerings, and students who have offerings.<br><br>Use Case: capacity<br>teachers cannot be assigned to more than 5 courses<br>students cannot have more enrollments than 6<br><br>Use Case: budget control<br>courses have a charge, which is copied to enrollments charge<br>a student's total enrollment charges cannot exceed their budget
231+
232+
&nbsp;

0 commit comments

Comments
 (0)