| 1 | package com.hammurapi.eventbus.tests.familyties; |
| 2 | |
| 3 | import java.util.concurrent.ExecutorService; |
| 4 | import java.util.concurrent.Executors; |
| 5 | |
| 6 | import com.hammurapi.eventbus.InferencePolicy; |
| 7 | import com.hammurapi.eventbus.tests.familyties.model.Child; |
| 8 | import com.hammurapi.eventbus.tests.familyties.model.Person; |
| 9 | import com.hammurapi.eventbus.tests.familyties.model.Relative; |
| 10 | import com.hammurapi.eventbus.tests.familyties.model.Spouse; |
| 11 | import com.hammurapi.eventbus.tests.familyties.rules.DaughterRule; |
| 12 | import com.hammurapi.eventbus.tests.familyties.rules.DaughterRuleJavaBinder; |
| 13 | import com.hammurapi.eventbus.tests.familyties.rules.GrandRules; |
| 14 | import com.hammurapi.eventbus.tests.familyties.rules.GrandRulesJavaBinder; |
| 15 | import com.hammurapi.eventbus.tests.familyties.rules.ParentChildRules; |
| 16 | import com.hammurapi.eventbus.tests.familyties.rules.ParentChildRulesJavaBinder; |
| 17 | import com.hammurapi.eventbus.tests.familyties.rules.ParentRules; |
| 18 | import com.hammurapi.eventbus.tests.familyties.rules.ParentRulesJavaBinder; |
| 19 | import com.hammurapi.eventbus.tests.familyties.rules.SecondaryRules; |
| 20 | import com.hammurapi.eventbus.tests.familyties.rules.SecondaryRulesJavaBinder; |
| 21 | import com.hammurapi.eventbus.tests.familyties.rules.SiblingRules; |
| 22 | import com.hammurapi.eventbus.tests.familyties.rules.SiblingRulesJavaBinder; |
| 23 | import com.hammurapi.eventbus.tests.familyties.rules.SonRule; |
| 24 | import com.hammurapi.eventbus.tests.familyties.rules.SonRuleJavaBinder; |
| 25 | import com.hammurapi.eventbus.tests.familyties.rules.SpouseRules; |
| 26 | import com.hammurapi.eventbus.tests.familyties.rules.SpouseRulesJavaBinder; |
| 27 | |
| 28 | public class FamilyTiesApp { |
| 29 | |
| 30 | /** |
| 31 | * @param args |
| 32 | */ |
| 33 | public static String infer() throws Exception { |
| 34 | ExecutorService executorService = Executors.newFixedThreadPool(2); |
| 35 | |
| 36 | // Create bus |
| 37 | FamilyTiesEventStoreImpl.Config storeConfig = new FamilyTiesEventStoreImpl.Config(); |
| 38 | storeConfig.setExecutorService(executorService); |
| 39 | FamilyTiesEventStore eventStore = new FamilyTiesEventStoreImpl(storeConfig); |
| 40 | |
| 41 | FamilyTiesEventBus.Config busConfig = new FamilyTiesEventBus.Config(); |
| 42 | busConfig.setStore(eventStore); |
| 43 | busConfig.setExecutorService(executorService); |
| 44 | |
| 45 | busConfig.setInferencePolicy(InferencePolicy.EXCLUSIVE); |
| 46 | |
| 47 | FamilyTiesEventBus bus = new FamilyTiesEventBus(busConfig); |
| 48 | |
| 49 | // Bind rules. |
| 50 | new DaughterRuleJavaBinder().bind(new DaughterRule(), bus); |
| 51 | new GrandRulesJavaBinder().bind(new GrandRules(), bus); |
| 52 | new ParentChildRulesJavaBinder().bind(new ParentChildRules(), bus); |
| 53 | new ParentRulesJavaBinder().bind(new ParentRules(), bus); |
| 54 | new SecondaryRulesJavaBinder().bind(new SecondaryRules(), bus); |
| 55 | new SiblingRulesJavaBinder().bind(new SiblingRules(), bus); |
| 56 | new SonRuleJavaBinder().bind(new SonRule(), bus); |
| 57 | new SpouseRulesJavaBinder().bind(new SpouseRules(), bus); |
| 58 | |
| 59 | long start = System.currentTimeMillis(); |
| 60 | |
| 61 | // Post seed relationships |
| 62 | Person kate = new Person("Kate", 58, false); |
| 63 | Person victor = new Person("Victor", 63, true); |
| 64 | bus.post(new Spouse(kate, victor)); |
| 65 | |
| 66 | Person peter = new Person("Peter", 37, true); |
| 67 | bus.post(new Child(peter, kate)); |
| 68 | bus.post(new Child(peter, victor)); |
| 69 | |
| 70 | Person alison = new Person("Alison", 36, false); |
| 71 | bus.post(new Spouse(peter, alison)); |
| 72 | |
| 73 | Person lucy = new Person("Lucy", 17, false); |
| 74 | bus.post(new Child(lucy, alison)); |
| 75 | |
| 76 | Person nancy = new Person("Nancy", 14, false); |
| 77 | bus.post(new Child(nancy, peter)); |
| 78 | |
| 79 | Person dan = new Person("Dan", 7, true); |
| 80 | bus.post(new Child(dan, peter)); |
| 81 | bus.post(new Child(dan, alison)); |
| 82 | |
| 83 | Person audrey = new Person("Audrey", 4, false); |
| 84 | bus.post(new Child(audrey, peter)); |
| 85 | bus.post(new Child(audrey, alison)); |
| 86 | |
| 87 | Person tanya = new Person("Tanya", 31, false); |
| 88 | Person max = new Person("Max", 32, true); |
| 89 | bus.post(new Spouse(tanya, max)); |
| 90 | bus.post(new Child(tanya, kate)); |
| 91 | bus.post(new Child(tanya, victor)); |
| 92 | |
| 93 | Person vilma = new Person("Vilma", 14, false); |
| 94 | bus.post(new Child(vilma, tanya)); |
| 95 | |
| 96 | Person george = new Person("George", 10, true); |
| 97 | bus.post(new Child(george, tanya)); |
| 98 | |
| 99 | Person lisa = new Person("Lisa", 5, false); |
| 100 | bus.post(new Child(lisa, tanya)); |
| 101 | bus.post(new Child(lisa, max)); |
| 102 | |
| 103 | bus.join(); |
| 104 | |
| 105 | StringBuilder sb = new StringBuilder(); |
| 106 | sb.append("Inference time (ms): "+(System.currentTimeMillis()-start)+"\n"); |
| 107 | for (Relative relative: bus.getStore().getRelatives(lisa)) { |
| 108 | sb.append(relative+"\n"); |
| 109 | } |
| 110 | executorService.shutdown(); |
| 111 | return sb.toString(); |
| 112 | } |
| 113 | |
| 114 | public static void main(String[] args) throws Exception { |
| 115 | // Thread.sleep(60000); |
| 116 | System.out.println(infer()); |
| 117 | } |
| 118 | |
| 119 | } |