Java Code Examples for org.semanticweb.owlapi.model.OWLObjectIntersectionOf#getOperands()

The following examples show how to use org.semanticweb.owlapi.model.OWLObjectIntersectionOf#getOperands() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CardinalityContraintsTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public HandlerResult visit(OWLObjectIntersectionOf intersectionOf) {
	Set<OWLClassExpression> newOperands = new HashSet<OWLClassExpression>();
	boolean changed = false;
	for (OWLClassExpression ce : intersectionOf.getOperands()) {
		HandlerResult handlerResult = ce.accept(this);
		if (handlerResult != null) {
			if (handlerResult.remove) {
				return HandlerResult.remove();
			}
			changed = true;
			newOperands.add(handlerResult.modified);
		}
		else {
			newOperands.add(ce);
		}
	}
	if (changed) {
		if (newOperands.size() == 1) {
			return HandlerResult.modified(newOperands.iterator().next());
		}
		return HandlerResult.modified(factory.getOWLObjectIntersectionOf(newOperands));
		
	}
	return null;
}
 
Example 2
Source File: AbstractSimPreProcessor.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String generateLabel(OWLClassExpression x) {
	StringBuffer sb = new StringBuffer();
	if (x instanceof OWLObjectSomeValuesFrom) {
		OWLObjectSomeValuesFrom svf = (OWLObjectSomeValuesFrom) x;	
		OWLObjectPropertyExpression p = svf.getProperty();
		if (propertyToFormatMap.containsKey(p)) {
			return String.format(propertyToFormatMap.get(p), generateLabel(svf.getFiller()));
		}
		else {
			String pStr = p.toString();
			if (p instanceof OWLEntity) {
				pStr = getAnyLabel((OWLEntity)p);
			}
			return pStr + " some "+generateLabel(svf.getFiller());
		}
	}
	else if (x instanceof OWLObjectIntersectionOf) {
		OWLObjectIntersectionOf oio = (OWLObjectIntersectionOf) x;
		for (OWLClassExpression op : oio.getOperands()) {
			if (sb.length() > 0) {
				sb.append(" and ");
			}
			sb.append(generateLabel(op));
		}
		return sb.toString();
	}
	else if (x instanceof OWLClass) {
		return this.getAnyLabel((OWLClass) x);
	}
	return x.toString();
}
 
Example 3
Source File: TBoxUnFoldingTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public OWLObjectIntersectionOf visit(OWLObjectIntersectionOf ce) {
	if (LOG.isDebugEnabled()) {
		LOG.debug("Unfolding intersection_of: "+ce);
	}
	
	Set<OWLClassExpression> operands = ce.getOperands();
	if (operands != null && !operands.isEmpty()) {
		Set<OWLClassExpression> unfolded = unfoldExpressions(operands);
		if (unfolded != null) {
			return factory.getOWLObjectIntersectionOf(unfolded);
		}
	}
	return null;
}
 
Example 4
Source File: FrameMakerLD.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ExpressionLD makeIntersectionOf(OWLObjectIntersectionOf x) {
	Set<ExpressionLD> ops = new HashSet<ExpressionLD>();
	for (OWLClassExpression op : x.getOperands()) {
		ops.add(makeExpression(op));
	}
	return new IntersectionOfLD(ops);
}
 
Example 5
Source File: CellularLocationTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static OWLClassExpression searchCellularLocation(OWLClassExpression cls, OWLGraphWrapper graph, Set<OWLObjectProperty> occurs_in) {
	Queue<OWLClass> queue = new Queue<OWLClass>();
	if (cls instanceof OWLClass) {
		queue.add((OWLClass) cls);
	}
	else if (cls instanceof OWLObjectIntersectionOf) {
		OWLObjectIntersectionOf intersectionOf = (OWLObjectIntersectionOf) cls;
		for (OWLClassExpression ce : intersectionOf.getOperands()) {
			if (ce instanceof OWLClass) {
				queue.add((OWLClass) ce);
			}
		}
	}
	return searchCellularLocation(queue, graph, occurs_in);
}
 
Example 6
Source File: GraphOwlVisitor.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public Void visit(OWLObjectIntersectionOf desc) {
  long subject =
      getOrCreateNode(getIri(desc), OwlLabels.OWL_INTERSECTION_OF, OwlLabels.OWL_ANONYMOUS);
  for (OWLClassExpression expression : desc.getOperands()) {
    long object = getOrCreateNode(getIri(expression));
    getOrCreateRelationship(subject, object, OwlRelationships.OPERAND);
  }
  return null;
}
 
