Skip to content

Commit

Permalink
policy grammar mistakes were being obscured. this should remedy
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Joseph Walsh committed Dec 30, 2011
1 parent 6a571fa commit 960e815
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 144 deletions.
17 changes: 15 additions & 2 deletions .settings/org.deved.antlride.core.prefs
@@ -1,6 +1,19 @@
#Thu Aug 04 12:34:26 EDT 2011
#Thu Dec 29 16:18:04 EST 2011
antlr_core_builder_Xconversiontimeout=1000
antlr_core_builder_Xdfaverbose=false
antlr_core_builder_Xm=4
antlr_core_builder_Xmaxdfaedges=65534
antlr_core_builder_Xnocollapse=false
antlr_core_builder_Xnomergestopstates=false
antlr_core_builder_Xnoprune=false
antlr_core_builder_dfa=false
antlr_core_builder_include_stacktrace_on_internal_errors=false
antlr_core_builder_max_number_of_problems_reported_per_grammar=25
antlr_core_builder_nfa=false
antlr_core_builder_report=false
antlr_core_builder_runtime=3.1.3
antlr_core_code_generator_XdbgST=false
antlr_core_code_generator_append_java_package_to_out_folder=true
antlr_core_code_generator_append_java_package_to_out_folder=false
antlr_core_code_generator_debug=false
antlr_core_code_generator_max_memory=0
antlr_core_code_generator_out_folder=antlr-generated
Expand Down
59 changes: 45 additions & 14 deletions intellect/Intellect.py
Expand Up @@ -38,7 +38,7 @@
@author: Michael Joseph Walsh
"""

import logging, os, re, traceback
import logging, os, re, traceback, sys

from antlr3 import FileStream, CommonTokenStream, ANTLRStringStream, RecognitionException

Expand All @@ -47,7 +47,6 @@
from intellect.Node import Policy
from intellect.Node import File
from intellect.PolicyTokenSource import PolicyTokenSource

from intellect.Callable import Callable
import intellect.IO as IO

Expand Down Expand Up @@ -160,11 +159,12 @@ def learn_policy(self, identifier):
either a file path to a policy or the text of the policy itself.
'''

isFile = False

if identifier:
if isinstance(identifier, basestring):
isFile = False

try:
if not os.path.isfile(identifier):
'''
Try treating 'identifier' as a String containing the text
of a policy.
Expand All @@ -179,12 +179,26 @@ def learn_policy(self, identifier):
parser = PolicyParser(tokens)

with IO.capture_stderr() as stderr:
file_node = parser.file()

if stderr.getvalue() != "":
raise Exception, "Error in String-based policy: {0}".format(stderr.getvalue())

except Exception as e:
try:
# ANTL3 may raise an exception, and doing so the stderror
# will not be printed hiding the underlying problem. GRRR!!!!
file_node = parser.file()
except Exception as e:
if stderr.getvalue().rstrip() != "":
trace = sys.exc_info()[2]
raise Exception(stderr.getvalue().rstrip()), None, trace
else:
raise e

# Some times the previous parser.file() will print to stderr,
# but not throw an exception. In this case, the parser may
# attempt to correct and continue onward, but we should
# print the msg to stderr for the benefit of the policy
# author
if stderr.getvalue().rstrip() != "":
print >> sys.stderr, stderr.getvalue().rstrip()

else:
'''
Try treating 'identifier' as a file path
'''
Expand All @@ -210,10 +224,24 @@ def learn_policy(self, identifier):
parser = PolicyParser(tokens)

with IO.capture_stderr() as stderr:
file_node = parser.file()

if stderr.getvalue() != "":
raise Exception, "Error in file-based policy for path {0}: {1}".format(identifier, stderr.getvalue())
try:
# ANTL3 may raise an exception, and doing so the stderror
# will not be printed hiding the underlying problem. GRRR!!!!
file_node = parser.file()
except Exception as e:
if stderr.getvalue().rstrip() != "":
trace = sys.exc_info()[2]
raise Exception(stderr.getvalue().rstrip()), None, trace
else:
raise e

# Some times the previous parser.file() will print to stderr,
# but not throw an exception. In this case, the parser may
# attempt to correct and continue onward, but we should
# print the msg to stderr for the benefit of the policy
# author
if stderr.getvalue().rstrip() != "":
print >> sys.stderr, stderr.getvalue().rstrip()

# set path attribute
file_node.path = identifier if isFile else None
Expand All @@ -231,6 +259,9 @@ def learn_policy(self, identifier):
# store add the policy file to the policy
self.policy.append_child(file_node)

self.log("learned a policy file")
print file_node.str_tree()

return file_node

else:
Expand Down
2 changes: 1 addition & 1 deletion intellect/__init__.py
Expand Up @@ -30,4 +30,4 @@

__author__ = "Michael Josephh Walsh"

VERSION = (1, 4, 3)
VERSION = (1, 4, 4)
79 changes: 79 additions & 0 deletions intellect/examples/testing/ExerciseGrammar.py
@@ -0,0 +1,79 @@
"""
Copyright (c) 2011, The MITRE Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the author.
4. Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

"""
ExerciseIntellect
Description: Exercises the Intellect grammar
Initial Version: Dec 29, 2011
@author: Michael Joseph Walsh
"""
import sys, traceback, logging

from intellect.Intellect import Intellect
from intellect.examples.testing.ClassA import ClassA

if __name__ == "__main__":

# tune down logging inside Intellect
logger = logging.getLogger('intellect')
logger.setLevel(logging.DEBUG) # change this to ERROR for less output
consoleHandler = logging.StreamHandler(stream=sys.stdout)
consoleHandler.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
logger.addHandler(consoleHandler)

# set up logging for the example
logger = logging.getLogger('example')
logger.setLevel(logging.DEBUG)

consoleHandler = logging.StreamHandler(stream=sys.stdout)
consoleHandler.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
logger.addHandler(consoleHandler)

print "*"*80
print """create an instance of MyIntellect extending Intellect, create some facts, and exercise the grammar"""
print "*"*80

try:
myIntellect = Intellect()

policy_a = myIntellect.learn("./rulesets/test_f.policy")

myIntellect.learn(ClassA( property1="apple"))
#myIntellect.learn(ClassA( property1="pear"))
#myIntellect.learn(ClassA( property1="grape"))

#logger.debug("reasoning over policy w/ objects in memory")

myIntellect.reason()
except Exception as e:
traceback.print_exc(limit=sys.getrecursionlimit(), file=sys.stdout)

0 comments on commit 960e815

Please sign in to comment.