fr.boreal.storage.natives.DefaultInMemoryAtomSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of integraal-storage Show documentation
Show all versions of integraal-storage Show documentation
This module represents the physical atom storage for integraal
The newest version!
/**
*
*/
package fr.boreal.storage.natives;
import fr.boreal.model.formula.api.FOFormula;
import fr.boreal.model.kb.api.FactBase;
import fr.boreal.model.kb.api.FactBaseType;
import fr.boreal.model.kb.impl.FactBaseDescription;
import fr.boreal.model.logicalElements.api.Atom;
import fr.boreal.model.logicalElements.api.Predicate;
import fr.boreal.model.logicalElements.api.Substitution;
import fr.boreal.model.logicalElements.api.Term;
import fr.boreal.model.logicalElements.impl.SubstitutionImpl;
import fr.lirmm.boreal.util.AtomType;
import fr.lirmm.boreal.util.stream.filter.FilterIteratorWithoutException;
import fr.lirmm.boreal.util.stream.filter.TypeFilter;
import java.util.*;
import java.util.stream.Stream;
/**
* The DefaultInMemoryAtomSet is a simple in memory atom set based on a java
* set. It is a wrapper for any Collection that implements the Set interface.
* Recommended sets : - By default, use a HashSet<Atom> - If you want an
* ordered set, you can use a TreeSet<Atom> - If you want to access
* elements in the order there are added into the set, use
* LinkedHashSet<Atom> that combine a HashSet and a Linked List
*
* @author Guillaume Pérution-Kihli
*
*/
public class DefaultInMemoryAtomSet implements FactBase {
private final Set atomSet;
/**
* Default constructor By default, we use a HashSet<Atom>
*/
public DefaultInMemoryAtomSet() {
atomSet = new HashSet<>();
}
/**
* This constructor allows you to use any Collection that implements the
* Set<Atom> interface
*
* @param javaAtomSet the Set that will contain all the atoms
*/
public DefaultInMemoryAtomSet(Set javaAtomSet) {
atomSet = javaAtomSet;
}
/**
* A copy constructor that can copy any Collection<Atom>
*
* @param collection the collection to copy
*/
public DefaultInMemoryAtomSet(Collection collection) {
this();
this.atomSet.addAll(collection);
}
/**
* A copy constructor using a variable number of Atoms
*
* @param atoms the atoms to initialize the storage with
*/
public DefaultInMemoryAtomSet(Atom... atoms) {
this();
Collections.addAll(this.atomSet, atoms);
}
public Set getAtomsInMemory() {
return atomSet;
}
@Override
public Iterator match(Atom a) {
return this.match(a, new SubstitutionImpl());
}
@Override
public Iterator match(Atom a, Substitution s) {
final AtomType atomType = new AtomType(a, s);
return new FilterIteratorWithoutException<>(this.getAtomsByPredicate(a.getPredicate()),
new TypeFilter(atomType, s.createImageOf(a)));
}
@Override
public Iterator getAtomsByPredicate(Predicate p) {
return this.atomSet.stream().filter(a -> a.getPredicate().equals(p)).iterator();
}
@Override
public Iterator getPredicates() {
return this.atomSet.stream().map(Atom::getPredicate).distinct().iterator();
}
@Override
public Iterator getTermsByPredicatePosition(Predicate p, int position) {
return this.atomSet.stream().filter(a -> a.getPredicate().equals(p)).map(a -> a.getTerm(position)).distinct()
.iterator();
}
@Override
public boolean add(FOFormula atoms) {
return this.addAll(atoms.asAtomSet());
}
@Override
public boolean addAll(Collection atoms) {
return this.atomSet.addAll(atoms);
}
@Override
public boolean add(Atom atom) {
return this.atomSet.add(atom);
}
@Override
public boolean remove(Atom atom) {
return this.atomSet.remove(atom);
}
@Override
public boolean remove(FOFormula atoms) {
return this.atomSet.removeAll(atoms.asAtomSet());
}
@Override
public boolean removeAll(Collection atoms) {
return this.atomSet.removeAll(atoms);
}
@Override
public boolean removeAll(Stream atoms) {
try {
atoms.forEach(atomSet::remove);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public Stream getAtoms() {
return this.atomSet.stream();
}
@Override
public FactBaseDescription getDescription(Predicate viewPredicate) {
return null;
}
@Override
public FactBaseType getType(Predicate viewPredicate) {
return FactBaseType.GRAAL;
}
}