Permalink
Browse files

the test physics22.npl is now passing

  • Loading branch information...
enriquepablo committed Mar 26, 2012
1 parent c3f501f commit 5f3da36c202b2e7f515c3b779960132f591e8ca7
Showing with 103 additions and 40 deletions.
  1. +1 −1 nl/__init__.py
  2. +48 −17 nl/metanl.py
  3. +15 −1 nl/nlc/compiler.py
  4. +3 −1 nl/nlc/lexer.py
  5. +11 −10 nl/nltime.py
  6. +24 −6 nl/npl_tests/physics22.npl
  7. +1 −4 nl/prop.py
View
@@ -19,7 +19,7 @@
import clips
import kb
-from metanl import Number, Arith, Noun, Word, Verb, Subword, Namable, Not, And, Or
+from metanl import Number, Arith, Noun, Word, Verb, Subword, Namable, Not, And, Or, Distinct
from thing import Thing
from state import Exists
from prop import Fact
View
@@ -519,23 +519,23 @@ class Number(Namable):
def __init__(self, value, arg1='', arg2=''):
if isinstance(value, Number):
self.value, self.arg1, self.arg2 = value.value, value.arg1, value.arg2
- return
- self.arg1 = str(arg1)
- self.arg2 = str(arg2)
- try:
- self.value = str(float(value))
- except (ValueError, TypeError):
- if value[0] == '(':
- args = utils.parens(value)
- self.value = args[0]
- self.arg1 = Number(args[1])
- self.arg2 = Number(args[2])
- else:
- self.value = value
- if self.arg1 != '':
- self.arg1 = Number(arg1)
- if self.arg2 != '':
- self.arg2 = Number(arg2)
+ else:
+ self.arg1 = str(arg1)
+ self.arg2 = str(arg2)
+ try:
+ self.value = str(float(value))
+ except (ValueError, TypeError):
+ if value[0] == '(':
+ args = utils.parens(value)
+ self.value = args[0]
+ self.arg1 = Number(args[1])
+ self.arg2 = Number(args[2])
+ else:
+ self.value = value
+ if self.arg1 != '':
+ self.arg1 = Number(arg1)
+ if self.arg2 != '':
+ self.arg2 = Number(arg2)
@classmethod
def from_clips(cls, instance):
@@ -587,6 +587,17 @@ def get_isc(self, queries, vrs, ancestor, mod_path):
def __str__(self):
return self._get_number({})
+ def tonl(self):
+ """
+ """
+ if utils.varpat.match(self.value):
+ return utils.var_tonl(self)
+ try:
+ return str(float(self.value))
+ except ValueError:
+ arg1 = self.arg1 != '' and self.arg1._get_number({}) or ''
+ arg2 = self.arg2 != '' and self.arg2._get_number({}) or ''
+ return '(%s %s %s)' % (arg1, self.value, arg2)
class Arith(Number):
@@ -816,3 +827,23 @@ class Or(Bi_conn_mixin):
def get_ce(self, vrs=None):
return self._get_ce(vrs, conn='or')
+
+
+class Distinct(Namable):
+
+ def __init__(self, *args):
+ self.args = args
+
+ def get_ce(self, vrs=None):
+ args = [arg.put(vrs) for arg in self.args]
+ ineqs = []
+ while args:
+ arg = args.pop()
+ if args:
+ ineqs.append('(neq %s %s)' % (arg, ' '.join(args)))
+ if len(ineqs) == 1:
+ return '(test %s)' % ' '.join(ineqs)
+ elif len(ineqs) > 1:
+ return '(test (and %s))' % ' '.join(ineqs)
+ else:
+ raise ValueError('not enough arguments for Distinct')
View
@@ -398,7 +398,8 @@ def p_condition(p):
| coincidence
| during
| subword
- | arith-condition'''
+ | arith-condition
+ | distinct-condition'''
p[0] = p[1]
def p_coincidence(p):
@@ -520,6 +521,19 @@ def p_arith_predicate(p):
| NEQ'''
p[0] = p[1]
+
+def p_distinct_condition(p):
+ '''distinct-condition : DISTINCT objects'''
+ p[0] = nl.Distinct(*p[2])
+
+def p_objects(p):
+ '''objects : object COMMA objects
+ | object'''
+ if len(p) == 2:
+ p[0] = [p[1]]
+ else:
+ p[0] = [p[1]] + p[3]
+
# Error rule for syntax errors
def p_error(p):
raise ParseError('syntax error: ' + str(p))
View
@@ -49,6 +49,7 @@
'GT',
'EQ',
'NEQ',
+ 'DISTINCT',
)
reserved = {
@@ -74,6 +75,7 @@
'subwordof': 'SUBWORDOF',
'passtime': 'PASSTIME',
'until': 'UNTIL',
+ 'distinct': 'DISTINCT',
}
t_COMMA = r','
@@ -84,7 +86,7 @@
t_COLON = r':'
t_SEMICOLON = r';'
from nl.utils import t_VAR
-t_NUMBER = r'(\d+)'
+t_NUMBER = r'(-?\d+)'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LCURL = r'\{'
View
@@ -35,7 +35,7 @@ class Time(Namable):
def from_clips(cls, instance):
if not isinstance(instance, clips._clips_wrap.Instance):
try:
- return Instant(str(float(instance)))
+ return Instant(float(str(instance)))
except ValueError:
instance = clips.FindInstance(instance)
return Duration.from_clips(instance)
@@ -101,7 +101,8 @@ def put(self, vrs):
if float(val) == utils._now:
return '(python-call ptime)'
except ValueError:
- return val
+ pass
+ return val
class Duration(Time):
@@ -166,11 +167,11 @@ def get_slot_constraint(self, vrs):
if utils.varpat.match(self.value):
return self.get_var_slot_constraint(vrs, self.value)
newvar = utils._newvar()
- constraint = '?'+newvar
- c = self.start.get_constraint(vrs, newvar, ['start'])
- if c:
- constraint += '&:(eq (send ?%s get-start) %s)' % (newvar, c)
- constraint += '&:(eq (send ?%s get-end) -1.0)' % (newvar)
+ constraint = '?' + newvar
+ for x in ('start', 'end'):
+ x_constraint = getattr(self, x).get_constraint(vrs, newvar, [x])
+ if x_constraint:
+ constraint += '&:(eq (send ?%s get-%s) %s)' % (newvar, x, x_constraint)
return constraint
def put(self, vrs, make=True):
@@ -430,7 +431,7 @@ class Past(Instant_op_mixin):
init'd with an instant, returns true if
the instant is in the past
"""
- def get_ce(vrs):
+ def get_ce(self, vrs):
i = self.instant.put(vrs)
return '''(test (and (neq %s -1.0)
(< %s (python-call ptime))))''' % (i, i)
@@ -440,7 +441,7 @@ class Present(Instant_op_mixin):
init'd with an instant, returns true if
the instant is the present
"""
- def get_ce(vrs):
+ def get_ce(self, vrs):
i = self.instant.put(vrs)
return '''(test (or (eq %s -1.0)
(eq %s (python-call ptime))))''' % (i, i)
@@ -450,7 +451,7 @@ class Future(Instant_op_mixin):
init'd with an instant, returns true if
the instant is in the future
"""
- def get_ce(vrs):
+ def get_ce(self, vrs):
i = self.instant.put(vrs)
return '''(test (> %s (python-call ptime)))''' % (i, i)
View
@@ -26,18 +26,36 @@ then:
Body1 [has_speed x {N1 + N3}, y {N2 + N4}] at {I1 + 1}.
if:
- Body1 [has_mass kg N1] at I1;
+ Body1 [has_mass kg N1] D1;
Body1 [is_forced x N2, y N3] at I1;
{I1 < 33};
+ I1 during D1;
then:
- Body1 [has_acceleration x {N2 / N1}, y {N3 / N1}] at {I1 + 1}.
+ Body1 [has_acceleration x {N2 / N1}, y {N3 / N1}] at I1.
if:
Body1 [has_position x N1, y N2] at I1;
- Body1 [has_mass kg N3] at I1;
+ Body1 [has_mass kg N3] D1;
Body2 [has_position x N4, y N5] at I1;
- Body2 [has_mass kg N6] at I1;
-# different Body1, Body2;
+ Body2 [has_mass kg N6] D2;
+ distinct Body1, Body2;
{I1 < 33};
+ I1 during D1;
+ I1 during D2;
then:
- Body1 [is_forced x {0 - ((N3 * N6) / (N1 - N4))}, y {0 - ((N3 * N6) / (N2 - N5))}] at {I1 + 1}.
+ Body1 [is_forced x {0 - ((N3 * N6) / (N1 - N4))}, y {0 - ((N3 * N6) / (N2 - N5))}] at I1.
+
+
+body1 isa body.
+body1 [has_mass kg 2] since 1 onwards.
+body1 [has_position x 2, y 2] at 1.
+body1 [has_speed x 2, y 2] at 1.
+body2 isa body.
+body2 [has_mass kg 2] since 1 onwards.
+body2 [has_position x -2, y -2] at 1.
+body2 [has_speed x 2, y -2] at 1.
+
+extend.
+
+body1 [has_position x N1, y N2] at 32?
+body1 \[has_position y -?[0-9\.]+, x -?[0-9\.]+\] at 32
View
@@ -171,10 +171,7 @@ def factback(csubj, cpred, ctime, ctruth, fr):
except clips.ClipsError:
subj = Word.from_clips(csubj)
pred = Namable.from_clips(cpred)
- try:
- t = Time.from_clips(ctime)
- except:
- t = Instant(10)
+ t = Time.from_clips(ctime)
truth = int(str(ctruth))
fact = Fact(subj, pred, t, truth=truth)
pred.in_fact(fact, fr)

0 comments on commit 5f3da36

Please sign in to comment.