Skip to content

ErgoText with pyergo has buggy inserts #8

@re26-brc

Description

@re26-brc

Describe the bug
When you use the pyergo_command("insert{...}.") function with an ErgoText template, no error occurs, but querying for what you just inserted results in an empty result.

To Reproduce
Steps to reproduce the behavior:

  1. Save ergotext_example.py in the same base directory where your XSB arch directory and Ergo root is (that way the script's test ErgoText template gets saved in the proper spot).
  2. Running the script just requires you pass in the XSB arch directory and the Ergo root. Like this:

python ergotext_example.py /opt/ergoai/XSB/config/x86_64-pc-linux-gnu /opt/ergoai/ERGOAI/ErgoEngine/ErgoAI

Expected behavior
I get this as the script output:

Starting Ergo...
Creating template...
Loading template...
Checking active template...

Active ErgoText templates:

Template context: body
Template phrase: ?A is a bank
Template definition: ?A : bank
Template module: main

Template context: head
Template phrase: ?A is a bank
Template definition: ?A : bank
Template module: main

Template context: toplevel
Template phrase: ?A is a bank
Template definition: ?A : bank
Template module: main

Testing phrase: JPMorgan is a bank

ErgoText phrase parsed form:
[JPMorgan, is, a, bank]

✓ Parsed successfully
✓ SUCCESS! ErgoText insert worked!
Testing what facts were created...
[]
✗ None of the queries found JPMorgan

Desktop (please complete the following information):

  • I am using the Dockerfile as implemented here.

Additional context
Here is the python code ergotext_example.py

#!/usr/bin/env python
"""
ErgoText Test
"""

import sys
import os

if len(sys.argv) < 3:
    print("Usage: python ergotext_example.py <XSBARCHDIR> <ERGOROOT>")
    sys.exit(1)

XSBARCHDIR = sys.argv[1]
ERGOROOT = sys.argv[2]

sys.path.append(ERGOROOT.replace('\\','/') + '/python')

from pyergo import pyergo_start_session, pyergo_end_session, pyergo_command, pyergo_query

def fixed_test():
    try:
        print("Starting Ergo...")
        pyergo_start_session(XSBARCHDIR, ERGOROOT)
        
        # Create template - use \( \) in template definition
        print("Creating template...")
        template_content = 'template(headbody, \\(?Bank is a bank\\), ?Bank:bank).\n'
        with open('fixed.ergotxt', 'w') as f:
            f.write(template_content)

        # Load template
        print("Loading template...")
        pyergo_command("ergotext{fixed}.")
        
        # Check what the template actually looks like
        print("Checking active template...")
        pyergo_command("ergo_show_active_templates@\\plg(flrparser).")


        print(f"\nTesting phrase: {'JPMorgan is a bank'}")
        try:
            phrase = "\\(JPMorgan is a bank\\)"
            pyergo_command(f"show_ergotext_phrase_as_term({phrase})@\\plg(flrparser).")
            print("✓ Parsed successfully")
        except Exception as e:
            print(f"✗ Failed: {e}")
        
        try:
            # ErgoText insert into knowledge base
            pyergo_command("insert{'\\(JPMorgan is a bank\\)'}.")

            # THIS WORKS WITH XSB
            # pyergo_command("insert{JPMorgan:bank}.")
            print("✓ SUCCESS! ErgoText insert worked!")
            
            # Query result - Let's test what was actually created
            print("Testing what facts were created...")
            
            # Test specific fact
            test_queries = [
                '?X:bank.'
            ]
            
            for test_query in test_queries:
                try:
                    results = pyergo_query(f'{test_query}')
                    print(results)
                    if results and any(r[2] for r in results):  # Check if any are True
                        print(f"✓ Query '{test_query}' succeeded")
                        if results[0][0]:  # Has variable bindings
                            bank = results[0][0][0][1]
                            print(f"  Found bank: {bank}")
                        break
                except Exception as e:
                    print(f"  Query '{test_query}' failed: {e}")
            else:
                print("✗ None of the queries found JPMorgan")
            
        except Exception as e:
            print(f"✗ Still failed: {e}")
        
    except Exception as e:
        print(f"ERROR: {e}")
    
    finally:
        try:
            pyergo_end_session()
            if os.path.exists('fixed.ergotxt'):
                os.remove('fixed.ergotxt')
        except:
            pass

if __name__ == "__main__":
    fixed_test()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions