/** * Creates the files containing all paths for all concepts up to a certain depth * * @param kb * @param concepts * @throws IOException */ public static void createIndexFiles(KnowledgeBase kb, Collection<String> concepts) throws IOException { Iterator<String> conceptIterator = kb.getConceptIterator(); String outputDir = KnowledgeConfiguration.getInstance().getKnowledgeBasePathIndexDir(kb) + TXT_FILE_DIR; if (!Files.isDirectoryEmpty(new File(outputDir))) { log.warn(outputDir + " IS NOT EMPTY, BAILING OUT ..."); return; } int maxSearchDepth = getMaxSearchDepth(); DirectoryFileManager dirManager = new DirectoryFileManager(outputDir, 1000); log.info("CREATING THE INDEXABLE PATH FILES FOR: " + kb.name() + " INTO " + outputDir); int idx = 0; List<String> goodConcepts = new ArrayList<String>(); while (conceptIterator.hasNext()) { String concept = conceptIterator.next(); if (concepts.isEmpty() || concepts.contains(concept)) goodConcepts.add(concept); idx++; if ((idx % 100000) == 0) log.info("ITERATED THROUGH " + idx + " CONCEPTS SO FAR ..."); } List<List<String>> splitConcepts = Collections.split(goodConcepts, goodConcepts.size() / NSPLITS); int nThreads = splitConcepts.size(); ExecutorService threadExecutor = Executors.newFixedThreadPool(nThreads); CountDownLatch doneSignal = new CountDownLatch(nThreads); for (int i = 0; i < nThreads; i++) { ConceptIndexer worker = new ConceptIndexer(splitConcepts.get(i), i, dirManager, kb, maxSearchDepth, doneSignal); threadExecutor.execute(worker); } Timer timer = new Timer(); try { doneSignal.await(); threadExecutor.shutdown(); } catch (InterruptedException ie) { ie.printStackTrace(); } timer.tick("TO CREATE INDEXABLE FILES"); }
/** Returns list of all user defined predicates with the specified name. */ public static List<PredicateKey> getPredicateKeysByName(KnowledgeBase kb, String predicateName) { List<PredicateKey> matchingKeys = new ArrayList<>(); for (PredicateKey key : kb.getUserDefinedPredicates().keySet()) { if (predicateName.equals(key.getName())) { matchingKeys.add(key); } } return matchingKeys; }
/** * Returns {@code true} if the predicate represented by the specified {@link Term} never succeeds * on re-evaluation. */ public static boolean isSingleAnswer(KnowledgeBase kb, Term term) { if (term.getType().isVariable()) { return false; } else if (isConjunction(term)) { return isConjunctionWithSingleResult(kb, term); } else { PredicateFactory ef = kb.getPredicateFactory(term); return ef instanceof AbstractSingletonPredicate; } }
public boolean verify(KnowledgeBase kb) { final Map<KnowledgeElement, Boolean> results = new LinkedHashMap<KnowledgeElement, Boolean>(); KnowledgeBaseVisitor visitor = new AbstractKnowledgeBaseVisitor() { Collection<Variable> vars = null; Collection<Variable> varsInBody = new HashSet<Variable>(); Collection<Variable> varsInHead = new ArrayList<Variable>(); DerivationRule context = null; public boolean visit(DerivationRule r) { context = r; varsInHead.clear(); varsInBody.clear(); return true; } public void endVisit(DerivationRule r) { context = null; Variable var = null; for (Variable v : varsInHead) { if (!varsInBody.contains(v)) var = v; } if (var == null) reportOK(r); else reportViolation("variable ", var, " in ", r, " occurs only in rule head"); results.put(r, var == null); } public boolean visit(Fact f) { if (context == null) { return false; } else if (context.getHead() == f) { vars = varsInHead; } else if (context.getBody().contains(f)) { vars = varsInBody; } return true; } public boolean visit(Variable t) { vars.add(t); return true; } }; kb.accept(visitor); for (boolean b : results.values()) { if (!b) return false; } return true; }
public void removeForwardReferences() { KnowledgeBase kb = this.getKnowledgeBase(); Slot branches = kb.getSlot("branches"); Slot followed_by = kb.getSlot("followed_by"); // logger.debug("Slot "+branches.getName()); // logger.debug("Slot "+followed_by.getName()); Slot label = kb.getSlot("label"); Collection steps = getOwnSlotValues(kb.getSlot("steps")); for (Iterator iterater = steps.iterator(); iterater.hasNext(); ) { Instance step = (Instance) iterater.next(); // logger.debug("removeForwardReferences: node ="+step.getOwnSlotValue(label)); /*for (Iterator slots=step.getOwnSlots().iterator(); slots.hasNext();){ logger.debug("has slot "+((Slot)slots.next()).getName()); }*/ try { if (step.hasOwnSlot(branches)) { /*logger.debug("removeForwardReferences: "+ step.getOwnSlotValue(label)+ " has branches"); */ step.setOwnSlotValues(branches, new ArrayList()); } else if (step.hasOwnSlot(followed_by)) { /* logger.debug("removeForwardReferences: "+ step.getOwnSlotValue(label)+ " has followed_by value "+step.getOwnSlotValue(followed_by));*/ step.setOwnSlotValue(followed_by, null); } else logger.error( "removeForwardReferences: " + step.getOwnSlotValue(label) + "has no followed_by or branches slot"); } catch (Exception e) { logger.error( "Exception making " + step.getOwnSlotValue(label) + " null, Message: " + e.getMessage(), e); } } }
public void readInput(String fileName) { int i1 = 0; try { @SuppressWarnings("resource") BufferedReader br = new BufferedReader(new FileReader(fileName)); int noOfQueries = Integer.parseInt(br.readLine()); queries = new ArrayList<Predicate>(); for (int i = 0; i < noOfQueries; i++) { queries.add(new Predicate(br.readLine())); } int noOfSentences = Integer.parseInt(br.readLine()); for (i1 = 0; i1 < noOfSentences; i1++) { KBObj.store(br.readLine()); } } catch (Exception e) { System.out.println(e.getMessage() + "sent number:" + i1); } }
public void addForwardReferences() { KnowledgeBase kb = this.getKnowledgeBase(); Collection transitions = getOwnSlotValues(kb.getSlot("transitions")); Slot fromSlot = kb.getSlot(":FROM"); Slot toSlot = kb.getSlot(":TO"); Instance from = null; Instance to = null; Slot branches = kb.getSlot("branches"); Slot followed_by = kb.getSlot("followed_by"); Slot label = kb.getSlot("label"); for (Iterator transition = transitions.iterator(); transition.hasNext(); ) { Instance inst = (Instance) transition.next(); from = (Instance) inst.getOwnSlotValue(fromSlot); if (from != null) { to = (Instance) inst.getOwnSlotValue(toSlot); if (to != null) { try { if (from.hasOwnSlot(branches)) { /*logger.debug("addForwardReferences: "+ from.getOwnSlotValue(label)+ " has branches"); */ if ((from.getOwnSlotValues(branches) == null) || (!from.getOwnSlotValues(branches).contains(to))) from.addOwnSlotValue(branches, to); } else if (from.hasOwnSlot(followed_by)) { /*logger.debug("addForwardReferences: "+ from.getOwnSlotValue(label)+ " has followed by");*/ if (!(to.equals(from.getOwnSlotValue(followed_by)))) from.setOwnSlotValue(followed_by, to); } else logger.error( "addForwardReferences: " + from.getOwnSlotValue(label) + "has no followed_by or branches slot"); } catch (Exception e) { logger.error( "Exception adding " + to.getOwnSlotValue(label) + " to " + from.getOwnSlotValue(label)); } } } } }
public void getConcepts() throws IOException { int counter = 0; Timer timer = new Timer(); for (String concept : concepts) { Set<List<String>> paths = kb.getAllPathsFrom(concept, maxSearchDepth); FileWriter writerFrom = dirManager.getFileWriter(concept + ".txt"); // saves the paths starting from the concept for (List<String> path : paths) writerFrom.write(Strings.join(path) + "\n"); writerFrom.close(); if ((counter % 100) == 0) timer.tick( "\t[WORKER] " + id + " TO CREATE INDEXABLE FILES FOR " + counter + " CONCEPTS SO FAR ... "); counter++; } }
public boolean useGraphWidget() { KnowledgeBase kb = this.getKnowledgeBase(); return ((kb.getSlot("transitions") != null) && (getOwnSlotValues(kb.getSlot("transitions")) != null)); }
/** Returns a {@link Predicate} instance for the specified {@link Term}. */ public static Predicate getPredicate(KnowledgeBase kb, Term t) { return kb.getPredicateFactory(t).getPredicate(t.getArgs()); }
protected void processRules(OrderDTO newOrder) throws TaskException { // now we have the line with good defaults, the order and the item // These have to be visible to the rules KnowledgeBase knowledgeBase; try { knowledgeBase = readKnowledgeBase(); } catch (Exception e) { throw new TaskException(e); } session = knowledgeBase.newStatefulKnowledgeSession(); List<Object> rulesMemoryContext = new ArrayList<Object>(); rulesMemoryContext.add(helperOrder); // add OrderDTO to rules memory context newOrder.setCurrency(new CurrencyDAS().find(newOrder.getCurrency().getId())); if (newOrder.getCreateDate() == null) { newOrder.setCreateDate(new Date()); } rulesMemoryContext.add(newOrder); for (OrderLineDTO line : newOrder.getLines()) { if (line.getItem() != null) { ItemBL item = new ItemBL(line.getItemId()); rulesMemoryContext.add( item.getDTO( helperOrder.getLanguage(), helperOrder.getUserId(), helperOrder.getEntityId(), helperOrder.getCurrencyId())); } rulesMemoryContext.add(line); } if (newOrder.getPricingFields() != null && newOrder.getPricingFields().size() > 0) { for (PricingField pf : newOrder.getPricingFields()) { rulesMemoryContext.add(pf); } } try { Integer userId = newOrder.getBaseUserByUserId().getId(); UserDTOEx user = DTOFactory.getUserDTOEx(userId); rulesMemoryContext.add(user); ContactBL contact = new ContactBL(); contact.set(userId); ContactDTOEx contactDTO = contact.getDTO(); rulesMemoryContext.add(contactDTO); // Add the subscriptions OrderBL order = new OrderBL(); for (OrderDTO myOrder : order.getActiveRecurringByUser(userId)) { for (OrderLineDTO myLine : myOrder.getLines()) { rulesMemoryContext.add(new Subscription(myLine)); } } } catch (Exception e) { throw new TaskException(e); } session.setGlobal("order", helperOrder); // then execute the rules executeStatefulRules(session, rulesMemoryContext); }