org.semanticweb.owlapi.model.OWLObjectIntersectionOf Java Examples

The following examples show how to use org.semanticweb.owlapi.model.OWLObjectIntersectionOf. 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: DanglingReferenceCheck.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void handleIntersection(List<CheckWarning> warnings, Set<OWLOntology> allOntologies,
		OWLEquivalentClassesAxiom axiom, OWLObjectIntersectionOf intersection, OWLPrettyPrinter pp) 
{
	for(OWLClassExpression operand : intersection.getOperandsAsList()) {
		OWLClass operandCls = null;
		if (!operand.isAnonymous()) {
			operandCls = operand.asOWLClass();
		}
		else if (operand instanceof OWLObjectSomeValuesFrom) {
			OWLObjectSomeValuesFrom ristriction = (OWLObjectSomeValuesFrom) operand;
			OWLClassExpression filler = ristriction.getFiller();
			if (!filler.isAnonymous()) {
				operandCls = filler.asOWLClass();
			}
		}
		else {
			// not translatable to OBO
			handleGeneric(warnings, allOntologies, axiom, operand, pp);
		}
		if (operandCls != null && isDangling(operandCls, allOntologies)) {
			final IRI iri = operandCls.getIRI();
			String message = "Dangling reference "+iri+" in INTERSECTION_OF axiom: "+pp.render(axiom);
			warnings.add(new CheckWarning(getID(), message , isFatal(), iri, OboFormatTag.TAG_INTERSECTION_OF.getTag()));
		}
	}
}
 
Example #2
Source File: CompositionalClassPredictor.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void buildSimpleDefMap() {
	simpleDefMap = new HashMap<OWLClass,Set<OWLClassExpression>>();
	OWLOntology o = getGraph().getSourceOntology();
	for (OWLClass c : o.getClassesInSignature()) {
		for (OWLEquivalentClassesAxiom eca : o.getEquivalentClassesAxioms(c)) {
			Set<OWLClassExpression> elts = new HashSet<OWLClassExpression>();
			for (OWLClassExpression x : eca.getClassExpressions()) {
				// assume one logical definitionper class - otherwise choose arbitrary
				if (x instanceof OWLObjectIntersectionOf) {
					if (getReachableOWLClasses(x, elts) && elts.size() > 0) {
						//LOG.info(c+" def= "+elts);
						simpleDefMap.put(c, elts);						
					}
				}
			}
		}
	}
}
 
Example #3
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private OWLClass expressionToClass(OWLClassExpression x) {
	if (x instanceof OWLClass)
		return (OWLClass)x;
	if (this.expressionToClassMap.containsKey(x))
		return this.expressionToClassMap.get(x);
	OWLClass c = owlDataFactory.getOWLClass(IRI.create("http://owlsim.org#"+idNum));
	idNum++;

	OWLEquivalentClassesAxiom eca =
		owlDataFactory.getOWLEquivalentClassesAxiom(c, x);
	owlOntologyManager.addAxiom(sourceOntology, eca);
	expressionToClassMap.put(x, c);

	// fully fold tbox (AND and SOME only)
	if (x instanceof OWLObjectIntersectionOf) {
		for (OWLClassExpression y : ((OWLObjectIntersectionOf)x).getOperands()) {
			expressionToClass(y);
		}
	}
	else if (x instanceof OWLObjectSomeValuesFrom) {
		expressionToClass(((OWLObjectSomeValuesFrom)x).getFiller());
	}
	return c;
}
 
Example #4
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 #5
Source File: FrameMaker.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Expression makeExpression(OWLClassExpression x) {
	if (x.isAnonymous()) {
		if (x instanceof OWLObjectIntersectionOf) {
			return makeIntersectionOf((OWLObjectIntersectionOf)x);
		}
		else if (x instanceof OWLObjectSomeValuesFrom) {
			return makeSomeValuesFrom((OWLObjectSomeValuesFrom)x);
		}
		else {
			return null;
		}
	}
	else {
		return makeClassStub(x);
	}
}
 
Example #6
Source File: AutomaticSimPreProcessor.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void gatherProperties(OWLClassExpression x) {
	if (x instanceof OWLClass) {
		return;
	}
	else if (x instanceof OWLObjectIntersectionOf) {
		for (OWLClassExpression y : ((OWLObjectIntersectionOf)x).getOperands()) {
			gatherProperties(y);
		}		
	}
	else if (x instanceof OWLObjectSomeValuesFrom) {
		OWLObjectSomeValuesFrom svf = (OWLObjectSomeValuesFrom)x;
		gatherProperties(svf.getProperty());
		gatherProperties(svf.getFiller());
	}
	else {
		//
	}
}
 
