<commit_before>"""
Runs PLN examples, similar to PLNUTest. Currently only works from the
cogserver due to linking issues.

Instructions:
Run the cogserver
loadpy pln_examples
py
from pln_examples import *
pln_examples = PLNExamples(ATOMSPACE)

To run one example:
pln_examples.run_pln_example('../tests/python/test_pln/scm/specific_rules/DeductionRule.scm')

To run all the examples:
pln_examples.test_all()
"""

from time import time
from opencog.atomspace import AtomSpace, Atom, types

try:
    from pln.chainers import Chainer
    from pln.rules import *
    import scheme_wrapper
except AttributeError:
    import unittest
    raise unittest.SkipTest("PLN automated unit test temporarily disabled - "
                            "can be ran manually in the cogserver")


class PLNExamples(object):
    def __init__(self, atomspace):
        self.atomspace = atomspace
        self.passed = []
        self.failed = []

    def test_all(self):
#        import plop.collector
#        plop_collector = plop.collector.Collector()
#        plop_collector.start()

        start = time()

        examples_directory = '../tests/python/test_pln/scm/'
        scheme_files = []

        import os
        for root, dirs, files in os.walk(examples_directory):
            for name in files:
                if name.endswith('.scm'):
                    scheme_files.append(os.path.join(root, name))
        
        scheme_files.sort() 
        print scheme_files

        for f in scheme_files:
            self.run_pln_example(f)

        print 'Passed %s out of %s tests' % (len(self.passed),
                                             len(self.passed + self.failed))
        if len(self.failed):
            print 'Failed tests:'
            for f in self.failed:
                print f
        
        print "Total time:", time() - start

#        plop_collector.stop()
#        profile_data = repr(dict(plop_collector.stack_counts))
#        f = open('pln_examples.plop_profile','w')
#        f.write(profile_data)
#        f.close()

    def run_pln_example(self, filename):
        self.atomspace.clear()
            
        # Todo: The variable 'tmp' is not used
        tmp = open(filename, 'r')
        if not scheme_wrapper.load_scm(self.atomspace, filename):
            print 'unable to load file', filename
            return
        print filename

        chainer = Chainer(self.atomspace,
                          stimulateAtoms=False,
                          agent=self,
                          learnRuleFrequencies=False)

        try:
            query = chainer.get_predicate_arguments('query')[0]
            rules_nodes = chainer.get_predicate_arguments('rules')
        except ValueError, e:
            print e
            return
        print query
        print rules_nodes

        # Todo: The variable 'all_rules' is not used
        all_rules = AllRules(self.atomspace, chainer)
        for r in rules_nodes:
            chainer.add_rule(all_rules.lookup_rule(r))
<commit_msg>Ooops -- missed a spot.<commit_after>"""
Runs PLN examples, similar to PLNUTest. Currently only works from the
cogserver due to linking issues.

Instructions:
Run the cogserver
loadpy pln_examples
py
from pln_examples import *
pln_examples = PLNExamples(ATOMSPACE)

To run one example:
pln_examples.run_pln_example('../tests/python/test_pln/scm/specific_rules/DeductionRule.scm')

To run all the examples:
pln_examples.test_all()
"""

from time import time
from opencog.atomspace import AtomSpace, Atom, types

try:
    from pln.chainers import Chainer
    from pln.rules import *
    import opencog.scheme_wrapper
except AttributeError:
    import unittest
    raise unittest.SkipTest("PLN automated unit test temporarily disabled - "
                            "can be ran manually in the cogserver")


class PLNExamples(object):
    def __init__(self, atomspace):
        self.atomspace = atomspace
        self.passed = []
        self.failed = []

    def test_all(self):
#        import plop.collector
#        plop_collector = plop.collector.Collector()
#        plop_collector.start()

        start = time()

        examples_directory = '../tests/python/test_pln/scm/'
        scheme_files = []

        import os
        for root, dirs, files in os.walk(examples_directory):
            for name in files:
                if name.endswith('.scm'):
                    scheme_files.append(os.path.join(root, name))
        
        scheme_files.sort() 
        print scheme_files

        for f in scheme_files:
            self.run_pln_example(f)

        print 'Passed %s out of %s tests' % (len(self.passed),
                                             len(self.passed + self.failed))
        if len(self.failed):
            print 'Failed tests:'
            for f in self.failed:
                print f
        
        print "Total time:", time() - start

#        plop_collector.stop()
#        profile_data = repr(dict(plop_collector.stack_counts))
#        f = open('pln_examples.plop_profile','w')
#        f.write(profile_data)
#        f.close()

    def run_pln_example(self, filename):
        self.atomspace.clear()
            
        # Todo: The variable 'tmp' is not used
        tmp = open(filename, 'r')
        if not opencog.scheme_wrapper.load_scm(self.atomspace, filename):
            print 'unable to load file', filename
            return
        print filename

        chainer = Chainer(self.atomspace,
                          stimulateAtoms=False,
                          agent=self,
                          learnRuleFrequencies=False)

        try:
            query = chainer.get_predicate_arguments('query')[0]
            rules_nodes = chainer.get_predicate_arguments('rules')
        except ValueError, e:
            print e
            return
        print query
        print rules_nodes

        # Todo: The variable 'all_rules' is not used
        all_rules = AllRules(self.atomspace, chainer)
        for r in rules_nodes:
            chainer.add_rule(all_rules.lookup_rule(r))

{'fraction_non_alphanumeric': 0.0867195242814668, 'fraction_numerical': 0.0003303600925008259, 'mean_word_length': 2.0004955401387514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '806611', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1736, 'n_words': 439}