triplevisitor.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,245 行 · 第 1/3 页

JAVA
1,245
字号
		} else {			node = getResource(obj);		}		return node;	}		*/		protected RDFNode getRDFNode(OWLObject obj) throws OWLException {		Object objKey = getKey(obj);		RDFNode objNode = null;						if (((obj instanceof OWLDescription) && !(obj instanceof OWLNamedObject)) 				|| (obj instanceof OWLDataEnumeration)) {						if (replaceParent) {				OWLDescription parent = currentClass;				pushCurrent(null, null, false);				objNode = getResource(parent);				popCurrent();				return objNode;			}						if (resources.containsKey(objKey)) {				objNode = (RDFNode) resources.get(objKey);			} else {				objNode = nodeFactory.createResource();			}		} else {			if (resources.containsKey(objKey)) {				objNode = (RDFNode) resources.get(objKey);			} else {				objNode = nodeProvider.getNode(obj);							}		}		if (!resources.containsKey(objKey)) {			resources.put(objKey, objNode);			if (objNode instanceof SwoopResource) {				SwoopResource objRes = (SwoopResource) objNode;				if (objRes.isAnonymous()) {					obj.accept(this);				}				addType(obj);			}		}		return objNode;	}		protected SwoopResource getResource(OWLObject obj) throws OWLException {		RDFNode node = getRDFNode(obj);		if (!(node instanceof SwoopResource)) {			throw new OWLException("Cannot use "+obj+" as a resource.");		}				return (SwoopResource) node;	}		protected SwoopResource getResource(URI uri) throws OWLException {			return getResource(uri.toString());	}		protected SwoopResource getResource(String uri) throws OWLException {		if (resources.containsKey(uri)) {			return (SwoopResource) resources.get(uri);		}		SwoopResource objres;		try {			objres = (SwoopResource) nodeFactory.createResource(uri);			resources.put(uri, objres);			return objres;		} catch (ModelException e) {			throw new OWLException(e);		}			}		protected void popCurrent() {		if ((currentClass != null) || (currentProperty != null)) {			objectStack.pop();		}				List v = (List) currentStack.pop();		currentClass = (OWLDescription) v.get(0);		currentProperty = (OWLProperty) v.get(1);		replaceParent = ((Boolean) v.get(2)).booleanValue();			}		protected void pushCurrent(OWLDescription class_, OWLProperty prop, boolean replace) {		List v = new Vector();		v.add(0, currentClass);		v.add(1, currentProperty);		v.add(2, new Boolean(replaceParent));		currentStack.push(v);				currentClass = class_;		currentProperty = prop;		replaceParent = replace;				if (class_ != null) {			objectStack.push(class_);		} else if (prop != null) {			objectStack.push(prop);		}	}		protected void renderAnonymousClass(OWLDescription node, String property, Collection values) throws OWLException {				if (checkRendered(currentClass, node)) {			return;		}		addAnnotations(node);		SwoopResource resource = getResource(node);				pushCurrent(node, null, false);		add(resource, getResource(property), addList(values));		popCurrent();	}		protected void renderProperty(final OWLProperty node) throws OWLException {		addAnnotations(node);		SwoopResource resource = getResource(node);				pushCurrent(null, node, false);				if (node.isFunctional(ontology)) {			addType(node, owl.getFunctionalProperty());		}		if (node.isDeprecated(ontology)) {			addType(node, owl.getDeprecatedProperty());		}				Map properties = new HashMap();				for (Iterator iter = ontology.getPropertyAxioms().iterator(); iter.hasNext();) {			OWLPropertyAxiom axiom = (OWLPropertyAxiom) iter.next();						OWLPropertyAxiomVisitor visitor = new OWLPropertyAxiomVisitor() {				public void visit(OWLDataPropertyRangeAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLEquivalentPropertiesAxiom axiom) throws OWLException {					if (axiom.getProperties().contains(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLFunctionalPropertyAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLInverseFunctionalPropertyAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLInversePropertyAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLObjectPropertyRangeAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLPropertyDomainAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLSubPropertyAxiom axiom) throws OWLException {					if (axiom.getSubProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLSymmetricPropertyAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}				public void visit(OWLTransitivePropertyAxiom axiom) throws OWLException {					if (axiom.getProperty().equals(node)) {						axiom.accept(TripleVisitor.this);					}				}			};			axiom.accept(visitor);		}				properties.put(owl.getSubPropertyOf(), node.getSuperProperties(ontology));		properties.put(owl.getDomain(), node.getDomains(ontology));		properties.put(owl.getRange(), node.getRanges(ontology));				addProperties(resource, properties);		popCurrent();							}			public void reset() throws OWLException {//		qnames = new QNameShortFormProvider();		currentProperty = null;		currentClass = null;		currentStack = new Stack();		objectStack = new Stack();		rendered = new HashSet();		resources = new HashMap();		serialized = new HashSet();	}		public void serialize(Writer writer) throws IOException, OWLException {		XMLWriter xml = new PrettyXMLWriter(writer);		serialize(xml);	}		public void serialize(XMLWriter xml) throws IOException, OWLException {		RDFSerializer serializer = new OntologySerializer(getResource(ontology));		serialize(xml, serializer);	}		public void serialize(XMLWriter xml, RDFSerializer serializer) throws IOException, OWLException {		serializer.serialize(xml, model);	}				protected void visitAll(Collection objects) throws OWLException {		visitAll(objects.iterator());	}		protected void visitAll(Iterator iterator) throws OWLException {		for (; iterator.hasNext();) {			OWLObject object = (OWLObject) iterator.next();			object.accept(this);		}	}		/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLAnd)	 */	public void visit(OWLAnd node) throws OWLException {		renderAnonymousClass(node, owl.getIntersectionOf(), node.getOperands());	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLAnnotationProperty)	 */	public void visit(OWLAnnotationProperty node) throws OWLException {		addType(node);		addAnnotations(node);	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLAnnotationInstance)	 */	public void visit(OWLAnnotationInstance node) throws OWLException {		SwoopResource subject = getResource(node.getSubject());		SwoopResource property = getResource(node.getProperty());				Object content = node.getContent();		RDFNode contentResource = null;				if (content instanceof OWLObject) {			contentResource = getRDFNode((OWLObject)content);		} else if (content instanceof URI) {			contentResource = getResource(((URI)content).toString());		} else if (content instanceof String) {			try {				contentResource = nodeFactory.createLiteral((String) content);			} catch (ModelException e) {				throw new OWLException(e);			}		} else {			throw new OWLException("Unknown type of owl annotation: "+content.getClass());		}				add(subject, property, contentResource);	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataValue)	 */	public void visit(OWLDataValue node) throws OWLException {		// nothing to do	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataType)	 */	public void visit(OWLDataType node) throws OWLException {		addAnnotations(node);		addType(node);	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataEnumeration)	 */	public void visit(OWLDataEnumeration node) throws OWLException {		//System.out.println("DataEnumeration: "+node.getValues());		addAnnotations(node);		add(getResource(node), getResource(owl.getOneOf()), addList(node.getValues()));	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataAllRestriction)	 */	public void visit(OWLDataAllRestriction node) throws OWLException {		addAnnotations(node);		addType(node);		add(getResource(node), getResource(owl.getOnProperty()), getResource(node.getDataProperty()));		add(getResource(node), getResource(owl.getAllValuesFrom()), getResource(node.getDataType()));	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataCardinalityRestriction)	 */	public void visit(OWLDataCardinalityRestriction node) throws OWLException {		addAnnotations(node);		addType(node);		add(getResource(node), getResource(owl.getOnProperty()), getResource(node.getDataProperty()));				SwoopResource property = null;		int value = 0;		if (node.isExactly()) {			property = getResource(owl.getCardinality());			value = node.getAtLeast();		} else if (node.isAtLeast()) {			property = getResource(owl.getMinCardinality());			value = node.getAtLeast();		} else if (node.isAtMost()) {			property = getResource(owl.getMaxCardinality());			value = node.getAtMost();		}				if (property == null) {			return;		}				RDFNode valueNode;		try {			valueNode = nodeFactory.createLiteral(Integer.toString(value), 					"http://www.w3.org/2001/XMLSchema#nonNegativeInteger", null);		} catch (ModelException e) {			throw new OWLException(e);		}		add(getResource(node), property, valueNode);	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataProperty)	 */	public void visit(OWLDataProperty node) throws OWLException {		addType(node);		renderProperty(node);	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataSomeRestriction)	 */	public void visit(OWLDataSomeRestriction node) throws OWLException {		addAnnotations(node);		addType(node);		add(getResource(node), getResource(owl.getOnProperty()), getResource(node.getDataProperty()));		add(getResource(node), getResource(owl.getSomeValuesFrom()), getResource(node.getDataType()));	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDataValueRestriction)	 */	public void visit(OWLDataValueRestriction node) throws OWLException {		addAnnotations(node);		addType(node);		add(getResource(node), getResource(owl.getOnProperty()), getResource(node.getDataProperty()));		add(getResource(node), getResource(owl.getHasValue()), getRDFNode(node.getValue()));	}	/* (non-Javadoc)	 * @see org.semanticweb.owl.model.OWLObjectVisitor#visit(org.semanticweb.owl.model.OWLDifferentIndividualsAxiom)	 */	public void visit(OWLDifferentIndividualsAxiom node) throws OWLException {//		if (checkRendered(null, node, false)) {//			return;//		}//		//		if (node.getIndividuals().contains(currentObject()) || node.getIndividuals().size() <= 2) {//			addSymmetric(getResource(owl.getDifferentFrom()), node.getIndividuals());//		} else {			if (checkRendered(null, node)) {				return;			}			addType(node);			addAnnotations(node);			add(getResource(node), getResource(owl.getDistinctMembers()), addList(node.getIndividuals()));//		}	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?