Example 7
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * given a CE of the form A1 and A2 and ... An,
 * get a class with an IRI formed by concatenating parts of the IRIs of Ai,
 * and create a label assertion, where the label is formed by concatenating label(A1)....
 * 
 * note that the reasoner will need to be synchronized after new classes are made
 * 
 * @param x
 * @return class
 */
public OWLClass makeClass(OWLObjectIntersectionOf x) {
	StringBuffer id = new StringBuffer();
	StringBuffer label = new StringBuffer();
	int n = 0;
	int nlabels = 0;

	// TODO - optimize following
	for (OWLClassExpression op : x.getOperands()) {
		n++;
		// throw exception if ops are not named
		OWLClass opc = (OWLClass)op;
		String opid = opc.getIRI().toString();
		String oplabel = getLabel(opc);


		if (n == 1) {
			// make base
			String prefix = opid.toString();
			prefix = prefix.replaceAll("#.*","#");
			if (prefix.startsWith(OBO_PREFIX))
				id.append(OBO_PREFIX);
			else 
				id.append(prefix);				
		}

		if (n > 1) {
			label.append(" and ");
		}
		if (oplabel != null) {
			nlabels++;
			label.append(oplabel);
		}
		else {
			label.append("?"+opid);
		}


		opid = opid.replaceAll(".*#", "");
		opid = opid.replaceAll(".*\\/", "");
		if (n > 1) {
			id.append("-and-");
		}

		id.append(opid);
	}
	OWLClass c = owlDataFactory.getOWLClass(IRI.create(id.toString()));
	Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>();
	newAxioms.add( owlDataFactory.getOWLEquivalentClassesAxiom(c, x) );
	newAxioms.add( owlDataFactory.getOWLDeclarationAxiom(c));
	//LOG.info(" Generated label("+c+")="+label.toString());
	if (nlabels > 0) {
		newAxioms.add( owlDataFactory.getOWLAnnotationAssertionAxiom(owlDataFactory.getRDFSLabel(), c.getIRI(), 
				owlDataFactory.getOWLLiteral(label.toString())));
	}
	lcsExpressionToClass.put(x, c);
	LOG.info(" new LCS: "+c+" label: "+label.toString()+" == "+x);
	owlOntologyManager.addAxioms(sourceOntology, newAxioms);
	return c;
}
 
Example 8
Source File: LCSEnabledSimPreProcessor.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * given a CE of the form A1 and A2 and ... An,
 * get a class with an IRI formed by concatenating parts of the IRIs of Ai,
 * and create a label assertion, where the label is formed by concatenating label(A1)....
 * 
 * note that the reasoner will need to be synchronized after new classes are made
 * 
 * Note: modifies ontology but does not flush
 * 
 * see materializeClassExpression(..)
 * 
 * @param x
 * @return class
 */
public OWLClass makeClass(OWLObjectIntersectionOf x) {
	if (materializedClassExpressionMap.containsKey(x)) {
		return materializedClassExpressionMap.get(x);
	}
	//StringBuffer id = new StringBuffer();
	StringBuffer label = new StringBuffer();
	int n = 0;
	int nlabels = 0;
	
	LOG.info("LCS INTERSECTION: "+x);

	IRI intersectionIRI = null;
	// TODO - optimize following
	for (OWLClassExpression op : x.getOperands()) {
		n++;
		// throw exception if ops are not named
		OWLClass opc = (OWLClass)op;
		//String opid = opc.getIRI().toString();
		String oplabel = getAnyLabel(opc);

		if (n == 1) {
			intersectionIRI = opc.getIRI();
			// make base
			/*
			String prefix = opid.toString();
			prefix = prefix.replaceAll("#.*","#");
			if (prefix.startsWith(OBO_PREFIX))
				id.append(OBO_PREFIX);
			else 
				id.append(prefix);
				*/				
		}
		else {
			intersectionIRI = makeViewClassIRI(intersectionIRI, opc.getIRI());
		}
		
		if (n > 1) {
			label.append(" and ");
		}
		if (oplabel != null) {
			nlabels++;
			label.append("["+oplabel+"]");
		}
		else {
			label.append("?"+opc.getIRI().toString());
		}

	}
	OWLClass c = getOWLDataFactory().getOWLClass(intersectionIRI);
	Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>();
	newAxioms.add( getOWLDataFactory().getOWLEquivalentClassesAxiom(c, x) );
	newAxioms.add( getOWLDataFactory().getOWLDeclarationAxiom(c));
	//LOG.info(" Generated label("+c+")="+label.toString());
	if (nlabels > 0) {
		newAxioms.add( getOWLDataFactory().getOWLAnnotationAssertionAxiom(getOWLDataFactory().getRDFSLabel(), c.getIRI(), 
				getOWLDataFactory().getOWLLiteral(label.toString())));
	}
	//TODO
	//lcsExpressionToClass.put(x, c);
	LOG.info(" new LCS: "+c+" label: "+label.toString()+" == "+x);
	this.addAxiomsToOutput(newAxioms, false);
	materializedClassExpressionMap.put(x, c);
	return c;
}
 
