ApiLogicServer 12.0.4__py3-none-any.whl → 12.1.26__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. {ApiLogicServer-12.0.4.dist-info → ApiLogicServer-12.1.26.dist-info}/METADATA +2 -1
  2. {ApiLogicServer-12.0.4.dist-info → ApiLogicServer-12.1.26.dist-info}/RECORD +63 -40
  3. {ApiLogicServer-12.0.4.dist-info → ApiLogicServer-12.1.26.dist-info}/WHEEL +1 -1
  4. api_logic_server_cli/api_logic_server.py +23 -204
  5. api_logic_server_cli/api_logic_server_info.yaml +2 -2
  6. api_logic_server_cli/cli.py +25 -84
  7. api_logic_server_cli/cli_args_base.py +2 -0
  8. api_logic_server_cli/cli_args_project.py +9 -3
  9. api_logic_server_cli/create_from_model/__pycache__/dbml.cpython-312.pyc +0 -0
  10. api_logic_server_cli/create_from_model/__pycache__/meta_model.cpython-312.pyc +0 -0
  11. api_logic_server_cli/create_from_model/__pycache__/model_creation_services.cpython-312.pyc +0 -0
  12. api_logic_server_cli/create_from_model/__pycache__/ont_create.cpython-312.pyc +0 -0
  13. api_logic_server_cli/create_from_model/__pycache__/ui_admin_creator.cpython-312.pyc +0 -0
  14. api_logic_server_cli/create_from_model/meta_model.py +1 -1
  15. api_logic_server_cli/create_from_model/model_creation_services.py +3 -1
  16. api_logic_server_cli/create_from_model/ont_create.py +3 -1
  17. api_logic_server_cli/create_from_model/ui_admin_creator.py +6 -4
  18. api_logic_server_cli/genai.py +420 -198
  19. api_logic_server_cli/logging.yml +5 -0
  20. api_logic_server_cli/prototypes/.DS_Store +0 -0
  21. api_logic_server_cli/prototypes/base/api_logic_server_run.py +0 -2
  22. api_logic_server_cli/prototypes/base/config/server_setup.py +15 -1
  23. api_logic_server_cli/prototypes/base/integration/kafka/kafka_consumer.py +1 -1
  24. api_logic_server_cli/prototypes/base/integration/kafka/kafka_producer.py +1 -1
  25. api_logic_server_cli/prototypes/base/readme.md +21 -8
  26. api_logic_server_cli/prototypes/genai_demo/logic/declare_logic.py +11 -12
  27. api_logic_server_cli/prototypes/manager/.DS_Store +0 -0
  28. api_logic_server_cli/prototypes/manager/.vscode/.DS_Store +0 -0
  29. api_logic_server_cli/prototypes/manager/.vscode/launch.json +98 -1
  30. api_logic_server_cli/prototypes/manager/README.md +40 -3
  31. api_logic_server_cli/prototypes/manager/system/.DS_Store +0 -0
  32. api_logic_server_cli/prototypes/manager/system/genai/.DS_Store +0 -0
  33. api_logic_server_cli/prototypes/manager/system/genai/create_db_models_inserts/create_db_models_create_db.py +11 -0
  34. api_logic_server_cli/prototypes/manager/system/genai/create_db_models_inserts/create_db_models_imports.py +22 -0
  35. api_logic_server_cli/prototypes/manager/system/genai/create_db_models_inserts/create_db_models_test_data.py +7 -0
  36. api_logic_server_cli/prototypes/manager/system/genai/examples/emp_depts/emp_dept.prompt +4 -0
  37. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo.prompt +1 -1
  38. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo.response_example +133 -130
  39. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo_informal.prompt +10 -0
  40. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo_iterative_logic/genai_demo_iterative_logic_000.response +1 -0
  41. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo_iterative_logic/genai_demo_iterative_logic_001.prompt +171 -0
  42. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo_iterative_logic/genai_demo_iterative_logic_002.prompt +21 -0
  43. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo_iterative_logic/genai_demo_iterative_logic_003.response +94 -0
  44. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo_iterative_logic/genai_demo_iterative_logic_004.prompt +6 -0
  45. api_logic_server_cli/prototypes/manager/system/genai/examples/genai_demo/genai_demo_iterative_logic/genai_demo_iterative_logic_005.response_example +122 -0
  46. api_logic_server_cli/prototypes/manager/system/genai/learning_requests/logic_bank_api.prompt +162 -12
  47. api_logic_server_cli/prototypes/manager/system/genai/prompt_inserts/.DS_Store +0 -0
  48. api_logic_server_cli/prototypes/manager/system/genai/prompt_inserts/logic_inserts.prompt +1 -0
  49. api_logic_server_cli/prototypes/manager/system/genai/prompt_inserts/response_format.prompt +30 -0
  50. api_logic_server_cli/prototypes/manager/system/genai/prompt_inserts/sqlite_inserts.prompt +17 -2
  51. api_logic_server_cli/prototypes/manager/system/genai/prompt_inserts/zsqlite_inserts_iterations.prompt +28 -0
  52. api_logic_server_cli/prototypes/manager/system/genai/retry/conv/create_db_models.py +96 -0
  53. api_logic_server_cli/prototypes/manager/system/genai/retry/conv/inf-1_iter_1_1_000.response +1 -0
  54. api_logic_server_cli/prototypes/manager/system/genai/retry/conv/inf-1_iter_1_1_001.prompt +208 -0
  55. api_logic_server_cli/prototypes/manager/system/genai/retry/conv/inf-1_iter_1_1_002.prompt +89 -0
  56. api_logic_server_cli/prototypes/manager/system/genai/retry/conv/inf-1_iter_1_1_003.prompt +40 -0
  57. api_logic_server_cli/prototypes/manager/system/genai/retry/conv/inf-1_iter_1_1_004.response +57 -0
  58. api_logic_server_cli/prototypes/manager/system/genai/retry/conv/inf-1_iter_1_1_005.response +57 -0
  59. api_logic_server_cli/prototypes/manager/system/genai/retry/readme.md +1 -0
  60. api_logic_server_cli/prototypes/manager/system/genai/retry/retry.response +57 -0
  61. api_logic_server_cli/prototypes/manager/system/genai/examples/emp_depts/emp_dept_explicit.prompt +0 -7
  62. api_logic_server_cli/prototypes/manager/system/genai/examples/emp_depts/emp_dept_implicit_fails.prompt +0 -5
  63. api_logic_server_cli/prototypes/manager/system/secrets.txt +0 -6
  64. {ApiLogicServer-12.0.4.dist-info → ApiLogicServer-12.1.26.dist-info}/LICENSE +0 -0
  65. {ApiLogicServer-12.0.4.dist-info → ApiLogicServer-12.1.26.dist-info}/entry_points.txt +0 -0
  66. {ApiLogicServer-12.0.4.dist-info → ApiLogicServer-12.1.26.dist-info}/top_level.txt +0 -0
