AI and Rules for Agile Microservices in Minutes
Create a project instantly using AI and API/Logic Automation. Customize later with Python, Rules, and Flask, which is open-source.
Join the DZone community and get the full member experience.
Join For FreeHere's how to use AI and API Logic Server to create complete running systems in minutes:
- Use ChatGPT for Schema Automation: create a database schema from natural language.
- Use Open Source API Logic Server: create working software with one command.
- App Automation: a multi-page, multi-table admin app.
- API Automation: A JSON: API, crud for each table, with filtering, sorting, optimistic locking, and pagination.
- Customize the project with your IDE:
- Logic Automation using rules: declare spreadsheet-like rules in Python for multi-table derivations and constraints - 40X more concise than code.
- Use Python and standard libraries (Flask, SQLAlchemy) and debug in your IDE.
- Iterate your project:
- Revise your database design and logic.
- Integrate with B2B partners and internal systems.
This process leverages your existing IT infrastructure: your IDE, GitHub, the cloud, your database… open source. Let's see how.
1. AI: Schema Automation
You can use an existing database or create a new one with ChatGPT or your database tools.
Use ChatGPT to generate SQL commands for database creation:
Create a sqlite database for customers, orders, items and product
Hints: use autonum keys, allow nulls, Decimal types, foreign keys, no check constraints.
Include a notes field for orders.
Create a few rows of only customer and product data.
Enforce the Check Credit requirement:
Customer.Balance <= CreditLimit
Customer.Balance = Sum(Order.AmountTotal where date shipped is null)
Order.AmountTotal = Sum(Items.Amount)
Items.Amount = Quantity * UnitPrice
Store the Items.UnitPrice as a copy from Product.UnitPrice
Note the hint above. As we've heard, "AI requires adult supervision." The hint was required to get the desired SQL.
This creates standard SQL like this. Copy the generated SQL commands into a file, say, sample-ai.sql
:
Then, create the database:
sqlite3 sample_ai.sqlite < sample_ai.sql
2. API Logic Server: Create
Given a database (whether or not it's created from AI), API Logic Server creates an executable, customizable project with the following single command:
$ ApiLogicServer create --project_name=sample_ai --db_url=sqlite:///sample_ai.sqlite
This creates a project you can open with your IDE, such as VSCode (see below). The project is now ready to run; press F5. It reflects the automation provided by the create
command:
- API Automation: a self-serve API ready for UI developers and;
- App Automation: an Admin app ready for Back Office Data Maintenance and Business User Collaboration.
Let's explore the App and API Automation from the create
command.
App Automation
App Automation means that ApiLogicServer create
creates a multi-page, multi-table Admin App automatically. This does not consist of hundreds of lines of complex HTML and JavaScript; it's a simple yaml file that's easy to customize.
Ready for business user collaboration,
back-office data maintenance...
in minutes.
API Automation
App Automation means that ApiLogicServer create
creates a JSON: API automatically. Your API provides an endpoint for each table, with related data access, pagination, optimistic locking, filtering, and sorting.
It would take days to months to create such an API
using frameworks.
UI App Developers can use the API to create custom apps immediately, using Swagger to design their API call and copying the URI into their JavaScript code. APIs are thus self-serve: no server coding is required.
Custom App Dev is unblocked: Day 1.
3. Customize
So, we have working software in minutes. It's running, but we really can't deploy it until we have logic and security, which brings us to customization.
Projects are designed for customization, using standards: Python, frameworks (e.g., Flask, SQLAlchemy), and your IDE for code editing and debugging.
Not only Python code but also Rules.
Logic Automation
Logic Automation means that you can declare spreadsheet-like rules using Python. Such logic maintains database integrity with multi-table derivations, constraints, and security. Rules are 40X more concise than traditional code and can be extended with Python.
Rules are an executable design. Use your IDE (code completion, etc.) to replace 280 lines of code with the five spreadsheet-like rules below. Note they map exactly to our natural language design:
1. Debugging
The screenshot above shows our logic declarations and how we debug them:
- Execution is paused at a breakpoint in the debugger, where we can examine the state and execute step by step.
- Note the logging for inserting an
Item
. Each line represents a rule firing and shows the complete state of the row.
2. Chaining: Multi-Table Transaction Automation
Note that it's a Multi-Table Transaction
, as indicated by the log indentation. This is because, like a spreadsheet, rules automatically chain, including across tables.
3. 40X More Concise
The five spreadsheet-like rules represent the same logic as 200 lines of code, shown here. That's a remarkable 40X decrease in the backend half of the system.
4. Automatic Re-use
The logic above, perhaps conceived for Place order, applies automatically to all transactions: deleting an order, changing items, moving an order to a new customer, etc. This reduces code and promotes quality (no missed corner cases).
5. Automatic Optimizations
SQL overhead is minimized by pruning, and by eliminating expensive aggregate queries. These can result in orders of magnitude impact.
This is because the rule engine is not based on a Rete algorithm but is highly optimized for transaction processing and integrated with the SQLAlchemy ORM (Object Relational Manager).
6. Transparent
Rules are an executable design. Note they map exactly to our natural language design (shown in comments) readable by business users. This complements running screens to facilitate agile collaboration.
Security Automation
Security Automation means you activate login-access security and declare grants (using Python) to control row access for user roles. Here, we filter less active accounts for users with the sales role:
Grant( on_entity = models.Customer,
to_role = Roles.sales,
filter = lambda : models.Customer.CreditLimit > 3000,
filter_debug = "CreditLimit > 3000")
4. Iterate: Rules + Python
So, we have completed our one-day project. The working screens and rules facilitate agile collaboration, which leads to agile iterations.
Automation helps here, too: not only are spreadsheet-like rules 40X more concise, but they meaningfully simplify iterations and maintenance. Let’s explore this with two changes:
Requirement 1: Green Discounts
Give a 10% discount for carbon-neutral products for 10 items or more.
Requirement 2: Application Integration
Send new Orders to Shipping using a Kafka message.
Enable B2B partners to place orders with a custom API.
Revise Data Model
In this example, a schema change was required to add the Product.CarbonNeutral column. This affects the ORM models, the API, etc. So, we want these updated but retain our customizations.
This is supported using the ApiLogicServer rebuild-from-database
command to update existing projects to a revised schema, preserving customizations.
Iterate Logic: Add Python
Here is our revised logic to apply the discount and send the Kafka message:
Extend API
We can also extend our API for our new B2BOrder endpoint using standard Python and Flask:
Note: Kafka is not activated in this example. To explore a running Tutorial for application integration with running Kafka, click here.
Notes on Iteration
This illustrates some significant aspects of how logic supports iteration.
Maintenance Automation
Along with perhaps documentation, one of the tasks programmers most loathe is maintenance. That’s because it’s not about writing code, but archaeology; deciphering code someone else wrote, just so you can add four or five lines that’ll hopefully be called and function correctly.
Logic Automation changes that with Maintenance Automation, which means:
- Rules automatically order their execution (and optimizations) based on system-discovered dependencies.
- Rules are automatically reused for all relevant transactions.
So, to alter logic, you just “drop a new rule in the bucket,” and the system will ensure it’s called in the proper order and re-used over all the relevant Use Cases.
Extensibility: With Python
In the first case, we needed to do some if/else testing, and it was more convenient to add a dash of Python. While this is pretty simple Python as a 4GL, you have the full power of object-oriented Python and its many libraries.
For example, our extended API leverages Flask and open-source libraries for Kafka messages.
Rebuild: Logic Preserved
Recall we were able to iterate the schema and use the ApiLogicServer rebuild-from-database
command. This updates the existing project, preserving customizations.
5. Deploy
API Logic Server provides scripts to create Docker images from your project. You can deploy these to the cloud or your local server.
For more information, see here.
Summary
In minutes, you've used ChatGPT and API Logic Server to convert an idea into working software. It required only five rules and a few dozen lines of Python. The process is simple:
Create the Schema with
ChatGPT.
Create the Project with
ApiLogicServer.
- A Self-Serve API to unblock UI Developers: Day 1
- An Admin App for Business User Collaboration: Day 1
Customize the project.
- With Rules: 40X more concise than code.
- With Python: for complete flexibility.
Iterate the project in your IDE to implement new requirements.
- Prior customizations are preserved.
It all works with standard tooling: Python, your IDE, and container-based deployment.
You can execute the steps in this article with the detailed tutorial: click here.
Published at DZone with permission of Val Huber, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments