[rules-users] Accessing properties of an object within a Nested Collection of a Nested Collection
Wolfgang Laun
wolfgang.laun at gmail.com
Wed May 2 13:52:32 EDT 2012
Consider not only inserting a single This-is-my-Universe fact but all
of its component objects: Brands, Models and Parts.
rule "parts of PV999999"
when
$p: Part( partVendor == "PV999999" )
then
...(...$p.getPartsName()...)
end
(In a plain old database: would you have just a single AutoManufacturer record?)
-W
On 02/05/2012, aa034373 <james_kosa at verizon.net> wrote:
> I am having a hard time trying to figure this out, maybe it is obvious and
> I
> have overlooked it.... I am trying to evaluate properties of a object
> contained within a nested collection of a nested collection on the LHS.
>
> Here is an example of the object model I am working with.
> *AutoManufacturer* is the parent class, with *Brand* residing in a
> collection on the object, with *Model* residing in a collection on *Brand*,
> and finally, *Part* residing in a collection of *Model*. These classes
> were
> created for illustration purposes for this question.....
>
> My question more specifically would be, on the LHS, how would I write a
> rule
> for instance that would find all *Part* objects with a Vendor of
> "PV999999".
>
> Secondly, on the RHS, how could I iterate the resulting collection of Part
> objects to print something like the partname for each of them.
>
> Here are the class objects:
>
> AutoManufacturer class
> -----------------------------------------------------
> package com.nabble.demo.model;
>
> import java.util.ArrayList;
> import java.util.List;
>
> /**
> * Class that represents an automobile manufacturer
> */
> public class AutoManufacturer
> {
> private String manufacturerName;
> private String manufacturerId;
> private List<Brand> brandsCollection;
>
>
> /**
> * Default constructor
> */
> public AutoManufacturer()
> {
> this.brandsCollection = new ArrayList<Brand>();
> }
>
>
> /**
> * @return the manufacturerName
> */
> public String getManufacturerName()
> {
> return this.manufacturerName;
> }
>
> /**
> * @param manufacturerName the manufacturerName to set
> */
> public void setManufacturerName( String manufacturerName )
> {
> this.manufacturerName = manufacturerName;
> }
>
> /**
> * @return the manufacturerId
> */
> public String getManufacturerId()
> {
> return this.manufacturerId;
> }
>
> /**
> * @param manufacturerId the manufacturerId to set
> */
> public void setManufacturerId( String manufacturerId )
> {
> this.manufacturerId = manufacturerId;
> }
>
> /**
> * @return the brandsCollection
> */
> public List<Brand> getBrandsCollection()
> {
> return this.brandsCollection;
> }
>
> /**
> * @param brand the Brand to add to the collection
> */
> public void addBrand( Brand brand )
> {
> this.brandsCollection.add(brand);
> }
>
> }
>
>
> Brand class
> ------------------------------------------------------
> package com.nabble.demo.model;
>
> import java.util.ArrayList;
> import java.util.List;
>
> public class Brand
> {
>
> private String brandName;
> private String brandId;
> private List<Model> brandModels;
>
> public Brand()
> {
> this.brandModels = new ArrayList<Model>();
> }
>
> /**
> * @return the brandName
> */
> public String getBrandName()
> {
> return brandName;
> }
>
> /**
> * @param brandName the brandName to set
> */
> public void setBrandName( String brandName )
> {
> this.brandName = brandName;
> }
>
> /**
> * @return the brandId
> */
> public String getBrandId()
> {
> return brandId;
> }
>
> /**
> * @param brandId the brandId to set
> */
> public void setBrandId( String brandId )
> {
> this.brandId = brandId;
> }
>
> /**
> * @return the brandModels
> */
> public List<Model> getBrandModels()
> {
> return brandModels;
> }
>
> /**
> * @param model the Model to add
> */
> public void addModel( Model model )
> {
> this.brandModels.add(model);
> }
>
> }
>
>
> Model class
> --------------------------------------------------------
> package com.nabble.demo.model;
>
> import java.util.ArrayList;
> import java.util.List;
>
> /**
> * Class that represents a particular model of an automaker's brand of car
> */
> public class Model
> {
> private String modelName;
> private String modelId;
> private List<Part> partsList;
>
> public Model()
> {
> this.partsList = new ArrayList<Part>();
> }
>
> /**
> * @return the modelName
> */
> public String getModelName()
> {
> return this.modelName;
> }
>
> /**
> * @param modelName the modelName to set
> */
> public void setModelName( String modelName )
> {
> this.modelName = modelName;
> }
>
> /**
> * @return the modelId
> */
> public String getModelId()
> {
> return this.modelId;
> }
>
> /**
> * @param modelId the modelId to set
> */
> public void setModelId( String modelId )
> {
> this.modelId = modelId;
> }
>
> /**
> * @return the partsList
> */
> public List<Part> getPartsList()
> {
> return this.partsList;
> }
>
> /**
> * Method to add new part to collection
> *
> * @param part
> */
> public void addPart( Part part )
> {
> this.partsList.add(part);
> }
>
> }
>
>
> Part Class
> ----------------------------------------------------------------
>
> package com.nabble.demo.model;
>
> /**
> * Class that represents a part used on a Model of car
> */
> public class Part
> {
> private String partsId;
> private String partsName;
> private String partVendor;
>
> /**
> * Default constructor
> */
> public Part()
> {
>
> }
>
> /**
> * @return the partsId
> */
> public String getPartsId()
> {
> return this.partsId;
> }
>
> /**
> * @param partsId the partsId to set
> */
> public void setPartsId( String partsId )
> {
> this.partsId = partsId;
> }
>
> /**
> * @return the partsName
> */
> public String getPartsName()
> {
> return this.partsName;
> }
>
> /**
> * @param partsName the partsName to set
> */
> public void setPartsName( String partsName )
> {
> this.partsName = partsName;
> }
>
> /**
> * @return the partVendor
> */
> public String getPartVendor()
> {
> return partVendor;
> }
>
> /**
> * @param partVendor the partVendor to set
> */
> public void setPartVendor( String partVendor )
> {
> this.partVendor = partVendor;
> }
>
> }
>
>
> Finally...... here is my class to create an instance of the
> AutoManufacturer
> object that would become the FACT for the rule to fire upon.
>
> package com.nabble.demo;
>
> import com.nabble.demo.model.AutoManufacturer;
> import com.nabble.demo.model.Brand;
> import com.nabble.demo.model.Model;
> import com.nabble.demo.model.Part;
>
> /**
> * Class to demonstrate using Autommaker model objects
>
> */
> public class ProcessAutomaker
> {
>
> /**
> * @param args
> */
> public static void main( String[] args )
> {
>
> //Create instance of AutoManufacturer
> AutoManufacturer automanf = new AutoManufacturer();
> automanf.setManufacturerId("123456");
> automanf.setManufacturerName("Large Auto Maker");
>
> //Create instance of Model
> Model model = new Model();
> model.setModelName("LargeSUV");
> model.setModelName("MN444555");
>
> //Create instances of Part, add to current Model instance
> Part part = new Part();
> part.setPartsId("PP123456");
> part.setPartsName("Steering Wheel");
> part.setPartVendor("PV999999");
> model.addPart(part);
>
> part = new Part();
> part.setPartsId("PP123499");
> part.setPartsName("Brake Pedal");
> part.setPartVendor("PV999999");
> model.addPart(part);
>
> part = new Part();
> part.setPartsId("PP123499");
> part.setPartsName("Floor Mat");
> part.setPartVendor("PV111111");
> model.addPart(part);
>
>
> //Create instance of Brand
> Brand brand = new Brand();
> brand.setBrandName("Brand1");
> brand.setBrandId("BB123456");
> brand.addModel(model);
>
> automanf.addBrand(brand);
>
>
> }
>
> }
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/Accessing-properties-of-an-object-within-a-Nested-Collection-of-a-Nested-Collection-tp3955591.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
More information about the rules-users
mailing list