Example 9
Source File: SpeciesMergeUtil.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 
 * builds ecmap, a map between species-classes to generic-classes
 * also populates ssClasses
 * 
 */
public void createMap() {

	// create a root class for a species - e.g. "Drosophila part" -
	// then make it equivalent to an expression like "part_of some Drosophila"
	rootSpeciesSpecificClass = fac.getOWLClass(IRI.create(taxClass.getIRI()
			.toString() + "-part"));
	OWLClassExpression rx = fac.getOWLObjectSomeValuesFrom(viewProperty,
			taxClass);
	OWLEquivalentClassesAxiom qax = fac.getOWLEquivalentClassesAxiom(
			rootSpeciesSpecificClass, rx);
	mgr.addAxiom(ont, qax);
	reasoner.flush();
	LOG.info("Getting species classes via: " + rootSpeciesSpecificClass
			+ " == " + rx);

	ssClasses = reasoner.getSubClasses(rootSpeciesSpecificClass, false)
			.getFlattened();
	LOG.info("num ss Classes = " + ssClasses.size());
	mgr.removeAxiom(ont, qax);
	mgr.removeAxiom(ont,
			fac.getOWLDeclarationAxiom(rootSpeciesSpecificClass));
	// reasoner.flush();

	/*
	 * PropertyViewOntologyBuilder pvob = new
	 * PropertyViewOntologyBuilder(ont, ont, viewProperty);
	 * pvob.buildInferredViewOntology(reasoner); for (OWLEntity ve :
	 * pvob.getViewEntities()) { OWLClass vc = (OWLClass)ve; OWLClass c =
	 * pvob.getOriginalClassForViewClass(vc);
	 * reasoner.getEquivalentClasses(vc); ecmap.put(vc, c);
	 * 
	 * eqmap.put(vc, x); }
	 */
	for (OWLEquivalentClassesAxiom eca : ont.getAxioms(
			AxiomType.EQUIVALENT_CLASSES, Imports.INCLUDED)) {
		//LOG.info("TESTING ECA: " + eca);

		// Looking for: FBbt:nnn = Ubr:nnn and part_of some NCBITaxon:7997
		if (eca.getClassesInSignature().contains(taxClass)
				&& eca.getObjectPropertiesInSignature().contains(
						viewProperty)) {
			LOG.info("  CONTAINS_TAX_AND_PROP: " + eca);
			for (OWLClass c : eca.getClassesInSignature()) {
				if (!ssClasses.contains(c))
					continue;
				LOG.info("   ssC = " + c);
				for (OWLClassExpression x : eca.getClassExpressionsMinus(c)) {
					if (x instanceof OWLObjectIntersectionOf) {

						OWLObjectIntersectionOf oio = (OWLObjectIntersectionOf) x;
						for (OWLClassExpression ux : oio.getOperands()) {
							if (ux instanceof OWLClass) {
								exmap.put(c, x); // e.g. fbbt:nn = ubr:nn
								// and part_of dmel
								ecmap.put(c, (OWLClass) ux);
								LOG.info("MAP: " + c + " --> " + ux
										+ " // " + x);
							}
						}
					}
				}
			}

		}

	}
}
 
Example 10
Source File: LinkMaker.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Search for the first class with a matching equivalent class definition. 
 * 
 * @param c
 * @param genus
 * @param relation
 * @return match or null
 */
