use of org.eclipse.collections.impl.map.mutable.UnifiedMap in project narchy by automenta.
the class PrologToNAL method N.
private static nars.term.Term N(alice.tuprolog.Term t) {
if (t instanceof alice.tuprolog.Term) {
Struct s = (Struct) t;
String name = s.name();
switch(name) {
case ":-":
assert (s.subs() == 2);
// reverse, prolog is backwards
nars.term.Term pre = N(s.sub(1));
nars.term.Term post = N(s.sub(0));
// convert to implication first, then promote variables on the resulting pre/post
Term impl = $.impl(pre, post);
pre = impl.sub(0);
post = impl.sub(1);
if (pre.varQuery() > 0 && post.varQuery() > 0) {
MutableSet<nars.term.var.Variable> prev = new UnifiedSet();
pre.recurseTerms(Termlike::hasVarQuery, (a) -> {
if (a.op() == Op.VAR_QUERY)
prev.add((Variable) a);
return true;
}, null);
MutableSet<nars.term.var.Variable> posv = new UnifiedSet();
post.recurseTerms(Termlike::hasVarQuery, (a) -> {
if (a.op() == Op.VAR_QUERY)
posv.add((Variable) a);
return true;
}, null);
MutableSet<nars.term.var.Variable> common = prev.intersect(posv);
int cs = common.size();
if (cs > 0) {
Map<nars.term.Term, nars.term.Term> x = new UnifiedMap(cs);
for (nars.term.var.Variable c : common) {
x.put(c, $.varIndep(c.toString().substring(1)));
}
impl = impl.replace(x);
}
}
return impl;
case ",":
return CONJ.the(N(s.sub(0)), N(s.sub(1)));
default:
nars.term.Term atom = $.the(name);
int arity = s.subs();
if (arity == 0) {
return atom;
} else {
return $.inh($.p((nars.term.Term[]) Util.map(0, arity, i -> N(s.sub(i)), nars.term.Term[]::new)), atom);
}
}
} else if (t instanceof Var) {
return $.varQuery(((Var) t).name());
// throw new RuntimeException(t + " untranslated");
} else if (t instanceof NumberTerm.Int) {
return $.the(((NumberTerm.Int) t).intValue());
} else {
throw new TODO(t + " (" + t.getClass() + ") untranslatable");
}
}
use of org.eclipse.collections.impl.map.mutable.UnifiedMap in project narchy by automenta.
the class Osm method load.
public void load(InputStream fis) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
// factory.setXIncludeAware(false);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(fis);
Osm osm = this;
Collection<Element> relationElements = new FasterList<>();
NodeList childNodes = document.getDocumentElement().getChildNodes();
int cl = childNodes.getLength();
for (int i = 0; i < cl; i++) {
Node childNode = childNodes.item(i);
switch(childNode.getNodeName()) {
case "bounds":
{
osm.bounds = new OsmBounds((Element) childNode);
break;
}
case "node":
{
Element childElement = (Element) childNode;
long id = l(childElement.getAttribute("id"));
Map<String, String> osmTags = null;
NodeList nodeChildren = ((Element) childNode).getElementsByTagName("tag");
int nnc = nodeChildren.getLength();
for (int j = 0; j < nnc; j++) {
Node nodeChild = nodeChildren.item(j);
/*if ("tag".equals(nodeChild.getNodeName()))*/
{
Element ne = (Element) nodeChild;
if (osmTags == null)
osmTags = new UnifiedMap(1);
osmTags.put(ne.getAttribute("k"), ne.getAttribute("v"));
}
}
OsmNode oo = new OsmNode(id, new GeoCoordinate(childElement), osmTags);
osm.nodes.put(id, oo);
break;
}
case "way":
{
Element childElement = (Element) childNode;
long id = l(childElement.getAttribute("id"));
List<OsmNode> refOsmNodes = new FasterList<>();
Map<String, String> osmTags = null;
NodeList wayChildren = childNode.getChildNodes();
int l = wayChildren.getLength();
for (int j = 0; j < l; j++) {
Node wayChild = wayChildren.item(j);
String node = wayChild.getNodeName();
if ("nd".equals(node)) {
Element wayChildElement = (Element) wayChild;
refOsmNodes.add(osm.nodes.get(l(wayChildElement.getAttribute("ref"))));
} else if ("tag".equals(node)) {
Element nodeChildElement = (Element) wayChild;
if (osmTags == null)
osmTags = new UnifiedMap<>(1);
osmTags.put(nodeChildElement.getAttribute("k"), nodeChildElement.getAttribute("v"));
}
}
OsmWay ow = new OsmWay(id, refOsmNodes, osmTags);
osm.ways.put(id, ow);
break;
}
case "relation":
Element childElement = (Element) childNode;
long id = l(childElement.getAttribute("id"));
NodeList relationChildren = childElement.getElementsByTagName("tag");
Map<String, String> osmTags = null;
int l = relationChildren.getLength();
for (int j = 0; j < l; j++) {
Node relationChild = relationChildren.item(j);
/*if ("tag".equals(relationChild.getNodeName()))*/
{
Element e = (Element) relationChild;
if (osmTags == null)
osmTags = new UnifiedMap<>(1);
osmTags.put(e.getAttribute("k"), e.getAttribute("v"));
}
}
OsmRelation or = new OsmRelation(id, null, osmTags);
osm.relations.put(id, or);
relationElements.add(childElement);
break;
}
}
// Relation 2nd pass
for (Element relationElement : relationElements) {
long id = l(relationElement.getAttribute("id"));
OsmRelation osmRelation = osm.relations.get(id);
Map<String, String> tags = osmRelation.tags;
String highway, natural, building, building_part, landuse;
if (tags.isEmpty()) {
highway = natural = building = building_part = landuse = null;
} else {
highway = tags.get("highway");
natural = tags.get("natural");
building = tags.get("building");
building_part = tags.get("building:part");
landuse = tags.get("landuse");
}
// getChildNodes();
NodeList relationChildren = relationElement.getElementsByTagName("member");
List<OsmElement> osmMembers = null;
int l = relationChildren.getLength();
for (int j = 0; j < l; j++) {
Node relationChild = relationChildren.item(j);
/*if ("member".equals(relationChild.getNodeName()))*/
{
Element r = (Element) relationChild;
String type = r.getAttribute("type");
long ref = l(r.getAttribute("ref"));
// String role = relationChildElement.getAttribute("role");
OsmElement member = null;
switch(type) {
case "node":
member = osm.nodes.get(ref);
break;
case "way":
member = osm.ways.get(ref);
if (member != null) {
if (highway != null) {
member.tag("highway", highway);
}
if (natural != null) {
member.tag("natural", natural);
}
if (building != null) {
member.tag("building", building);
}
if (building_part != null) {
member.tag("building:part", building_part);
}
if (landuse != null) {
member.tag("landuse", landuse);
}
}
break;
case "relation":
member = osm.relations.get(ref);
break;
}
if (member != null) {
if (osmMembers == null)
osmMembers = new FasterList(1);
osmMembers.add(member);
}
}
}
if (osmMembers != null && !osmMembers.isEmpty())
osmRelation.addChildren(osmMembers);
}
// Relation 3rd pass: merge multipolygon
for (Element relationElement : relationElements) {
long id = l(relationElement.getAttribute("id"));
OsmRelation osmRelation = osm.relations.get(id);
Map<String, String> tags = osmRelation.tags;
String type = tags.get("type");
if (!"multipolygon".equals(type))
continue;
List<? extends OsmElement> oc = osmRelation.children();
int s = oc.size();
for (int i = 0; i < s; i++) {
OsmElement e1 = oc.get(i);
if (e1 == null || e1.getClass() != OsmWay.class)
continue;
OsmWay w1 = (OsmWay) e1;
if (w1.isClosed())
continue;
repeat: {
ListIterator<? extends OsmElement> ii = oc.listIterator(i);
while (ii.hasNext()) {
OsmElement e2 = ii.next();
if (e2 == null || e2.getClass() != OsmWay.class)
continue;
OsmWay w2 = (OsmWay) e2;
if (w1.isFollowedBy(w2)) {
w1.addOsmWay(w2);
ii.remove();
// break repeat; // loop again
}
}
}
}
}
}
use of org.eclipse.collections.impl.map.mutable.UnifiedMap in project eclipse-collections by eclipse.
the class UnifiedMapTest method newWith.
@Override
public <T> MutableMap<Object, T> newWith(T... elements) {
Random random = new Random(CURRENT_TIME_MILLIS);
MutableMap<Object, T> result = new UnifiedMap<>();
for (T each : elements) {
assertNull(result.put(random.nextDouble(), each));
}
return result;
}
Aggregations