### Generate SQL Schema from Python Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Generates SQL CREATE TABLE statements based on the structure of TrainDB. It takes an instance of TrainDB as input and outputs a string containing the SQL schema. This is useful for database setup and understanding data structures. ```python print(generate_schema_sql(TrainDB())) ``` -------------------------------- ### Connect to PostgreSQL using LibPQ.jl in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md Establishes a connection to a PostgreSQL database using the LibPQ.jl library. This is the initial step before preparing and executing queries against the database. It requires a database name for the connection. ```julia conn = Connection("dbname=test_db"); ``` -------------------------------- ### Execute SQL Queries on PostgreSQL with Prepared Statements in Julia Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt This code shows how to connect to a PostgreSQL database using `LibPQ.jl`, prepare an SQL statement generated by AlgebraicRelations.jl, and execute it with parameters. It demonstrates creating a database connection, preparing a statement from a query object `q`, and executing the statement with a specific parameter value. The results are returned as a `DataFrame`. ```julia using AlgebraicRelations using LibPQ using DataFrames # Create database connection conn = Connection("dbname=test_db user=postgres password=mypass host=localhost") # Prepare statement from query statement = prepare(conn, q) # Execute with parameters (accuracy threshold >= 0.6) result = execute(statement, [0.6]) # Result is a DataFrame println(result) ``` -------------------------------- ### Create PostgreSQL Database Connection Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Establishes a connection to a PostgreSQL database using the Connection constructor. The connection string specifies the database name and other connection parameters. This connection object is used for subsequent query operations. ```julia conn = Connection("dbname=test_wf"); ``` -------------------------------- ### Import AlgebraicRelations Libraries Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Imports essential AlgebraicRelations modules for database operations, query generation, workflow presentations, and user interface functionality. These modules provide the core functionality needed to define workflows and generate SQL schemas. ```julia using AlgebraicRelations.DB using AlgebraicRelations.Queries using AlgebraicRelations.Presentations using AlgebraicRelations.Interface ``` -------------------------------- ### Prepare and Execute SQL Statement with Arguments in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md Prepares an SQL statement for execution and then executes it with provided arguments. This allows for parameterized queries, enhancing security and efficiency. The `execute` function returns a DataFrame containing the query results. ```julia statement = prepare(conn,q) execute(statement, [0.6]) ``` -------------------------------- ### Construct Neural Network Query in Python Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Demonstrates constructing a query using the `@query` macro in Python. It specifies the database, output variables, and a `where` clause that defines relationships between tables using workflow processes and a comparison operator. The output includes generating the SQL query and a visual graph representation. ```python q = @query TrainDB() (im_train, nn, im_test, acc, md2) where (im_train, im_test, nn, nn_trained, acc, md, md2, _base_acc, im) begin train(nn, im_train, nn_trained, md) evaluate(nn_trained, im_test, acc, md2) split(im, im_train, im_test) >=(acc, _base_acc) end print(to_sql(q)) draw_query(q) ``` -------------------------------- ### Initialize Data Fabric with Multiple Heterogeneous Sources Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt Create a unified Data Fabric that integrates in-memory ACSet sources with database backends (SQLite, PostgreSQL). The fabric automatically routes data operations to the correct source and maintains foreign key relationships across sources. Dependencies: AlgebraicRelations, Catlab, ACSets, SQLite, DBInterface. ```Julia using AlgebraicRelations using Catlab, ACSets using SQLite, DBInterface # Initialize data fabric fabric = DataFabric() # Define first schema (Climate) @present SchClimate(FreeSchema) begin Name::AttrType Climate::Ob climate_type::Attr(Climate, Name) end @acset_type Climate(SchClimate) # Add in-memory data source climate = InMemory(Climate{Symbol}()) climate_src = add_source!(fabric, climate) # Add data to fabric add_part!(fabric, :Climate, climate_type=:Cool) add_part!(fabric, :Climate, climate_type=:Warm) # Define second schema (Winemaker) with database backend @present SchWinemaker(FreeSchema) begin (Name, Region)::AttrType Winemaker::Ob region::Attr(Winemaker, Region) winemaker::Attr(Winemaker, Name) end @acset_type Winemaker(SchWinemaker) winemakers = Winemaker{Symbol, FK{Climate}}() winemaker_db = DBSource(SQLite.DB(), acset_schema(winemakers)) # Load table schema into database τ = AlgebraicRelations.Fabric.DatabaseDS.DBSourceTrait() execute![τ](winemaker_db, FunSQL.render(winemaker_db, winemakers)) # Add database source to fabric winemaker_src = add_source!(fabric, winemaker_db, :Winemaker) # Add foreign key relationship between sources add_fk!(fabric, winemaker_src, climate_src, :Winemaker!region => :Climate!Climate_id) # Insert data across fabric (automatically routed to correct source) add_part!(fabric, :Winemaker, (_id=1, region=FK{Climate}(1), winemaker=:Gaja)) # Query across multiple sources catalog(fabric) ``` -------------------------------- ### Create Graphviz Edges with NodeID and Styling Attributes Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb Constructs Catlab Graphviz Edge objects connecting pairs of NodeID objects with empty identifiers and graph styling attributes. Uses OrderedCollections.OrderedDict to define font, shape, margin, and edge length properties for graph visualization. ```julia Catlab.Graphics.Graphviz.Edge( Catlab.Graphics.Graphviz.NodeID[ Catlab.Graphics.Graphviz.NodeID("n4", "", ""), Catlab.Graphics.Graphviz.NodeID("n10", "", "") ], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}() ), Catlab.Graphics.Graphviz.Edge( Catlab.Graphics.Graphviz.NodeID[ Catlab.Graphics.Graphviz.NodeID("n3", "", ""), Catlab.Graphics.Graphviz.NodeID("n11", "", "") ], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}() ) OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}( :fontname => "Serif" ) OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}( :fontname => "Serif", :shape => "ellipse", :margin => "0.05,0.025", :width => "0.5", :height => "0.5" ) OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}( :fontname => "Serif", :len => "0.5" ) ``` -------------------------------- ### Define and Visualize ML Workflow with @program Macro in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Creates a declarative workflow program that extracts images from files, splits them into training and test sets, trains a neural network, and evaluates its accuracy. The @program macro captures the workflow structure, and draw_workflow() generates a visual representation using Graphviz. This approach allows verification that all workflow components are properly included in the schema definition. ```julia p = @program present (nn::NeuralNet, file::Files) begin images = extract(file) im_train, im_test = split(images) trained_nn, _ = train(nn, im_train) acc, _ = evaluate(trained_nn, im_test) return acc end; draw_workflow(p) ``` -------------------------------- ### Convert Query to SQL - Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb Demonstrates conversion of the algebraic query to executable SQL. The to_sql() function generates a parameterized SQL SELECT statement that joins two tables with WHERE conditions for salary range filtering. Output includes table aliases and parameter placeholders ($1, $2) for dynamic salary bounds. ```sql SELECT t1.Name2 AS emp_name, t2.Salary2 AS salary FROM get_name AS t1, get_salary AS t2 WHERE t2.Person1=t1.Person1 AND t2.Salary2<=$2 AND t2.Salary2>=$1 ``` -------------------------------- ### Convert Algebraic Presentation to SQL Schema with Type Mapping Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt Transform a Catlab FreeSchema presentation into an executable SQL schema with automatic type mapping and foreign key generation. The render_schema function produces CREATE TABLE and ALTER TABLE statements ready for database execution. Dependencies: AlgebraicRelations, Catlab, SQLite, DBInterface. ```Julia using AlgebraicRelations using Catlab using SQLite # Define business schema presentation @present Business(FreeSchema) begin (val!Salary, Name)::AttrType (Employee, Manager, Income, Salary)::Ob name::Attr(Employee, Name) (man!employee, man!manager)::Hom(Manager, Employee) inc!employee::Hom(Income, Employee) inc!salary::Hom(Income, Salary) sal!salary::Attr(Salary, val!Salary) end # Create SQL schema with type mapping busSchema = SQLSchema(Business; types = Dict( :val!Salary => Float64, :Name => String )) # Render schema as SQL statements schema_sql = render_schema(busSchema) println(schema_sql) # Output: # CREATE TABLE IF NOT EXISTS Employee(Employee_id SERIAL PRIMARY KEY, name TEXT); # CREATE TABLE IF NOT EXISTS Manager(Manager_id SERIAL PRIMARY KEY, ...); # CREATE TABLE IF NOT EXISTS Income(Income_id SERIAL PRIMARY KEY, ...); # CREATE TABLE IF NOT EXISTS Salary(Salary_id SERIAL PRIMARY KEY, val!salary REAL); # ALTER TABLE Manager ADD COLUMN employee_Manager_id INTEGER REFERENCES Employee(Employee_id); # ... # Execute schema creation db = SQLite.DB() for stmt in split(schema_sql, "\n") DBInterface.execute(db, stmt) end ``` -------------------------------- ### Generate PostgreSQL Schema from Presentation in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md This code converts a defined presentation into a database schema and then generates the corresponding SQL `CREATE TABLE` statements. It first transforms the presentation object into a schema object and then uses `generate_schema_sql` to produce the SQL DDL. The output is a string containing the SQL commands to create the tables. ```julia TrainDB = present_to_schema(present); print(generate_schema_sql(TrainDB())) ``` -------------------------------- ### PostgreSQL CREATE TABLE Statements for AlgebraicRelations Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md These are SQL `CREATE TABLE` statements generated by AlgebraicRelations.jl from a defined presentation schema. Each statement defines a table corresponding to a process in the schema, with columns representing the input and output data types of that process. The column names include a number suffix to distinguish between multiple inputs/outputs of the same type. ```sql CREATE TABLE evaluate (NeuralNet1 text, Images2 text, Accuracy3 real, Metadata4 text); CREATE TABLE extract (File1 text, Images2 text); CREATE TABLE split (Images1 text, Images2 text, Images3 text); CREATE TABLE train (NeuralNet1 text, Images2 text, NeuralNet3 text, Metadata4 text); ``` -------------------------------- ### Define Schema with Types and Processes in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb Initializes a presentation object and adds custom types (Person, ID, Name, Salary) and processes (get_employees, get_manager, get_salary, get_name) to it. This defines the structure and operations for the schema. The presentation is then converted into a schema object. ```julia present = Presentation() Person, ID, Name, Salary = add_types!(present, [(:Person, Int), (:ID, Int), (:Name, String), (:Salary, Real)]); get_employees, get_manager, get_salary, get_name = add_processes!(present, [(:get_employees, Person, ID), (:get_manager, Person, Person), (:get_salary, Person, Salary), (:get_name, Person, Name)]); Workplace = present_to_schema(present) schema = Workplace(); ``` -------------------------------- ### Import Libraries for Algebraic Relations Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb Imports essential modules from the AlgebraicRelations library for database operations, queries, interface, and presentations. These modules are necessary for defining and interacting with algebraic relations. ```julia using AlgebraicRelations.DB, AlgebraicRelations.Queries, AlgebraicRelations.Interface, AlgebraicRelations.Presentations; ``` -------------------------------- ### Define Workflow Types and Processes in AlgebraicRelations Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Creates a presentation object to define a machine learning workflow by adding typed objects (Files, Images, NeuralNet, Accuracy, Metadata) and processes (extract, split, train, evaluate) that operate on these types. The workflow captures data flow through each process, enabling automatic SQL schema generation and query construction based on the defined workflow structure. ```julia # Initialize presentation object present = Presentation() # Add types to presentation Files, Images, NeuralNet, Accuracy, Metadata = add_types!(present, [(:Files, String), (:Images, String), (:NeuralNet, String), (:Accuracy, Real), (:Metadata, String)]); # Add Processes to presentation extract, split, train, evaluate = add_processes!(present, [(:extract, Files, Images), (:split, Images, Images⊗Images), (:train, NeuralNet⊗Images, NeuralNet⊗Metadata), (:evaluate, NeuralNet⊗Images, Accuracy⊗Metadata)]); ``` -------------------------------- ### SQL Schema Definitions Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Provides the SQL CREATE TABLE statements generated by the `generate_schema_sql` function. These definitions outline the structure of tables used for evaluating, extracting, splitting, and training models. ```sql CREATE TABLE evaluate (NeuralNet1 text, Images2 text, Accuracy3 real, Metadata4 text); CREATE TABLE extract (Files1 text, Images2 text); CREATE TABLE split (Images1 text, Images2 text, Images3 text); CREATE TABLE train (NeuralNet1 text, Images2 text, NeuralNet3 text, Metadata4 text); ``` -------------------------------- ### Execute Parameterized PostgreSQL Query Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Executes a prepared statement with parameterized input values. The execute function takes the prepared statement and an array of parameters (in this case [0.6]). Returns a DataFrame containing the query results with columns for training images, neural networks, test images, accuracy, and model identifiers. ```julia execute(statement1, [0.6]) ``` -------------------------------- ### Interact with SQLite Databases using ACSet and DBSource in Julia Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt This snippet illustrates interacting with SQLite databases using AlgebraicRelations.jl's `DBSource` and Catlab.jl's ACSet framework. It defines a database schema using `@present`, creates an ACSet type, and initializes a `DBSource` for SQLite. The code covers creating tables, inserting data via the ACSet interface, querying data using `subpart`, finding records with `incident`, and updating records using `set_subpart!` with a `WhereClause`. ```julia using AlgebraicRelations using Catlab, ACSets using SQLite, DBInterface # Define schema presentation @present SchClass(FreeSchema) begin Name::AttrType Class::Ob subject::Attr(Class, Name) end # Create ACSet type from schema @acset_type Class(SchClass) classes = Class{Symbol}() # Create database source class_db = DBSource(SQLite.DB(), acset_schema(classes)) # Create table in database execute!(class_db, "create table `Class` (_id int, subject varchar(255))") # Insert data using ACSet interface add_part!(class_db, :Class, [ (_id=1, subject="Chemistry"), (_id=2, subject="Physics") ]) # Query data using subpart subjects = subpart(class_db, :subject) println(subjects) # Find specific records using incident chem_records = incident(class_db, [:Physics, :Chemistry], :subject) println(chem_records) # Update records with where clause set_subpart!(class_db, :Class, [(_id=3, subject="Mathematics")]; wheres = WhereClause(:(=), :_id => 3)) ``` -------------------------------- ### Prepare PostgreSQL Query Statement Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Prepares a SQL query statement on the established database connection using the prepare function. The prepared statement object can be reused multiple times with different parameter values for efficient query execution. ```julia statement1 = prepare(conn, q); ``` -------------------------------- ### Convert Presentation to Schema and Draw in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Converts a presentation object to a schema format using present_to_schema() and visualizes it as a directed graph using draw_schema(). The resulting Graphviz.Graph object contains nodes representing data schemas (S_Files, S_Images, etc.) and transformations (T_evaluate, T_extract, etc.) with styled attributes including shapes, colors, and labeled edges indicating data flow relationships. ```julia TrainDB = present_to_schema(present); draw_schema(present) ``` -------------------------------- ### Generate SQL Queries from Categorical Diagrams with @query Macro in Julia Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt This snippet demonstrates how to construct SQL queries using the @query macro in Julia, based on categorical relationships defined by the schema. It illustrates specifying query variables, defining relationships between processes (e.g., train, evaluate, split), and including conditions (e.g., >=). The macro translates these categorical definitions into executable SQL queries. ```julia using AlgebraicRelations using Catlab # Define query using @query macro q = @query TrainDB() (im_train, nn, im_test, acc, md2) where ( im_train, im_test, nn, nn_trained, acc, md, md2, _base_acc, im ) begin train(nn, im_train, nn_trained, md) evaluate(nn_trained, im_test, acc, md2) split(im, im_train, im_test) >=(acc, _base_acc) end # Convert to SQL sql_query = to_sql(q) println(sql_query) ``` -------------------------------- ### Julia Query for Employee Salaries within Range Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb This Julia code defines a query to retrieve employee names, manager names, and salaries. It filters employees based on their manager's salary falling within a user-defined lower and upper bound. The query is then converted to SQL and visualized as a graph. ```julia using AlgebraicRelations using Catlab.Graphics.Graphviz # Define the schema (assuming it's defined elsewhere or implicitly) # schema = ... # We generate a second query that gets all employees whose managers make a salary within some range q2 = @query schema (emp_name, man_name,salary) where (p, m, emp_name, man_name, salary, _slower, _supper) begin # Define name relationships get_name(p, emp_name) get_name(m, man_name) # Define manager relationships get_manager(p,m) # Define salary relationships (with input range) get_salary(m,salary) <=(salary, _supper) >=(salary, _slower) end # Convert to SQL and print println(to_sql(q2)) # Draw the query graph # draw_query(q2) ``` -------------------------------- ### Visualize Algebraic Relations in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb Generates a Graphviz visualization for algebraic relations. It utilizes Catlab.Graphics.Graphviz for rendering and OrderedCollections for managing display attributes. This function is primarily for internal use within the Catlab library. ```julia hviz.Html}}(:rankdir => "LR"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:shape => "plain",:style => "filled",:color => "white"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:splines => "splines") ``` -------------------------------- ### Julia Graphviz Representation of Query Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb This output represents the visualization of the Julia query in Graphviz format. It defines nodes for each relation and variable, and edges to show the connections and flow of data within the query structure. ```julia Catlab.Graphics.Graphviz.Graph("G", false, "neato", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node("n1", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => "box1",:label => "get_name")), Catlab.Graphics.Graphviz.Node("n2", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => "box2",:label => "get_name")), Catlab.Graphics.Graphviz.Node("n3", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => "box3",:label => "get_manager")), Catlab.Graphics.Graphviz.Node("n4", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => "box4",:label => "get_salary")), Catlab.Graphics.Graphviz.Node("n5", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => "box5",:label => "<=")), Catlab.Graphics.Graphviz.Node("n6", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => "box6",:label => ">=")), Catlab.Graphics.Graphviz.Node("n7", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:height => "0",:id => "outer1",:label => "",:margin => "0",:shape => "none",:style => "invis",:width => "0")), Catlab.Graphics.Graphviz.Node("n8", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:height => "0",:id => "outer2",:label => "",:margin => "0",:shape => "none",:style => "invis",:width => "0")), Catlab.Graphics.Graphviz.Node("n9", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:height => "0",:id => "outer3",:label => "",:margin => "0",:shape => "none",:style => "invis",:width => "0")), Catlab.Graphics.Graphviz.Node("n10", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => "junction",:fillcolor => "black",:height => "0.075",:id => "junction1",:label => "",:shape => "circle",:style => "filled",:width => "0.075",:xlabel => "p")) … Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n1", "", ""), Catlab.Graphics.Graphviz.NodeID("n12", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n7", "", ""), Catlab.Graphics.Graphviz.NodeID("n12", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n2", "", ""), Catlab.Graphics.Graphviz.NodeID("n13", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n8", "", ""), Catlab.Graphics.Graphviz.NodeID("n13", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n4", "", ""), Catlab.Graphics.Graphviz.NodeID("n14", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n5", "", ""), Catlab.Graphics.Graphviz.NodeID("n14", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n6", "", ""), Catlab.Graphics.Graphviz.NodeID("n14", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n9", "", ""), Catlab.Graphics.Graphviz.NodeID("n14", "", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}()), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n6", "", ""), ``` -------------------------------- ### Define Employee Salary Range Query with @query Macro - Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb Creates a declarative query using AlgebraicRelations.jl @query macro to fetch employees matching salary criteria. The query joins get_name and get_salary relations on person identity and filters by salary bounds. Dependencies include schema definition and AlgebraicRelations library; inputs are salary range parameters (_sallower, _salupper); output is a query object that can be converted to SQL or visualized. ```julia q1 = @query schema (emp_name, salary) where (p, emp_name, salary, _sallower, _salupper) begin # Define name relationships get_name(p, emp_name) # Define salary relationships (with input range) get_salary(p,salary) <=(salary, _salupper) >=(salary, _sallower) end; print(to_sql(q1)) draw_query(q1) ``` -------------------------------- ### Generate SQL Schema from Julia Schema Object Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb Generates the SQL `CREATE TABLE` statements based on the defined schema object. This function translates the abstract schema definition into concrete SQL table structures. ```julia print(generate_schema_sql(schema)); ``` -------------------------------- ### Define Training Schema using ACSet in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb This snippet defines a data schema named 'TrainingSchema' that inherits from 'TheorySQL' using Julia's Catlab.jl library. It specifies various tables (Ob) like 'split', 'extract', 'train', and 'evaluate', along with their respective attributes (Attr) which represent columns with associated data types and foreign keys. ```julia @present TrainingSchema <: TheorySQL begin # Tables split::Ob extract::Ob train::Ob evaluate::Ob # Columns of tables extract_1_Files1::Attr(extract, String) extract_2_Images2::Attr(extract, String) split_1_Images1::Attr(split, String) split_2_Images2::Attr(split, String) split_3_Images3::Attr(split, String) train_1_NeuralNet1::Attr(train, NeuralNet) train_2_Images2::Attr(train, Images) train_3_NeuralNet3::Attr(train, NeuralNet) train_4_Metadata4::Attr(train, Metadata) evaluate_1_NeuralNet1::Attr(evaluate, NeuralNet) evaluate_2_Images2::Attr(evaluate, Images) evaluate_3_Accuracy3::Attr(evaluate, Accuracy) evaluate_4_Metadata4::Attr(evaluate, Metadata) end; ``` -------------------------------- ### Add Data with Foreign Key References in Julia Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt Demonstrates how to add data parts (e.g., Wine) to a fabric, establishing foreign key relationships to other entities (e.g., Country). This is useful for building structured datasets with referential integrity. ```julia add_part!(fabric, :Wine, name=:Barolo, origin=FK{Country}(1)) # Italy add_part!(fabric, :Wine, name=:Bordeaux, origin=FK{Country}(2)) # France ``` -------------------------------- ### Generate Graphviz Edge Objects in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/ml_workflow_demo/ml_demo.ipynb This code defines a series of edges for a Graphviz graph, representing relationships between different components. Each edge is created using Catlab.jl's Graphics.Graphviz.Edge, specifying source and target nodes with their IDs and ports, and includes associated metadata like comments and unique identifiers. ```julia Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n1p2", "e", ""), Catlab.Graphics.Graphviz.NodeID("n3", "in1", "w")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html"}}(:comment => "Files",:id => "e1")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n5", "out1", "e"), Catlab.Graphics.Graphviz.NodeID("n6", "in1", "w")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html"}}(:comment => "NeuralNet",:id => "e2")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n4", "out1", "e"), Catlab.Graphics.Graphviz.NodeID("n5", "in2", "w")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html"}}(:comment => "Images",:id => "e3")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n3", "out1", "e"), Catlab.Graphics.Graphviz.NodeID("n4", "in1", "w")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html"}}(:comment => "Images",:id => "e4")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n6", "out1", "e"), Catlab.Graphics.Graphviz.NodeID("n2p1", "w", "")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html"}}(:comment => "Accuracy",:id => "e5")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n4", "out2", "e"), Catlab.Graphics.Graphviz.NodeID("n6", "in2", "w")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html"}}(:comment => "Images",:id => "e6")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID("n1p1", "e", ""), Catlab.Graphics.Graphviz.NodeID("n5", "in1", "w")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html"}}(:comment => "NeuralNet",:id => "e7")) ``` -------------------------------- ### SQL Translation of Julia Employee Salary Query Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/examples/company_demo/concise_demo_ACT.ipynb This SQL query is generated from the Julia algebraic relation. It joins multiple 'get_name', 'get_manager', and 'get_salary' tables to retrieve employee and manager information, filtering based on a salary range provided as parameters ($1 and $2). ```sql SELECT t1.Name2 AS emp_name, t2.Name2 AS man_name, t4.Salary2 AS salary FROM get_name AS t1, get_name AS t2, get_manager AS t3, get_salary AS t4 WHERE t3.Person1=t1.Person1 AND t3.Person2=t2.Person1 AND t4.Person1=t2.Person1 AND t4.Salary2<=$2 AND t4.Salary2>=$1 ``` -------------------------------- ### Define Database Schema with Presentations in Julia Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt This code defines a database schema by creating a Presentation object, adding types (objects) and processes (morphisms), and then converting it into a database schema representation. It demonstrates how to add types with associated Julia datatypes and processes that transform these types, including handling multiple inputs/outputs using ⊗. Finally, it generates SQL schema statements for the defined types. ```julia using AlgebraicRelations using Catlab # Initialize presentation object present = Presentation() # Add types to presentation with their Julia datatypes File, Images, NeuralNet, Accuracy, Metadata = add_types!(present, [ (:File, String), (:Images, String), (:NeuralNet, String), (:Accuracy, Real), (:Metadata, String) ]) # Add processes that transform types (using ⊗ for multiple inputs/outputs) extract, split, train, evaluate = add_processes!(present, [ (:extract, File, Images), (:split, Images, Images⊗Images), (:train, NeuralNet⊗Images, NeuralNet⊗Metadata), (:evaluate, NeuralNet⊗Images, Accuracy⊗Metadata) ]) # Convert presentation to database schema TrainDB = present_to_schema(present) # Generate SQL schema statements println(generate_schema_sql(TrainDB())) ``` -------------------------------- ### Catalog Unified Schema with Foreign Key Relationships in Julia Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt Shows how to query and display the unified schema of a data fabric, including its foreign key relationships. This function is essential for understanding the structure and connections within the managed data. ```julia catalog(fabric) # Shows unified schema with FK relationships ``` -------------------------------- ### Define Processes for Presentation Schema in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md This code defines the processes (functions or transformations) that operate on the data types within a presentation schema. For each process, it specifies a name, input types, and output types. The `⊗` symbol is used to denote the monoidal product for multiple input/output types. The function returns the newly added process objects. ```julia extract, split, train, evaluate = add_processes!(present, [(:extract, File, Images), (:split, Images, Images⊗Images), (:train, NeuralNet⊗Images, NeuralNet⊗Metadata), (:evaluate, NeuralNet⊗Images, Accuracy⊗Metadata)]); ``` -------------------------------- ### Define Foreign Key Relationships Between Data Sources Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt Establish complex relationships between multiple data sources using foreign keys in the Data Fabric. The add_fk! function maps attributes across sources, enabling joins and referential integrity across heterogeneous backends. Dependencies: AlgebraicRelations, Catlab, ACSets. ```Julia using AlgebraicRelations using Catlab, ACSets fabric = DataFabric() # Define Country schema @present SchCountry(FreeSchema) begin Name::AttrType Country::Ob country::Attr(Country, Name) end @acset_type Country(SchCountry) country = InMemory(Country{Symbol}()) country_src = add_source!(fabric, country) add_part!(fabric, :Country, country=:Italy) add_part!(fabric, :Country, country=:France) # Define Wine schema with foreign key to Country @present SchWine(FreeSchema) begin (Name, Country)::AttrType Wine::Ob origin::Attr(Wine, Country) name::Attr(Wine, Name) end @acset_type Wine(SchWine) wine = InMemory(Wine{Symbol, FK{Country}}()) wine_src = add_source!(fabric, wine) # Establish foreign key relationship add_fk!(fabric, wine_src, country_src, :Wine!origin => :Country!Country_id) ``` -------------------------------- ### Create Query with @query Macro in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md Defines a query using the @query macro, specifying output objects, all objects, and relationships. The macro simplifies the process of constructing complex queries based on defined workflow primitives. It takes relationships as input and generates a structured query object. ```julia q = @query TrainDB() (im_train, nn, im_test, acc, md2) where (im_train, im_test, nn, nn_trained, acc, md, md2, _base_acc, im) begin train(nn, im_train, nn_trained, md) evaluate(nn_trained, im_test, acc, md2) split(im, im_train, im_test) >=(acc, _base_acc) end print(to_sql(q)) ``` -------------------------------- ### Create and Query In-Memory ACSet Data Source Source: https://context7.com/algebraicjulia/algebraicrelations.jl/llms.txt Establish an in-memory data source using ACSet structures within the Data Fabric framework. Supports adding, querying, and filtering data through fabric interface. Limitations: Data persists only in memory; suitable for small to medium datasets. Dependencies: AlgebraicRelations, Catlab, ACSets. ```Julia using AlgebraicRelations using Catlab, ACSets # Define schema @present SchGrape(FreeSchema) begin (Name, Color)::AttrType Grape::Ob (color, species)::Attr(Grape, Name) end @acset_type Grape(SchGrape) # Create in-memory data source grape = InMemory(Grape{Symbol, Symbol}()) # Initialize fabric and add source fabric = DataFabric() grape_src = add_source!(fabric, grape) # Add data using fabric interface add_part!(fabric, :Grape, color=:Green, species=:Chardonnay) add_part!(fabric, :Grape, color=:Red, species=:PinotNoir) add_part!(fabric, :Grape, color=:Red, species=:CabernetSauvignon) # Query data nparts(grape, :Grape) # Returns: 3 subpart(grape, :, :Grape => :color) # Returns: [:Green, :Red, :Red] incident(grape, :Red, :color) # Returns: [2, 3] ``` -------------------------------- ### Define Data Types for Presentation Schema in Julia Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md This code defines the data types for a presentation schema in AlgebraicRelations.jl. It initializes a Presentation object and adds named types, associating each with a Julia data type (e.g., String, Real). These types represent the data products within a workflow. The function returns the newly added type objects. ```julia present = Presentation() File, Images, NeuralNet, Accuracy, Metadata = add_types!(present, [(:File, String), (:Images, String), (:NeuralNet, String), (:Accuracy, Real), (:Metadata, String)]); ``` -------------------------------- ### SQL Query Generated by @query Macro Source: https://github.com/algebraicjulia/algebraicrelations.jl/blob/main/README.md The SQL query generated by the @query macro from the Julia code. This SQL statement selects specific columns and joins tables based on the relationships defined in the macro. It also includes a condition for filtering results based on accuracy. ```sql SELECT t1.Images2 AS im_train, t1.NeuralNet1 AS nn, t2.Images2 AS im_test, t2.Accuracy3 AS acc, t2.Metadata4 AS md2 FROM train AS t1, evaluate AS t2, split AS t3 WHERE t2.NeuralNet1=t1.NeuralNet3 AND t3.Images2=t1.Images2 AND t3.Images3=t2.Images2 AND t2.Accuracy3>=$1 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.