@@ -1,130 +1,133 @@
1
- To achieve the given requirements using SQLAlchemy, follow these steps to create a SQLAlchemy ORM model for a SQLite database, create the database, and insert some test data.
2
-
3
- ### 1. Setup and Import Libraries
4
-
5
- First install SQLAlchemy if you haven't already:
6
- ```bash
7
- pip install sqlalchemy
8
- ```
9
-
10
- Then, you can write the code:
11
-
12
- ```python
13
- from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey, DECIMAL
14
- from sqlalchemy.ext.declarative import declarative_base
15
- from sqlalchemy.orm import sessionmaker, relationship
16
- import datetime
17
-
18
- # Create an instance of the declarative base class
19
- Base = declarative_base()
20
-
21
- # Define the Customer model
22
- class Customer(Base):
23
- """
24
- description: This table stores customer information, including a balance and credit limit.
25
- """
26
- __tablename__ = 'customers'
27
- id = Column(Integer, primary_key=True, autoincrement=True)
28
- name = Column(String, nullable=False)
29
- balance = Column(DECIMAL, nullable=False, default=0)
30
- credit_limit = Column(DECIMAL, nullable=False, default=0)
31
-
32
- # Define the Order model
33
- class Order(Base):
34
- """
35
- description: This table stores information about each order, including total amount and notes.
36
- """
37
- __tablename__ = 'orders'
38
- id = Column(Integer, primary_key=True, autoincrement=True)
39
- customer_id = Column(Integer, ForeignKey('customers.id'), nullable=False)
40
- date_placed = Column(DateTime, default=datetime.datetime.utcnow)
41
- date_shipped = Column(DateTime, nullable=True)
42
- amount_total = Column(DECIMAL, nullable=False)
43
- notes = Column(String, nullable=True)
44
-
45
- # Define the Product model
46
- class Product(Base):
47
- """
48
- description: This table stores product information, including unit prices.
49
- """
50
- __tablename__ = 'products'
51
- id = Column(Integer, primary_key=True, autoincrement=True)
52
- name = Column(String, nullable=False)
53
- unit_price = Column(DECIMAL, nullable=False)
54
-
55
- # Define the Item model
56
- class Item(Base):
57
- """
58
- description: This table stores each item in an order, including quantity, and unit price.
59
- """
60
- __tablename__ = 'items'
61
- id = Column(Integer, primary_key=True, autoincrement=True)
62
- order_id = Column(Integer, ForeignKey('orders.id'), nullable=False)
63
- product_id = Column(Integer, ForeignKey('products.id'), nullable=False)
64
- quantity = Column(Integer, nullable=False, default=1)
65
- unit_price = Column(DECIMAL, nullable=False)
66
- amount = Column(DECIMAL, nullable=False)
67
-
68
- # SQLite Database
69
- engine = create_engine('sqlite:///system/genai/temp/create_db_models.sqlite')
70
- Base.metadata.create_all(engine)
71
-
72
- Session = sessionmaker(bind=engine)
73
- session = Session()
74
-
75
- # Create some test data
76
- # Add test products
77
- product1 = Product(name='Product 1', unit_price=10.50)
78
- product2 = Product(name='Product 2', unit_price=15.75)
79
-
80
- # Add test customers
81
- customer1 = Customer(name='Customer 1', balance=0, credit_limit=500)
82
- customer2 = Customer(name='Customer 2', balance=0, credit_limit=1000)
83
-
84
- # Add test orders and items for customer1
85
- order1 = Order(customer_id=1, amount_total=0, notes='First Order')
86
- item1 = Item(order_id=1, product_id=1, quantity=2, unit_price=10.50, amount=21.00)
87
- item2 = Item(order_id=1, product_id=2, quantity=1, unit_price=15.75, amount=15.75)
88
-
89
- # Calculate order total
90
- order1.amount_total = item1.amount + item2.amount
91
-
92
- # Add order2
93
- order2 = Order(customer_id=1, amount_total=0, notes='Second Order')
94
- item3 = Item(order_id=2, product_id=1, quantity=1, unit_price=10.50, amount=10.50)
95
-
96
- # Calculate order2 total
97
- order2.amount_total = item3.amount
98
-
99
- # Set balance for customer1
100
- customer1.balance = order1.amount_total + order2.amount_total
101
-
102
- # Add test orders and items for customer2
103
- order3 = Order(customer_id=2, amount_total=0, notes='First Order for customer 2')
104
- item4 = Item(order_id=3, product_id=2, quantity=3, unit_price=15.75, amount=47.25)
105
-
106
- # Calculate order3 total
107
- order3.amount_total = item4.amount
108
-
109
- # Set balance for customer2
110
- customer2.balance = order3.amount_total
111
-
112
- # Add records to the session
113
- session.add_all([product1, product2, customer1, customer2, order1, item1, item2, order2, item3, order3, item4])
114
-
115
- # Commit the session
116
- session.commit()
117
-
118
- # Close the session
119
- session.close()
120
- ```
121
-
122
- ### Explanation:
123
-
124
- 1. **Database Models**: There are four main models: Customer, Order, Product, and Item. Each has its own table and columns defined.
125
-
126
- 2. **No Check Constraints**: Instead of using constraints for calculations, calculations are made in Python and are reflected in the ORM models (like order totals, item amounts, etc.).
127
-
128
- 3. **Adding Data**: Insert test data for products, customers, orders, and items. This includes calculating the amounts manually before adding them to ensure the database consistency requirements are met.
129
-
130
- Make sure to adapt the paths and environment as needed. This code should create the SQLite database, define the relationships, insert some records, and enforce the credit requirements logically within Python.
1
+ {
2
+ "models": [
3
+ {
4
+ "classname": "Customer",
5
+ "code": "class Customer(Base):\n __tablename__ = 'customers'\n id = Column(Integer, primary_key=True, autoincrement=True)\n name = Column(String, nullable=False)\n balance = Column(Integer, default=0)\n credit_limit = Column(Integer, nullable=False)\n\n def __repr__(self):\n return f\"Customer(id={self.id}, name='{self.name}', balance={self.balance}, credit_limit={self.credit_limit})\"",
6
+ "sqlite_create": "CREATE TABLE customers (\n id INTEGER PRIMARY KEY AUTOINCREMENT, \n name VARCHAR NOT NULL, \n balance INTEGER DEFAULT 0, \n credit_limit INTEGER NOT NULL\n)",
7
+ "description": "description: Customers table",
8
+ "name": "customers"
9
+ },
10
+ {
11
+ "classname": "Order",
12
+ "code": "class Order(Base):\n __tablename__ = 'orders'\n id = Column(Integer, primary_key=True, autoincrement=True)\n customer_id = Column(Integer, ForeignKey('customers.id'))\n notes = Column(String)\n date_shipped = Column(Date)\n amount_total = Column(Integer, default=0)\n\n def __repr__(self):\n return f\"Order(id={self.id}, customer_id={self.customer_id}, notes='{self.notes}', amount_total={self.amount_total}, date_shipped={self.date_shipped})\"",
13
+ "sqlite_create": "CREATE TABLE orders (\n id INTEGER PRIMARY KEY AUTOINCREMENT, \n customer_id INTEGER, \n notes VARCHAR, \n date_shipped DATE, \n amount_total INTEGER DEFAULT 0, \n FOREIGN KEY(customer_id) REFERENCES customers (id)\n)",
14
+ "description": "description: Orders table",
15
+ "name": "orders"
16
+ },
17
+ {
18
+ "classname": "Item",
19
+ "code": "class Item(Base):\n __tablename__ = 'items'\n id = Column(Integer, primary_key=True, autoincrement=True)\n order_id = Column(Integer, ForeignKey('orders.id'))\n product_id = Column(Integer, ForeignKey('products.id'))\n quantity = Column(Integer, nullable=False)\n unit_price = Column(Integer, nullable=False)\n amount = Column(Integer, default=0)\n\n def __repr__(self):\n return f\"Item(id={self.id}, order_id={self.order_id}, product_id={self.product_id}, quantity={self.quantity}, unit_price={self.unit_price}, amount={self.amount})\"",
20
+ "sqlite_create": "CREATE TABLE items (\n id INTEGER PRIMARY KEY AUTOINCREMENT, \n order_id INTEGER, \n product_id INTEGER, \n quantity INTEGER NOT NULL, \n unit_price INTEGER NOT NULL, \n amount INTEGER DEFAULT 0, \n FOREIGN KEY(order_id) REFERENCES orders (id), \n FOREIGN KEY(product_id) REFERENCES products (id)\n)",
21
+ "description": "description: Items table",
22
+ "name": "items"
23
+ },
24
+ {
25
+ "classname": "Product",
26
+ "code": "class Product(Base):\n __tablename__ = 'products'\n id = Column(Integer, primary_key=True, autoincrement=True)\n name = Column(String, nullable=False)\n unit_price = Column(Integer, nullable=False)\n\n def __repr__(self):\n return f\"Product(id={self.id}, name='{self.name}', unit_price={self.unit_price})\"",
27
+ "sqlite_create": "CREATE TABLE products (\n id INTEGER PRIMARY KEY AUTOINCREMENT, \n name VARCHAR NOT NULL, \n unit_price INTEGER NOT NULL\n)",
28
+ "description": "description: Products table",
29
+ "name": "products"
30
+ }
31
+ ],
32
+ "rules": [
33
+ {
34
+ "name": "Customer Balance Constraint",
35
+ "description": "Ensures the customer's balance does not exceed the credit limit.",
36
+ "use_case": "Customer.balance <= credit_limit",
37
+ "code": "Rule.constraint(validate=Customer,\n as_condition=lambda row: row.balance <= row.credit_limit,\n error_msg=\"Customer balance ({row.balance}) exceeds credit limit ({row.credit_limit})\")"
38
+ },
39
+ {
40
+ "name": "Customer Balance Derivation",
41
+ "description": "Calculates the customer's balance as the sum of orders' amount_total where date_shipped is null.",
42
+ "use_case": "Customer.balance = Sum(Order.amount_total where date_shipped is null)",
43
+ "code": "Rule.sum(derive=Customer.balance, as_sum_of=Order.amount_total, where=lambda row: row.date_shipped is None)"
44
+ },
45
+ {
46
+ "name": "Order Amount Total Derivation",
47
+ "description": "Calculates order's amount_total as the sum of item amounts.",
48
+ "use_case": "Order.amount_total = Sum(Item.amount)",
49
+ "code": "Rule.sum(derive=Order.amount_total, as_sum_of=Item.amount)"
50
+ },
51
+ {
52
+ "name": "Item Amount Derivation",
53
+ "description": "Calculates item amount as quantity multiplied by unit price.",
54
+ "use_case": "Item.amount = quantity * unit_price",
55
+ "code": "Rule.formula(derive=Item.amount, as_expression=lambda row: row.quantity * row.unit_price)"
56
+ },
57
+ {
58
+ "name": "Copy Item Unit Price",
59
+ "description": "Copies the product's unit price to the item.",
60
+ "use_case": "Store the Item.unit_price as a copy from Product.unit_price",
61
+ "code": "Rule.copy(derive=Item.unit_price, from_parent=Product.unit_price)"
62
+ }
63
+ ],
64
+ "test_data": "Insert test data into the tables with values compatible with the derived logic.",
65
+ "test_data_rows": [
66
+ {
67
+ "test_data_row_variable": "test_customer_1",
68
+ "code": "test_customer_1 = Customer(name='Customer 1', balance=300, credit_limit=1000)"
69
+ },
70
+ {
71
+ "test_data_row_variable": "test_customer_2",
72
+ "code": "test_customer_2 = Customer(name='Customer 2', balance=500, credit_limit=750)"
73
+ },
74
+ {
75
+ "test_data_row_variable": "test_customer_3",
76
+ "code": "test_customer_3 = Customer(name='Customer 3', balance=0, credit_limit=1500)"
77
+ },
78
+ {
79
+ "test_data_row_variable": "test_customer_4",
80
+ "code": "test_customer_4 = Customer(name='Customer 4', balance=200, credit_limit=1200)"
81
+ },
82
+ {
83
+ "test_data_row_variable": "test_order_1",
84
+ "code": "test_order_1 = Order(customer_id=1, notes='Order 1 notes', amount_total=150)"
85
+ },
86
+ {
87
+ "test_data_row_variable": "test_order_2",
88
+ "code": "test_order_2 = Order(customer_id=2, notes='Order 2 notes', amount_total=0)"
89
+ },
90
+ {
91
+ "test_data_row_variable": "test_order_3",
92
+ "code": "test_order_3 = Order(customer_id=2, notes='Order 3 notes', amount_total=0)"
93
+ },
94
+ {
95
+ "test_data_row_variable": "test_order_4",
96
+ "code": "test_order_4 = Order(customer_id=3, notes='Order 4 notes', amount_total=150)"
97
+ },
98
+ {
99
+ "test_data_row_variable": "test_item_1",
100
+ "code": "test_item_1 = Item(order_id=1, product_id=1, quantity=3, unit_price=50, amount=150)"
101
+ },
102
+ {
103
+ "test_data_row_variable": "test_item_2",
104
+ "code": "test_item_2 = Item(order_id=2, product_id=2, quantity=2, unit_price=0, amount=0)"
105
+ },
106
+ {
107
+ "test_data_row_variable": "test_item_3",
108
+ "code": "test_item_3 = Item(order_id=3, product_id=3, quantity=1, unit_price=0, amount=0)"
109
+ },
110
+ {
111
+ "test_data_row_variable": "test_item_4",
112
+ "code": "test_item_4 = Item(order_id=4, product_id=1, quantity=3, unit_price=50, amount=150)"
113
+ },
114
+ {
115
+ "test_data_row_variable": "test_product_1",
116
+ "code": "test_product_1 = Product(name='Product 1', unit_price=50)"
117
+ },
118
+ {
119
+ "test_data_row_variable": "test_product_2",
120
+ "code": "test_product_2 = Product(name='Product 2', unit_price=25)"
121
+ },
122
+ {
123
+ "test_data_row_variable": "test_product_3",
124
+ "code": "test_product_3 = Product(name='Product 3', unit_price=75)"
125
+ },
126
+ {
127
+ "test_data_row_variable": "test_product_4",
128
+ "code": "test_product_4 = Product(name='Product 4', unit_price=100)"
129
+ }
130
+ ],
131
+ "test_data_sqlite": "INSERT INTO customers (name, balance, credit_limit) VALUES ('Customer 1', 300, 1000);\nINSERT INTO customers (name, balance, credit_limit) VALUES ('Customer 2', 500, 750);\nINSERT INTO customers (name, balance, credit_limit) VALUES ('Customer 3', 0, 1500);\nINSERT INTO customers (name, balance, credit_limit) VALUES ('Customer 4', 200, 1200);\n\nINSERT INTO orders (customer_id, notes, date_shipped, amount_total) VALUES (1, 'Order 1 notes', NULL, 150);\nINSERT INTO orders (customer_id, notes, date_shipped, amount_total) VALUES (2, 'Order 2 notes', NULL, 0);\nINSERT INTO orders (customer_id, notes, date_shipped, amount_total) VALUES (2, 'Order 3 notes', NULL, 0);\nINSERT INTO orders (customer_id, notes, date_shipped, amount_total) VALUES (3, 'Order 4 notes', NULL, 150);\n\nINSERT INTO items (order_id, product_id, quantity, unit_price, amount) VALUES (1, 1, 3, 50, 150);\nINSERT INTO items (order_id, product_id, quantity, unit_price, amount) VALUES (2, 2, 2, 25, 0);\nINSERT INTO items (order_id, product_id, quantity, unit_price, amount) VALUES (3, 3, 1, 75, 0);\nINSERT INTO items (order_id, product_id, quantity, unit_price, amount) VALUES (4, 1, 3, 50, 150);\n\nINSERT INTO products (name, unit_price) VALUES ('Product 1', 50);\nINSERT INTO products (name, unit_price) VALUES ('Product 2', 25);\nINSERT INTO products (name, unit_price) VALUES ('Product 3', 75);\nINSERT INTO products (name, unit_price) VALUES ('Product 4', 100);",
132
+ "name": "OrderManagementSystem"
133
+ }
@@ -0,0 +1,10 @@
1
+ Create a system with customers, orders, items and products.
2
+
3
+ Include a notes field for orders.
4
+
5
+ Use LogicBank to enforce the Check Credit requirement:
6
+ 1. The Customer's balance is less than the credit limit
7
+ 2. The Customer's balance is the sum of the Order amount_total where date_shipped is null
8
+ 3. The Order's amount_total is the sum of the Item amount
9
+ 4. The Item amount is the quantity * unit_price
10
+ 5. The Item unit_price is copied from the Product unit_price
@@ -0,0 +1 @@
1
+ You are a data modelling expert and python software architect who expands on user input ideas. You create data models with at least 4 tables
@@ -0,0 +1,171 @@
1
+ Here is the simplified API for LogicBank:
2
+
3
+ Create a function called declare_logic(), consisting of calls to Rule methods.
4
+
5
+ Do not generate import statements.
6
+
7
+ Use only the methods provided below.
8
+
9
+ class Rule:
10
+ """Invoke these functions to declare rules in the created declare_logic function. """
11
+
12
+ @staticmethod
13
+ def sum(derive: Column, as_sum_of: any, where: any = None, child_role_name: str = "", insert_parent: bool=False):
14
+ """
15
+ Derive parent column as sum of designated child column, optional where
16
+
17
+ Example
18
+ Prompt
19
+ Customer.Balance = Sum(Order.amount_total where date_shipped is null)
20
+ Response
21
+ Rule.sum(derive=Customer.Balance, as_sum_of=Order.AmountTotal,
22
+ where=lambda row: row.ShippedDate is None)
23
+
24
+ Args:
25
+ derive: name of parent <class.attribute> being derived
26
+ as_sum_of: name of child <class.attribute> being summed
27
+ child_role_name: parent's child accessor attribute (required only for disambiguation)
28
+ where: optional where clause, designates which child rows are summed
29
+
30
+ """
31
+ return Sum(derive, as_sum_of, where, child_role_name, insert_parent)
32
+
33
+ @staticmethod
34
+ def count(derive: Column, as_count_of: object, where: any = None, child_role_name: str = "", insert_parent: bool=False):
35
+ """
36
+ Derive parent column as count of designated child rows
37
+
38
+ Example
39
+ Prompt
40
+ Customer.UnPaidOrders = count(Orders where ShippedDate is None)
41
+ Response
42
+ Rule.count(derive=Customer.UnPaidOrders, as_count_of=Order,
43
+ where=Lambda row: row.ShippedDate is None)
44
+
45
+ Args:
46
+ derive: name of parent <class.attribute> being derived
47
+ as_count_of: name of child <class> being counted
48
+ child_role_name: parent's child accessor attribute (required only for disambiguation)
49
+ where: optional where clause, designates which child rows are counted
50
+ """
51
+ return Count(derive, as_count_of, where, child_role_name, insert_parent)
52
+
53
+ @staticmethod
54
+ def constraint(validate: object,
55
+ calling: Callable = None,
56
+ as_condition: any = None,
57
+ error_msg: str = "(error_msg not provided)",
58
+ error_attributes=None):
59
+ """
60
+ Constraints declare condition that must be true for all commits
61
+
62
+ Example
63
+ Prompt
64
+ Customer.balance <= credit_limit
65
+ Response
66
+ Rule.constraint(validate=Customer,
67
+ as_condition=lambda row: row.Balance <= row.CreditLimit,
68
+ error_msg="balance ({row.Balance}) exceeds credit ({row.CreditLimit})")
69
+
70
+ Args:
71
+ validate: name of mapped <class>
72
+ as_condition: lambda, passed row (simple constraints)
73
+ error_msg: string, with {row.attribute} replacements
74
+ error_attributes: list of attributes
75
+
76
+ """
77
+ if error_attributes is None:
78
+ error_attributes = []
79
+ return Constraint(validate=validate, as_condition=as_condition,
80
+ error_attributes=error_attributes, error_msg=error_msg)
81
+
82
+ @staticmethod
83
+ def parent_check(validate: object,
84
+ error_msg: str = "(error_msg not provided)",
85
+ enable: bool = True):
86
+ """
87
+ Parent Checks ensure that non-null foreign keys are present in parent class
88
+
89
+ Example
90
+ Rule.parent_check(validate=Customer, enable=True, error_msg="Missing Parent")
91
+
92
+ Parent_check failures raise ConstraintException, e.g.:
93
+ try:
94
+ session.commit()
95
+ except ConstraintException as ce:
96
+ print("Constraint raised: " + str(ce))
97
+
98
+ Args:
99
+ validate: name of mapped class
100
+ error_msg: message included in exception (can have {} syntax)
101
+ enable: True (default) = enable, False means disable (tolerate orphans)
102
+
103
+ Note: False not recommended - for existing databases with bad data
104
+ Behavior is undefined for other rules (sum, count, parent references, etc)
105
+
106
+ """
107
+ return ParentCheck(validate=validate, error_msg=error_msg, enable=enable)
108
+
109
+ @staticmethod
110
+ def formula(derive: Column,
111
+ as_expression: Callable = None,
112
+ no_prune: bool = False):
113
+ """
114
+ Formulas declare column value, based on current and parent rows
115
+
116
+ Example
117
+ Prompt
118
+ Item.amount = quantity * unit_price
119
+ Response
120
+ Rule.formula(derive=OrderDetail.Amount,
121
+ as_expression=lambda row: row.UnitPrice * row.Quantity)
122
+
123
+ Args:
124
+ derive: <class.attribute> being derived
125
+ as_expression: lambda, passed row (for syntax checking)
126
+ no_prune: disable pruning (rarely used, default False)
127
+ """
128
+ return Formula(derive=derive,
129
+ as_expression=as_expression,
130
+ no_prune=no_prune)
131
+
132
+ @staticmethod
133
+ def copy(derive: Column, from_parent: any):
134
+ """
135
+ Copy declares child column copied from parent column
136
+
137
+ Example
138
+ Prompt
139
+ Store the Item.unit_price as a copy from Product.unit_price
140
+ Response
141
+ Rule.copy(derive=OrderDetail.UnitPrice, from_parent=Product.UnitPrice)
142
+
143
+ Args:
144
+ derive: <class.attribute> being copied into
145
+ from_parent: <parent-class.attribute> source of copy
146
+ """
147
+ return Copy(derive=derive, from_parent=from_parent)
148
+
149
+
150
+ Expanded example:
151
+
152
+ Prompt:
153
+
154
+ Use Logic Bank to enforce the Check Credit requirement (do not generate check constraints):
155
+ 1. Customer.balance <= credit_limit
156
+ 2. Customer.balance = Sum(Order.amount_total where date_shipped is null)
157
+ 3. Order.amount_total = Sum(Item.amount)
158
+ 4. Item.amount = quantity * unit_price
159
+ 5. Store the Item.unit_price as a copy from Product.unit_price
160
+
161
+ Response:
162
+
163
+ def declare_logic(): # created by Web/GenAI for ApiLogicServer, LogicBank
164
+
165
+ Rule.sum(derive=CustomerAccount.balance, as_sum_of=Order.amount_total, where=lambda row: row.date_shipped is None)
166
+ Rule.sum(derive=Order.amount_total, as_sum_of=Item.amount)
167
+ Rule.formula(derive=Item.amount, as_expression=lambda row: row.quantity * row.unit_price)
168
+ Rule.copy(derive=Item.unit_price, from_parent=Product.unit_price)
169
+ Rule.constraint(validate=CustomerAccount,
170
+ as_condition=lambda row: row.balance <= row.credit_limit,
171
+ error_msg="Customer balance ({row.balance}) exceeds credit limit ({row.credit_limit})")
@@ -0,0 +1,21 @@
1
+ Use SQLAlchemy to create a sqlite database named system/genai/temp/create_db_models.sqlite, to
2
+
3
+ Create a system with customers, orders, items and products.
4
+
5
+ Do not generate any logic, including logic bank.
6
+
7
+ Do not generate derived attributes such as Customer.balance, Customer.credit_limit, Order.amount_total, Item.unit_price, or Item.amount.
8
+
9
+ Include a notes field for orders.
10
+
11
+ Hints: use autonum keys (for all tables - including for link/join/junction/intersection tables), allow nulls, foreign keys, no check constraints.
12
+
13
+ Remember that SQLite DateTime type only accepts Python datetime and date objects as input,
14
+ this means you can not enter string attributes where a date or datetime object is expected.
15
+
16
+ Don't install additional packages.
17
+ Don't use the faker pip package.
18
+
19
+ Create some test data, use foreign key columns instead of relationship names for the data. Do not print the test data. Be sure to initialize derived attributes, but do not rely on Logic Bank.
20
+
21
+ For each data model class, create a docstring describing the table, prefixed with 'description: '.
@@ -0,0 +1,94 @@
1
+ Certainly! Here's how you might create a system with `customers`, `orders`, `items`, and `products` using SQLAlchemy. After defining the data model, I will generate test data and then proceed to use LogicBank for business rules in the `declare_logic()` function.
2
+
3
+ ```python
4
+ from sqlalchemy import create_engine, Column, Integer, String, Float, ForeignKey, Date, DateTime
5
+ from sqlalchemy.ext.declarative import declarative_base
6
+ from sqlalchemy.orm import sessionmaker, relationship
7
+ from datetime import datetime
8
+
9
+ Base = declarative_base()
10
+
11
+ class Customer(Base):
12
+ """
13
+ description: Table for storing customer information.
14
+ """
15
+ __tablename__ = 'customers'
16
+
17
+ id = Column(Integer, primary_key=True, autoincrement=True)
18
+ name = Column(String, nullable=True)
19
+ credit_limit = Column(Float, nullable=True)
20
+
21
+ class Order(Base):
22
+ """
23
+ description: Table for orders linked to customers.
24
+ """
25
+ __tablename__ = 'orders'
26
+
27
+ id = Column(Integer, primary_key=True, autoincrement=True)
28
+ customer_id = Column(Integer, ForeignKey('customers.id'), nullable=True)
29
+ date_shipped = Column(Date, nullable=True)
30
+ notes = Column(String, nullable=True)
31
+ # Placeholder for derived attribute (commented out but indicated)
32
+ # amount_total = Column(Float, default=0.0)
33
+
34
+ class Product(Base):
35
+ """
36
+ description: Table for products.
37
+ """
38
+ __tablename__ = 'products'
39
+
40
+ id = Column(Integer, primary_key=True, autoincrement=True)
41
+ name = Column(String, nullable=True)
42
+ unit_price = Column(Float, nullable=True)
43
+
44
+ class Item(Base):
45
+ """
46
+ description: Table for items linked to orders and products.
47
+ """
48
+ __tablename__ = 'items'
49
+
50
+ id = Column(Integer, primary_key=True, autoincrement=True)
51
+ order_id = Column(Integer, ForeignKey('orders.id'), nullable=True)
52
+ product_id = Column(Integer, ForeignKey('products.id'), nullable=True)
53
+ quantity = Column(Integer, nullable=True)
54
+ # Placeholder for derived attributes (commented out but indicated)
55
+ # unit_price = Column(Float, nullable=True)
56
+ # amount = Column(Float, default=0.0)
57
+
58
+ # Create an SQLite database
59
+ engine = create_engine('sqlite:///system/genai/temp/create_db_models.sqlite')
60
+ Base.metadata.create_all(engine)
61
+
62
+ Session = sessionmaker(bind=engine)
63
+ session = Session()
64
+
65
+ # Creating test data for tables
66
+ customer1 = Customer(name="John Doe", credit_limit=5000.0)
67
+ customer2 = Customer(name="Jane Smith", credit_limit=10000.0)
68
+ session.add_all([customer1, customer2])
69
+ session.commit()
70
+
71
+ order1 = Order(customer_id=customer1.id, date_shipped=None, notes="Express delivery")
72
+ order2 = Order(customer_id=customer2.id, date_shipped=datetime.now().date(), notes="Standard delivery")
73
+ session.add_all([order1, order2])
74
+ session.commit()
75
+
76
+ product1 = Product(name="Laptop", unit_price=1000.0)
77
+ product2 = Product(name="Smartphone", unit_price=500.0)
78
+ session.add_all([product1, product2])
79
+ session.commit()
80
+
81
+ item1 = Item(order_id=order1.id, product_id=product1.id, quantity=2)
82
+ item2 = Item(order_id=order1.id, product_id=product2.id, quantity=3)
83
+ item3 = Item(order_id=order2.id, product_id=product2.id, quantity=1)
84
+ session.add_all([item1, item2, item3])
85
+ session.commit()
86
+
87
+ ```
88
+
89
+ ### Expanded Explanation of Data Model and Logic
90
+ - **`Customer` Table**: Stores basic customer information including `name` and `credit_limit`.
91
+ - **`Order` Table**: Links orders to customers, and contains fields for shipment date (`date_shipped`) and additional comments or `notes`.
92
+ - **`Product` Table**: Contains product information like `name` and `unit_price`.
93
+ - **`Item` Table**: Represents purchased items within a particular order, tracking `quantity` and relating to `Product` and `Order`.
94
+
@@ -0,0 +1,6 @@
1
+ Use LogicBank to enforce the Check Credit requirement:
2
+ 1. Customer.balance <= credit_limit
3
+ 2. Customer.balance = Sum(Order.amount_total where date_shipped is null)
4
+ 3. Order.amount_total = Sum(Item.amount)
5
+ 4. Item.amount = quantity * unit_price
6
+ 5. Store the Item.unit_price as a copy from Product.unit_price