public TestScenarioWorkingMemoryWrapper(InternalWorkingMemory workingMemory, final TypeResolver resolver, final ClassLoader classLoader, Map<String, Object> populatedData, Map<String, Object> globalData) { this.workingMemory = workingMemory; this.populatedData = populatedData; this.methodExecutor = new MethodExecutor( populatedData ); this.classLoader = classLoader; factVerifier = initFactVerifier( resolver, globalData ); }
public void executeMethod(CallMethod callMethod) { methodExecutor.executeMethod( callMethod ); }
@Test public void testCallMethodNoArgumentOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("setTotalAmountToZero"); methodExecutor.executeMethod(mCall); assertTrue(listChesse.getTotalAmount() == 0); }
private String build(CallMethod callMethod, Map<String, Object> vars) { StringBuilder methodCallAsSting = new StringBuilder(); methodCallAsSting.append("__fact__." + callMethod.getMethodName()); methodCallAsSting.append("("); for (int i = 0; i < callMethod.getCallFieldValues().length; i++) { CallFieldValue field = callMethod.getCallFieldValues()[i]; if (field.hasValue()) { String variableId = String.format("__val%d__", i); vars.put( variableId, getFieldValue(field)); methodCallAsSting.append(variableId); if (isThisTheLastVariable(callMethod, i)) { methodCallAsSting.append(","); } } } methodCallAsSting.append(")"); return methodCallAsSting.toString(); }
private Object getFieldValue(CallFieldValue field) { Object val; if (isTheValueAPreviouslyDefinedObject(field)) { // eval the val into existence val = populatedData.get(field.value.substring(1)); } else { val = field.value; } return val; }
@Test public void testCallMethodOnStandardArgumentOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("addToTotalAmount"); CallFieldValue field = new CallFieldValue(); field.value = "5"; mCall.addFieldValue(field); methodExecutor.executeMethod(mCall); assertTrue(listChesse.getTotalAmount() == 1005); }
private String build(CallMethod callMethod, Map<String, Object> vars) { StringBuilder methodCallAsSting = new StringBuilder(); methodCallAsSting.append("__fact__." + callMethod.getMethodName()); methodCallAsSting.append("("); for (int i = 0; i < callMethod.getCallFieldValues().length; i++) { CallFieldValue field = callMethod.getCallFieldValues()[i]; if (field.hasValue()) { String variableId = String.format("__val%d__", i); vars.put( variableId, getFieldValue(field)); methodCallAsSting.append(variableId); if (isThisTheLastVariable(callMethod, i)) { methodCallAsSting.append(","); } } } methodCallAsSting.append(")"); return methodCallAsSting.toString(); }
private Object getFieldValue(CallFieldValue field) { Object val; if (isTheValueAPreviouslyDefinedObject(field)) { // eval the val into existence val = populatedData.get(field.value.substring(1)); } else { val = field.value; } return val; }
@Test public void testCallMethodOnClassArgumentAndOnArgumentStandardOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); Cheesery.Maturity m = new Cheesery.Maturity("veryYoung"); populatedData.put("m", m); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("setAgeToMaturity"); CallFieldValue field = new CallFieldValue(); field.value = "=m"; mCall.addFieldValue(field); CallFieldValue field2 = new CallFieldValue(); field2.value = "veryold"; mCall.addFieldValue(field2); methodExecutor.executeMethod(mCall); assertTrue(m.getAge().equals("veryold")); } }
public TestScenarioWorkingMemoryWrapper(InternalWorkingMemory workingMemory, final TypeResolver resolver, final ClassLoader classLoader, Map<String, Object> populatedData, Map<String, Object> globalData) { this.workingMemory = workingMemory; this.populatedData = populatedData; this.methodExecutor = new MethodExecutor( populatedData ); this.classLoader = classLoader; factVerifier = initFactVerifier( resolver, globalData ); }
public void executeMethod(CallMethod callMethod) { methodExecutor.executeMethod( callMethod ); }
@Test public void testCallMethodOnClassArgumentOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); Cheesery.Maturity m = new Cheesery.Maturity(); populatedData.put("m", m); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("setGoodMaturity"); CallFieldValue field = new CallFieldValue(); field.value = "=m"; mCall.addFieldValue(field); methodExecutor.executeMethod(mCall); assertTrue(listChesse.getMaturity().equals(m)); assertTrue(listChesse.getMaturity() == m); }