protected List<FieldPopulator> getFieldPopulators(Object factObject) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { FieldPopulatorFactory fieldPopulatorFactory = new FieldPopulatorFactory(factObject, typeResolver, classLoader); List<FieldPopulator> fieldPopulators = new ArrayList<FieldPopulator>(); for (Field field : fact.getFieldData()) { try { fieldPopulators.add(fieldPopulatorFactory.getFieldPopulator(field)); } catch (IllegalArgumentException e) { // This should never happen, but I don't trust myself or the legacy test scenarios we have. // If the field value is null then it is safe to ignore it. } } return fieldPopulators; }
public String getName() { return fact.getName(); }
/** * * @return A mapping of variable names to their fact type. */ public Map<String, String> getVariableTypes() { Map<String, String> map = new HashMap<String, String>(); for ( Fixture fixture : fixtures ) { if ( fixture instanceof FactData ) { FactData factData = (FactData) fixture; map.put( factData.getName(), factData.getType() ); } } for ( FactData factData : globals ) { map.put( factData.getName(), factData.getType() ); } return map; }
@Test public void testPopulateFactWithoutFields() throws Exception { factPopulator.add( new NewFactPopulator( populatedData, getTypeResolver(), getClassLoader(), new FactData("Cheese", "c1", new ArrayList(), false))); factPopulator.populate(); assertTrue(populatedData.containsKey("c1")); assertTrue(populatedData.get("c1") instanceof Cheese); }
@Test public void testAdd() { FactData fd = new FactData("x", "y", new ArrayList(), false ); assertEquals(0, fd.getFieldData().size()); fd.getFieldData().add(new FieldData("x", "y")); assertEquals(1, fd.getFieldData().size()); fd.getFieldData().add(new FieldData("q", "x")); assertEquals(2, fd.getFieldData().size()); } }
protected String getTypeName(TypeResolver resolver, FactData fact) throws ClassNotFoundException { String fullName = resolver.getFullTypeName(fact.getType()); if (fullName.equals("java.util.List") || fullName.equals("java.util.Collection")) { return "java.util.ArrayList"; } else { return fullName; } } }
public FactData(String type, String name, List<Field> fieldData, boolean modify) { this(type, name, modify); this.setFieldData(fieldData); }
public boolean isFieldNameInUse(String fieldName) { for (Fixture fixture : this) { if (fixture instanceof FactData && ((FactData) fixture).isFieldNameInUse(fieldName)) { return true; } } return false; } }
public Populator createFactPopulator(FactData fact) throws ClassNotFoundException, IllegalAccessException, InstantiationException { if ( fact.isModify() ) { return new ExistingFactPopulator( populatedData, typeResolver, classLoader, fact ); } else { return new NewFactPopulator( populatedData, typeResolver, classLoader, fact ); } }
@Test public void testMapFactTypes() { Scenario sc = new Scenario(); sc.getFixtures().add( new FactData( "X", "q", null, false ) ); sc.getGlobals().add( new FactData( "Q", "x", null, false ) ); Map r = sc.getVariableTypes(); assertEquals( 2, r.size() ); assertEquals( "X", r.get( "q" ) ); assertEquals( "Q", r.get( "x" ) ); }
protected String getTypeName(TypeResolver resolver, FactData fact) throws ClassNotFoundException { String fullName = resolver.getFullTypeName(fact.getType()); if (fullName.equals("java.util.List") || fullName.equals("java.util.Collection")) { return "java.util.ArrayList"; } else { return fullName; } } }
public FactData(String type, String name, List<Field> fieldData, boolean modify) { this(type, name, modify); this.setFieldData(fieldData); }
public boolean isFieldNameInUse(String fieldName) { for (Fixture fixture : this) { if (fixture instanceof FactData && ((FactData) fixture).isFieldNameInUse(fieldName)) { return true; } } return false; } }
public Populator createFactPopulator(FactData fact) throws ClassNotFoundException, IllegalAccessException, InstantiationException { if ( fact.isModify() ) { return new ExistingFactPopulator( populatedData, typeResolver, classLoader, fact ); } else { return new NewFactPopulator( populatedData, typeResolver, classLoader, fact ); } }
public String getName() { return fact.getName(); }
protected List<FieldPopulator> getFieldPopulators(Object factObject) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { FieldPopulatorFactory fieldPopulatorFactory = new FieldPopulatorFactory(factObject, typeResolver, classLoader); List<FieldPopulator> fieldPopulators = new ArrayList<FieldPopulator>(); for (Field field : fact.getFieldData()) { try { fieldPopulators.add(fieldPopulatorFactory.getFieldPopulator(field)); } catch (IllegalArgumentException e) { // This should never happen, but I don't trust myself or the legacy test scenarios we have. // If the field value is null then it is safe to ignore it. } } return fieldPopulators; }
@Test public void testDummyRunNoRules() throws Exception { typeResolver.addImport("org.drools.Cheese"); List<Field> fieldData = new ArrayList<Field>(); fieldData.add(new FieldData("type", "cheddar")); fieldData.add(new FieldData("price", "42")); FactData fact = new FactData("Cheese", "c1", fieldData, false); NewFactPopulator newFactPopulator = new NewFactPopulator( populatedData, typeResolver, Thread.currentThread().getContextClassLoader(), fact); newFactPopulator.populate(workingMemory, new HashMap<String, FactHandle>()); assertTrue(populatedData.containsKey("c1")); assertNotNull(populatedData.get("c1")); assertEquals(populatedData.get("c1"), workingMemory.facts.get(0)); }
/** * * @return A mapping of variable names to their fact type. */ public Map<String, String> getVariableTypes() { Map<String, String> map = new HashMap<String, String>(); for ( Fixture fixture : fixtures ) { if ( fixture instanceof FactData ) { FactData factData = (FactData) fixture; map.put( factData.getName(), factData.getType() ); } } for ( FactData factData : globals ) { map.put( factData.getName(), factData.getType() ); } return map; }
/** * * @return A mapping of Fact types to their Fact definitions. */ public Map<String, List<FactData>> getFactTypesToFactData() { Map<String, List<FactData>> map = new HashMap<String, List<FactData>>(); for ( Fixture fixture : fixtures ) { if ( fixture instanceof FactData ) { FactData factData = (FactData) fixture; List<FactData> fd = map.get( factData.getType() ); if ( fd == null ) { fd = new ArrayList<FactData>(); map.put( factData.getType(), fd ); } fd.add( factData ); } } for ( FactData factData : globals ) { List<FactData> fd = map.get( factData.getType() ); if ( fd == null ) { fd = new ArrayList<FactData>(); map.put( factData.getType(), fd ); } fd.add( factData ); } return map; }