/** * Delete all the rules. */ public void deleteAllRules() { allRules.clear(); goalMap.clear(); ruleIndex.clear(); }
/** * Helper. Record a new predicate/value pattern. Ensure there is either * one wildcard or a number of specific patterns */ private void recordPredicatePattern(Node predicate, Node value) { if (predicatePatterns.contains(predicate, Node.ANY)) { // already fully covered } else if (value.equals(Node.ANY)) { predicatePatterns.remove(predicate); predicatePatterns.put(predicate, value); } else { // TODO possibly introduce a threshold here for the number of patterns allowed before generalizing predicatePatterns.put(predicate, value); } return; }
/** * <p>Construct a new one-to-many map whose contents are * initialised from the existing map.</p> * * @param map An existing one-to-many map */ public OneToManyMap( OneToManyMap<From, To> map ) { // copy the contents of the existing map // note we can't just use the copying constructor for hashmap // as we don't want to share the arraylists that are the key values for ( From key : map.keySet() ) { for ( Iterator<To> j = map.getAll( key ); j.hasNext(); ) { put( key, j.next() ); } } }
/** * <p> * Prepare the local alias map by reading the alias table from the concrete sub-class. * </p> */ protected OneToManyMap<Resource, Resource> aliasMap() { if (m_aliasesMap == null) { // aliases map not prepared yet, so initialise using the data from // the concrete profile class m_aliasesMap = new OneToManyMap<>(); Resource[][] aliases = aliasTable(); for ( Resource[] aliase : aliases ) { // since alias relationship is symmetric, we record both directions m_aliasesMap.put( aliase[0], aliase[1] ); m_aliasesMap.put( aliase[1], aliase[0] ); } } return m_aliasesMap; }
public void testConstruct1() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); map0.put( s0, s1 ); assertTrue( map0.contains( s0, s1 ) ); OneToManyMap<String, String> map1 = new OneToManyMap<>( map0 ); assertTrue( map1.contains( s0, s1 ) ); map0.put( s0, s2 ); assertTrue( map0.contains( s0, s2 ) ); assertFalse( map1.contains( s0, s2 ) ); assertFalse( map0.contains( s1, s2 ) ); }
/** * Set the internal rule from a precomputed state. */ @Override public void setRuleStore(Object ruleStore) { RuleStore rs = (RuleStore)ruleStore; predicatePatterns = rs.predicatePatterns; wildcardRule = rs.wildcardRule; isMonotonic = rs.isMonotonic; // Clone the RETE network to this engine RETERuleContext context = new RETERuleContext(infGraph, this); Map<RETENode, RETENode> netCopy = new HashMap<>(); clauseIndex = new OneToManyMap<>(); for ( Map.Entry<Node, RETENode> entry : rs.clauseIndex.entrySet() ) { clauseIndex.put( entry.getKey(), entry.getValue().clone( netCopy, context ) ); } }
public void testPutAll0() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); map0.put( s0, s1 ); map0.put( s0, s2 ); map0.put( s3, s4 ); OneToManyMap<String, String> map1 = new OneToManyMap<>(); map1.put( s0, s2 ); map1.put( s3, s4 ); map1.put( s0, s1 ); OneToManyMap<String, String> map2 = new OneToManyMap<>(); map2.putAll( map1 ); assertEquals( map0, map2 ); }
public void testSize() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); assertEquals( 0, map0.size() ); map0.put( s0, s1 ); assertEquals( 1, map0.size() ); map0.put( s0, s2 ); assertEquals( 2, map0.size() ); map0.put( s3, s4 ); assertEquals( 3, map0.size() ); map0.remove( s0, s2 ); assertEquals( 2, map0.size() ); map0.remove( s3, s4 ); assertEquals( 1, map0.size() ); map0.remove( s0, s1 ); assertEquals( 0, map0.size() ); }
public void testContainsKey() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); assertFalse( map0.containsKey( s0 ) ); assertFalse( map0.containsKey( s1 ) ); map0.put( s0, s1 ); assertTrue( map0.containsKey( s0 ) ); assertFalse( map0.containsKey( s1 ) ); }
public void testKeySet() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); Set<String> keys = new HashSet<>(); assertEquals( keys, map0.keySet() ); map0.put( s0, s1 ); keys.add( s0 ); assertEquals( keys, map0.keySet() ); map0.put( s2, s1 ); keys.add( s2 ); assertEquals( keys, map0.keySet() ); }
public void testValues() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); Set<String> vals = new HashSet<>(); assertEquals( vals, map0.values() ); map0.put( s0, s1 ); vals.add( s1 ); assertEquals( vals, map0.values() ); map0.put( s2, s1 ); assertEquals( vals, map0.values() ); map0.put( s2, s3 ); vals.add( s3 ); assertEquals( vals, map0.values() ); }
public void testEquals() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); map0.put( s0, s1 ); map0.put( s0, s2 ); map0.put( s3, s4 ); OneToManyMap<String, String> map1 = new OneToManyMap<>(); map1.put( s3, s4 ); map1.put( s0, s1 ); map1.put( s0, s2 ); OneToManyMap<String, String> map2 = new OneToManyMap<>(); map2.put( s0, s2 ); map2.put( s3, s4 ); assertTrue( map0.equals( map1 ) ); assertTrue( map1.equals( map0 ) ); assertTrue( map0.hashCode() == map1.hashCode() ); assertFalse( map0.equals( map2 )); assertFalse( map2.equals( map0 )); }
public void testGetAll() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); Iterator<String> i = map0.getAll(s0); assertNotNull( i ); assertFalse( i.hasNext() ); map0.put( s0, s1 ); i = map0.getAll(s0); assertNotNull( i ); assertTrue( i.hasNext() ); assertFalse( i.hasNext() ); map0.put( s0, s2 ); i = map0.getAll(s0); assertNotNull( i ); boolean founds1 = false, founds2 = false;
/** * <p>Put all entries from one map into this map. Tests for m being a * OneToManyMap, and, if so, copies all of the entries for each key.</p> * @param m The map whose contents are to be copied into this map */ @Override public void putAll( Map<? extends From, ? extends To> m ) { boolean many = (m instanceof OneToManyMap<?,?>); for ( From key : m.keySet() ) { if ( many ) { // Bizare way to write it but this way makes all compilers happy. OneToManyMap<?, ?> Z = (OneToManyMap<?, ?>) m; @SuppressWarnings( "unchecked" ) OneToManyMap<? extends From, ? extends To> X = (OneToManyMap<? extends From, ? extends To>) Z; Iterator<? extends To> j = X.getAll( key ); for (; j.hasNext(); ) { put( key, j.next() ); } } else { put( key, m.get( key ) ); } } }
/** * <p>Answer true if this mapping contains the pair * <code>(key, value)</code>.</p> * @param key A key object * @param value A value object * @return True if <code>key</code> has <code>value</code> * as one of its values in this mapping */ public boolean contains( Object key, Object value ) { for (Iterator<To> i = getAll( key ); i.hasNext(); ) { if (i.next().equals( value )) return true; } return false; }
public void testContainsValue() { OneToManyMap<String, String> map0 = new OneToManyMap<>(); assertFalse( map0.containsValue( s0 ) ); assertFalse( map0.containsValue( s1 ) ); assertFalse( map0.containsValue( s2 ) ); map0.put( s0, s1 ); assertFalse( map0.containsValue( s0 ) ); assertTrue( map0.containsValue( s1 ) ); assertFalse( map0.containsValue( s2 ) ); map0.put( s0, s2 ); assertFalse( map0.containsValue( s0 ) ); assertTrue( map0.containsValue( s1 ) ); assertTrue( map0.containsValue( s2 ) ); }
/** * Return a list of rules that match the given goal pattern * @param goal the goal being matched */ public List<Rule> rulesFor(TriplePattern goal) { List<Rule> rules = new ArrayList<>(); if (goal.getPredicate().isVariable()) { checkAll(goalMap.values().iterator(), goal, rules); } else { checkAll(goalMap.getAll(goal.getPredicate()), goal, rules); checkAll(goalMap.getAll(Node.ANY), goal, rules); } return rules; }