Example #7
Source File: OwlApiUtilsTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void getIris() throws URISyntaxException {
  OWLClass clazz = factory.getOWLClass(IRI.create("http://example.org/Thing"));
  assertThat(OwlApiUtils.getIri((OWLClassExpression)clazz), is("http://example.org/Thing"));
  OWLObjectIntersectionOf expression = factory.getOWLObjectIntersectionOf(clazz);
  assertThat(OwlApiUtils.getIri(expression), is("_:" + OwlApiUtils.hash(expression.toString())));
}
 
Example #8
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 #9
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 #10
Source File: LegoTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private OWLClassExpression getType(OWLNamedIndividual individual) {
	NodeSet<OWLClass> types = reasoner.getTypes(individual, true);
	if (types.isEmpty() || types.isBottomSingleton() || types.isTopSingleton()) {
		return null;
	}
	Set<OWLClass> set = types.getFlattened();
	
	if (set.size() == 1) {
		return set.iterator().next();
	}
	OWLDataFactory fac = graph.getManager().getOWLDataFactory();
	OWLObjectIntersectionOf intersectionOf = fac.getOWLObjectIntersectionOf(set);
	return intersectionOf;
	
}
 
Example #11
Source File: Ancestors2Test.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testIntersectionsReturnedInClosure() throws Exception {
	OWLGraphWrapper  g =  getOntologyWrapper();
	g.config.isGraphReasonedAndRelaxed = false;
	OWLObject obj = g.getOWLObject("http://example.org#o1");
	boolean ok = false;
	for (OWLGraphEdge e : g.getOutgoingEdgesClosureReflexive(obj)) {
		if (RENDER_ONTOLOGY_FLAG) {
			System.out.println(e);
		}
		if (e.getTarget() instanceof OWLObjectIntersectionOf)
			ok = true;
	}
	assertTrue(ok);
}
 
Example #12
Source File: TemplatedTransformer.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private OWLClassExpression replaceVariables(OWLClassExpression inx,
		BindingSet bset) {
	if (inx instanceof OWLNamedObject) {
		IRI y = replaceIRI(((OWLNamedObject)inx).getIRI(), bset);
		if (inx instanceof OWLClass) {
			return getOWLDataFactory().getOWLClass(y);
		}
	}
	else if (inx instanceof OWLObjectSomeValuesFrom) {
		OWLObjectSomeValuesFrom svf = (OWLObjectSomeValuesFrom)inx;
		return getOWLDataFactory().getOWLObjectSomeValuesFrom(
				replaceVariables(svf.getProperty(),bset),
				replaceVariables(svf.getFiller(),bset));

	}
	else if (inx instanceof OWLObjectIntersectionOf) {
		Set<OWLClassExpression> es = new HashSet<OWLClassExpression>();
		for (OWLClassExpression e : ((OWLObjectIntersectionOf)inx).getOperands()) {
			es.add(replaceVariables(e, bset));
		}
		return getOWLDataFactory().getOWLObjectIntersectionOf(es);
	}
	else {
		
	}
	return null;
}
 
Example #13
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 #14
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 #15
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 #16
Source File: SimEngine.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public OWLClassExpression edgeSetToExpression(Set<OWLGraphEdge> edges) {
	Set<OWLClassExpression> xs = new HashSet<OWLClassExpression>();
	for (OWLGraphEdge e : edges) {
		OWLObject x = graph.edgeToTargetExpression(e);
		xs.add((OWLClassExpression)x);
	}
	if (xs.size() == 1)
		return xs.iterator().next();
	OWLObjectIntersectionOf ix = graph.getDataFactory().getOWLObjectIntersectionOf(xs);
	return ix;
}
 
Example #17
Source File: CompositionalClassPredictor.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean getReachableOWLClasses(OWLClassExpression c, Set<OWLClassExpression> elts) {
	if (c instanceof OWLObjectIntersectionOf) {
		for (OWLClassExpression x : ((OWLObjectIntersectionOf)c).getOperands()) {
			if (x instanceof OWLClass) {
				elts.add((OWLClass) x);
			}
			else if (x instanceof OWLObjectSomeValuesFrom) {
				OWLObjectPropertyExpression p = ((OWLObjectSomeValuesFrom)x).getProperty();
				String pLabel = getGraph().getLabel(p);
				if (pLabel != null && pLabel.contains("regulates")) {
					// fairly hacky:
					//  fail on this for now - no inference for regulates
					//elts.add(x);
					return false;
				}
				else {
					OWLClassExpression filler = ((OWLObjectSomeValuesFrom)x).getFiller();
					if (!getReachableOWLClasses( filler, elts)) {
						return false;
					}
				}
			}
			else {
				return false;
			}

		}
		return true;
	}
	else if (c instanceof OWLClass) {
		elts.add((OWLClass) c);
		return true;
	}
	return false;
}
 
