def do(self, theEnv, theFirst, *args, **kargs): """ handler of the function @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.5.html#Heading256 """ theFirst = self.resolve(theEnv, theFirst) if isinstance( theFirst, (types.FunctionCall, types.Variable)) else theFirst if not isinstance(theFirst, (types.Integer, types.Float)): raise InvalidArgTypeError( "Function * expected argument #1 to be of type integer or float" ) theMul = theFirst.evaluate() for (index, theSecond) in enumerate(args): if isinstance(theSecond, (types.FunctionCall, types.Variable)): theSecond = self.resolve(theEnv, theSecond) if not isinstance(theSecond, (types.Integer, types.Float)): raise InvalidArgTypeError( "Function * expected argument #%d to be of type integer or float" % index + 2) theMul = theMul * theSecond.evaluate() # CLIPS documentation: # after all computations, if the value is still an integer, then create a new <Integer:theMul> # otherwise <Float:theMul> if isinstance(theMul, int): return types.Integer(theMul) else: return types.Float(theMul)
def test_ErrorOnInvalidInput(self): self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, types.Integer(2), types.String("blabla")) self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, types.String("blabla"), types.Float(2))
def do(self, theEnv, theFirst, *args, **kargs): """ handler of the function @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.5.html#Heading257 """ theFirst = self.resolve(theEnv, theFirst) if isinstance(theFirst, (types.FunctionCall, types.Variable)) else theFirst if not isinstance(theFirst, (types.Integer, types.Float)): raise InvalidArgTypeError("Function / expected argument #1 to be of type integer or float") theDiv = float(theFirst.evaluate()) for (index, theSecond) in enumerate(args): if isinstance(theSecond, (types.FunctionCall, types.Variable)): theSecond = self.resolve(theEnv, theSecond) if not isinstance(theSecond, (types.Integer, types.Float)): raise InvalidArgTypeError("Function / expected argument #%d to be of type integer or float"%index + 2) theDiv = theDiv / float(theSecond.evaluate()) # WARNING: # this function always return a Float! return types.Float(theDiv)
def test_Concatenation(self): theWme = WME(1, OrderedFact([])) self.assertTrue( self.forInput(types.Symbol("ciao"), types.String("mucca"), types.Integer(1), types.Float(1.5), theWme).expect( types.String("ciaomucca11.5" + str(theWme))))
def test_ErrorOnNotStringOrSymbolInpyt(self): self.assertRaisesRegexp(InvalidArgTypeError, "Function build expected argument #1 to be of type string or symbol", self.theFunc.do, self.theEnv, types.Integer(10)) self.assertRaisesRegexp(InvalidArgTypeError, "Function build expected argument #1 to be of type string or symbol", self.theFunc.do, self.theEnv, types.Float(1.5)) self.assertRaisesRegexp(InvalidArgTypeError, "Function build expected argument #1 to be of type string or symbol", self.theFunc.do, self.theEnv, [types.Symbol("bla")])
def test_ErrorOnNotStringOrSymbolInput(self): self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, types.Integer(10), types.String("a")) self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, types.Float(1.5), types.String("a")) self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, [types.Symbol("bla")], types.String("a")) self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, types.String("a"), types.Integer(10)) self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, types.String("a"), types.Float(1.5)) self.assertRaises(InvalidArgTypeError, self.theFunc.do, self.theEnv, types.String("a"), [types.Symbol("bla")])
def test_EvalBaseTypes(self): typesMap = { "1": types.Integer(1), "1.5": types.Float(1.5), "symbol": types.Symbol("symbol"), '"string"': types.String("string") } for (theString, theResultCmp) in typesMap.items(): theResult = self.theFunc.do(self.theEnv, types.String(theString)) self.assertEqual(theResult, theResultCmp)
def do(self, theEnv, theNumber, *args, **kargs): """ handler of the function @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.5.html#Heading262 """ theNumber = self.resolve(theEnv, theNumber) if isinstance(theNumber, (types.FunctionCall, types.Variable)) else theNumber if not isinstance(theNumber, (types.Integer, types.Float)): raise InvalidArgTypeError("Function float expected argument #1 to be of type integer or float") return types.Float(float(theNumber.evaluate()))
def do(self, theEnv, theRuns=None, *args, **kargs): """ function handler implementation """ if theRuns is not None: theRuns = self.resolve( theEnv, self.semplify(theEnv, theRuns, types.Integer, ("1", "integer"))) start_time = time.time() theEnv.network.run(theRuns) runTime = time.time() - start_time return types.Float(runTime)
def test_IntegerIsNotEqualFloatWithSameContent(self): s1 = types.Integer("1") s2 = types.Float("1") self.assertFalse(s1 == s2)