private OWLClass hasMatchingIntersection(OWLClass c, OWLClass genus, OWLObjectProperty relation) {
	for(OWLOntology o : allOntologies) {
		Set<OWLEquivalentClassesAxiom> eqAxioms = o.getEquivalentClassesAxioms(c);
		for (OWLEquivalentClassesAxiom eqAxiom : eqAxioms) {
			Set<OWLClassExpression> expressions = eqAxiom.getClassExpressionsMinus(c);
			for (OWLClassExpression expression : expressions) {
				if (expression instanceof OWLObjectIntersectionOf) {
					OWLObjectIntersectionOf intersection = (OWLObjectIntersectionOf) expression;
					OWLClass differentiaCls = null;
					boolean matchesGenus = false;
					boolean matchesRelation = false;
					Set<OWLClassExpression> operands = intersection.getOperands();
					if (operands.size() == 2) {
						for (OWLClassExpression operand : operands) {
							if (operand.isAnonymous() == false) {
								OWLClass currentGenus = operand.asOWLClass();
								if (genus.equals(currentGenus)) {
									matchesGenus = true;
								}
							}
							else if (operand instanceof OWLObjectSomeValuesFrom) {
								OWLObjectSomeValuesFrom differentia = (OWLObjectSomeValuesFrom) operand;
								if (relation.equals(differentia.getProperty())) {
									matchesRelation = true;
									OWLClassExpression filler = differentia.getFiller();
									if (!filler.isAnonymous() && !filler.isOWLNothing() && !filler.isOWLThing()) {
										differentiaCls = filler.asOWLClass();
									}
								}
							}
						}
						if (matchesGenus && matchesRelation ) {
							 return differentiaCls;
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example 11
Source File: OWLGraphManipulator.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Relaxes all {@code OWLObjectIntersectionOf}s. This method will relax 
 * {@code OWLSubClassOfAxiom}s, whose superclass is an {@code OWLObjectIntersectionOf}, 
 * into multiple {@code OWLSubClassOfAxiom}s, using a <a 
 * href='http://owlapi.sourceforge.net/javadoc/org/semanticweb/owlapi/SplitSubClassAxioms.html'>
 * SplitSubClassAxioms</a>. It will also relax {@code OWLSubClassOfAxiom}s, whose 
 * superclass is an {@code OWLObjectSomeValuesFrom} with a filler being an 
 * {@code OWLObjectIntersectionOf}, into multiple {@code OWLSubClassOfAxiom}s with 
 * an {@code OWLObjectSomeValuesFrom} as superclass, with the same 
 * {@code OWLPropertyExpression}, and individual operands as filler. 
 * <p>
 * Note that it is likely that the {@code OWLObjectIntersectionOf}s where used in
 * {@code OWLEquivalentClassesAxiom}s, rather than in {@code OWLSubClassOfAxiom}s. 
 * But the method {@link #convertEquivalentClassesToSuperClasses()} would have transformed 
 * them into {@code OWLSubClassOfAxiom}s. It must be called before this method.
 * 
 * @see #performDefaultModifications()
 * @see #convertEquivalentClassesToSuperClasses()
 */
private void splitSubClassAxioms() {
    log.info("Relaxing OWLSubClassOfAxioms whose superclass is an OWLObjectIntersectionOf");
    
    //first, split subClassOf axioms whose superclass is an OWLObjectIntersectionOf
    SplitSubClassAxioms split = new SplitSubClassAxioms(
            this.getOwlGraphWrapper().getAllOntologies(), 
            this.getOwlGraphWrapper().getDataFactory());
    this.getOwlGraphWrapper().getManager().applyChanges(split.getChanges());
    this.triggerWrapperUpdate();
    
    //some ontologies use an OWLObjectIntersectionOf as the filler of 
    //an OWLObjectSomeValuesFrom class expression. We go only one level down 
    //(i.e., we would not translate another OWLObjectSomeValuesFrom part of the 
    //OWLObjectIntersectionOf)
    OWLDataFactory dataFactory = this.getOwlGraphWrapper().getDataFactory();
    List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
    for (OWLOntology ont : this.getOwlGraphWrapper().getAllOntologies()) {
        for (OWLSubClassOfAxiom ax : ont.getAxioms(AxiomType.SUBCLASS_OF)) {
            OWLClassExpression superClsExpr = ax.getSuperClass();
            if (superClsExpr instanceof OWLObjectSomeValuesFrom) {
                OWLObjectSomeValuesFrom someValuesFrom = 
                        (OWLObjectSomeValuesFrom) superClsExpr;
                if (someValuesFrom.getFiller() instanceof OWLObjectIntersectionOf) {
                    //remove original axiom
                    changes.add(new RemoveAxiom(ont, ax));
                    
                    OWLObjectIntersectionOf filler = 
                            (OWLObjectIntersectionOf) someValuesFrom.getFiller();
                    for (OWLClassExpression op : filler.getOperands()) {
                        //we accept only OWLClasses, otherwise we would need to compose 
                        //OWLObjectPropertyExpressions
                        if (op instanceof OWLClass) {
                            OWLAxiom replAx = dataFactory.
                                    getOWLSubClassOfAxiom(ax.getSubClass(), 
                                    dataFactory.getOWLObjectSomeValuesFrom(
                                                someValuesFrom.getProperty(), op));
                            changes.add(new AddAxiom(ont, replAx));
                        }
                    }
                }
                
            }
        }
    }
    this.getOwlGraphWrapper().getManager().applyChanges(changes);
    this.triggerWrapperUpdate();
    
    log.info("OWLObjectIntersectionOf relaxation done.");
}
 
Example 12
Source File: LegoShuntGraphTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private String getLabel(OWLClassExpression expression, OWLPrettyPrinter owlpp, OWLGraphWrapper graph) {
	if (expression instanceof OWLClass) {
		return graph.getLabelOrDisplayId(expression);
	}
	else if (expression instanceof OWLObjectIntersectionOf) {
		StringBuilder sb = new StringBuilder();
		OWLObjectIntersectionOf intersectionOf = (OWLObjectIntersectionOf) expression;
		sb.append("<TABLE>");
		for (OWLClassExpression ce : intersectionOf.getOperands()) {
			sb.append("<TR><TD>");
			if (ce instanceof OWLClass) {
				sb.append(graph.getLabelOrDisplayId((OWLClass)ce));
			}
			else if (ce instanceof OWLObjectSomeValuesFrom){
				OWLObjectSomeValuesFrom some = (OWLObjectSomeValuesFrom) ce;
				OWLObjectPropertyExpression property = some.getProperty();
				if (property.isAnonymous()) {
					sb.append(owlpp.render(property));
				}
				else {
					sb.append(graph.getLabelOrDisplayId(property.asOWLObjectProperty()));
				}
				sb.append(" <B>some</B> ");
				OWLClassExpression filler = some.getFiller();
				if (filler instanceof OWLClass) {
					sb.append(graph.getLabelOrDisplayId((OWLClass)filler));
				}
				else {
					sb.append(owlpp.render(filler));
				}
			}
			else {
				sb.append(ce.toString());
			}
			sb.append("</TD></TR>");
		}
		sb.append("</TABLE>");
		return sb.toString();
	}
	return owlpp.render(expression);
}
 
Example 13
Source File: LegoDotWriter.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private CharSequence getLabel(OWLClassExpression expression, OWLPrettyPrinter owlpp) {
	if (expression instanceof OWLClass) {
		return insertLineBrakes(graph.getLabelOrDisplayId(expression));
	}
	else if (expression instanceof OWLObjectIntersectionOf) {
		StringBuilder sb = new StringBuilder();
		OWLObjectIntersectionOf intersectionOf = (OWLObjectIntersectionOf) expression;
		sb.append("<TABLE>");
		for (OWLClassExpression ce : intersectionOf.getOperands()) {
			sb.append("<TR><TD>");
			if (ce instanceof OWLClass) {
				sb.append(insertLineBrakes(graph.getLabelOrDisplayId((OWLClass)ce)));
			}
			else if (ce instanceof OWLObjectSomeValuesFrom){
				OWLObjectSomeValuesFrom some = (OWLObjectSomeValuesFrom) ce;
				OWLObjectPropertyExpression property = some.getProperty();
				if (property.isAnonymous()) {
					sb.append(owlpp.render(property));
				}
				else {
					sb.append(insertLineBrakes(graph.getLabelOrDisplayId(property.asOWLObjectProperty())));
				}
				sb.append(" <B>some</B> ");
				OWLClassExpression filler = some.getFiller();
				if (filler instanceof OWLClass) {
					sb.append(insertLineBrakes(graph.getLabelOrDisplayId((OWLClass)filler)));
				}
				else {
					sb.append(insertLineBrakes(escape(owlpp.render(filler))));
				}
			}
			else {
				sb.append(insertLineBrakes(escape(ce.toString())));
			}
			sb.append("</TD></TR>");
		}
		sb.append("</TABLE>");
		return sb.toString();
	}
	return insertLineBrakes(escape(owlpp.render(expression)));
}