Example #18
Source File: AbstractElkObjectConverter.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
@Override
public OWLObjectIntersectionOf visit(ElkObjectIntersectionOf expression) {
	return owlFactory_.getOWLObjectIntersectionOf(
			toClassExpressionSet(expression.getClassExpressions()));
}
 
Example #19
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 #20
Source File: OwlClassExpressionConverterVisitor.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
@Override
public ElkObjectIntersectionOf visit(
		OWLObjectIntersectionOf owlObjectIntersectionOf) {
	return CONVERTER.convert(owlObjectIntersectionOf);
}
 
Example #21
Source File: OwlConverter.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("static-method")
public ElkObjectIntersectionOf convert(
		OWLObjectIntersectionOf owlObjectIntersectionOf) {
	return new ElkObjectIntersectionOfWrap<OWLObjectIntersectionOf>(
			owlObjectIntersectionOf);
}
 
Example #22
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)));
}
 
Example #23
Source File: ManchesterOWLSyntaxObjectHTMLRenderer.java    From robot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Given an OWLClassExpression, determine the particular type of OWLClassExpression that it is,
 * and then call the appropriate visit() function for it.
 */
public void visit(OWLClassExpression ce) throws ClassNotFoundException {
  if (ce instanceof OWLClass) {
    visit((OWLClass) ce);
  } else if (ce instanceof OWLObjectSomeValuesFrom) {
    visit((OWLObjectSomeValuesFrom) ce);
  } else if (ce instanceof OWLObjectAllValuesFrom) {
    visit((OWLObjectAllValuesFrom) ce);
  } else if (ce instanceof OWLObjectMinCardinality) {
    visit((OWLObjectMinCardinality) ce);
  } else if (ce instanceof OWLObjectMaxCardinality) {
    visit((OWLObjectMaxCardinality) ce);
  } else if (ce instanceof OWLObjectExactCardinality) {
    visit((OWLObjectExactCardinality) ce);
  } else if (ce instanceof OWLObjectHasValue) {
    visit((OWLObjectHasValue) ce);
  } else if (ce instanceof OWLObjectHasSelf) {
    visit((OWLObjectHasSelf) ce);
  } else if (ce instanceof OWLDataSomeValuesFrom) {
    visit((OWLDataSomeValuesFrom) ce);
  } else if (ce instanceof OWLDataAllValuesFrom) {
    visit((OWLDataAllValuesFrom) ce);
  } else if (ce instanceof OWLDataMinCardinality) {
    visit((OWLDataMinCardinality) ce);
  } else if (ce instanceof OWLDataMaxCardinality) {
    visit((OWLDataMaxCardinality) ce);
  } else if (ce instanceof OWLDataExactCardinality) {
    visit((OWLDataExactCardinality) ce);
  } else if (ce instanceof OWLDataHasValue) {
    visit((OWLDataHasValue) ce);
  } else if (ce instanceof OWLObjectIntersectionOf) {
    visit((OWLObjectIntersectionOf) ce);
  } else if (ce instanceof OWLObjectUnionOf) {
    visit((OWLObjectUnionOf) ce);
  } else if (ce instanceof OWLObjectComplementOf) {
    visit((OWLObjectComplementOf) ce);
  } else if (ce instanceof OWLObjectOneOf) {
    visit((OWLObjectOneOf) ce);
  } else {
    logger.error(
        "Could not visit class expression: {} of type: {}",
        ce.toString(),
        ce.getClass().toString());
  }
}
 
