/** * @param tier the index of the desired tier. * @return a copy of this tier. */ public final List<String> getTier(int tier) { ensureTiers(tier); List<String> names = new ArrayList<>(); for (MyNode MyNode : tierSpecs.get(tier)) names.add(MyNode.getName()); return names; }
/** @return the list of edges not in any tier. */ public final List<String> getVariablesNotInTiers() { List<MyNode> notInTier = new ArrayList<>(myNodes); for (Set<MyNode> tier : tierSpecs) { if (tier == null) tier = new HashSet<>(); notInTier.removeAll(tier); } List<String> names = new ArrayList<>(); for (MyNode MyNode : notInTier) names.add(MyNode.getName()); return names; }
/** Iterator over the KnowledgeEdge's representing required edges. */ public final Iterator<KnowledgeEdge> requiredEdgesIterator() { Set<KnowledgeEdge> edges = new HashSet<>(); for (OrderedPair<Set<MyNode>> o : requiredRulesSpecs) { final Set<MyNode> first = o.getFirst(); for (MyNode s1 : first) { final Set<MyNode> second = o.getSecond(); for (MyNode s2 : second) { if (!s1.equals(s2)) { edges.add(new KnowledgeEdge(s1.getName(), s2.getName())); } } } } return edges.iterator(); }
private Set<MyNode> getExtent(String spec) { Set<String> split = split(spec); Set<MyNode> matches = new HashSet<>(); for (String _spec : split) { _spec = _spec.replace("*", ".*"); java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(_spec); for (MyNode var : myNodes) { Matcher matcher = pattern.matcher(var.getName()); if (matcher.matches()) { matches.add(var); } } } return matches; }
/** * Adds the given variable or wildcard pattern to the given tier. The tier is a non-negative * integer. */ public final void addToTier(int tier, String spec) { if (tier < 0) { throw new IllegalArgumentException(); } if (spec == null) { throw new NullPointerException(); } spec = checkSpec(spec); final Set<MyNode> vars = getExtent(spec); ensureTiers(tier); for (MyNode s : vars) { if (checkVarName(s.getName())) { addVariable(s.getName()); tierSpecs.get(tier).add(s); } } }
/** Marks the edge var1 --> var2 as required. */ public final void setRequired(String spec1, String spec2) { addVariable(spec1); addVariable(spec2); spec1 = checkSpec(spec1); spec2 = checkSpec(spec2); Set<MyNode> f1 = getExtent(spec1); Set<MyNode> f2 = getExtent(spec2); for (MyNode s : f1) { if (checkVarName(s.getName())) { addVariable(s.getName()); } } for (MyNode s : f2) { if (checkVarName(s.getName())) { addVariable(s.getName()); } } OrderedPair<Set<MyNode>> o = new OrderedPair<>(f1, f2); requiredRulesSpecs.add(o); }
/** Iterator over the knowledge's explicitly forbidden edges. */ public final Iterator<KnowledgeEdge> explicitlyForbiddenEdgesIterator() { Set<OrderedPair<Set<MyNode>>> copy = new HashSet<>(forbiddenRulesSpecs); copy.removeAll(forbiddenTierRules()); for (KnowledgeGroup group : knowledgeGroups) { copy.remove(knowledgeGroupRules.get(group)); } Set<KnowledgeEdge> edges = new HashSet<>(); for (OrderedPair<Set<MyNode>> o : copy) { final Set<MyNode> first = o.getFirst(); for (MyNode s1 : first) { final Set<MyNode> second = o.getSecond(); for (MyNode s2 : second) { edges.add(new KnowledgeEdge(s1.getName(), s2.getName())); } } } return edges.iterator(); }
/** @return a copy of the list of variable, in alphabetical order. */ public List<String> getVariables() { List<String> names = new ArrayList<>(); for (MyNode MyNode : myNodes) names.add(MyNode.getName()); return new ArrayList<>(names); }