Example usage for com.google.common.collect Multimaps invertFrom

List of usage examples for com.google.common.collect Multimaps invertFrom

Introduction

In this page you can find the example usage for com.google.common.collect Multimaps invertFrom.

Prototype

public static <K, V, M extends Multimap<K, V>> M invertFrom(Multimap<? extends V, ? extends K> source, M dest) 

Source Link

Document

Copies each key-value mapping in source into dest , with its key and value reversed.

Usage

From source file:org.dllearner.reasoning.ClosedWorldReasoner.java

@SuppressWarnings("unchecked")
public SortedSet<OWLIndividual> getIndividualsImplFast(OWLClassExpression description)
        throws ReasoningMethodUnsupportedException {
    // policy: returned sets are clones, i.e. can be modified
    // (of course we only have to clone the leafs of a class OWLClassExpression tree)
    if (description.isOWLThing()) {
        return (TreeSet<OWLIndividual>) individuals.clone();
    } else if (description.isOWLNothing()) {
        return new TreeSet<>();
    } else if (!description.isAnonymous()) {
        if (classInstancesPos.containsKey(description.asOWLClass())) {
            return (TreeSet<OWLIndividual>) classInstancesPos.get(description).clone();
        } else {/*from   w  w w.jav  a2 s  . c om*/
            return new TreeSet<>();
        }
    } else if (description instanceof OWLObjectComplementOf) {
        OWLClassExpression operand = ((OWLObjectComplementOf) description).getOperand();
        if (!operand.isAnonymous()) {
            if (isDefaultNegation()) {
                if (precomputeNegations) {
                    return (TreeSet<OWLIndividual>) classInstancesNeg.get(operand).clone();
                }
                SetView<OWLIndividual> diff = Sets.difference(individuals, classInstancesPos.get(operand));
                return new TreeSet<>(diff);
            } else {
                return (TreeSet<OWLIndividual>) classInstancesNeg.get(operand).clone();
            }
        }
        // implement retrieval as default negation
        return new TreeSet<>(Sets.difference(individuals, getIndividualsImpl(operand)));
    } else if (description instanceof OWLObjectUnionOf) {
        SortedSet<OWLIndividual> ret = new TreeSet<>();
        for (OWLClassExpression operand : ((OWLObjectUnionOf) description).getOperands()) {
            ret.addAll(getIndividualsImpl(operand));
        }
        return ret;
    } else if (description instanceof OWLObjectIntersectionOf) {
        Iterator<OWLClassExpression> iterator = ((OWLObjectIntersectionOf) description).getOperands()
                .iterator();
        // copy instances of first element and then subtract all others
        SortedSet<OWLIndividual> ret = getIndividualsImpl(iterator.next());
        while (iterator.hasNext()) {
            ret.retainAll(getIndividualsImpl(iterator.next()));
        }
        return ret;
    } else if (description instanceof OWLObjectSomeValuesFrom) {
        SortedSet<OWLIndividual> returnSet = new TreeSet<>();

        OWLObjectPropertyExpression property = ((OWLObjectSomeValuesFrom) description).getProperty();
        OWLClassExpression filler = ((OWLObjectSomeValuesFrom) description).getFiller();

        //get instances of filler concept
        SortedSet<OWLIndividual> targetSet = getIndividualsImpl(filler);

        // the mapping of instances related by r
        Map<OWLIndividual, ? extends Collection<OWLIndividual>> mapping = opPos
                .get(property.getNamedProperty());

        if (property.isAnonymous()) { // \exists r^{-1}.C
            // invert the mapping
            // get all objects that are related by r to (at least) one subject which is of type C
            Multimap<OWLIndividual, OWLIndividual> mappingInv = Multimaps.invertFrom(
                    MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())),
                    TreeMultimap.<OWLIndividual, OWLIndividual>create());

            mapping = mappingInv.asMap();
        }

        // each individual is connected to a set of individuals via the property;
        // we loop through the complete mapping
        for (Entry<OWLIndividual, ? extends Collection<OWLIndividual>> entry : mapping.entrySet()) {
            Collection<OWLIndividual> inds = entry.getValue();
            for (OWLIndividual ind : inds) {
                if (targetSet.contains(ind)) {
                    returnSet.add(entry.getKey());
                    // once we found an individual, we do not need to check the others
                    break;
                }
            }
        }

        return returnSet;
    } else if (description instanceof OWLObjectAllValuesFrom) {
        // \forall restrictions are difficult to handle; assume we want to check
        // \forall hasChild.male with domain(hasChild)=Person; then for all non-persons
        // this is satisfied trivially (all of their non-existing children are male)
        //         if(!configurator.getForallRetrievalSemantics().equals("standard")) {
        //            throw new Error("Only forallExists semantics currently implemented.");
        //         }

        // problem: we need to make sure that \neg \exists r.\top \equiv \forall r.\bot
        // can still be reached in an algorithm (\forall r.\bot \equiv \bot under forallExists
        // semantics)
        OWLObjectPropertyExpression property = ((OWLObjectAllValuesFrom) description).getProperty();
        OWLClassExpression filler = ((OWLObjectAllValuesFrom) description).getFiller();

        // get instances of filler concept
        SortedSet<OWLIndividual> targetSet = getIndividualsImpl(filler);

        // the mapping of instances related by r
        Map<OWLIndividual, ? extends Collection<OWLIndividual>> mapping = opPos
                .get(property.getNamedProperty());

        if (property.isAnonymous()) { // \forall r^{-1}.C
            // invert the mapping
            // get all objects that are related by r to (at least) one subject which is of type C
            Multimap<OWLIndividual, OWLIndividual> mappingInv = Multimaps.invertFrom(
                    MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())),
                    TreeMultimap.<OWLIndividual, OWLIndividual>create());

            mapping = mappingInv.asMap();
        }

        //         SortedSet<OWLIndividual> returnSet = new TreeSet<OWLIndividual>(mapping.keySet());
        SortedSet<OWLIndividual> returnSet = (SortedSet<OWLIndividual>) individuals.clone();

        // each individual is connected to a set of individuals via the property;
        // we loop through the complete mapping
        for (Entry<OWLIndividual, ? extends Collection<OWLIndividual>> entry : mapping.entrySet()) {
            Collection<OWLIndividual> inds = entry.getValue();
            for (OWLIndividual ind : inds) {
                if (!targetSet.contains(ind)) {
                    returnSet.remove(entry.getKey());
                    break;
                }
            }
        }
        return returnSet;
    } else if (description instanceof OWLObjectMinCardinality) {
        OWLObjectPropertyExpression property = ((OWLObjectMinCardinality) description).getProperty();
        OWLClassExpression filler = ((OWLObjectMinCardinality) description).getFiller();

        //get instances of filler concept
        SortedSet<OWLIndividual> targetSet = getIndividualsImpl(filler);

        // the mapping of instances related by r
        Map<OWLIndividual, ? extends Collection<OWLIndividual>> mapping = opPos
                .get(property.getNamedProperty());

        if (property.isAnonymous()) { // \forall r^{-1}.C
            // invert the mapping
            // get all objects that are related by r to (at least) one subject which is of type C
            Multimap<OWLIndividual, OWLIndividual> mappingInv = Multimaps.invertFrom(
                    MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())),
                    TreeMultimap.<OWLIndividual, OWLIndividual>create());

            mapping = mappingInv.asMap();
        }

        SortedSet<OWLIndividual> returnSet = new TreeSet<>();

        int number = ((OWLObjectMinCardinality) description).getCardinality();

        for (Entry<OWLIndividual, ? extends Collection<OWLIndividual>> entry : mapping.entrySet()) {
            int nrOfFillers = 0;
            int index = 0;
            Collection<OWLIndividual> inds = entry.getValue();

            // we do not need to run tests if there are not sufficiently many fillers
            if (inds.size() < number) {
                continue;
            }

            for (OWLIndividual ind : inds) {
                // stop inner loop when nr of fillers is reached
                if (nrOfFillers >= number) {
                    returnSet.add(entry.getKey());
                    break;
                }
                // early abort when too many instance checks failed
                if (inds.size() - index < number) {
                    break;
                }
                if (targetSet.contains(ind)) {
                    nrOfFillers++;
                }
                index++;
            }
        }

        return returnSet;
    } else if (description instanceof OWLObjectMaxCardinality) {
        OWLObjectPropertyExpression property = ((OWLObjectMaxCardinality) description).getProperty();
        OWLClassExpression filler = ((OWLObjectMaxCardinality) description).getFiller();
        int number = ((OWLObjectMaxCardinality) description).getCardinality();

        //get instances of filler concept
        SortedSet<OWLIndividual> targetSet = getIndividualsImpl(filler);

        // the mapping of instances related by r
        Map<OWLIndividual, ? extends Collection<OWLIndividual>> mapping = opPos
                .get(property.getNamedProperty());

        if (property.isAnonymous()) { // \forall r^{-1}.C
            // invert the mapping
            // get all objects that are related by r to (at least) one subject which is of type C
            Multimap<OWLIndividual, OWLIndividual> mappingInv = Multimaps.invertFrom(
                    MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())),
                    TreeMultimap.<OWLIndividual, OWLIndividual>create());

            mapping = mappingInv.asMap();
        }

        // initially all individuals are in the return set and we then remove those
        // with too many fillers
        SortedSet<OWLIndividual> returnSet = (SortedSet<OWLIndividual>) individuals.clone();

        for (Entry<OWLIndividual, ? extends Collection<OWLIndividual>> entry : mapping.entrySet()) {
            int nrOfFillers = 0;
            int index = 0;
            Collection<OWLIndividual> inds = entry.getValue();

            // we do not need to run tests if there are not sufficiently many fillers
            if (number < inds.size()) {
                returnSet.add(entry.getKey());
                continue;
            }

            for (OWLIndividual ind : inds) {
                // stop inner loop when nr of fillers is reached
                if (nrOfFillers >= number) {
                    break;
                }
                // early abort when too many instance are true already
                if (inds.size() - index < number) {
                    returnSet.add(entry.getKey());
                    break;
                }
                if (targetSet.contains(ind)) {
                    nrOfFillers++;
                }
                index++;
            }
        }

        return returnSet;
    } else if (description instanceof OWLObjectHasValue) {
        OWLObjectPropertyExpression property = ((OWLObjectHasValue) description).getProperty();
        OWLIndividual value = ((OWLObjectHasValue) description).getFiller();

        // the mapping of instances related by r
        Map<OWLIndividual, ? extends Collection<OWLIndividual>> mapping = opPos
                .get(property.getNamedProperty());

        if (property.isAnonymous()) { // \exists r^{-1}.{a}
            // invert the mapping
            // get all objects that are related by r to (at least) one subject which is of type C
            Multimap<OWLIndividual, OWLIndividual> mappingInv = Multimaps.invertFrom(
                    MapUtils.createSortedMultiMap(opPos.get(property.getNamedProperty())),
                    TreeMultimap.<OWLIndividual, OWLIndividual>create());

            mapping = mappingInv.asMap();
        }

        SortedSet<OWLIndividual> returnSet = new TreeSet<>();

        for (Entry<OWLIndividual, ? extends Collection<OWLIndividual>> entry : mapping.entrySet()) {
            if (entry.getValue().contains(value)) {
                returnSet.add(entry.getKey());
            }
        }
        return returnSet;
    } else if (description instanceof OWLDataSomeValuesFrom) {
        OWLDataPropertyExpression property = ((OWLDataSomeValuesFrom) description).getProperty();
        OWLDataRange filler = ((OWLDataSomeValuesFrom) description).getFiller();

        if (filler.isDatatype()) {
            //we assume that the values are of the given datatype
            return new TreeSet<>(dpPos.get(property).keySet());
            //            OWLDatatype dt = filler.asOWLDatatype();
            //            if(dt.isDouble()){
            //               return new TreeSet<OWLIndividual>(dd.get(property).keySet());
            //            } else if(dt.isInteger()){
            //               return new TreeSet<OWLIndividual>(id.get(property).keySet());
            //            } else if(dt.isBoolean()){
            //               return bdPos.get(property);
            //            }
        } else if (filler instanceof OWLDatatypeRestriction) {
            OWLDatatype datatype = ((OWLDatatypeRestriction) filler).getDatatype();
            Set<OWLFacetRestriction> facetRestrictions = ((OWLDatatypeRestriction) filler)
                    .getFacetRestrictions();

            if (OWLAPIUtils.floatDatatypes.contains(datatype)) {
                double min = -Double.MAX_VALUE;
                double max = Double.MAX_VALUE;
                for (OWLFacetRestriction facet : facetRestrictions) {
                    if (facet.getFacet() == OWLFacet.MIN_INCLUSIVE) {
                        min = Double.parseDouble(facet.getFacetValue().getLiteral());
                    } else if (facet.getFacet() == OWLFacet.MAX_INCLUSIVE) {
                        max = Double.parseDouble(facet.getFacetValue().getLiteral());
                    }
                }
                Map<OWLIndividual, SortedSet<Double>> mapping = dd.get(property);
                SortedSet<OWLIndividual> returnSet = new TreeSet<>();

                for (Entry<OWLIndividual, SortedSet<Double>> entry : mapping.entrySet()) {
                    //we can skip of largest number is below minimum or lowest number is above maximum
                    if (entry.getValue().last() < min || entry.getValue().first() > max) {
                        continue;
                    }

                    //search a value which is in the interval
                    for (Double value : entry.getValue()) {
                        if (value >= min && value <= max) {
                            returnSet.add(entry.getKey());
                            break;
                        }
                    }
                }
                return returnSet;
            } else if (OWLAPIUtils.intDatatypes.contains(datatype)) {
                int min = Integer.MIN_VALUE;
                int max = Integer.MAX_VALUE;
                for (OWLFacetRestriction facet : facetRestrictions) {
                    if (facet.getFacet() == OWLFacet.MIN_INCLUSIVE) {
                        min = facet.getFacetValue().parseInteger();
                    } else if (facet.getFacet() == OWLFacet.MAX_INCLUSIVE) {
                        max = facet.getFacetValue().parseInteger();
                    }
                }
                Map<OWLIndividual, SortedSet<Integer>> mapping = id.get(property);
                SortedSet<OWLIndividual> returnSet = new TreeSet<>();
                for (Entry<OWLIndividual, SortedSet<Integer>> entry : mapping.entrySet()) {
                    //we can skip of largest number is below minimum or lowest number is above maximum
                    if (entry.getValue().last() < min || entry.getValue().first() > max) {
                        continue;
                    }

                    //search a value which is in the interval
                    for (Integer value : entry.getValue()) {
                        if (value >= min && value <= max) {
                            returnSet.add(entry.getKey());
                            break;
                        }
                    }
                }
                return returnSet;
            } else if (OWLAPIUtils.dtDatatypes.contains(datatype)) {
                // TODO we cannot ensure the sorting, because OWL API does only String comparison
                // on the lexical String value
                OWLLiteral min = null;
                OWLLiteral max = null;
                for (OWLFacetRestriction facet : facetRestrictions) {
                    if (facet.getFacet() == OWLFacet.MIN_INCLUSIVE) {
                        min = facet.getFacetValue();
                    } else if (facet.getFacet() == OWLFacet.MAX_INCLUSIVE) {
                        max = facet.getFacetValue();
                    }
                }
                Map<OWLIndividual, SortedSet<OWLLiteral>> mapping = dpPos.get(property);
                // we can return false if largest number is below minimum or lowest number is above maximum
                DateTimeFormatter parser = OWLAPIUtils.dateTimeParsers.get(datatype);
                DateTime minDateTime = null;
                if (min != null) {
                    minDateTime = parser.parseDateTime(min.getLiteral());
                }
                DateTime maxDateTime = null;
                if (max != null) {
                    maxDateTime = parser.parseDateTime(max.getLiteral());
                }
                SortedSet<OWLIndividual> returnSet = new TreeSet<>();
                for (Entry<OWLIndividual, SortedSet<OWLLiteral>> entry : mapping.entrySet()) {
                    //search a value which is in the interval
                    for (OWLLiteral value : entry.getValue()) {
                        if (OWLAPIUtils.inRange(value, min, max)) {
                            returnSet.add(entry.getKey());
                        }
                    }
                }
                return returnSet;
            }
        } else if (filler.getDataRangeType() == DataRangeType.DATA_ONE_OF) {
            OWLDataOneOf dataOneOf = (OWLDataOneOf) filler;
            Set<OWLLiteral> values = dataOneOf.getValues();

            Map<OWLIndividual, SortedSet<OWLLiteral>> mapping = dpPos.get(property);
            SortedSet<OWLIndividual> returnSet = new TreeSet<>();

            for (Entry<OWLIndividual, SortedSet<OWLLiteral>> entry : mapping.entrySet()) {
                OWLIndividual ind = entry.getKey();
                SortedSet<OWLLiteral> indValues = entry.getValue();

                if (!Sets.intersection(values, indValues).isEmpty()) {
                    returnSet.add(ind);
                }
            }
            return returnSet;
        }
    } else if (description instanceof OWLDataHasValue) {
        OWLDataPropertyExpression property = ((OWLDataHasValue) description).getProperty();
        OWLLiteral value = ((OWLDataHasValue) description).getFiller();

        SortedSet<OWLIndividual> returnSet = new TreeSet<>();

        Map<OWLIndividual, SortedSet<OWLLiteral>> mapping = dpPos.get(property);

        for (Entry<OWLIndividual, SortedSet<OWLLiteral>> entry : mapping.entrySet()) {
            if (entry.getValue().contains(value)) {
                returnSet.add(entry.getKey());
            }
        }

        return returnSet;
    } else if (description instanceof OWLObjectOneOf) {
        return new TreeSet(((OWLObjectOneOf) description).getIndividuals());
    }

    throw new ReasoningMethodUnsupportedException(
            "Retrieval for class expression " + description + " unsupported.");

}