Example #24
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 #25
Source File: OWLGraphManipulatorTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Regression test for the default operations performed at instantiation 
 * of the {@code OWLGraphManipulator}, following problems with OWLIntersectionOfs 
 * nested in OWLObjectSomeValuesFrom. 
 * Note that this test used a different test ontology than the one loaded by 
 * {@link #loadTestOntology()} before each test. 
 */
@Test
public void regressionTestAxiomRelaxation() throws OWLOntologyCreationException, 
    OBOFormatParserException, IOException {
    
    log.debug("Loading ontology for testing axiom relaxation at instantiation...");
    ParserWrapper parserWrapper = new ParserWrapper();
    OWLOntology ont = parserWrapper.parse(
            this.getClass().getResource("/graph/relaxAxiomsTest.owl").getFile());
    log.debug("Done loading the ontology.");
    
    log.debug("Loading the ontology into OWLGraphManipulator, testing default operations...");
    this.graphManipulator = new OWLGraphManipulator(new OWLGraphWrapper(ont));
    log.debug("Default operations done.");      
    
    //test that are no ECAs left
    assertEquals("Some EquivalentClassesAxioms were not removed", 0, 
            ont.getAxiomCount(AxiomType.EQUIVALENT_CLASSES));
    //test that there is no more OWLSubClassOfAxioms with OWLObjectIntersectionOf or 
    //OWLObjectUnionOf as sub or superclass
    for (OWLSubClassOfAxiom ax: ont.getAxioms(AxiomType.SUBCLASS_OF)) {
        for (OWLClassExpression ce: ax.getNestedClassExpressions()) {
            if (ce instanceof OWLObjectIntersectionOf || ce instanceof OWLObjectUnionOf) {
                throw new AssertionError("An OWLObjectIntersectionOf or " +
                        "OWLObjectUnionOf was not removed: " + ax);
            }
        }
    }
    //test that they were replaced as expected
    OWLDataFactory factory = ont.getOWLOntologyManager().getOWLDataFactory();
    OWLObjectProperty partOf = this.graphManipulator.getOwlGraphWrapper().
        getOWLObjectPropertyByIdentifier("BFO:0000050");
    OWLGraphWrapper wrapper = this.graphManipulator.getOwlGraphWrapper();
    
    Set<OWLAxiom> expectedAxioms = new HashSet<OWLAxiom>();
    expectedAxioms.add(factory.getOWLSubClassOfAxiom(
            wrapper.getOWLClass("http://purl.obolibrary.org/obo/CL_1000321"), 
            factory.getOWLObjectSomeValuesFrom(partOf, 
                    wrapper.getOWLClass("http://purl.obolibrary.org/obo/UBERON_0000483"))));
    expectedAxioms.add(factory.getOWLSubClassOfAxiom(
            wrapper.getOWLClass("http://purl.obolibrary.org/obo/CL_1000321"), 
            factory.getOWLObjectSomeValuesFrom(partOf, 
                    wrapper.getOWLClass("http://purl.obolibrary.org/obo/UBERON_0001983"))));
    //other existing axioms
    expectedAxioms.add(factory.getOWLSubClassOfAxiom(
            wrapper.getOWLClass("http://purl.obolibrary.org/obo/CL_1000321"), 
            wrapper.getOWLClass("http://purl.obolibrary.org/obo/CL_1000320")));
    expectedAxioms.add(factory.getOWLSubClassOfAxiom(
            wrapper.getOWLClass("http://purl.obolibrary.org/obo/CL_1000321"), 
            wrapper.getOWLClass("http://purl.obolibrary.org/obo/CL_0000160")));
    assertEquals("Axioms from import ontology incorrectly merged", 
            expectedAxioms, 
            ont.getAxioms(wrapper.getOWLClass("http://purl.obolibrary.org/obo/CL_1000321"), Imports.EXCLUDED));
}
 
Example #26
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 #27
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 #28
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 #29
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 #30
Source File: RelaxOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Given an OWLClassExpression y, return a set of named classes c, such that c is a superclass of
 * y,
 *
 * <p>obtained by relaxing/unwinding expression, in a way that is guaranteed valid but not
 * guaranteed complete.
 *
 * <p>This is effectively poor-mans reasoning over IntersectionOf; e.g
 *
 * <pre>
 * C SubClassOf (C and ...)
 * </pre>
 *
 * @param x The OWLClassExpression to unwind.
 * @return the set of OWLClass superclasses
 */
private static Set<OWLClass> getNamedAncestors(OWLClassExpression x) {
  Set<OWLClass> cs = new HashSet<>();
  if (!x.isAnonymous()) {
    cs.add(x.asOWLClass());
  } else if (x instanceof OWLObjectIntersectionOf) {
    for (OWLClassExpression op : ((OWLObjectIntersectionOf) x).getOperands()) {
      cs.addAll(getNamedAncestors(op));
    }
  }
  return cs;
}