private void addEntailedKBSuccessors(OWLObjectSomeValuesFrom some) {
    TRACKER.start(StaticValues.TIME_KB_SUCCESSORS, BlockOutputMode.COMPLETE, true);
    // add all role-successors entailed by the TBox
    addEntailedTBoxSuccessors(some, m_normalize); // for KB mode only normalize TBox contained roles

    DomainNode<?> toNode = getDomainElement(some.getFiller());

    OWLClass someClass = m_ontologyOperator.getFlatteningTransformer().getIntermediary(some);

    long start = System.currentTimeMillis();
    //		StatStore.getInstance().enterValue("instance accessing", 1.0);
    NodeSet<OWLNamedIndividual> instances =
        m_ontologyOperator.getReasoner().getInstances(someClass, false);
    sum += System.currentTimeMillis() - start;
    //		NodeSet<OWLNamedIndividual> instances =
    // m_ontologyOperator.getReasoner().getInstances(getClassRepresentation(some), false);
    //		LOG.fine(" creating successors to " + some + " from instances: " + instances.toString());
    Iterator<Node<OWLNamedIndividual>> it1 = instances.iterator();
    while (it1.hasNext()) {
      Iterator<OWLNamedIndividual> it2 = it1.next().iterator();
      while (it2.hasNext()) {
        DomainNode<?> from = m_domain.getDomainNode(it2.next());
        if (from != null) {
          //					if(!isSuccessorRepresented(from, getDomainElement(some.getFiller()),
          // (OWLObjectProperty)some.getProperty())){
          from.addSuccessor((OWLObjectProperty) some.getProperty(), toNode);
          //					}
        }
      }
    }
    TRACKER.stop(StaticValues.TIME_KB_SUCCESSORS);
  }
 protected OWLClass getFreshQueryClass(String base) {
   long cnt = 0;
   String iriString = base;
   while (m_ontologyOperator.getOntology().containsClassInSignature(IRI.create(iriString))) {
     iriString =
         base + (cnt++); // not guaranteed to succeed.. however |long| amount of possibilities
   }
   return OWLManager.getOWLDataFactory().getOWLClass(IRI.create(iriString));
 }
  public OWLClass getClassRepresentation(OWLClassExpression ex) {
    if (ex != null
        && ex.equals(
            m_referenceExpression)) // before OWLClass check, in case referenceExpression is also a
      // class
      return m_referenceClass;

    if (ex instanceof OWLClass) {
      return (OWLClass) ex;
    }

    return m_ontologyOperator.getFlatteningTransformer().getIntermediary(ex);
  }
  protected void insertQueryAxiom(OWLClass queryClass) {
    //		Main.getOntologyManager().addAxiom(m_ontologyOperator.getOntology(),
    //				OWLManager.getOWLDataFactory().getOWLEquivalentClassesAxiom(queryClass,
    // m_referenceExpression));

    m_ontologyOperator
        .getOntology()
        .getOWLOntologyManager()
        .addAxiom(
            m_ontologyOperator.getOntology(),
            //				OWLManager.getOWLDataFactory().getOWLEquivalentClassesAxiom(
            OWLManager.getOWLDataFactory()
                .getOWLSubClassOfAxiom(
                    queryClass,
                    m_referenceExpression.accept(
                        m_ontologyOperator.getFlatteningTransformer().getVisitor())));
    m_ontologyOperator
        .getOntology()
        .getOWLOntologyManager()
        .applyChanges(m_ontologyOperator.getFlatteningTransformer().getVisitor().getChanges());
    m_ontologyOperator.getFlatteningTransformer().getVisitor().resetChangeList();
    m_ontologyOperator.ontologyChanged();
  }
  @Override
  public CanonicalInterpretation generate(OWLOntology ontology) {
    CanonicalInterpretation canonInterpretation = new CanonicalInterpretation();
    m_domain = new CanonicalDomain();
    canonInterpretation.initDomain(m_domain);

    m_ontologyOperator = OntologyOperator.getOntologyOperator(ontology);
    IDomainElementGenerator elemGen;
    if (isKBMode()) {
      elemGen = new KBDomainElementGenerator();
    } else {
      elemGen = new TBoxDomainElementGenerator();
    }

    if (!isKBMode()) { // TBox + Query mode
      // define class Q as equivalence to the reference expression (query)
      m_referenceClass = getFreshQueryClass("Q");
      insertQueryAxiom(m_referenceClass);
      ((TBoxDomainElementGenerator) elemGen).registerConcept(m_referenceClass);
    }

    OWLAxiomFlatteningTransformer exRestStore =
        m_ontologyOperator.getFlatteningTransformer(); // flattens here
    m_ontologyOperator.getReasoner(isKBMode()); // precomputes inferences

    // the element generator creates all necessary domain elements and adds their instantiators
    elemGen.generate(this, m_keepSmall);
    LOG.info("a total of " + m_classAssociations.size() + " classes are mapped to individuals");

    TRACKER.start(StaticValues.TIME_DOMAIN_SUCCESSORS);
    if (!isKBMode()) { // TBox + Query mode

      // add all successor relations by iterating all known existential restrictions
      for (OWLObjectSomeValuesFrom some : exRestStore.getRestrictions()) {
        addEntailedTBoxSuccessors(some, m_normalize);
      }
    } else { // KB mode (ABox + TBox)
      // add all ABox property assertion successors
      for (OWLAxiom ax : m_ontologyOperator.getOntology().getABoxAxioms(true)) {
        if (ax instanceof OWLObjectPropertyAssertionAxiom) {
          //					LOG.fine("Adding role assertion successor: " + ax);
          OWLObjectPropertyAssertionAxiom pAx = (OWLObjectPropertyAssertionAxiom) ax;
          getDomainElement(pAx.getSubject())
              .addSuccessor(
                  (OWLObjectProperty) pAx.getProperty(), getDomainElement(pAx.getObject()));
        }
      }

      // add all successor relations by iterating all known existential restrictions
      int exR = 1;
      for (OWLObjectSomeValuesFrom some :
          m_ontologyOperator.getFlatteningTransformer().getRestrictions()) {
        if (exR % 1000 == 0) {
          System.out.println(exR + " restrictions handled");
        }
        addEntailedKBSuccessors(some);
        exR++;
      }

      LOG.info("TOTAL TIME FOR INSTANCE RETRIEVAL: " + sum + " ms");

      // normalize later
      // if(m_normalize){ startSimulationComputation(); }

    }
    TRACKER.stop(StaticValues.TIME_DOMAIN_SUCCESSORS);

    /* ************* TEST SPECIFIC STUFF ************** */
    //		LOG.info("Start checking for useless domain nodes ...");
    //		Map<OWLClassExpression, DomainNode<OWLClassExpression>> conceptDomainNodes =
    // canonInterpretation.getDomain().getConceptElements();
    //		Set<DomainNode<OWLClassExpression>> noPredecessors = new
    // HashSet<DomainNode<OWLClassExpression>>();
    //		noPredecessors.addAll(conceptDomainNodes.values());
    //		for(OWLClassExpression ce : conceptDomainNodes.keySet()){
    //			for(OWLObjectProperty r : conceptDomainNodes.get(ce).getSuccessorRoles()){
    //				noPredecessors.removeAll(conceptDomainNodes.get(ce).getSuccessors(r));
    //			}
    //
    //			NodeSet<OWLClass> subClasses =
    // m_ontologyOperator.getReasoner().getSubClasses(ce.asOWLClass(), false);
    //			Node<OWLClass> eqClasses =
    // m_ontologyOperator.getReasoner().getEquivalentClasses(ce.asOWLClass());
    //			for(OWLClassExpression ce2 : conceptDomainNodes.keySet()){
    //				if(!ce.equals(ce2)){
    //					if(subClasses.containsEntity(ce2.asOWLClass())){
    //						LOG.info("Domain node " + conceptDomainNodes.get(ce2) + " represents something more
    // specific than " + conceptDomainNodes.get(ce));
    //					}else if(eqClasses.contains(ce2.asOWLClass())){
    //						LOG.info("Domain node " + conceptDomainNodes.get(ce2) + " is equivalent to " +
    // conceptDomainNodes.get(ce));
    //					}
    //				}
    //			}
    //		}
    //		for(DomainNode<OWLClassExpression> d : noPredecessors){
    //			LOG.info(d + " does not have any predecessors.");
    //			if(d.getSuccessorObjects().isEmpty())
    //				LOG.info(d + " is not connected to the model at all!");
    //		}
    /* ************ END TEST ***************** */

    return canonInterpretation;
  }
 public boolean isRestrictedInstantiator(OWLClass c) {
   return c == null
       || c.isTopEntity()
       || m_ontologyOperator.getFlatteningTransformer().isIntermediary(c)
       || c.equals(m_referenceClass);
 }
  private void addEntailedTBoxSuccessors(OWLObjectSomeValuesFrom some, boolean doNormalizing) {
    //		StatStore.getInstance().enterValue("lookup " + some.getFiller(), 1.0);
    //		StatStore.getInstance().enterValue("restrictions handled", 1.0);
    TRACKER.start(StaticValues.TIME_TBOX_SUCCESSORS, BlockOutputMode.COMPLETE, true);
    //		TRACKER.start("fetch domain element and intermediary", BlockOutputMode.COMPLETE, true);
    DomainNode<?> toNode = getDomainElement(some.getFiller());
    // the super class, intermediary stands for (some r B)
    OWLClass superClass = m_ontologyOperator.getFlatteningTransformer().getIntermediary(some);
    //		TRACKER.stop("fetch domain element and intermediary");
    // add successors from all
    //		TRACKER.start("query elk", BlockOutputMode.COMPLETE, true);
    //		StatStore.getInstance().enterValue("subclass accessing", 1.0);
    NodeSet<OWLClass> classes = m_ontologyOperator.getReasoner().getSubClasses(superClass, false);

    // fill current class relations from Id
    //		StatStore.getInstance().enterValue("subclass accessing", 1.0);
    currentIdSubClasses = m_ontologyOperator.getReasoner().getSubClasses(some.getFiller(), false);
    //		StatStore.getInstance().enterValue("superclass accessing", 1.0);
    currentIdSuperClasses =
        m_ontologyOperator.getReasoner().getSuperClasses(some.getFiller(), false);
    //		StatStore.getInstance().enterValue("equivalent classes accessing", 1.0);
    currentIdEqClasses = m_ontologyOperator.getReasoner().getEquivalentClasses(some.getFiller());

    //		TRACKER.stop("query elk");
    //		LOG.fine(" creating successors to " + some + ": " + classes.toString());
    Iterator<Node<OWLClass>> nodeIt = classes.iterator();
    while (nodeIt.hasNext()) {
      Iterator<OWLClass> it = nodeIt.next().iterator();
      while (it.hasNext()) {
        DomainNode<?> node = m_domain.getDomainNode(it.next());
        // if it.next() yields no domain node, there exists an ABox identity for it
        // and this case is covered by addEntailedKBSuccessors
        if (node != null) {
          if (doNormalizing) {
            //						if(!isSuccessorRepresented(node, (OWLObjectProperty)some.getProperty(),
            // some.getFiller())){
            TRACKER.start("normalizing and adding", BlockOutputMode.COMPLETE, true);
            if (!isSuccessorRepresented(node, toNode, (OWLObjectProperty) some.getProperty())) {
              removeIncludedSuccessors(
                  (OWLObjectProperty) some.getProperty(), node, some.getFiller());

              node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode);
            }
            TRACKER.stop("normalizing and adding");
          } else {
            node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode);
          }
        }
      }
    }
    // add all equivalent class successors
    //		TRACKER.start("query elk", BlockOutputMode.COMPLETE, true);
    Iterator<OWLClass> cIt =
        m_ontologyOperator.getReasoner().getEquivalentClasses(superClass).iterator();
    //		TRACKER.stop("query elk");
    while (cIt.hasNext()) {
      DomainNode<?> node = m_domain.getDomainNode(cIt.next());
      if (node != null) {
        if (doNormalizing) {
          //					if(!isSuccessorRepresented(node, (OWLObjectProperty)some.getProperty(),
          // some.getFiller())){
          TRACKER.start("normalizing and adding", BlockOutputMode.COMPLETE, true);
          if (!isSuccessorRepresented(node, toNode, (OWLObjectProperty) some.getProperty())) {
            removeIncludedSuccessors(
                (OWLObjectProperty) some.getProperty(), node, some.getFiller());

            node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode);
          }
          TRACKER.stop("normalizing and adding");
        } else {
          node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode);
        }
      }
    }
    TRACKER.stop(StaticValues.TIME_TBOX_SUCCESSORS);
  }