Search in sources :

Example 6 with UnifiedMap

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");
    }
}
Also used : Struct(alice.tuprolog.Struct) NumberTerm(alice.tuprolog.NumberTerm) Iterables(com.google.common.collect.Iterables) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) CONJ(nars.Op.CONJ) Var(alice.tuprolog.Var) nars.$(nars.$) Theory(alice.tuprolog.Theory) Util(jcog.Util) Variable(nars.term.var.Variable) MutableSet(org.eclipse.collections.api.set.MutableSet) TODO(jcog.TODO) Op(nars.Op) Termlike(nars.term.Termlike) Map(java.util.Map) Term(nars.term.Term) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) TODO(jcog.TODO) Variable(nars.term.var.Variable) NumberTerm(alice.tuprolog.NumberTerm) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Var(alice.tuprolog.Var) NumberTerm(alice.tuprolog.NumberTerm) Term(nars.term.Term) Struct(alice.tuprolog.Struct) Termlike(nars.term.Termlike) Variable(nars.term.var.Variable) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) Term(nars.term.Term)

Example 7 with UnifiedMap

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
                    }
                }
            }
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) FasterList(jcog.list.FasterList) List(java.util.List) FasterList(jcog.list.FasterList) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) LongObjectHashMap(org.eclipse.collections.impl.map.mutable.primitive.LongObjectHashMap) Map(java.util.Map)

Example 8 with UnifiedMap

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;
}
Also used : Random(java.util.Random) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap)

Aggregations

UnifiedMap (org.eclipse.collections.impl.map.mutable.UnifiedMap)8 Random (java.util.Random)5 Map (java.util.Map)2 NumberTerm (alice.tuprolog.NumberTerm)1 Struct (alice.tuprolog.Struct)1 Theory (alice.tuprolog.Theory)1 Var (alice.tuprolog.Var)1 Financial (com.github.robozonky.api.notifications.Financial)1 PortfolioOverview (com.github.robozonky.api.strategies.PortfolioOverview)1 Iterables (com.google.common.collect.Iterables)1 List (java.util.List)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 TODO (jcog.TODO)1 Util (jcog.Util)1 FasterList (jcog.list.FasterList)1 nars.$ (nars.$)1 Op (nars.Op)1 CONJ (nars.Op.CONJ)1 Term (nars.term.Term)1